You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@etch.apache.org by ve...@apache.org on 2012/01/12 11:05:14 UTC

svn commit: r1230464 - in /incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu: ./ include/capu/os/ include/capu/os/arch/ include/capu/os/arch/Linux/ include/capu/os/arch/Win32/ test/os/

Author: veithm
Date: Thu Jan 12 10:05:14 2012
New Revision: 1230464

URL: http://svn.apache.org/viewvc?rev=1230464&view=rev
Log:
ETCH-132 OS Abstraction for StringUtils

Abstraction for sprintf, strcmp, strcpy, etc.

Change-Id: Ia8688d57950102961a13671a346aca24bea9b8f1

Added:
    incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/StringUtils.h
    incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Linux/StringUtils.inc
    incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/StringUtils.inc
    incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Win32/StringUtils.inc
    incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/os/StringUtilsTest.cpp
Modified:
    incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/CMakeLists.txt

Modified: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/CMakeLists.txt
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/CMakeLists.txt?rev=1230464&r1=1230463&r2=1230464&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/CMakeLists.txt (original)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/CMakeLists.txt Thu Jan 12 10:05:14 2012
@@ -22,6 +22,7 @@ ADD_CLASS(os/AtomicOperation SOURCE_GROU
 ADD_CLASS(util/SmartPointer)
 ADD_CLASS(os/Thread SOURCE_GROUP arch_source_group)
 ADD_CLASS(os/CondVar SOURCE_GROUP arch_source_group)
+ADD_CLASS(os/StringUtils SOURCE_GROUP arch_source_group)
 
 REQUIRED_PACKAGE(Thread)
 

Added: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/StringUtils.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/StringUtils.h?rev=1230464&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/StringUtils.h (added)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/StringUtils.h Thu Jan 12 10:05:14 2012
@@ -0,0 +1,91 @@
+/* $Id$
+*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to you under the Apache License, Version
+* 2.0 (the "License"); you may not use this file except in compliance
+* with the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+#ifndef __STRINGUTILS_H__
+#define __STRINGUTILS_H__
+
+#include "capu/Config.h"
+#include "capu/Error.h"
+
+#define STRINGUTILS_INC_HEADER
+#include "arch/StringUtils.inc"
+#undef STRINGUTILS_INC_HEADER
+
+namespace capu
+{
+  class StringUtils
+  {
+#define STRINGUTILS_INC_MEMBER
+#include "arch/StringUtils.inc"
+#undef STRINGUTILS_INC_MEMBER
+
+  public:
+    /**
+    * Static method to copy a String of length dstSize from src to dst
+    * @param dst destination buffer
+    * @param dstSize number of chars to be copied
+    * @param src source buffer
+    */
+    inline static void Strncpy(char* dst, uint32_t dstSize, const char* src);
+
+    /**
+    * Static method to write a C string according to the given format into the array pointed by buffer.
+    * After the format parameter, the function expects at least as many additional arguments as specified in format.
+    * @param buffer which contains the string
+    * @param bufferSize size of the buffer
+    * @param format the format of the string
+    * @param arguments 
+    */
+    inline static void Sprintf(char* buffer, uint32_t bufferSize, const char* format, ...);
+    
+    /**
+    * Static method to write a C string according to the given format into the array pointed by buffer.
+    * The arguments specified in the format have to be passed by the values parameter.
+    * @param buffer which contains the string
+    * @param bufferSize size of the buffer
+    * @param format the format of the string
+    * @param values arguments for the format 
+    */
+    inline static void Vsprintf(char* buffer, uint32_t bufferSize, const char* format, va_list values);
+
+    /**
+    * Static method to return the length of the given String
+    * @param str the string
+    * @return length of the string
+    */
+    inline static uint32_t Strlen(const char* str); 
+
+    /**
+    * Static method to compare two C strings.
+    * @param str1 first string
+    * @param str2 second string
+    * @return 0 if both strings are equal
+    *         > 0 if the first character which does not match has a greater value in str1
+    *         < 0 otherwise
+    */
+    inline static int32_t Strcmp(const char* str1, const char* str2);
+
+  };
+
+#define STRINGUTILS_INC_IMPL
+#include "arch/StringUtils.inc"
+#undef STRINGUTILS_INC_IMPL
+
+}
+#endif
+

