You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@etch.apache.org by fi...@apache.org on 2012/08/02 17:42:44 UTC

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

Author: fitzner
Date: Thu Aug  2 15:42:44 2012
New Revision: 1368555

URL: http://svn.apache.org/viewvc?rev=1368555&view=rev
Log:
ETCH-207 Implementation of OS file abstraction

Change-Id: I2e74f147231077d1ae5e7712a03834ccc0724ab4

Added:
    incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/File.h
    incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/File.inc
      - copied, changed from r1368550, incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/Error.h
    incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Linux/File.inc
    incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Windows/File.inc
    incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/os/FileTest.cpp
Modified:
    incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/CMakeLists.txt
    incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/Error.h

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=1368555&r1=1368554&r2=1368555&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 Aug  2 15:42:44 2012
@@ -26,6 +26,7 @@ ADD_FILE(os/CondVar SOURCE_GROUP arch_so
 ADD_FILE(os/StringUtils SOURCE_GROUP arch_source_group)
 ADD_FILE(os/NumericLimits SOURCE_GROUP arch_source_group)
 ADD_FILE(os/Time SOURCE_GROUP arch_source_group)
+ADD_FILE(os/File SOURCE_GROUP arch_source_group)
 ADD_FILE(container/List)
 ADD_FILE(container/Comparator)
 ADD_FILE(container/Hash)

Modified: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/Error.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/Error.h?rev=1368555&r1=1368554&r2=1368555&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/Error.h (original)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/Error.h Thu Aug  2 15:42:44 2012
@@ -40,7 +40,8 @@ namespace capu {
     CAPU_ETIMEOUT = 12,
     CAPU_ENOT_EXIST = 13,
     CAPU_ENOT_SUPPORTED = 14,
-    CAPU_EIO = 15
+    CAPU_EIO = 15,
+    CAPU_EOF = 16
   };
 }
 #endif

Added: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/File.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/File.h?rev=1368555&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/File.h (added)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/File.h Thu Aug  2 15:42:44 2012
@@ -0,0 +1,104 @@
+/* $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 __FILE_H__
+#define __FILE_H__
+
+#include "capu/Config.h"
+#include "capu/Error.h"
+
+#define FILE_INC_HEADER
+#include "arch/File.inc"
+#undef FILE_INC_HEADER
+
+namespace capu {
+
+  class File {
+
+#define FILE_INC_MEMBER
+  #include "arch/File.inc"
+#undef FILE_INC_MEMBER
+
+  public:
+
+    /**
+     * Create a new instance for a file.
+     * @name of the file
+     * @mode to specify the file mode
+              "r"  opens file for reading.
+              "w"  opens file as an empty file for writing.
+              "r+" opens file for reading and writing. The file must exist.
+              "w+" opens file for reading and writing. Create a new file also of old one exists.
+     */
+    inline File(const char* name, const char* mode);
+
+    /**
+     * return true if file is open else false
+     */
+    inline bool_t isOpen();
+
+    /**
+     * return true if file end was reachde
+     */
+    inline bool_t isEof();
+
+    /**
+     * Reads data from the stream and store it into the buffer.
+     * @buffer elements to be read
+     * @length of the buffer
+     * @numBytes of bytes read from the stream
+     * return CAPU_EINVAL if params are wrong
+              CAPU_EOF    if end of stream 
+              CAPU_ERROR  if invalid state or file not open
+     */
+    inline status_t read(char * buffer, int32_t length, int32_t* numBytes);
+
+    /**
+     * Writes the given byte buffer to the stream.
+     * @buffer elements to be written
+     * @length of the buffer
+     * return CAPU_OK buffer could be written to the stream
+     *        CAPU_ERROR otherwise
+     */
+    inline status_t write(char * buffer, int32_t length);
+
+    /**
+     * Writes any unwritten data to the file.
+     */
+    inline status_t flush();
+
+    /**
+     * Close the stream.
+     *@return
+     */
+    inline status_t close();
+
+    /**
+     * Destruct current instance.
+     */
+    inline ~File();
+
+  };
+
+#define FILE_INC_IMPL
+#include "arch/File.inc"
+#undef FILE_INC_IMPL
+
+}
+
+#endif /* __FILE_H__ */

