home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / a / bin / modules-.2 / modules- / modules-1.2.8 / depmod / fgets.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-19  |  2.5 KB  |  106 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "link.h"
  4.  
  5. /*
  6.     Read a line of a configuration file and process continuation line.
  7.     Return buf, or NULL si eof
  8.     Blank at the end of line are always stripped.
  9.     Everything following comcar is a comment. The line is cut before.
  10. */
  11. char *fgets_strip (
  12.     char *buf,
  13.     int sizebuf,
  14.     FILE *fin,
  15.     char contcar,        // Continuation char, generally backslash
  16.     char comcar,        // Comment character, generally #
  17.     int *noline,        // This will be updated and contain the line number
  18.                         // Can be NULL
  19.     int *empty)            // Will be != 0 if the line is empty and did not
  20.                         // even hold a comment.
  21. {
  22.     int nocomment = 1;        // No comments found ?
  23.     int contline=0;
  24.     char *debut = buf;
  25.     char *ret = NULL;
  26.     *buf = '\0';
  27.     *empty = 1;
  28.     while (fgets(buf,sizebuf,fin)!=NULL){
  29.         char *end = strip_end (buf);
  30.         char *pt = strchr(buf,comcar);
  31.         if (pt != NULL){
  32.             nocomment = 0;
  33.             *pt = '\0';
  34.             end = strip_end (buf);
  35.         }
  36.         if (noline != NULL) (*noline)++;
  37.         ret = debut;
  38.         if (contline){
  39.             char *pt = str_skip(buf);
  40.             if (pt > buf+1){
  41.                 strcpy (buf+1,pt);
  42.                 buf[0] = ' ';
  43.                 end-=(int)(pt-buf)-1;
  44.             }else if (pt == buf+1){
  45.                 buf[0] = ' ';
  46.             }
  47.         }
  48.         if (end > buf && *(end-1) == contcar){
  49.             if (end == buf+1 || *(end-2) != contcar){
  50.                 /* Continuation demandé */
  51.                 contline = 1;
  52.                 end--;
  53.                 *end = '\0';
  54.                 buf = end;
  55.             }else{
  56.                 *(end-1) = '\0';
  57.                 break;
  58.             }
  59.         }else{
  60.             break;
  61.         }
  62.     }
  63.     *empty = debut[0] == '\0' && nocomment;
  64.     return ret;
  65. }        
  66.  
  67. /*
  68.     Read a line of a configuration file and process continuation line.
  69.     Return buf, or NULL si eof
  70.     Blank at the end of line are always stripped.
  71.     Everything following comcar is a comment. The line is cut before.
  72. */
  73. char *fgets_strip (
  74.     char *buf,
  75.     int sizebuf,
  76.     FILE *fin,
  77.     char contcar,        // Continuation char, generally backslash
  78.     char comcar,        // Comment character, generally #
  79.     int *noline)        // This will be updated and contain the line number
  80.                         // Can be NULL
  81. {
  82.     int empty;
  83.     return fgets_strip (buf,sizebuf,fin,contcar,comcar,noline,&empty);
  84. }
  85.  
  86. /*
  87.     Read a line of a configuration file and process continuation line.
  88.     Return buf, or NULL si eof
  89.     Blank at the end of line are always stripped.
  90.     Everything following comcar is a comment. The line is cut before.
  91.  
  92.     Continuation character is \
  93.     Comment character is #
  94. */
  95. char *fgets_strip (
  96.     char *buf,
  97.     int sizebuf,
  98.     FILE *fin,
  99.     int *noline)        // This will be updated and contain the line number
  100.                         // Can be NULL
  101. {
  102.     int empty;
  103.     return fgets_strip (buf,sizebuf,fin,'\\','#',noline,&empty);
  104. }
  105.  
  106.