home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 024 / psi110g.zip / GETLINE.C < prev    next >
C/C++ Source or Header  |  1994-04-17  |  2KB  |  79 lines

  1. /* getline.c version 1.0 PA0GRI */
  2. #include <ctype.h>
  3. #include "global.h"
  4.   
  5. /*
  6.  *  getline()
  7.  *  to read a line from a domain file and return a usefull line
  8.  *  completely assembled (if multy line)
  9.  *  It skips any connent lines. It follows rfc 1133 , 1134 , 1135 , 1136.
  10.  */
  11. char *
  12. getline(fp)
  13. FILE *fp;
  14. {
  15.     char *line;
  16.     char *contline;
  17.     char *chrptr1;
  18.     char *chrptr2;
  19.     int s1,s2;
  20.     int loop = 1;
  21.   
  22.     if(fp == NULLFILE)
  23.         return NULLCHAR;    /* just in case */
  24.     line = mallocw(256);        /* get buffer space */
  25.     while(loop){
  26.         if(fgets(line,256,fp) == NULL){
  27.             free(line);
  28.             return NULLCHAR;
  29.         }
  30.         if(line[0] == '#' || line[0] == ';')
  31.             continue;   /* skip comment lines */
  32.         if((s1 = strlen(line)) < 2)
  33.             continue;   /* empty line */
  34.         loop = 0;       /* none of above */
  35.     }
  36.     line[s1-1] = '\0';      /* kill nl */
  37.     if((chrptr1 = strchr(line,';')) != NULLCHAR){
  38.         *chrptr1 = '\0';    /* eliminate comment */
  39.         s1 = strlen(line);  /* recompute line size */
  40.     }
  41.     if((chrptr1 = strchr(line,'(')) != NULLCHAR){ /* continuation */
  42.         *chrptr1 = ' ';         /* replace with space */
  43.         if((chrptr2 = strchr(line,')')) != NULLCHAR){ /* continuation */
  44.             *chrptr2 = ' ';     /* replace with space */
  45.             return line;        /* complete line */
  46.         }
  47.         loop = 1;
  48.     }
  49.     contline = mallocw(256);    /* get buffer space */
  50.     while(loop){
  51.         if(fgets(contline,256,fp) == NULL){
  52.             free(line);
  53.             free(contline);
  54.             return NULLCHAR;
  55.         }
  56.         s2 = strlen(contline);
  57.         contline[s2-1] = '\0';  /* kill nl */
  58.         if(contline[0] == '#' || contline[0] == ';')
  59.             continue;   /* skip comment lines */
  60.         if((s2 = strlen(contline)) < 2)
  61.             continue;   /* empty line */
  62.         if((chrptr1 = strchr(contline,';')) != NULLCHAR){
  63.             *chrptr1 = '\0';    /* eliminate comment */
  64.             s2 = strlen(contline);  /* recompute line size */
  65.         }
  66.         chrptr1 = mallocw(s1+s2+2);
  67.         sprintf(chrptr1,"%s %s",line,contline);
  68.         free(line);
  69.         line = chrptr1;
  70.         if((chrptr2 = strchr(line,')')) != NULLCHAR){ /* continuation */
  71.             *chrptr2 = ' ';     /* replace with space */
  72.             loop = 0;
  73.         }
  74.   
  75.     }
  76.     free(contline);
  77.     return line;
  78. }
  79.