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