lsflib/src/get_line.c
changeset 6 c3dc3eb3b541
child 9 c3fecc82ade6
equal deleted inserted replaced
5:d752cbe8208e 6:c3dc3eb3b541
       
     1 /*
       
     2  *  $Id: get_line.c 94 2008-04-05 01:27:30Z mbroeker $
       
     3  * $URL: http://localhost/svn/c/lsflib/trunk/src/get_line.c $
       
     4  *
       
     5  */
       
     6 
       
     7 #include <stdio.h>
       
     8 #include <stdlib.h>
       
     9 #include <string.h>
       
    10 
       
    11 #include <lsf.h>
       
    12 
       
    13 #ifndef LINE_LENGTH
       
    14 #define LINE_LENGTH 100
       
    15 #endif
       
    16 
       
    17 char *get_line (const char *filename, const char *cmd, size_t size)
       
    18 {
       
    19     int i, j;
       
    20     char *line, *ptr;
       
    21     FILE *f = fopen (filename, "r");
       
    22 
       
    23     if (!f) {
       
    24         perror ("FOPEN:");
       
    25         return NULL;
       
    26     }
       
    27 
       
    28     line = malloc (LINE_LENGTH + 1);
       
    29     ptr = malloc ((LINE_LENGTH + 1) * size);
       
    30     i = 0;
       
    31 
       
    32     while (fgets (line, LINE_LENGTH, f) != NULL) {
       
    33         if (strstr (line, cmd)) {
       
    34             do {
       
    35                 if ((strlen (ptr) + strlen (line)) < LINE_LENGTH * size) {
       
    36                     strcat (ptr, line);
       
    37                     fgets (line, LINE_LENGTH, f);
       
    38                     i++;
       
    39                 } else {
       
    40                     printf ("OVERFLOW: [%d]: %s\n", i * LINE_LENGTH, line);
       
    41                     exit (0);
       
    42                 }
       
    43             } while (strchr (line, '"') == NULL);
       
    44             if (i > 1)
       
    45                 strcat (ptr, line);
       
    46         }
       
    47         for (i = 0; i < strlen (ptr); i++)
       
    48             switch (ptr[i]) {
       
    49             case '\n':
       
    50             case '\\':
       
    51             case '"':
       
    52                 for (j = i; j < strlen (ptr) - 1; j++)
       
    53                     ptr[j] = ptr[j + 1];
       
    54                 ptr[j] = 0;
       
    55                 break;
       
    56             }
       
    57     }
       
    58 
       
    59     fclose (f);
       
    60     free (line);
       
    61 
       
    62     return ptr;
       
    63 }