You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/10/12 14:23:12 UTC

svn commit: r824315 - in /commons/sandbox/runtime/trunk/src: main/java/org/apache/commons/runtime/io/ main/native/include/arch/unix/ main/native/os/unix/ test/org/apache/commons/runtime/

Author: mturk
Date: Mon Oct 12 12:23:11 2009
New Revision: 824315

URL: http://svn.apache.org/viewvc?rev=824315&view=rev
Log:
Add simplest FileInfo test

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileInstance.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileWrapper.java
    commons/sandbox/runtime/trunk/src/main/native/include/arch/unix/acr_arch.h
    commons/sandbox/runtime/trunk/src/main/native/os/unix/uutils.c
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileInstance.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileInstance.java?rev=824315&r1=824314&r2=824315&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileInstance.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileInstance.java Mon Oct 12 12:23:11 2009
@@ -392,6 +392,23 @@
     }
 
     /**
+     * Return new {@link FileInfo} object.
+     *
+     * @throws ClosedDescriptorException
+     *          If this file is closed.
+     * @throws AsyncClosedDescriptorException
+     *          If another thread closes this file while the lock
+     *          operation is in progress.
+     * @throws IOException
+     *          If some other I/O error occurs.
+     */
+    public FileInfo stat()
+        throws IOException, SecurityException
+    {
+        return FileWrapper.stat(fd);
+    }
+
+    /**
      * Lock the entire file.
      * <p>
      * To simulate the {@link java.nio.channels.FileChannel#tryLock} use

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileWrapper.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileWrapper.java?rev=824315&r1=824314&r2=824315&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileWrapper.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileWrapper.java Mon Oct 12 12:23:11 2009
@@ -144,7 +144,7 @@
     /**
      * Return new {@link FileInfo} object.
      */
