putcol.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright (c) 1983-2013 Trevor Wishart and Composers Desktop Project Ltd
  3. * http://www.trevorwishart.co.uk
  4. * http://www.composersdesktop.com
  5. *
  6. This file is part of the CDP System.
  7. The CDP System is free software; you can redistribute it
  8. and/or modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. The CDP System is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with the CDP System; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  18. 02111-1307 USA
  19. *
  20. */
  21. /*
  22. * Insert a column of figures, in a textfile,
  23. * into an existing file
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <ctype.h>
  28. #include <string.h>
  29. #include <math.h>
  30. FILE *fp2, *fp1, *fpout;
  31. #define PROG "PUTCOL"
  32. #define NEW_COL_SPACE (128)
  33. #define ENDOFSTR '\0'
  34. #define NEWLINE '\n'
  35. #define TAB '\t'
  36. void usage(void), logo(void);
  37. char *skip_column(char *,int,char *);
  38. int sloom = 0;
  39. int sloombatch = 0;
  40. int ignore_s_and_e = 0, ignore_c = 0;
  41. char errstr[400];
  42. const char* cdp_version = "7.1.0";
  43. int main(int argc,char *argv[])
  44. {
  45. char temp[400], temp2[400], *p, c;
  46. int n, skip = 0, column, cnt=0, replace = 0, end_col = 0, keep = 1, outlinecnt, newlen;
  47. char **line = (char **)0;
  48. if(argc==2 && (strcmp(argv[1],"--version") == 0)) {
  49. fprintf(stdout,"%s\n",cdp_version);
  50. fflush(stdout);
  51. return 0;
  52. }
  53. if(argc < 2)
  54. usage();
  55. if(!strcmp(argv[1],"#")) {
  56. sloom = 1;
  57. argv++;
  58. argc--;
  59. } else if (!strcmp(argv[1],"##")) {
  60. p = argv[0];
  61. argc--;
  62. argv++;
  63. argv[0] = p;
  64. sloombatch = 1;
  65. }
  66. if(!strcmp(argv[argc-1],"-e")) {
  67. ignore_s_and_e = 1;
  68. argc--;
  69. }
  70. if(!strcmp(argv[argc-1],"-ec")) {
  71. ignore_c = 1;
  72. argc--;
  73. }
  74. if(argc<6 || argc>7)
  75. usage();
  76. if(!sloom) {
  77. if(!strcmp(argv[3],argv[2]) || !strcmp(argv[3],argv[1])) {
  78. fprintf(stdout,"ERROR: Cannot use same file for input and ouput.\n");
  79. fflush(stdout);
  80. usage();
  81. }
  82. if((fpout = fopen(argv[3],"w"))==NULL) {
  83. fprintf(stdout,"ERROR: Cannot open file '%s'\n",argv[3]);
  84. fflush(stdout);
  85. usage();
  86. }
  87. }
  88. if((fp1 = fopen(argv[1],"r"))==NULL) {
  89. fprintf(stdout,"ERROR: Cannot open file '%s'\n",argv[1]);
  90. fflush(stdout);
  91. usage();
  92. }
  93. if((fp2 = fopen(argv[2],"r"))==NULL) {
  94. fprintf(stdout,"ERROR: Cannot open file '%s'\n",argv[2]);
  95. fflush(stdout);
  96. usage();
  97. }
  98. if(sscanf(argv[4],"%d",&column)!=1) {
  99. fprintf(stdout,"ERROR: Cannot read column value.\n");
  100. fflush(stdout);
  101. usage();
  102. }
  103. if(column<1) {
  104. fprintf(stdout,"ERROR: Column value cannot be less than 1.\n");
  105. fflush(stdout);
  106. usage();
  107. }
  108. column--;
  109. if(argc==7 && sscanf(argv[6],"%d",&skip)!=1) {
  110. fprintf(stdout,"ERROR: Cannot read skip value.\n");
  111. fflush(stdout);
  112. usage();
  113. }
  114. if(skip<0) {
  115. skip = -skip;
  116. keep = 0;
  117. }
  118. if(*argv[5]++!='-') {
  119. fprintf(stdout,"ERROR: Unknown parameter %s.\n",--argv[5]);
  120. fflush(stdout);
  121. usage();
  122. }
  123. switch(*argv[5]) {
  124. case('r'): replace = 1; break;
  125. case('i'): break;
  126. default:
  127. fprintf(stdout,"ERROR: Unknown flag -%c\n",*argv[5]);
  128. fflush(stdout);
  129. usage();
  130. }
  131. outlinecnt = 0;
  132. if(skip > 0) {
  133. if(keep) {
  134. if((line = (char **)malloc(skip * sizeof(char *)))==NULL) {
  135. fprintf(stdout,"ERROR: Out of memory\n");
  136. fflush(stdout);
  137. }
  138. }
  139. for(n=0;n<skip;n++) {
  140. if(fgets(temp,400,fp2)!=temp) {
  141. fprintf(stdout,"ERROR: Failed to get skippable line %d in file '%s'\n",n+1,argv[1]);
  142. fflush(stdout);
  143. exit(1);
  144. }
  145. if(keep) {
  146. if((line[outlinecnt] = (char *)malloc((strlen(temp)+2) * sizeof(char)))==NULL) {
  147. fprintf(stdout,"ERROR: Out of memory\n");
  148. fflush(stdout);
  149. }
  150. line[outlinecnt][0] = ENDOFSTR;
  151. strcat(line[outlinecnt],temp);
  152. outlinecnt++;
  153. }
  154. }
  155. }
  156. while(fgets(temp,400,fp2)!=NULL) {
  157. /*RWD*/ p = temp;
  158. /*RWD*/ while(isspace(*p)) p++;
  159. /*and*/ if(*p=='\n' || *p == ENDOFSTR){
  160. if((line = (char **)realloc((char *)line,(outlinecnt+1) * sizeof(char *)))==NULL) {
  161. fprintf(stdout,"ERROR: Out of memory\n");
  162. fflush(stdout);
  163. }
  164. if((line[outlinecnt] = (char *)malloc(2 * sizeof(char)))==NULL) {
  165. fprintf(stdout,"ERROR: Out of memory\n");
  166. fflush(stdout);
  167. }
  168. line[outlinecnt][0] = ENDOFSTR;
  169. outlinecnt++;
  170. continue;
  171. }
  172. if((ignore_s_and_e && (!strcmp(temp,"e\n") || !strcmp(temp,"e") || !strcmp(temp,"s\n") || !strncmp(temp,";",1)))
  173. || (ignore_c && !strncmp(temp,";",1))) {
  174. if(line==(char **)0) {
  175. if((line = (char **)malloc((outlinecnt+1) * sizeof(char *)))==NULL) {
  176. fprintf(stdout,"ERROR: Out of memory\n");
  177. fflush(stdout);
  178. }
  179. } else {
  180. if((line = (char **)realloc((char *)line,(outlinecnt+1) * sizeof(char *)))==NULL) {
  181. fprintf(stdout,"ERROR: Out of memory\n");
  182. fflush(stdout);
  183. }
  184. }
  185. if((line[outlinecnt] = (char *)malloc((strlen(temp)+2) * sizeof(char)))==NULL) {
  186. fprintf(stdout,"ERROR: Out of memory\n");
  187. fflush(stdout);
  188. }
  189. line[outlinecnt][0] = ENDOFSTR;
  190. strcat(line[outlinecnt],temp);
  191. outlinecnt++;
  192. continue;
  193. }
  194. cnt++;
  195. end_col = 0;
  196. for(n=0;n<column;n++) {
  197. if(*p==ENDOFSTR) {
  198. fprintf(stdout,"ERROR: Not enough columns in line %d in origfile.\n",cnt);
  199. fflush(stdout);
  200. exit(1);
  201. }
  202. p = skip_column(p,cnt,argv[1]);
  203. }
  204. if(*p==ENDOFSTR) {
  205. if(replace) {
  206. fprintf(stdout,"ERROR: Not enough columns in line %d in origfile.\n",cnt);
  207. fflush(stdout);
  208. exit(1);
  209. } else {
  210. p--; /* HANG ONTO NEWLINE */
  211. if(*p != NEWLINE)
  212. p++;
  213. end_col = 1;
  214. }
  215. }
  216. c = *p;
  217. *p = ENDOFSTR;
  218. if(line==(char **)0) {
  219. if((line = (char **)malloc((outlinecnt+1) * sizeof(char *)))==NULL) {
  220. fprintf(stdout,"ERROR: Out of memory\n");
  221. fflush(stdout);
  222. }
  223. } else {
  224. if((line = (char **)realloc((char *)line,(outlinecnt+1) * sizeof(char *)))==NULL) {
  225. fprintf(stdout,"ERROR: Out of memory\n");
  226. fflush(stdout);
  227. }
  228. }
  229. if((line[outlinecnt] = (char *)malloc((strlen(temp)+2+NEW_COL_SPACE) * sizeof(char)))==NULL) {
  230. fprintf(stdout,"ERROR: Out of memory\n");
  231. fflush(stdout);
  232. }
  233. line[outlinecnt][0] = ENDOFSTR;
  234. strcat(line[outlinecnt],temp);
  235. if(end_col)
  236. strcat(line[outlinecnt],"\t");
  237. *p = c;
  238. if(replace) {
  239. p = skip_column(p,cnt,argv[1]);
  240. if(*p==ENDOFSTR) {
  241. p--; /* KEEP NEWLINE !!! */
  242. if(*p != NEWLINE)
  243. p++;
  244. end_col = 1;
  245. }
  246. }
  247. strcpy(temp2,p);
  248. if(fgets(temp,400,fp1)==NULL) {
  249. fprintf(stdout,"ERROR: No more vals in file %s at line %d\n",argv[1],cnt);
  250. fflush(stdout);
  251. break;
  252. }
  253. p = temp + strlen(temp);
  254. p--; /* DELETE NEWLINE */
  255. if(*p != NEWLINE)
  256. p++;
  257. if(!end_col)
  258. *p++ = TAB;
  259. *p = ENDOFSTR;
  260. strcat(temp,temp2);
  261. newlen = strlen(line[outlinecnt]) + strlen(temp) + 2;
  262. if((line[outlinecnt] = (char *)realloc((char *)line[outlinecnt],newlen * sizeof(char)))==NULL) {
  263. fprintf(stdout,"ERROR: Memory problem.\n");
  264. fflush(stdout);
  265. }
  266. strcat(line[outlinecnt],temp);
  267. outlinecnt++;
  268. }
  269. if(sloom) {
  270. if((fpout = fopen(argv[3],"w"))==NULL) {
  271. fprintf(stdout,"ERROR: Cannot reopen file '%s'\n",argv[3]);
  272. fflush(stdout);
  273. usage();
  274. }
  275. }
  276. for(n=0;n<outlinecnt;n++) {
  277. if(!sloom && !sloombatch) {
  278. strcat(line[n],"\n");
  279. fputs(line[n],fpout);
  280. } else
  281. fprintf(stdout,"INFO: %s\n",line[n]);
  282. }
  283. fflush(stdout);
  284. fclose(fp2);
  285. fclose(fp1);
  286. return 0;
  287. }
  288. /******************************** USAGE *****************************/
  289. void usage(void)
  290. {
  291. if(!sloom) {
  292. logo();
  293. fprintf(stderr,"USAGE : putcol columnfile intofile outfile colno -r|-i [[-]skiplines] [-e]\n\n");
  294. fprintf(stderr,"COLUMNFILE File with NEW column.\n");
  295. fprintf(stderr,"INTOFILE File INTO which the new column goes.\n");
  296. fprintf(stderr,"OUTFILE Resultant file.\n");
  297. fprintf(stderr,"COLNO POSITION of the column to be added.\n");
  298. fprintf(stderr,"-r REPLACE original column.\n");
  299. fprintf(stderr,"-i INSERT new column amongst original columns.\n");
  300. fprintf(stderr,"SKIPLINES SKIP OVER skiplines before inserting column.\n");
  301. fprintf(stderr,"-SKIPLINES THROW AWAY skiplines: then insert column.\n");
  302. fprintf(stderr,"-e Ignore lines which contain only 'e' or 's'.\n");
  303. fprintf(stderr," and comment lines starting with ';'\n");
  304. fprintf(stderr," (skipped lines should still be counted).\n");
  305. }
  306. exit(1);
  307. }
  308. /*************************** SKIP_COLUMN **************************/
  309. char *skip_column(char *p,int cnt,char *argv)
  310. {
  311. if(*p==ENDOFSTR) {
  312. fprintf(stdout,"ERROR: Not enough columns in line %d file %s\n",cnt,argv);
  313. fflush(stdout);
  314. exit(0);
  315. }
  316. while(!isspace(*p)) {
  317. p++;
  318. if(*p==ENDOFSTR) return(p);
  319. }
  320. while(isspace(*p)) {
  321. p++;
  322. if(*p==ENDOFSTR) return(p);
  323. }
  324. return(p);
  325. }
  326. /******************************** LOGO() **********************************/
  327. void logo(void)
  328. { printf("\t ***************************************************\n");
  329. printf("\t * COMPOSERS DESKTOP PROJECT *\n");
  330. printf("\t %s $Revision: 1.3 $ \n",PROG);
  331. printf("\t * *\n");
  332. printf("\t * Insert a column of figures, in a textfile, *\n");
  333. printf("\t * into an existing file. *\n");
  334. printf("\t * *\n");
  335. printf("\t * by TREVOR WISHART *\n");
  336. printf("\t ***************************************************\n\n");
  337. }