Index: approxBasisSollya/read-lines-from-file.h
===================================================================
--- approxBasisSollya/read-lines-from-file.h	(revision 0)
+++ approxBasisSollya/read-lines-from-file.h	(revision 299)
@@ -0,0 +1,49 @@
+/** @file read-lines-from-file.h
+ * Read lines from a text file, filtering out empty, #-comment lines,
+ * leading spaces and tab, trailing comments.
+ * @author S.T.
+ * @date 2016/10/18
+ */
+/******************************************************************************/
+ 
+/*
+ * Prerequisites for use:
+ *   The user must include
+ *   #include <stdio.h>
+ *   #include <libgen.h>
+ */
+
+#ifndef RLFF_INCLUDE_h
+#define RLFF_INCLUDE_h
+  
+  /* Types, constants and macros definitions. */
+  typedef struct rlff_readLines rlff_readLines_t;
+  struct rlff_readLines
+  {
+    unsigned int linesNum;
+    unsigned int linesLenght;
+    char* lines; 
+  };
+
+  /* Exported functions. */
+  /** Free a readLines_t object. */
+  void 
+  rlff_free_lines(rlff_readLines_t* linesRead);
+  
+  char*
+  rlff_get_line_at(rlff_readLines_t* linesRead, unsigned int lineIndex);
+  
+  /** Print the lines. */
+  void
+  rlff_print_lines(rlff_readLines_t* linesRead);
+  
+  /** Read the lines from a file give by name. */
+  rlff_readLines_t*
+  rlff_read_lines_from_file_name(char* fileName);
+
+  /** Read the lines from a file give by a handle on the file file. */
+  rlff_readLines_t* 
+  rlff_read_lines_from_file_handle(FILE* fileHandle);
+ 
+#endif
+  

Property changes on: approxBasisSollya/read-lines-from-file.h
___________________________________________________________________
Added: svn:mime-type
   + text/plain

