gnuastro-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnuastro-commits] master ad2810d 029/125: Work started on getting text


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master ad2810d 029/125: Work started on getting text table information
Date: Sun, 23 Apr 2017 22:36:30 -0400 (EDT)

branch: master
commit ad2810dab6a7ea72afeff473afec194ec9795481
Author: Mohammad Akhlaghi <address@hidden>
Commit: Mohammad Akhlaghi <address@hidden>

    Work started on getting text table information
    
    `gal_txt_table_info' is in charge of reading the table information from a
    plain text file. Work has started on doing the job, with this commit, it
    will open the file, and identify the comment, non-comment, and blank lines.
---
 bin/table/ui.c |  8 +++--
 bootstrap.conf |  1 +
 lib/txt.c      | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 99 insertions(+), 6 deletions(-)

diff --git a/bin/table/ui.c b/bin/table/ui.c
index c3f8c29..1ecc510 100644
--- a/bin/table/ui.c
+++ b/bin/table/ui.c
@@ -322,10 +322,12 @@ sanitycheck(struct tableparams *p)
   else
     {
       if( gal_fits_name_is_fits(p->cp.output)
-          && p->outtabletype==GAL_TABLE_TYPE_TXT)
+          && ( p->outtabletype !=GAL_TABLE_TYPE_AFITS
+               && p->outtabletype !=GAL_TABLE_TYPE_BFITS ) )
         error(EXIT_FAILURE, 0, "desired output table is a FITS file, but "
-              "`outtabletype' is not a FITS table type. Please set it to "
-              "`fits-ascii', or `fits-binary'");
+              "`outtabletype' is not %s. Please set it to "
+              "`fits-ascii', or `fits-binary'",
+              up->outtabletypeset ? "a FITS table type" : "set");
     }
 }
 
diff --git a/bootstrap.conf b/bootstrap.conf
index 0409677..a9c3d30 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -164,6 +164,7 @@ gnulib_modules="
     regex
     error
     nproc
+    getline
     strcase
     gendocs
     git-version-gen
diff --git a/lib/txt.c b/lib/txt.c
index 7272238..f784c9f 100644
--- a/lib/txt.c
+++ b/lib/txt.c
@@ -35,13 +35,103 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 #include <checkset.h>
 
 
-/************************************************************************/
-/***************     Information about a txt table        ***************/
-/************************************************************************/
+
+
+
+/* Status of a line: */
+enum txt_line_stat
+{
+  TXT_LINESTAT_BLANK,
+  TXT_LINESTAT_ISCOMMENT,
+  TXT_LINESTAT_NOTCOMMENT,
+};
+
+
+
+
+
+/* Return one of the `txt_line_stat' constant values. */
+int
+get_line_stat(char *line)
+{
+  while(*line!='\n')
+    {
+      switch(*line)
+        {
+          /* Characters to ignore. */
+        case ' ': case ',': case '\t':
+          break;
+        case '#':
+          return TXT_LINESTAT_ISCOMMENT;
+        default:
+          return TXT_LINESTAT_NOTCOMMENT;
+        }
+      ++line;
+    }
+  return TXT_LINESTAT_BLANK;
+}
+
+
+
+
+
+/* Return the information about a text file table. */
 gal_data_t *
 gal_txt_table_info(char *filename, size_t *numcols)
 {
+  FILE *fp;
+  char *line;
+  size_t linelen=10;  /* This will be increased later by `getline'. */
+
+  /* Open the file. */
+  errno=0;
+  fp=fopen(filename, "r");
+  if(fp==NULL)
+    error(EXIT_FAILURE, errno, "%s: could't open to read as a text table",
+          filename);
+
+  /* Get the maximum line length and allocate the space necessary to keep
+     copies of all lines as we parse them. Note that `getline' is going to
+     put the string NULL character also, so we need one more character. */
+  errno=0;
+  line=malloc(linelen*sizeof *line);
+  if(line==NULL)
+    error(EXIT_FAILURE, errno, "%zu bytes for line in `gal_txt_table_info'",
+          linelen*sizeof *line);
+
+  /* Read the comments of the line for possible information about the
+     lines, but also confirm the info by trying to read the first
+     uncommented line. */
+  while( getline(&line, &linelen, fp) != -1 )
+    switch(get_line_stat(line))
+      {
+      case TXT_LINESTAT_BLANK:
+        printf("blank\n");
+        break;
+
+      case TXT_LINESTAT_ISCOMMENT:
+        printf("comment\n");
+        break;
+
+      case TXT_LINESTAT_NOTCOMMENT:
+        printf("not comment\n");
+        break;
+
+      default:
+        error(EXIT_FAILURE, 0, "linestatus code %d not recognized in "
+              "`gal_txt_table_info'",
+              linestat);
+      }
+
+  /* Clean up, close the file and return. */
+  free(line);
+  errno=0;
+  if(fclose(fp))
+    error(EXIT_FAILURE, errno, "%s: couldn't close file after reading ASCII "
+          "table information", filename);
 
+  printf("\n----end of tableinfo----\n");
+  exit(0);
   return NULL;
 }
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]