Copied: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/File.inc (from r1368550, incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/Error.h)
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/File.inc?p2=incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/File.inc&p1=incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/Error.h&r1=1368550&r2=1368555&rev=1368555&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/Error.h (original)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/File.inc Thu Aug  2 15:42:44 2012
@@ -1,47 +1,27 @@
-/* $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 __ERROR_H__
-#define __ERROR_H__
-
-#include "Config.h"
-
-namespace capu {
-  typedef int32_t status_t;
-
-  enum Errors{
-    CAPU_OK = 0,
-    CAPU_EUNIMPL = 1,
-    CAPU_ERANGE = 2,
-    CAPU_EINVAL = 3,
-    CAPU_ERROR = 4,
-    CAPU_SOCKET_EBIND = 5,
-    CAPU_SOCKET_ESOCKET = 6,
-    CAPU_SOCKET_ECONNECT = 7,
-    CAPU_SOCKET_ELISTEN = 8,
-    CAPU_SOCKET_ECLOSE = 9,
-    CAPU_SOCKET_EADDR = 10,
-    CAPU_ENO_MEMORY = 11,
-    CAPU_ETIMEOUT = 12,
-    CAPU_ENOT_EXIST = 13,
-    CAPU_ENOT_SUPPORTED = 14,
-    CAPU_EIO = 15
-  };
-}
-#endif
-
+/* $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 OS_LINUX
+    #include "Linux/File.inc"
+#elif OS_WINDOWS
+    #include "Windows/File.inc"
+#elif defined(OS_INTEGRITY)
+    #include "Linux/Memory.inc"
+#elif defined(OS_QNX)
+    #include "Linux/Memory.inc"
+#endif

Added: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Linux/File.inc
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Linux/File.inc?rev=1368555&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Linux/File.inc (added)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Linux/File.inc Thu Aug  2 15:42:44 2012
@@ -0,0 +1,118 @@
+/* $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 FILE_INC_HEADER
+#endif
+
+#ifdef FILE_INC_MEMBER
+  private:
+    FILE*   mHandle;
+    bool_t  mIsOpen;
+#endif
+
+#ifdef FILE_INC_IMPL
+
+  inline File::File(const char* name, const char* mode) :
+    mHandle(NULL) ,
+    mIsOpen(false) {
+
+    // try to open file
+    mHandle  = fopen(name, mode);
+    if(mHandle != NULL) {
+      mIsOpen = true;
+    }
+  }
+
+  inline bool_t File::isOpen() {
+    return mIsOpen;
+  }
+
+  inline bool_t File::isEof() {
+    if(mHandle == NULL) {
+      return false;
+    }
+    return (feof(mHandle) != 0);
+  }
+
+  inline status_t File::read(char * buffer, int32_t length, int32_t* numBytes) {
+    if(buffer == NULL) {
+      return CAPU_EINVAL;
+    }
+    if(mHandle == NULL) {
+      return CAPU_ERROR;
+    }
+
+    size_t result = fread(buffer, 1, length, mHandle);
+    if(result == length) {
+        if(numBytes != NULL) {
+          *numBytes = result;
+        }
+        return CAPU_OK;
+    }
+    if(feof(mHandle)) {
+      if(numBytes != NULL) {
+        *numBytes = result;
+      }
+      return CAPU_EOF;
+    }
+
+    return CAPU_ERROR;
+  }
+
+  inline status_t File::write(char * buffer, int32_t length) {
+    if(buffer == NULL) {
+      return CAPU_EINVAL;
+    }
+    if(mHandle == NULL) {
+      return CAPU_ERROR;
+    }
+
+    size_t result = fwrite(buffer, 1, length, mHandle);
+    if(result != length) {
+      return CAPU_ERROR;
+    }
+    return CAPU_OK;
+  }
+
+  inline status_t File::flush() {
+    if(mHandle != NULL) {
+      int error = fflush(mHandle);
+      if(error == 0) {
+        return CAPU_OK;
+      }
+    }
+    return CAPU_ERROR;
+  }
+
+  inline status_t File::close() {
+    if(mHandle != NULL) {
+      fclose(mHandle);
+      mHandle = NULL;
+      mIsOpen = false;
+      return CAPU_OK;
+    }
+    return CAPU_ERROR;
+  }
+
+  inline File::~File() {
+    if(mHandle != NULL) {
+      fclose(mHandle);
+    }
+  }
+
+#endif

Added: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Windows/File.inc
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Windows/File.inc?rev=1368555&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Windows/File.inc (added)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/os/arch/Windows/File.inc Thu Aug  2 15:42:44 2012
@@ -0,0 +1,119 @@
+/* $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 FILE_INC_HEADER
+#endif
+
+#ifdef FILE_INC_MEMBER
+  private:
+    FILE*   mHandle;
+    bool_t  mIsOpen;
+#endif
+
+#ifdef FILE_INC_IMPL
+
+  inline File::File(const char* name, const char* flags) : 
+    mHandle(NULL) ,
+    mIsOpen(false) {
+
+    errno_t error;
+    // try to open file
+    error  = fopen_s(&mHandle, name, flags);
+    if(error == 0) {
+      mIsOpen = true;
+    }
+  }
+
+  inline bool_t File::isOpen() {
+    return mIsOpen;
+  }
+
+  inline bool_t File::isEof() {
+    if(mHandle == NULL) {
+      return false;
+    }
+    return (feof(mHandle) != 0);
+  }
+
+  inline status_t File::read(char * buffer, int32_t length, int32_t* numBytes) {
+    if(buffer == NULL) {
+      return CAPU_EINVAL;
+    }
+    if(mHandle == NULL) {
+      return CAPU_ERROR;
+    }
+
+    size_t result = fread(buffer, 1, length, mHandle);
+    if(result == length) {
+        if(numBytes != NULL) {
+          *numBytes = result;
+        }
+        return CAPU_OK;
+    }
+    if(feof(mHandle)) {
+      if(numBytes != NULL) {
+        *numBytes = result;
+      }
+      return CAPU_EOF;
+    }
+
+    return CAPU_ERROR;
+  }
+
+  inline status_t File::write(char * buffer, int32_t length) {
+    if(buffer == NULL) {
+      return CAPU_EINVAL;
+    }
+    if(mHandle == NULL) {
+      return CAPU_ERROR;
+    }
+
+    size_t result = fwrite(buffer, 1, length, mHandle);
+    if(result != length) {
+      return CAPU_ERROR;
+    }
+    return CAPU_OK;
+  }
+
+  inline status_t File::flush() {
+    if(mHandle != NULL) {
+      int error = fflush(mHandle);
+      if(error == 0) {
+        return CAPU_OK;
+      }
+    }
+    return CAPU_ERROR;
+  }
+
+  inline status_t File::close() {
+    if(mHandle != NULL) {
+      fclose(mHandle);
+      mHandle = NULL;
+      mIsOpen = false;
+      return CAPU_OK;
+    }
+    return CAPU_ERROR;
+  }
+
+  inline File::~File() {
+    if(mHandle != NULL) {
+      fclose(mHandle);
+    }
+  }
+
+#endif

Added: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/os/FileTest.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/os/FileTest.cpp?rev=1368555&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/os/FileTest.cpp (added)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/os/FileTest.cpp Thu Aug  2 15:42:44 2012
@@ -0,0 +1,131 @@
+/* $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/File.h"
+
+TEST(File, ConstructorTest) {
+  capu::File* f1 = new capu::File("foobar.txt", "r");
+  EXPECT_TRUE(f1 != NULL);
+  EXPECT_FALSE(f1->isOpen());
+  delete f1;
+
+  capu::File* f2 = new capu::File("test.txt", "w");
+  EXPECT_TRUE(f1 != NULL);
+  EXPECT_TRUE(f1->isOpen());
+  delete f2;
+}
+
+TEST(File, IsOpenTest) {
+  capu::File* f1 = new capu::File("foobar.txt", "r");
+  EXPECT_TRUE(f1 != NULL);
+  EXPECT_FALSE(f1->isOpen());
+  delete f1;
+
+  capu::File* f2 = new capu::File("test.txt", "w");
+  EXPECT_TRUE(f2 != NULL);
+  EXPECT_TRUE(f2->isOpen());
+  delete f2;
+}
+
+TEST(File, WriteTest) {
+  char buf1[15] = "This is a test";
+  capu::status_t status;
+
+  capu::File* f1 = new capu::File("test.txt", "w");
+  EXPECT_TRUE(f1 != NULL);
+  EXPECT_TRUE(f1->isOpen());
+
+  // invalid params
+  status = f1->write(NULL, 0);
+  EXPECT_TRUE(status == capu::CAPU_EINVAL);
+
+  // write data
+  status = f1->write(buf1, sizeof(buf1)-1);
+  EXPECT_TRUE(status == capu::CAPU_OK);
+
+  delete f1;
+}
+
+TEST(File, ReadTest) {
+  char buf1[20] = "This is a test";
+  char buf2[20];
+  
+  capu::status_t status;
+  capu::int32_t read = 0;
+
+  // read data
+  capu::File* f1 = new capu::File("test1.txt", "r");
+  EXPECT_TRUE(f1 != NULL);
+  EXPECT_FALSE(f1->isOpen());
+
+  // file not open
+  status = f1->write(buf2, strlen(buf1));
+  EXPECT_TRUE(status == capu::CAPU_ERROR);
+
+  delete f1;
+
+  // write data
+  capu::File* f2 = new capu::File("test.txt", "w");
+  EXPECT_TRUE(f2 != NULL);
+  EXPECT_TRUE(f2->isOpen());
+
+  status = f2->write(buf1, strlen(buf1));
+  EXPECT_TRUE(status == capu::CAPU_OK);
+  delete f2;
+
+  // read data
+  capu::File* f3 = new capu::File("test.txt", "r");
+  EXPECT_TRUE(f3 != NULL);
+  EXPECT_TRUE(f3->isOpen());
+
+  // invalid params
+  status = f3->read(NULL, 0, NULL);
+  EXPECT_TRUE(status == capu::CAPU_EINVAL);
+
+  read = 0;
+  memset(buf2, 0, sizeof(buf2));
+  status = f3->read(buf2, strlen(buf1), &read);
+  EXPECT_TRUE(status == capu::CAPU_OK);
+  EXPECT_TRUE(read == strlen(buf1));
+  delete f3;
+
+  // read data
+  capu::File* f4 = new capu::File("test.txt", "r");
+  EXPECT_TRUE(f4 != NULL);
+  EXPECT_TRUE(f4->isOpen());
+
+  memset(buf2, 0, sizeof(buf2));
+  status = f4->read(buf2, strlen(buf1), NULL);
+  EXPECT_TRUE(status == capu::CAPU_OK);
+  EXPECT_TRUE(read == strlen(buf1));
+  delete f4;
+
+  // read data Eof
+  capu::File* f5 = new capu::File("test.txt", "r");
+  EXPECT_TRUE(f5 != NULL);
+  EXPECT_TRUE(f5->isOpen());
+
+  read = 0;
+  memset(buf2, 0, sizeof(buf2));
+  status = f5->read(buf2, sizeof(buf2), &read);
+  EXPECT_TRUE(status == capu::CAPU_EOF);
+  EXPECT_TRUE(read == strlen(buf1));
+  EXPECT_TRUE(strcmp(buf1, buf2) == 0);
+  delete f5;
+}