-    private static FileInfo stat(Descriptor fd)
+    public static FileInfo stat(Descriptor fd)
         throws IOException, SecurityException
     {
         if (!fd.valid())

Modified: commons/sandbox/runtime/trunk/src/main/native/include/arch/unix/acr_arch.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/arch/unix/acr_arch.h?rev=824315&r1=824314&r2=824315&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/arch/unix/acr_arch.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/arch/unix/acr_arch.h Mon Oct 12 12:23:11 2009
@@ -53,7 +53,7 @@
 
 #define ACR_FTOK_ID          'a'
 #define ACR_MIN_FREAD_LEN    4096
-#define ACR_MAX_FREAD_LEN    65536
+#define ACR_MAX_FREAD_LEN    (64 * 1024)
 #define INVALID_HANDLE_VALUE ((void *)(ptrdiff_t)-1)
 #define IS_INVALID_HANDLE(h) (((h) == NULL || (h) == INVALID_HANDLE_VALUE))
 #define IS_VALID_HANDLE(h)   (((h) != NULL && (h) != INVALID_HANDLE_VALUE))
@@ -235,11 +235,44 @@
  * Read the content of a txt file, up to the ACR_MAX_FREAD_LEN
  * and remove all trailing space and control characters.
  * @param name The file name to read.
- * @return buffer with the content of the file. Use free() to
+ * @return buffer with the content of the file. Use x_free() to
  *          deallocate the buffer when done.
  *          If the file cannot be read, function returns NULL.
  */
-char *acr_FileReadTxt(const char *name);
+char  *acr_FileReadTxt(const char *name);
+
+/**
+ * Read the content of a file.
+ * @param name The file name to read.
+ * @param readed OPtional number of bytes readed from the file.
+ * @return buffer with the content of the file. Use x_free() to
+ *          deallocate the buffer when done.
+ *          If the file cannot be read, function returns NULL.
+ */
+char  *acr_FileReadAll(const char *name, size_t *readed);
+
+/**
+ * Read the content of a text file, up to the ACR_MAX_FREAD_LEN
+ * and create array from content lines separated by '\n' character.
+ * Each line has removed leading and trailing spaces.
+ * Empty lines that contain only spaces or tab characters are removed
+ * from the resulting array.
+ *
+ * @param name The file name to read.
+ * @return array with the content of the file. Array is terminated
+ *         with NULL pointer. First array element contains the pointer
+ *         to the memory content and must be free'd when no longer needed.
+ * <pre>
+ *  char **array = acr_FileReadLines ...
+ *  ...
+ *  x_free(array[0]);
+ *  x_free(array);
+ * </pre>
+ *
+ * @note Empty line that contain only spaces or tab characters are removed
+ * from the resulting array.
+ */
+char **acr_FileReadLines(const char *name);
 
 /**
  * Get the absolute path by merging the current

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/uutils.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/uutils.c?rev=824315&r1=824314&r2=824315&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/uutils.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/uutils.c Mon Oct 12 12:23:11 2009
@@ -35,7 +35,9 @@
     if ((rb = malloc(rd))) {
         size_t nr = fread(rb, 1, rd - 2, f);
         if (nr == (rd - 2)) {
-            /* Try with larger buffer size */
+            /* Try with larger buffer size
+             * but never larger then ACR_MAX_FREAD_LEN
+             */
             char *nb = x_malloc(ACR_MAX_FREAD_LEN);
             if (nb) {
                 memcpy(nb, rb, nr);
@@ -76,6 +78,135 @@
     return NULL;
 }
 
+char *acr_FileReadAll(const char *name, size_t *readed)
+{
+    FILE  *f;
+    size_t rd = ACR_MIN_FREAD_LEN;
+    char  *rb;
+    int    rc = 0;
+
+    if (readed)
+        *readed = 0;
+    if (!(f = fopen(name, "r")))
+        return NULL;
+    if ((rb = malloc(rd))) {
+        for (;;) {
+            size_t nr = fread(rb, 1, rd - 1, f);
+            if (nr == (rd - 1)) {
+                /* Try with larger buffer size.
+                 */
+                char *nb = x_malloc(rd << 1);
+                if (nb) {
+                    memcpy(nb, rb, nr);
+                    x_free(rb);
+                    rb = nb;
+                    rd = (rd << 1) - nr;
+                    nr += fread(rb + nr, 1, rd - 1, f);
+                }
+                else {
+                    rc = errno;
+                    nr = 0;
+                }
+            }
+            if (nr > 0) {
+                rb[nr] = '\0';
+                if (readed)
+                    *readed = nr;
+                break;
+            }
+            else {
+                x_free(rb);
+                goto cleanup;
+            }
+        }
+    }
+    else {
+        rc = errno;
+        goto cleanup;
+    }
+    fclose(f);
+    return rb;
+
+cleanup:
+    fclose(f);
+    errno = rc;
+    return NULL;
+}
+
+/**
+ * Remove all leading and trailing space characters
+ * from the string.
+ * @return pointer to the first non space character.
+ * @note String is modified in place
+ */
+static char *strtrim(char *s)
+{
+    int i;
+    /* check for empty strings */
+    if (!(i = (int)strlen(s)))
+        return s;
+    for (i = i - 1; i >= 0 && acr_isspace(s[i]); i--)
+        ;
+    s[i + 1] = '\0';
+    for (i = 0; s[i] != '\0' && acr_isspace(s[i]); i++)
+        ;
+
+    return s + i;
+}
+
+/**
+ * rv[0] contains the entire data and must be always freed.
+ * rv[n] is NULL.
+ */
+char **acr_FileReadLines(const char *name)
+{
+    char **arr;
+    char *ctxt;
+    char *cp, *line;
+    size_t  i = 1, nl = 0;
+
+    if (!(ctxt = acr_FileReadAll(name, NULL)))
+        return NULL;
+    cp = ctxt;
+    while ((cp = strchr(cp, '\n'))) {
+        cp++;
+        nl++;
+    }
+    /* Add room for first, last and terminating line
+     */
+    nl += 3;
+    arr = (char **)x_malloc(nl * sizeof(char *));
+    if (!arr) {
+        x_free(ctxt);
+        ACR_SET_OS_ERROR(ENOMEM);
+        return NULL;
+    }
+    arr[0] = ctxt;
+    line   = ctxt;
+    while ((cp = strchr(line, '\n'))) {
+        /* Separate file content by NL
+         */
+        *(cp++) = '\0';
+        line = strtrim(line);
+        if (*line) {
+            arr[i++] = line;
+        }
+        line = cp;
+        if (!*line)
+            break;
+    }
+    /* Last line might not have the '\n'
+     */
+    if (*line) {
+        line = strtrim(line);
+        if (*line) {
+            arr[i++] = line;
+        }
+    }
+    arr[i] = NULL;
+    return arr;
+}
+
 #define IS_PATH_SEP(C) ((C) == '/' || (C) == '\0')
 /* Calculate the absolute path by merging the current
  * working directory in case of relative paths.

Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java?rev=824315&r1=824314&r2=824315&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java Mon Oct 12 12:23:11 2009
@@ -141,6 +141,20 @@
         f.close();
     }
 
+    public void testFileInfo()
+        throws Exception
+    {
+        FileInstance f = FileInstance.createTemp("tmp.", null, false);
+        assertFalse("FileInstance", f == null);
+        System.out.println();
+        System.out.println("Temporary file " + f.getPath());
+        FileInfo info = f.stat();
+        System.out.println("Info Name   : " + info.Name);
+        System.out.println("     Size   : " + info.Size);
+        System.out.println("     Inode  : " + info.InodeId);
+        f.close();
+    }
+
 
 }