Index: approxBasisSollya/read-lines-from-file.c
===================================================================
--- approxBasisSollya/read-lines-from-file.c	(revision 0)
+++ approxBasisSollya/read-lines-from-file.c	(revision 299)
@@ -0,0 +1,444 @@
+/** @file read-lines-from-file.c */
+/******************************************************************************/
+/**
+ * Read lines from a text file, filtering out empty, #-comment lines,
+ * leading spaces and tab, trailing comments.
+ * 
+ * @author: ST
+ * @date: 2016-10-21
+ *
+ */
+
+/* Includes according to the "general to particular" rule. */
+/* includes of system headers */
+#include <stdlib.h>
+#include <stdio.h>
+#include <libgen.h>
+
+/* includes of project headers */
+
+/* includes of local headers */
+#include <read-lines-from-file.h>
+
+/* Types, constants and macros definitions */
+
+/* Global variables */
+
+/* Functions */
+
+/** Scout the file for lines count and maximum lines length.
+ * The file is specified by its name.
+ * The data are returned through pointer arguments.
+ * @param fileName (in);
+ * @param linesCountPtr (in/out);
+ * @param maxLineLengthPtr (in/out);
+ * @return 0 if OK and != 0 if something goes wrong. */
+
+int
+rlff_scout_file_from_name(char* fileName,
+                          unsigned int* linesCountPtr,
+                          unsigned int* maxLineLengthPtr);
+
+/** Scout the file for lines count and maximum lines length.
+ * The file is specified by a handle open on it in "read" mode (at least).
+ * The data are returned through pointer arguments.
+ * @param fileHandle (in);
+ * @param linesCountPtr (in/out);
+ * @param maxLineLengthPtr (in/out);
+* @return 0 if OK and != 0 if something goes wrong. */
+
+int
+rlff_scout_file_from_handle(FILE* fileHandle,
+                            unsigned int* linesCountPtr,
+                            unsigned int* maxLineLengthPtr);
+
+/** Dealloc a readLines struct that has been initialized (even partly)
+ * @see read-lines-from-file.h#rlff_free_lines.*/
+void 
+rlff_free_lines(rlff_readLines_t* linesRead)
+{
+  if (linesRead == NULL) { return; }
+  if (linesRead->lines == NULL) { return; }
+  free(linesRead->lines);
+  free(linesRead);
+} /* End free_lines. */
+
+/** @see read-lines-from-file.h#rlff_get_line_at. */ 
+char*
+rlff_get_line_at(rlff_readLines_t* linesRead, unsigned int lineIndex)
+{
+  if (linesRead == NULL)
+  {
+    #ifndef NDEBUG
+    fprintf(stderr,
+            "\n%s: linesRead is a NULL pointer.\n",
+            __func__);
+    #endif
+    return NULL;
+  }
+  if (lineIndex > (linesRead->linesNum -1))
+  {
+    #ifndef NDEBUG
+    fprintf(stderr,
+            "\n%s: requested line inted (%d) is beyhond\n",
+            __func__,
+            lineIndex);
+    fprintf(stderr,
+            "maximum index (%d).\n",
+            linesRead->linesNum -1);
+    #endif
+    return NULL;
+  }
+  return linesRead->lines + lineIndex * linesRead->linesLenght;
+} /* End rlff_get_line_at. */
+
+/** @see read-lines-from-file.h#rlff_print_lines */
+void
+  rlff_print_lines(rlff_readLines_t* linesRead)
+{
+  unsigned int i = 0;
+  if (linesRead == NULL) { return; }
+  for (i = 0 ; i < linesRead->linesNum ; i++)
+  {
+    fprintf(stdout,
+            "%s\n",
+            rlff_get_line_at(linesRead, i));
+  }
+} /* End rlff_print_lines. */
+
+/** @see read-lines-from-file.h#rlff_read_lines_from file_name */
+rlff_readLines_t*
+rlff_read_lines_from_file_name(char* fileName)
+{
+  FILE* linesFile               = NULL;
+
+  /* Current character position in read file. Starts at 0. */
+  rlff_readLines_t* linesRead   = NULL;
+  
+  /* Argument check. */
+  if (fileName == NULL)
+  {
+    #ifndef NDEBUG
+    fprintf(stderr,
+            "\n%s: fileName is a NULL pointer.\n",
+            __func__);
+    #endif
+    return NULL;
+  }
+  /* Try to open the file in read-only mode. */
+  linesFile = fopen(fileName, "r");
+  if (linesFile == NULL) 
+  {
+    #ifndef NDEBUG
+    fprintf(stderr, 
+            "\n%s: could not open file %s.\n",
+            __func__,
+            fileName);
+    #endif
+    return NULL;
+  }
+  linesRead = rlff_read_lines_from_file_handle(linesFile);
+  fclose(linesFile);
+  return linesRead;
+} /* End rlff_read_lines_from_file_name. */
+
+/** @see read-lines-from-file.h#rlff_read_lines_from file_handle */
+rlff_readLines_t*
+rlff_read_lines_from_file_handle(FILE* fileHandle)
+{
+  char c                        = '\0';
+  char* bufferCurrentLinePtr    = NULL;
+  char* bufferCurrentCharPtr    = NULL;
+  /* Current character position in read file. Starts at 0. */
+  rlff_readLines_t* linesRead   = NULL;
+  unsigned int rlffLinesCount   = 0;
+  unsigned int rlffLinesLength  = 0;
+
+  /* Argument check. */
+  if (fileHandle == NULL)
+  {
+    #ifndef NDEBUG
+    fprintf(stderr,
+            "\n%s: fileHandle is a NULL pointer.\n",
+            __func__);
+    #endif
+    return NULL;
+  }
+  /* Get the file data. */
+  if (rlff_scout_file_from_handle(fileHandle,
+                                  &rlffLinesCount,
+                                  &rlffLinesLength))
+  {
+    #ifndef NDEBUG
+    fprintf(stderr,
+            "\n%s: could not scout the file.\n",
+            __func__);
+    #endif
+    return NULL;
+  }
+  /* Try to allocate the pointer returned structure. */
+  linesRead = malloc(sizeof(rlff_readLines_t));
+  if (linesRead == NULL)
+  {
+    #ifndef NDEBUG
+    fprintf(stderr,
+            "\n%s: could not alloc memory for the rlff_readLines_t.\n",
+            __func__);
+    #endif
+    return NULL;
+  }
+
+  linesRead->linesNum     = rlffLinesCount;
+  linesRead->linesLenght  = rlffLinesLength;
+  if ((rlffLinesCount == 0) || (rlffLinesLength == 0))
+  {
+    /* The file is somehow empty: not an actual error. */
+    linesRead->linesNum     = 0;
+    linesRead->linesLenght  = 0;
+    linesRead->lines        = NULL;
+    return linesRead;
+  }
+  /* Try to allocate the lines buffer. */
+  linesRead->lines =
+      malloc(rlffLinesCount * rlffLinesLength * sizeof(char));
+  if (linesRead->lines == NULL)
+  {
+    #ifndef NDEBUG
+    fprintf(stderr,
+            "\n%s: could not alloc memory for the lines buffer.\n",
+            __func__);
+    #endif
+    free(linesRead);
+    return NULL;
+  }
+  bufferCurrentLinePtr = linesRead->lines;
+  bufferCurrentCharPtr = bufferCurrentLinePtr;
+  /* Get the chars of the file. */
+  while ((c = fgetc(fileHandle)) != EOF)
+  {
+    /* Skipping initial spaces. */
+    while((c == ' ') || (c == '\t'))
+    {
+      c = fgetc(fileHandle);
+      if (c == EOF)
+      {
+        return linesRead;
+      }
+    } /* End while */
+    /* A line of spaces or an empty line. */
+    if (c == '\n')
+    {
+      continue;
+    }
+    /* First character afer spaces is '#'.  Read the comment
+     * to the end of the line or of the file. */
+    if (c == '#')
+    {
+      while(((c = fgetc(fileHandle)) != EOF) && (c != '\n')) {}
+      if (c == '\n')
+      {
+        continue;
+      }
+      else /* c == EOF */
+      {
+        return linesRead;
+      }
+    } /* End c == '#'. */
+    *bufferCurrentCharPtr = c;
+    bufferCurrentCharPtr++;
+    /* A line with usefull content. */
+    while(((c = fgetc(fileHandle)) != EOF) && (c != '\n') && (c != '#'))
+    {
+      *bufferCurrentCharPtr = c;
+      bufferCurrentCharPtr++;
+    }
+    if (c == '#')
+    {
+      while(((c = fgetc(fileHandle)) != EOF) && (c != '\n')) {}
+    }
+    if (c == EOF)
+    {
+        return linesRead;
+    } /* End c == EOF. */
+    /* End of a line with useful content. */
+    /* At the end of a useful lines. */
+    if (c == '\n')
+    {
+      *bufferCurrentCharPtr = '\0';
+      /* Delete trailing spaces. */
+      while (bufferCurrentCharPtr > bufferCurrentLinePtr)
+      {
+        bufferCurrentCharPtr--;
+        if ((*bufferCurrentCharPtr == ' ') ||
+            (*bufferCurrentCharPtr == '\t'))
+        {
+          *bufferCurrentCharPtr = '\0';
+        }
+        else
+        {
+          break;
+        }
+      } /* End while. */
+      bufferCurrentLinePtr += linesRead->linesLenght;
+      bufferCurrentCharPtr = bufferCurrentLinePtr;
+    } /* End c == '\n'. */
+  } /* End main while; ancillary*/
+  /* Normal reading exit. */
+  return linesRead;
+} /* End rlff_read_lines_from_file_handle. */
+
+int
+rlff_scout_file_from_name(char* fileName,
+                          unsigned int* linesCountPtr,
+                          unsigned int* maxLineLengthPtr)
+{
+  FILE* linesFileHandle         = NULL;
+  int returnValue               = 0;
+
+  /* Arguments check. */
+  if ((fileName         == NULL)    || 
+      (linesCountPtr    == NULL)  ||
+      (maxLineLengthPtr == NULL))
+  {
+    #ifndef NDEBUG
+    if (fileName == NULL)
+    {
+      fprintf(stderr,
+              "\n%s: \"fileName\" is a NULL pointer.\n",
+              __func__);
+    }
+    if (linesCountPtr == NULL)
+    {
+      fprintf(stderr,
+              "\n%s: \"linesCountPtr\" is a NULL pointer.\n",
+              __func__);
+    }
+    if (maxLineLengthPtr == NULL)
+    {
+      fprintf(stderr,
+              "\n%s: \"maxLineLengthPtr\" is a NULL pointer.\n",
+              __func__);
+    }
+    #endif
+    return 1;
+  } /* End arguments check. */
+  
+  /* Open file and deal with errors. */
+  linesFileHandle = fopen(fileName, "r");
+  if (linesFileHandle == NULL)
+  {
+    fprintf(stderr, 
+            "\n%s: could not open file %s.\n",
+            __func__,
+            fileName);
+    return 1;
+  }
+  *linesCountPtr      = 0;
+  *maxLineLengthPtr   = 0;
+  /* Scan the file. */
+  returnValue = rlff_scout_file_from_handle(linesFileHandle,
+                                            linesCountPtr,
+                                            maxLineLengthPtr);
+  fclose(linesFileHandle);
+  return returnValue;
+} /* End rlff_scout_file_from_name. */
+
+int
+rlff_scout_file_from_handle(FILE* fileHandle,
+                            unsigned int* linesCountPtr,
+                            unsigned int* maxLineLengthPtr)
+{
+  char c                        = '\0';
+  unsigned int charsCount       = 0;
+  /* Arguments check. */
+  if ((fileHandle       == NULL)  ||
+      (linesCountPtr    == NULL)  ||
+      (maxLineLengthPtr == NULL))
+  {
+    #ifndef NDEBUG
+    if (fileHandle == NULL)
+    {
+      fprintf(stderr,
+              "\n%s: \"fileHandle\" is a NULL pointer.\n",
+              __func__);
+    }
+    if (linesCountPtr == NULL)
+    {
+      fprintf(stderr,
+              "\n%s: \"linesCountPtr\" is a NULL pointer.\n",
+              __func__);
+    }
+    if (maxLineLengthPtr == NULL)
+    {
+      fprintf(stderr,
+              "\n%s: \"maxLineLengthPtr\" is a NULL pointer.\n",
+              __func__);
+    }
+    #endif
+    return 1;
+  } /* End arguments check. */
+
+  *linesCountPtr      = 0;
+  *maxLineLengthPtr   = 0;
+  /* Scan the file. */
+  while((c = fgetc(fileHandle)) != EOF)
+  {
+    (*linesCountPtr)++;
+    charsCount++;
+    while((c == ' ') || (c == '\t'))
+    {
+      c = fgetc(fileHandle);
+      if (c == EOF)
+      {
+        (*linesCountPtr)--;
+        return 0;
+      }
+    } /* End while */
+    /* A line of spaces. */
+    if (c == '\n')
+    {
+      (*linesCountPtr)--;
+      charsCount = 0;
+      continue;
+    }
+    /* First character after spaces is '#'.  Read the comment
+     * to the end of the line or of the file. */
+    if (c == '#')
+    {
+      (*linesCountPtr)--;
+      charsCount = 0;
+      while(((c = fgetc(fileHandle)) != EOF) && (c != '\n')) {}
+      if (c == '\n')
+      {
+        continue;
+      }
+      else /* c == EOF */
+      {
+        break;
+      }
+    } /* End c == '#'. */
+    while(((c = fgetc(fileHandle)) != EOF) && (c != '\n'))
+    {
+      charsCount++;
+    }
+    if (c == EOF)
+    {
+      /* Allow for space of the terminal '\0'. */
+      if (charsCount + 1 > *maxLineLengthPtr)
+      {
+        (*maxLineLengthPtr) = charsCount + 1;
+      }
+      break;
+    } /* End c == EOF. */
+    if (c == '\n')
+    {
+      /* Allow for space of the terminal '\0'. */
+      if (charsCount + 1 > *maxLineLengthPtr)
+      {
+        (*maxLineLengthPtr) = charsCount + 1;
+      }
+      charsCount = 0;
+    } /* End c == '\n'. */
+  } /* End while. */
+  return 0;
+} /* End rlff_scout_file_from_handle. */

Property changes on: approxBasisSollya/read-lines-from-file.c
___________________________________________________________________
Added: svn:mime-type
   + text/plain