Added: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Linux/StringUtils.inc
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Linux/StringUtils.inc?rev=1230464&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Linux/StringUtils.inc (added)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Linux/StringUtils.inc Thu Jan 12 10:05:14 2012
@@ -0,0 +1,59 @@
+/* $Id$
+*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to you under the Apache License, Version
+* 2.0 (the "License"); you may not use this file except in compliance
+* with the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+#ifdef STRINGUTILS_INC_HEADER
+#include "capu/Config.h"
+#include <stdarg.h>
+#endif
+
+#ifdef STRINGUTILS_INC_MEMBER
+private:
+#endif
+
+#ifdef STRINGUTILS_INC_IMPL
+
+  inline void StringUtils::Strncpy(char* dst, uint32_t dstSize, const char* src)
+  {
+    strncpy(dst, src, dstSize);
+  }
+
+  inline uint32_t StringUtils::Strlen(const char* str)
+  {
+    return strlen(str);
+  }
+
+  inline int32_t StringUtils::Strcmp(const char* str1, const char* str2)
+  {
+    return strcmp(str1, str2);
+  }
+
+  inline void StringUtils::Vsprintf(char* buffer, uint32_t bufferSize,const char* format, va_list values)
+  {
+    vsnprintf(buffer, bufferSize, format, values);
+  }
+  
+  inline void StringUtils::Sprintf(char* buffer, uint32_t bufferSize, const char* format, ...) 
+  {
+    va_list argptr;
+    va_start(argptr,format);
+    StringUtils::Vsprintf(buffer, bufferSize, format, argptr);
+    va_end(argptr);
+  }
+
+
+#endif

Added: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/StringUtils.inc
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/StringUtils.inc?rev=1230464&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/StringUtils.inc (added)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/StringUtils.inc Thu Jan 12 10:05:14 2012
@@ -0,0 +1,25 @@
+/* $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version
+ * 2.0 (the "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+
+#ifdef ARCH_LINUX
+    #include "Linux/StringUtils.inc"
+#elif ARCH_WIN32
+    #include "Win32/StringUtils.inc"
+#endif

Added: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Win32/StringUtils.inc
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Win32/StringUtils.inc?rev=1230464&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Win32/StringUtils.inc (added)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Win32/StringUtils.inc Thu Jan 12 10:05:14 2012
@@ -0,0 +1,62 @@
+/* $Id$
+*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to you under the Apache License, Version
+* 2.0 (the "License"); you may not use this file except in compliance
+* with the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+#ifdef STRINGUTILS_INC_HEADER
+#define _WINSOCKAPI_
+#include <windows.h>
+#include <stdarg.h>
+#include "capu/Config.h"
+#endif
+
+#ifdef STRINGUTILS_INC_MEMBER
+private:
+#endif
+
+#ifdef STRINGUTILS_INC_IMPL
+
+  inline void StringUtils::Strncpy(char* dst, uint32_t dstSize, const char* src)
+  {
+
+    strcpy_s(dst, dstSize, src);
+  }
+
+  inline uint32_t StringUtils::Strlen(const char* str)
+  {
+    return strlen(str);
+  }
+
+  inline int32_t StringUtils::Strcmp(const char* str1, const char* str2)
+  {
+    return strcmp(str1, str2);
+  }
+
+  inline void StringUtils::Vsprintf(char* buffer, size_t bufferSize,const char* format, va_list values)
+  {
+    vsprintf_s(buffer, bufferSize, format, values);
+  }
+
+  inline void StringUtils::Sprintf(char* buffer, uint32_t bufferSize, const char* format, ...) 
+  {
+    va_list argptr;
+    va_start(argptr,format);
+    StringUtils::Vsprintf(buffer, bufferSize, format, argptr);
+    va_end(argptr);
+  }
+
+
+#endif

Added: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/os/StringUtilsTest.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/os/StringUtilsTest.cpp?rev=1230464&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/os/StringUtilsTest.cpp (added)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/os/StringUtilsTest.cpp Thu Jan 12 10:05:14 2012
@@ -0,0 +1,53 @@
+/* $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version
+ * 2.0 (the "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <gtest/gtest.h>
+#include "capu/os/StringUtils.h"
+
+
+TEST(StringUtils,Strcmp)
+{
+  char string1[] = "My String";
+  char string2[] = "My String";
+  char string3[] = "Another String";
+  EXPECT_EQ(0,capu::StringUtils::Strcmp(string1,string2));
+  EXPECT_TRUE(0 <= capu::StringUtils::Strcmp(string1,string3));
+}
+
+TEST(StringUtils,Strncpy)
+{
+  char string1[20] = "My String";
+  char string2[20];
+  capu::StringUtils::Strncpy(string2,20,string1);
+  EXPECT_EQ(0,capu::StringUtils::Strcmp(string1,string2));
+}
+
+TEST(StringUtils,Strlen)
+{
+  char string1[20] = "My String";
+  EXPECT_TRUE(9 == capu::StringUtils::Strlen(string1));
+}
+
+TEST(StringUtils,Sprintf)
+{
+  char string1[20];
+  capu::StringUtils::Sprintf(string1,20,"%d",12345);
+  EXPECT_EQ(0,capu::StringUtils::Strcmp("12345",string1));
+  capu::StringUtils::Sprintf(string1,20,"%d %f",12345,12.45);
+  EXPECT_EQ(0,capu::StringUtils::Strcmp("12345 12.450000",string1));
+}
+