lsflib/src/get_line.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Dec 2008 17:58:02 +0100
changeset 9 c3fecc82ade6
parent 6 c3dc3eb3b541
child 29 7abf6146898e
permissions -rw-r--r--
standard tags for git projects committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9
c3fecc82ade6 standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 6
diff changeset
     1
/**
c3fecc82ade6 standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 6
diff changeset
     2
 * test/demos/lsflib/src/get_line.c
c3fecc82ade6 standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 6
diff changeset
     3
 * Copyright (C) 2008 Markus Broeker
6
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 */
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
#include <stdio.h>
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
#include <stdlib.h>
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <string.h>
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
#include <lsf.h>
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
#ifndef LINE_LENGTH
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
#define LINE_LENGTH 100
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
#endif
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
char *get_line (const char *filename, const char *cmd, size_t size)
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
{
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
    int i, j;
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
    char *line, *ptr;
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
    FILE *f = fopen (filename, "r");
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
    if (!f) {
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
        perror ("FOPEN:");
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
        return NULL;
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
    }
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
    line = malloc (LINE_LENGTH + 1);
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
    ptr = malloc ((LINE_LENGTH + 1) * size);
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
    i = 0;
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
    while (fgets (line, LINE_LENGTH, f) != NULL) {
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
        if (strstr (line, cmd)) {
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
            do {
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
                if ((strlen (ptr) + strlen (line)) < LINE_LENGTH * size) {
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
                    strcat (ptr, line);
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
                    fgets (line, LINE_LENGTH, f);
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
                    i++;
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
                } else {
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
                    printf ("OVERFLOW: [%d]: %s\n", i * LINE_LENGTH, line);
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
                    exit (0);
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
                }
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
            } while (strchr (line, '"') == NULL);
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
            if (i > 1)
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
                strcat (ptr, line);
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
        }
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
        for (i = 0; i < strlen (ptr); i++)
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
            switch (ptr[i]) {
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
            case '\n':
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
            case '\\':
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
            case '"':
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
                for (j = i; j < strlen (ptr) - 1; j++)
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
                    ptr[j] = ptr[j + 1];
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
                ptr[j] = 0;
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
                break;
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
            }
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
    }
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    57
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
    fclose (f);
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    59
    free (line);
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    60
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    61
    return ptr;
c3dc3eb3b541 a small libtool demo added to the demo distribution
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    62
}