JWS C Library
C language utility library
file_io.c
Go to the documentation of this file.
00001 /*
00002  * This work is licensed under a Creative Commons 
00003  * Attribution-Noncommercial-Share Alike 3.0 United States License.
00004  * 
00005  *    http://creativecommons.org/licenses/by-nc-sa/3.0/us/
00006  * 
00007  * You are free:
00008  * 
00009  *    to Share - to copy, distribute, display, and perform the work
00010  *    to Remix - to make derivative works
00011  * 
00012  * Under the following conditions:
00013  * 
00014  *    Attribution. You must attribute the work in the manner specified by the
00015  *    author or licensor (but not in any way that suggests that they endorse you
00016  *    or your use of the work).
00017  * 
00018  *    Noncommercial. You may not use this work for commercial purposes.
00019  * 
00020  *    Share Alike. If you alter, transform, or build upon this work, you may
00021  *    distribute the resulting work only under the same or similar license to
00022  *    this one.
00023  * 
00024  * For any reuse or distribution, you must make clear to others the license
00025  * terms of this work. The best way to do this is by including this header.
00026  * 
00027  * Any of the above conditions can be waived if you get permission from the
00028  * copyright holder.
00029  * 
00030  * Apart from the remix rights granted under this license, nothing in this
00031  * license impairs or restricts the author's moral rights.
00032  */
00033 
00034 
00046 #include <jwsc/config.h>
00047 
00048 #include <stdlib.h>
00049 #include <stdio.h>
00050 #include <ctype.h>
00051 #include <assert.h>
00052 #include <inttypes.h>
00053 
00054 #include "jwsc/base/file_io.h"
00055 
00056 
00065 int skip_fp_spaces_and_comments(FILE* fp)
00066 {
00067     char c;
00068     int done;
00069 
00070     done = 0;
00071 
00072     while (!done && (c = fgetc(fp)) != EOF)
00073     {
00074         if (c == '#')
00075         {
00076             while ((c = fgetc(fp)) != EOF && c != '\n') 
00077                 ;
00078             if (c == EOF) 
00079                 done = 1;
00080         }
00081         else if (!isspace(c))
00082         {
00083             ungetc(c, fp);
00084             done = 1;
00085         }
00086     }
00087 
00088     return (c != EOF);
00089 }
00090 
00091 
00100 int skip_line(FILE* fp)
00101 {
00102     char c;
00103 
00104     while ((c = fgetc(fp)) != EOF && c != '\n') ;
00105 
00106     return (c != EOF);
00107 }
00108 
00109 
00118 int count_fp_line_columns(FILE* fp)
00119 {
00120     int  count;
00121     int  done;
00122     int  in_column;
00123     char c;
00124 
00125     count     = 0;
00126     done      = 0;
00127     in_column = 0;
00128 
00129     while(!done)
00130     {
00131         if ((c = fgetc(fp)) == EOF || c == '\n')
00132         {
00133             done = 1;
00134         }
00135         else if (isspace(c))
00136         {
00137             in_column = 0;
00138         }
00139         else if (!in_column)
00140         {
00141             count++;
00142             in_column = 1;
00143         }
00144     }
00145 
00146     return count;
00147 }