File: arb_replace.cxx

package info (click to toggle)
arb 6.0.6-8
  • links: PTS, VCS
  • area: non-free
  • in suites: sid, trixie
  • size: 66,204 kB
  • sloc: ansic: 394,911; cpp: 250,290; makefile: 19,644; sh: 15,879; perl: 10,473; fortran: 6,019; ruby: 683; xml: 503; python: 53; awk: 32
file content (185 lines) | stat: -rw-r--r-- 6,697 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// =============================================================== //
//                                                                 //
//   File      : arb_replace.cxx                                   //
//   Purpose   :                                                   //
//                                                                 //
//   Institute of Microbiology (Technical University Munich)       //
//   http://www.arb-home.de/                                       //
//                                                                 //
// =============================================================== //

#include <arb_strbuf.h>
#include <arb_file.h>
#include <arbdb.h>

int ARB_main(int argc, char *argv[]) {
    char       *data;
    char       *ndata;
    FILE       *out;
    int         arg;
    const char *eval;
    const char *fname;
    int         linemode           = false;
    int         delete_empty_lines = false;
    int         startarg;
    int         patchmode          = false;

    if (argc <= 1 || (argc >= 2 && strcmp(argv[1], "--help") == 0)) {
        printf("syntax: arb_replace [-l/L/p] \"old=newdata\" [filepattern]\n");
        printf("        -l      linemode, parse each line separately\n");
        printf("        -L      linemode, parse each line separately, delete empty lines\n");
        printf("        -p      patchmode, (no wildcards allowed, rightside<leftside)\n");
        return -1;
    }
    if (!strcmp(argv[1], "-l")) {
        eval               = argv[2];
        linemode           = true;
        startarg           = 3;
    }
    else if (!strcmp(argv[1], "-L")) {
        eval               = argv[2];
        linemode           = true;
        delete_empty_lines = true;
        startarg           = 3;
    }
    else if (!strcmp(argv[1], "-p")) {
        eval      = argv[2];
        patchmode = true;
        startarg  = 3;
    }
    else {
        eval     = argv[1];
        startarg = 2;
    }
    int usestdin = false;
    if (startarg==argc) {
        usestdin = true;                            // stdin stdout
        argc++;
    }

    for (arg = startarg; arg<argc; arg++) {
        if (usestdin) {
            fname = "-";
        }
        else {
            fname = argv[arg];
        }
        data = GB_read_file(fname);
        if (data) {
            if (patchmode) {
                char *evaldup = strdup(eval);
                char *right   = strchr(evaldup, '=');

                int result = 0;
                if (!right) {
                    fprintf(stderr, "'=' not found in replace string\n");
                    result = -1;
                }
                else {
                    if (strlen(right) > strlen(evaldup)) {
                        fprintf(stderr, "You cannot replace a shorter string by a longer one!!!\n");
                        result = -1;
                    }
                    else {
                        *(right++) = 0;

                        int           leftsize = strlen(evaldup);
                        unsigned long size     = GB_size_of_file(fname)-leftsize;

                        bool patched = false;
                        for (unsigned long i=0; i<size; i++) {
                            if (!strncmp(data+i, evaldup, leftsize)) {
                                strcpy(data+i, right);
                                patched = true;
                            }
                        }
                        if (patched) {
                            out = fopen(fname, "w");
                            if (out) {
                                if (fwrite(data, (unsigned int)size, 1, out) != 1) {
                                    fprintf(stderr, "Write failed %s\n", fname);
                                    result = -1;
                                }
                                else {
                                    fprintf(out, "%s", data);
                                    printf("File %s parsed\n", fname);
                                }
                                fclose(out);
                            }
                            else {
                                fprintf(stderr, "Write failed %s\n", fname);
                                result = -1;
                            }
                        }
                    }
                }
                free(evaldup);
                return result;
            }

            if (linemode) {
                char          *p         = data;
                char          *nextp;
                char          *h;
                GBS_strstruct *strstruct = GBS_stropen(1024);

                while ((nextp = strchr(p, '\n'))) {
                    nextp[0] = 0;                       // remove '\n'
                    h = GBS_string_eval(p, eval, 0);
                    if (!h) {
                        h = strdup (p);
                        fprintf(stderr, "%s\n", GB_await_error());
                    }

                    if (usestdin) {
                        fprintf(stdout, "%s", h);
                        if (h[0] || !delete_empty_lines) {
                            fprintf(stdout, "\n");
                        }
                    }
                    else {
                        GBS_strcat(strstruct, h);
                        if (h[0] || !delete_empty_lines) {      // delete empty lines
                            GBS_chrcat(strstruct, '\n');         // insert '\n'
                        }
                    }
                    p = nextp+1;
                    nextp[0] = '\n';            // reinsert '\n'
                    free(h);
                }
                h = GBS_string_eval(p, eval, 0);
                GBS_strcat(strstruct, h);
                ndata = GBS_strclose(strstruct);
                free(h);

            }
            else {
                ndata = GBS_string_eval(data, eval, 0);
            }

            if (!ndata) {
                fprintf(stderr, "%s\n", GB_await_error());
                exit(-1);
            }
            if (strcmp(data, ndata)) {
                if (usestdin) {
                    fprintf(stdout, "%s", ndata);
                }
                else {
                    out = fopen(fname, "w");
                    if (out) {
                        fprintf(out, "%s", ndata);
                        fclose(out);
                        printf("File %s parsed\n", fname);
                    }
                    else {
                        printf("cannot write %s\n", fname);
                    }
                }
            }
            free(ndata);
            free(data);
        }
    }
    return 0;
}