| 1 | // =============================================================== // |
|---|
| 2 | // // |
|---|
| 3 | // File : arb_replace.cxx // |
|---|
| 4 | // Purpose : // |
|---|
| 5 | // // |
|---|
| 6 | // Institute of Microbiology (Technical University Munich) // |
|---|
| 7 | // http://www.arb-home.de/ // |
|---|
| 8 | // // |
|---|
| 9 | // =============================================================== // |
|---|
| 10 | |
|---|
| 11 | #include <arb_strbuf.h> |
|---|
| 12 | #include <arb_file.h> |
|---|
| 13 | #include <arbdb.h> |
|---|
| 14 | |
|---|
| 15 | int ARB_main(int argc, char *argv[]) { |
|---|
| 16 | char *data; |
|---|
| 17 | char *ndata; |
|---|
| 18 | FILE *out; |
|---|
| 19 | int arg; |
|---|
| 20 | const char *eval; |
|---|
| 21 | const char *fname; |
|---|
| 22 | int linemode = false; |
|---|
| 23 | int delete_empty_lines = false; |
|---|
| 24 | int startarg; |
|---|
| 25 | int patchmode = false; |
|---|
| 26 | |
|---|
| 27 | if (argc <= 1 || (argc >= 2 && strcmp(argv[1], "--help") == 0)) { |
|---|
| 28 | printf("syntax: arb_replace [-l/L/p] \"old=newdata\" [filepattern]\n"); |
|---|
| 29 | printf(" -l linemode, parse each line separately\n"); |
|---|
| 30 | printf(" -L linemode, parse each line separately, delete empty lines\n"); |
|---|
| 31 | printf(" -p patchmode, (no wildcards allowed, rightside<leftside)\n"); |
|---|
| 32 | return -1; |
|---|
| 33 | } |
|---|
| 34 | if (!strcmp(argv[1], "-l")) { |
|---|
| 35 | eval = argv[2]; |
|---|
| 36 | linemode = true; |
|---|
| 37 | startarg = 3; |
|---|
| 38 | } |
|---|
| 39 | else if (!strcmp(argv[1], "-L")) { |
|---|
| 40 | eval = argv[2]; |
|---|
| 41 | linemode = true; |
|---|
| 42 | delete_empty_lines = true; |
|---|
| 43 | startarg = 3; |
|---|
| 44 | } |
|---|
| 45 | else if (!strcmp(argv[1], "-p")) { |
|---|
| 46 | eval = argv[2]; |
|---|
| 47 | patchmode = true; |
|---|
| 48 | startarg = 3; |
|---|
| 49 | } |
|---|
| 50 | else { |
|---|
| 51 | eval = argv[1]; |
|---|
| 52 | startarg = 2; |
|---|
| 53 | } |
|---|
| 54 | int usestdin = false; |
|---|
| 55 | if (startarg==argc) { |
|---|
| 56 | usestdin = true; // stdin stdout |
|---|
| 57 | argc++; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | for (arg = startarg; arg<argc; arg++) { |
|---|
| 61 | if (usestdin) { |
|---|
| 62 | fname = "-"; |
|---|
| 63 | } |
|---|
| 64 | else { |
|---|
| 65 | fname = argv[arg]; |
|---|
| 66 | } |
|---|
| 67 | data = GB_read_file(fname); |
|---|
| 68 | if (data) { |
|---|
| 69 | if (patchmode) { |
|---|
| 70 | char *evaldup = strdup(eval); |
|---|
| 71 | char *right = strchr(evaldup, '='); |
|---|
| 72 | |
|---|
| 73 | int result = 0; |
|---|
| 74 | if (!right) { |
|---|
| 75 | fprintf(stderr, "'=' not found in replace string\n"); |
|---|
| 76 | result = -1; |
|---|
| 77 | } |
|---|
| 78 | else { |
|---|
| 79 | if (strlen(right) > strlen(evaldup)) { |
|---|
| 80 | fprintf(stderr, "You cannot replace a shorter string by a longer one!!!\n"); |
|---|
| 81 | result = -1; |
|---|
| 82 | } |
|---|
| 83 | else { |
|---|
| 84 | *(right++) = 0; |
|---|
| 85 | |
|---|
| 86 | int leftsize = strlen(evaldup); |
|---|
| 87 | unsigned long size = GB_size_of_file(fname)-leftsize; |
|---|
| 88 | |
|---|
| 89 | bool patched = false; |
|---|
| 90 | for (unsigned long i=0; i<size; i++) { |
|---|
| 91 | if (!strncmp(data+i, evaldup, leftsize)) { |
|---|
| 92 | strcpy(data+i, right); |
|---|
| 93 | patched = true; |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | if (patched) { |
|---|
| 97 | out = fopen(fname, "w"); |
|---|
| 98 | if (out) { |
|---|
| 99 | if (fwrite(data, (unsigned int)size, 1, out) != 1) { |
|---|
| 100 | fprintf(stderr, "Write failed %s\n", fname); |
|---|
| 101 | result = -1; |
|---|
| 102 | } |
|---|
| 103 | else { |
|---|
| 104 | fprintf(out, "%s", data); |
|---|
| 105 | printf("File %s parsed\n", fname); |
|---|
| 106 | } |
|---|
| 107 | fclose(out); |
|---|
| 108 | } |
|---|
| 109 | else { |
|---|
| 110 | fprintf(stderr, "Write failed %s\n", fname); |
|---|
| 111 | result = -1; |
|---|
| 112 | } |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | free(evaldup); |
|---|
| 117 | return result; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | if (linemode) { |
|---|
| 121 | char *p = data; |
|---|
| 122 | char *nextp; |
|---|
| 123 | GBS_strstruct buf(1024); |
|---|
| 124 | |
|---|
| 125 | while ((nextp = strchr(p, '\n'))) { |
|---|
| 126 | nextp[0] = 0; // remove '\n' |
|---|
| 127 | char *h = GBS_string_eval(p, eval); |
|---|
| 128 | if (!h) { |
|---|
| 129 | h = strdup (p); |
|---|
| 130 | fprintf(stderr, "%s\n", GB_await_error()); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | if (usestdin) { |
|---|
| 134 | fprintf(stdout, "%s", h); |
|---|
| 135 | if (h[0] || !delete_empty_lines) { |
|---|
| 136 | fprintf(stdout, "\n"); |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | else { |
|---|
| 140 | buf.cat(h); |
|---|
| 141 | if (h[0] || !delete_empty_lines) { // delete empty lines |
|---|
| 142 | buf.put('\n'); // insert '\n' |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | p = nextp+1; |
|---|
| 146 | nextp[0] = '\n'; // reinsert '\n' |
|---|
| 147 | free(h); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | char *h = GBS_string_eval(p, eval); |
|---|
| 151 | buf.cat(h); |
|---|
| 152 | ndata = buf.release(); |
|---|
| 153 | free(h); |
|---|
| 154 | } |
|---|
| 155 | else { |
|---|
| 156 | ndata = GBS_string_eval(data, eval); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | if (!ndata) { |
|---|
| 160 | fprintf(stderr, "%s\n", GB_await_error()); |
|---|
| 161 | exit(-1); |
|---|
| 162 | } |
|---|
| 163 | if (strcmp(data, ndata)) { |
|---|
| 164 | if (usestdin) { |
|---|
| 165 | fprintf(stdout, "%s", ndata); |
|---|
| 166 | } |
|---|
| 167 | else { |
|---|
| 168 | out = fopen(fname, "w"); |
|---|
| 169 | if (out) { |
|---|
| 170 | fprintf(out, "%s", ndata); |
|---|
| 171 | fclose(out); |
|---|
| 172 | printf("File %s parsed\n", fname); |
|---|
| 173 | } |
|---|
| 174 | else { |
|---|
| 175 | printf("cannot write %s\n", fname); |
|---|
| 176 | } |
|---|
| 177 | } |
|---|
| 178 | } |
|---|
| 179 | free(ndata); |
|---|
| 180 | free(data); |
|---|
| 181 | } |
|---|
| 182 | } |
|---|
| 183 | return 0; |
|---|
| 184 | } |
|---|