getcol.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. * get Nth column from a file of columned data
  23. * (skipping M lines at start of file)
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <ctype.h>
  28. #include <math.h>
  29. #include <string.h>
  30. #define PROG "GETCOL"
  31. #define ENDOFSTR '\0'
  32. FILE *fp1, *fpout;
  33. char *skip_column(char *,int,char *);
  34. char *skip_column_wanted(char *,int,char *);
  35. void usage(void), logo(void);
  36. int ignore_s_and_e = 0, ignore_c = 0;
  37. int sloom = 0;
  38. int sloombatch = 0;
  39. const char* cdp_version = "7.1.0";
  40. int main(int argc,char *argv[])
  41. {
  42. char temp[2000], *p, *q;
  43. int n, skip=0, column, cnt=0;
  44. if(argc==2 && (strcmp(argv[1],"--version") == 0)) {
  45. fprintf(stdout,"%s\n",cdp_version);
  46. fflush(stdout);
  47. return 0;
  48. }
  49. /* VERSION 3 */
  50. if(argc < 2)
  51. usage();
  52. if(!strcmp(argv[1],"#")) {
  53. sloom = 1;
  54. argv++;
  55. argc--;
  56. } else if (!strcmp(argv[1],"##")) {
  57. p = argv[0];
  58. argc--;
  59. argv++;
  60. argv[0] = p;
  61. sloombatch = 1;
  62. }
  63. if(!strcmp(argv[argc-1],"-e")) {
  64. ignore_s_and_e = 1;
  65. argc--;
  66. } else if(!strcmp(argv[argc-1],"-ec")) {
  67. ignore_c = 1;
  68. argc--;
  69. }
  70. if(argc<4 || argc>5)
  71. usage();
  72. if((fp1 = fopen(argv[1],"r"))==NULL) {
  73. fprintf(stdout,"ERROR: Cannot open file '%s'\n",argv[1]);
  74. fflush(stdout);
  75. usage();
  76. }
  77. if((fpout = fopen(argv[2],"w"))==NULL) {
  78. fprintf(stdout,"ERROR: Cannot open file '%s'\n",argv[2]);
  79. fflush(stdout);
  80. usage();
  81. }
  82. if(sscanf(argv[3],"%d",&column)!=1) {
  83. fprintf(stdout,"ERROR: Cannot read column value.\n");
  84. fflush(stdout);
  85. usage();
  86. }
  87. if(column<1) {
  88. fprintf(stdout,"ERROR: Column value cannot be less than 1.\n");
  89. fflush(stdout);
  90. usage();
  91. }
  92. column--;
  93. if(argc==5 && sscanf(argv[4],"%d",&skip)!=1) {
  94. fprintf(stdout,"ERROR: Cannot read skip value.\n");
  95. fflush(stdout);
  96. usage();
  97. }
  98. for(n=0;n<skip;n++) {
  99. if(fgets(temp,200,fp1)!=temp) {
  100. fprintf(stdout,"ERROR: Failed to get skippable line %d in file '%s'\n",
  101. n+1,argv[1]);
  102. fflush(stdout);
  103. exit(1);
  104. }
  105. q = temp; /* IGNORE EMPTY LINES */
  106. p = temp;
  107. while(*p != ENDOFSTR) {
  108. if(isspace(*p))
  109. q++;
  110. p++;
  111. }
  112. if(p==q)
  113. n--;
  114. }
  115. while(fgets(temp,2000,fp1)!=NULL) {
  116. cnt++;
  117. p = temp;
  118. /*RWD*/ while(isspace(*p)) p++;
  119. /*and*/ if(*p=='\n' || *p==ENDOFSTR){
  120. if(!sloom && !sloombatch)
  121. fputs("\n",fpout);
  122. else
  123. fprintf(stdout,"INFO: \n");
  124. continue;
  125. }
  126. if(ignore_s_and_e && (!strcmp(temp,"e\n") || !strcmp(temp,"e") || !strcmp(temp,"s\n") || !strncmp(temp,";",1)))
  127. continue;
  128. if(ignore_c && !strncmp(temp,";",1))
  129. continue;
  130. for(n=0;n<column;n++) {
  131. if(*p==ENDOFSTR) {
  132. fprintf(stdout,"ERROR: Not enough columns in line %d.\n",cnt);
  133. fflush(stdout);
  134. exit(1);
  135. }
  136. p = skip_column(p,cnt,argv[1]);
  137. }
  138. q = p;
  139. if(*p==ENDOFSTR) {
  140. fprintf(stdout,"ERROR: Not enough columns in line %d.\n",cnt);
  141. fflush(stdout);
  142. exit(1);
  143. }
  144. p = skip_column_wanted(p,cnt,argv[1]);
  145. if(*p==ENDOFSTR) {
  146. p--; /* DELETE NEWLINE */
  147. if(*p!='\n')
  148. p++;
  149. }
  150. *p = ENDOFSTR;
  151. if(!sloom && !sloombatch) {
  152. fputs(q,fpout);
  153. fputs("\n",fpout);
  154. } else
  155. fprintf(stdout,"INFO: %s\n",q);
  156. }
  157. fflush(stdout);
  158. return 0;
  159. }
  160. /******************************** USAGE *****************************/
  161. void usage(void)
  162. {
  163. if(!sloom && !sloombatch) {
  164. logo();
  165. fprintf(stderr,"USAGE : getcol infile outfile colno [skiplines] [-e]\n\n");
  166. fprintf(stderr,"skiplines Ignores this number of lines at start of file.\n");
  167. fprintf(stderr,"-e Ignores lines with a single 'e' or 's' (as in CSound scores)\n");
  168. fprintf(stderr," and comment lines starting with ';'\n");
  169. fprintf(stderr," (skipped lines should still be counted).\n");
  170. }
  171. exit(1);
  172. }
  173. /*************************** SKIP_COLUMN **************************/
  174. char *skip_column(char *p,int cnt,char *argv)
  175. {
  176. if(*p==ENDOFSTR) {
  177. fprintf(stdout,"ERROR: Not enough columns in line %d file %s\n",cnt,argv);
  178. fflush(stdout);
  179. exit(0);
  180. }
  181. while(!isspace(*p)) {
  182. p++;
  183. if(*p==ENDOFSTR)
  184. return(p);
  185. }
  186. while(isspace(*p)) {
  187. p++;
  188. if(*p==ENDOFSTR)
  189. return(p);
  190. }
  191. return(p);
  192. }
  193. /*************************** SKIP_COLUMN_WANTED **************************/
  194. char *skip_column_wanted(char *p,int cnt,char *argv)
  195. {
  196. if(*p==ENDOFSTR) {
  197. fprintf(stdout,"ERROR: Not enough columns in line %d file %s\n",cnt,argv);
  198. fflush(stdout);
  199. exit(0);
  200. }
  201. while(!isspace(*p++)) {
  202. if(*p==ENDOFSTR)
  203. return(p);
  204. }
  205. return(p);
  206. }
  207. /******************************** LOGO() **********************************/
  208. void logo(void)
  209. {
  210. printf("\t ***************************************************\n");
  211. printf("\t * COMPOSERS DESKTOP PROJECT *\n");
  212. printf("\t %s $Revision: 1.2 $\n",PROG);
  213. printf("\t * *\n");
  214. printf("\t * get Nth column from a file of columned data *\n");
  215. printf("\t * (skipping M lines at start of file). *\n");
  216. printf("\t * *\n");
  217. printf("\t * by TREVOR WISHART *\n");
  218. printf("\t ***************************************************\n\n");
  219. }