Statistics
| Branch: | Revision:

root / src / utils / opp_replace.c @ master

History | View | Annotate | Download (2.98 KB)

1 01873262 Georg Kunz
/*
2
 * Title: Mini (sed like) replacement application
3
 * Author: Balazs KELEMEN (kelemen@sch.bme.hu)
4
 * Date: Mar 14 2000
5
 *
6
 * Description: Replaces <pattern> with <value> in <proto-file> file
7
 *     and write out result to <ouput-file> file.
8
 *     Using - (minus) in place of filenames means stdin and stdout.
9
 */
10
11
#include <stdio.h>
12
#include <string.h>
13
#include <errno.h>
14
15
#define BUF_L         128
16
#define CACHE_L (BUF_L-1)
17
18
#define OFFSET(i)       (i % BUF_L)
19
20
static int _readOffs;
21
static int _writeOffs;
22
char _buffer[BUF_L];
23
24
25
void writeValue(FILE *fp, char *val);
26
void emptyCache(FILE *fp, int toOffs);
27
28
int main(int argc, char *argv[])
29
{
30
    char *protoFn, *outputFn, *pattern, *val;
31
    FILE *protoFp, *outputFp;
32
    int patternOffs;
33
    int patternL;
34
    int isWriting;
35
36
    if(argc < 5)
37
    {
38
        fprintf(stderr, "Usage:\n\t%s <proto-file> <output-file> <pattern> <value>\n", argv[0]);
39
        return 1;
40
    }
41
42
    protoFn = argv[1];
43
    outputFn = argv[2];
44
    pattern = argv[3];
45
    patternL = strlen(pattern);
46
    val = argv[4];
47
48
    if(patternL > CACHE_L)
49
    {
50
        fprintf(stderr, "Sorry! Maximum allowed pattern length is %d\n",
51
                CACHE_L);
52
        return 1;
53
    }
54
55
    /* open prototype input */
56
    if(strcmp(protoFn, "-") == 0)
57
    {
58
        protoFp = stdin;
59
    }
60
    else if((protoFp = fopen(protoFn, "rt")) == NULL)
61
    {
62
        fprintf(stderr, "Can't open prototype file: '%s': %s\n", protoFn,
63
                strerror(errno) );
64
        return 1;
65
    }
66
67
    /* open output */
68
    if(strcmp(outputFn, "-") == 0)
69
    {
70
        outputFp = stdout;
71
    }
72
    else if((outputFp = fopen(outputFn, "wt")) == NULL)
73
    {
74
        fprintf(stderr, "Can't open output file: '%s': %s\n", outputFn,
75
                strerror(errno) );
76
        return 1;
77
    }
78
79
    _readOffs = 0;
80
    _writeOffs = 0;
81
    patternOffs = 0;
82
    isWriting = 0;
83
    while((_buffer[OFFSET(_readOffs)] = fgetc(protoFp)) != EOF)
84
    {
85
        /* check match */
86
        if(_buffer[OFFSET(_readOffs)] == pattern[patternOffs])
87
        {
88
            patternOffs++;
89
            if(patternOffs == patternL)
90
            {
91
                emptyCache(outputFp, _readOffs-patternL+1);
92
                isWriting = 0;
93
                writeValue(outputFp, val);
94
                patternOffs = 0;
95
                _writeOffs = _readOffs;
96
                continue;
97
            }
98
        }
99
        else
100
        {
101
            patternOffs = 0;
102
        }
103
104
        if((_readOffs - _writeOffs) >= CACHE_L)
105
            isWriting = 1;
106
107
        if(isWriting)
108
        {
109
            fputc(_buffer[OFFSET(_writeOffs)], outputFp);
110
            _writeOffs++;
111
        }
112
113
        _readOffs++;
114
    } /* while */
115
116
    emptyCache(outputFp, _readOffs);
117
118
    fclose(outputFp);
119
    fclose(protoFp);
120
121
    return 0;
122
} /* main */
123
124
125
void writeValue(FILE *fp, char *val)
126
{
127
    fwrite(val, 1, strlen(val), fp);
128
    return;
129
}
130
131
void emptyCache(FILE *fp, int toOffs)
132
{
133
    while(_writeOffs != toOffs)
134
    {
135
        fputc(_buffer[OFFSET(_writeOffs)], fp);
136
        _writeOffs++;
137
    }
138
139
    return;
140
}