vectors.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752
  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. /* Manipulate 2 columns of numbers (vectors) */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <math.h>
  25. #include <string.h>
  26. #include <ctype.h>
  27. #include <osbind.h>
  28. #include <time.h>
  29. #define PROG "VECTORS"
  30. #define BIGARRAY 200
  31. #define FLTERR 0.000002
  32. #define ENDOFSTR '\0'
  33. #include <sfsys.h>
  34. //#ifdef unix
  35. #define round lround
  36. //#endif
  37. int strgetfloat(char **,double *);
  38. double miditohz(double);
  39. char *exmalloc(int), *exrealloc(char *,int);
  40. void logo(void), usage(void), help(void);
  41. void read_flags(int,int,char *[]);
  42. int do_infile(char *,double **), do_outfile(char *);
  43. void check_usage(int argc,char *argv[]), truncate_vectors(void);
  44. void do_add(void), do_subtract(void), do_multiply(void), do_mean(void);
  45. void do_divide(void), do_overwrite(void), do_insert(void);
  46. void do_interleave(void), do_power(void), do_randadd(void);
  47. void do_patterned_interleave(void);
  48. void do_randmult(void), do_randscat(void), do_substitute(void);
  49. void do_quantise(void), do_squeezed_pan(void);
  50. void quantise_sort(double *scti,int *scatcnt);
  51. void conjoina(void);
  52. void conjoinb(void);
  53. void conjoin_sort(void);
  54. void do_max(void);
  55. void do_min(void);
  56. void do_keep(void);
  57. void do_del(void);
  58. void compare(void);
  59. void del_copys(void);
  60. void insert_non_dupls(void);
  61. void partition_col(int atpos,int partition_outcol);
  62. void do_keep_in_spans(int is_del);
  63. void do_del_in_spans(void);
  64. void do_keep_cycles(int do_keep);
  65. void do_matches(void);
  66. void do_morph(void);
  67. void do_morphseq(void);
  68. static void do_error(void);
  69. static void do_valout(double val);
  70. static int do_stringfile(char *argv,char ***stringptr);
  71. double other_param;
  72. double *other_params;
  73. char **string0, **string1;
  74. int cnt = 0, cnt2, arraysize = BIGARRAY, ifactor, flagstart;
  75. double *number1, *number2,/* *diffs, scatter, */ factor, errorbnd = FLTERR;
  76. FILE *fp = NULL;
  77. char flag = 0, errstr[400];
  78. int ro = 0;
  79. int sloom = 0;
  80. int sloombatch = 0;
  81. const char* cdp_version = "7.1.0";
  82. int main(int argc,char *argv[])
  83. {
  84. char *p;
  85. fp = stdout;
  86. if(argc==2 && (strcmp(argv[1],"--version") == 0)) {
  87. fprintf(stdout,"%s\n",cdp_version);
  88. fflush(stdout);
  89. return 0;
  90. }
  91. if(argc < 2) {
  92. fprintf(stdout,"ERROR: Invalid command\n");
  93. fflush(stdout);
  94. usage();
  95. }
  96. if(!strcmp(argv[1],"#")) {
  97. sloom = 1;
  98. argv++;
  99. argc--;
  100. } else if (!strcmp(argv[1],"##")) {
  101. p = argv[0];
  102. argc--;
  103. argv++;
  104. argv[0] = p;
  105. sloombatch = 1;
  106. }
  107. check_usage(argc,argv);
  108. if(sloom)
  109. flagstart = 3;
  110. initrand48();
  111. read_flags(flagstart,argc,argv);
  112. if(flag == 'I' && ro == 'P') {
  113. if(((string0 = (char **)exmalloc(arraysize * sizeof(char *)))==NULL)
  114. || ((string1 = (char **)exmalloc(arraysize * sizeof(char *)))==NULL)) {
  115. sprintf(errstr,"Memory allocation failed.\n");
  116. do_error();
  117. }
  118. if((cnt = do_stringfile(argv[1],&string0)) < 1) {
  119. fprintf(stdout,"ERROR: No numeric data in 1st column.\n");
  120. fflush(stdout);
  121. exit(1);
  122. }
  123. if((cnt2 = do_stringfile(argv[2],&string1)) < 1) {
  124. fprintf(stdout,"ERROR: No numeric data in 2nd column.\n");
  125. fflush(stdout);
  126. exit(1);
  127. }
  128. } else {
  129. if(((number1 = (double *)exmalloc(arraysize * sizeof(double)))==NULL)
  130. || ((number2 = (double *)exmalloc(arraysize * sizeof(double)))==NULL)) {
  131. sprintf(errstr,"Memory allocation failed.\n");
  132. do_error();
  133. }
  134. if((cnt = do_infile(argv[1],&number1)) < 1) {
  135. fprintf(stdout,"ERROR: No numeric data in 1st column.\n");
  136. fflush(stdout);
  137. exit(1);
  138. }
  139. if((cnt2 = do_infile(argv[2],&number2)) < 1) {
  140. fprintf(stdout,"ERROR: No numeric data in 2nd column.\n");
  141. fflush(stdout);
  142. exit(1);
  143. }
  144. }
  145. if((flag == 'C' || flag == 'y') && (cnt != cnt2)) {
  146. fprintf(stdout,"ERROR: Columns are not of the same length.\n");
  147. fflush(stdout);
  148. exit(1);
  149. }
  150. if(flag != 'l' && flag != 'K' && flag != 'k' && flag != 'M' && !(flag=='I' && ro=='P'))
  151. truncate_vectors();
  152. switch(flag) {
  153. case('a'): do_add(); break;
  154. case('A'): do_mean(); break;
  155. case('b'): do_max(); break;
  156. case('B'): do_min(); break;
  157. case('d'): do_divide(); break;
  158. case('i'): do_insert(); break;
  159. case('m'): do_multiply(); break;
  160. case('M'):
  161. switch(ro) {
  162. case('O'):do_morph(); break;
  163. case('o'):do_morphseq(); break;
  164. default: do_matches(); break;
  165. }
  166. break;
  167. case('o'): do_overwrite(); break;
  168. case('p'):
  169. switch(ro) {
  170. case('y'): partition_col(1,1); break; /* partition, at given positions in outcol, the input col*/
  171. case('n'): partition_col(0,1); break; /* partition, at other than given positions, the input col */
  172. case('Y'): partition_col(1,0); break; /* partition, at given positions in incol, the output col*/
  173. case('N'): partition_col(0,0); break; /* partition, at other than given positions, the output col */
  174. }
  175. break;
  176. case('q'): do_quantise(); break;
  177. case('s'): do_subtract(); break;
  178. case('C'): compare(); break;
  179. case('I'):
  180. switch(ro) {
  181. case(ENDOFSTR): do_interleave(); break;
  182. case('P'): do_patterned_interleave(); break;
  183. }
  184. break;
  185. case('P'): do_power(); break;
  186. case('S'): do_substitute(); break;
  187. case('l'):
  188. switch(ro) {
  189. case('a'): conjoina(); break;
  190. case('b'): conjoinb(); break;
  191. case('s'): conjoin_sort(); break;
  192. }
  193. break;
  194. case('K'):
  195. switch(ro) {
  196. case('k'): do_keep(); break;
  197. case('d'): do_del(); break;
  198. case('K'): do_keep_in_spans(0); break;
  199. case('D'): do_keep_in_spans(1); break;
  200. case('c'): del_copys(); break;
  201. case('S'): insert_non_dupls(); break;
  202. }
  203. break;
  204. case('k'):
  205. switch(ro) {
  206. case('k'): do_keep_cycles(1); break;
  207. case('d'): do_keep_cycles(0); break;
  208. }
  209. break;
  210. case('R'):
  211. switch(ro) {
  212. case('a'): do_randadd(); break;
  213. case('m'): do_randmult(); break;
  214. case('s'): do_randscat(); break;
  215. }
  216. break;
  217. case('y'):
  218. switch(ro) {
  219. case('z'): do_squeezed_pan(); break;
  220. }
  221. break;
  222. }
  223. fflush(stdout);
  224. return 0;
  225. }
  226. /************************ CHECK_USAGE *************************/
  227. void check_usage(int argc,char *argv[])
  228. {
  229. if(argc==2 && !strncmp(argv[1],"-h",2))
  230. help();
  231. switch(argc) {
  232. case(8):
  233. flagstart = argc-4;
  234. break;
  235. case(6):
  236. flagstart = argc-2;
  237. if(!do_outfile(argv[3])) {
  238. fprintf(stdout,"ERROR: Don't understand.\n");
  239. fflush(stdout);
  240. usage();
  241. }
  242. break;
  243. case(5):
  244. flagstart = argc-1;
  245. if(!do_outfile(argv[3])) {
  246. flagstart--;
  247. }
  248. break;
  249. case(4):
  250. flagstart = argc-1;
  251. break;
  252. case(2):
  253. if(!strncmp(argv[1],"-h",2))
  254. help();
  255. default:
  256. usage();
  257. }
  258. }
  259. /**************************STRGETFLOAT **************************
  260. * takes a pointer TO A POINTER to a string. If it succeeds in finding
  261. * a float it returns the float value (*val), and it's new position in the
  262. * string (*str).
  263. */
  264. int strgetfloat(char **str,double *val)
  265. {
  266. char *p, *q, *end;
  267. double numero;
  268. int point, valid;
  269. for(;;) {
  270. point = 0;
  271. p = *str;
  272. while(isspace(*p))
  273. p++;
  274. q = p;
  275. if(!isdigit(*p) && *p != '.' && *p!='-')
  276. return(0);
  277. if(*p == '.'|| *p == '-') {
  278. if(*p == '-')
  279. p++;
  280. else {
  281. point++;
  282. p++;
  283. }
  284. }
  285. for(;;) {
  286. if(*p == '.') {
  287. if(point)
  288. return(0);
  289. else {
  290. point++;
  291. p++;
  292. continue;
  293. }
  294. }
  295. if(isdigit(*p)) {
  296. p++;
  297. continue;
  298. } else {
  299. if(!isspace(*p) && *p!='\0')
  300. return(0);
  301. else {
  302. end = p;
  303. p = q;
  304. valid = 0;
  305. while(p!=end) {
  306. if(isdigit(*p))
  307. valid++;
  308. p++;
  309. }
  310. if(valid) {
  311. if(sscanf(q,"%lf",&numero)!=1)
  312. return(0);
  313. *val = numero;
  314. *str = end;
  315. return(1);
  316. }
  317. return(0);
  318. }
  319. }
  320. }
  321. }
  322. return(0); /* NOTREACHED */
  323. }
  324. /****************************** USAGE ****************************/
  325. void usage(void)
  326. {
  327. if(!sloom && !sloombatch) {
  328. logo();
  329. fprintf(stdout,"USAGE: vectors column1 column2 [outfile] -flag [-eERROR]\n\n");
  330. fprintf(stdout,"\tcolumnN textfile containing list of numbers.\n");
  331. fprintf(stdout,"\toutfile receives output as text (Default: screen)\n");
  332. fprintf(stdout,"\tflag determines operation.\n");
  333. fprintf(stdout,"\t use 'vectors -h' to see flags.\n");
  334. fprintf(stdout,"\t-e ERROR is acceptable error in checking equality of numbers.\n");
  335. fprintf(stdout,"\t (Default: %lf).\n",FLTERR);
  336. fflush(stdout);
  337. }
  338. exit(1);
  339. }
  340. /****************************** HELP *******************************/
  341. void help(void)
  342. {
  343. fprintf(stdout,
  344. "---------------------- flags for VECTORS -----------------------\n");
  345. fprintf(stdout,"-a ADD add column1 val to column2 val.\n");
  346. fprintf(stdout,"-d DIVIDE divide column1 val by column2 val.\n");
  347. fprintf(stdout,"-q QUANTISE quantise column1 vals onto set of (ascending) vals in column2.\n");
  348. fprintf(stdout,"-o@ OVERWRITE overwrite 1st & every @th val in column1 with column2 val.\n");
  349. fprintf(stdout,"-i@ INSERT insert column2 vals at start & after every @th item\n");
  350. fprintf(stdout," of column1.\n");
  351. fprintf(stdout,"-m MULTIPLY multiply column1 val by column2 val.\n");
  352. fprintf(stdout,"-s SUBTRACT subtract column2 val from column1 val.\n");
  353. fprintf(stdout,"-C COMPARE compare vals in column1 & column2.\n");
  354. fprintf(stdout,"-C@ COMPARE within error range @.\n");
  355. fprintf(stdout,"-I INTERLEAVE column 2 vals with column 1 vals.\n");
  356. fprintf(stdout,"-IP x1 x2 x3 x4 Patterned Interleave, between x1 & x2 vals col1,\n");
  357. fprintf(stdout," then between x3 & x4 vals col2, etc\n");
  358. fprintf(stdout,"-P POWER raise column1 vals (must be >=0) to powers in column2.\n");
  359. fprintf(stdout,"-Ra RANDADD add to column1 val a randval btwn +-val in column2.\n");
  360. fprintf(stdout,"-Rm RANDMULT multiply column1 val by randval between 0 and column2 val.\n");
  361. fprintf(stdout,"-Rs RANDSCATTER random scatter (ascending) column1 vals\n");
  362. fprintf(stdout," by degree of scatter(0-1) in column2.\n");
  363. fprintf(stdout,"-S@ SUBSTITUTE substitute any val @ in column1 by next val in column2.\n");
  364. fflush(stdout);
  365. exit(1);
  366. }
  367. /********************** DO_OUTFILE *****************************/
  368. int do_outfile(char *argv)
  369. {
  370. if(*argv=='-')
  371. return(0);
  372. if((fp = fopen(argv,"w"))==NULL) {
  373. if(!sloom && !sloombatch)
  374. fprintf(stderr,"Cannot open file %s to write.\n",argv);
  375. else {
  376. fprintf(stdout,"ERROR: Cannot open file %s to write.\n",argv);
  377. fflush(stdout);
  378. }
  379. usage();
  380. }
  381. return(1);
  382. }
  383. /******************************** EXMALLOC ****************************/
  384. char *exmalloc(int n)
  385. {
  386. char *p;
  387. //RWD 2025 move to calloc to pacify gcc on Linux, but probably good idea anyway
  388. if((p = (char *)calloc(n,sizeof(char)))==NULL) {
  389. sprintf(errstr,"ERROR: memory allocation failed.\n");
  390. do_error();
  391. }
  392. return(p);
  393. }
  394. /********************************** READ_FLAGS *************************/
  395. /*RWD Nov 2025 renamed flagstart (global decl) to flag_start*/
  396. void read_flags(int flag_start, int argc, char* argv[])
  397. {
  398. char dummy =0;
  399. int bas = flag_start, k;
  400. while (flag_start < argc) {
  401. if (sloom) {
  402. if (flag_start == bas) {
  403. if (*argv[flag_start]++ != '-') {
  404. fprintf(stdout, "ERROR: Cannot interpret program mode\n");
  405. fflush(stdout);
  406. usage();
  407. }
  408. if (sscanf(argv[flag_start]++, "%c", &dummy) != 1) {
  409. fprintf(stdout, "ERROR: Cannot interpret program mode\n");
  410. fflush(stdout);
  411. usage();
  412. }
  413. }
  414. else {
  415. if (argc < 8)
  416. dummy = 'z';
  417. }
  418. }
  419. else {
  420. if (*argv[flag_start]++ != '-') {
  421. fprintf(stdout, "ERROR: Cannot read flag '%s'.\n", --argv[flag_start]);
  422. fflush(stdout);
  423. usage();
  424. }
  425. if (sscanf(argv[flag_start]++, "%c", &dummy) != 1) {
  426. fprintf(stdout, "ERROR: Cannot read flag '%s'\n", argv[flag_start] - 2);
  427. fflush(stdout);
  428. usage();
  429. }
  430. }
  431. switch (dummy) {
  432. case('C'):
  433. flag = dummy;
  434. ro = (int)*argv[flag_start];
  435. if (ro == ENDOFSTR)
  436. factor = 0;
  437. else if (sscanf(argv[flag_start], "%lf", &factor) != 1) {
  438. fprintf(stdout, "ERROR: Cannot read error range.\n");
  439. fflush(stdout);
  440. usage();
  441. }
  442. break;
  443. case('M'):
  444. flag = dummy;
  445. ro = (int)*argv[flag_start];
  446. if ((ro == 'o') || (ro == 'O')) {
  447. argv[flag_start]++;
  448. dummy = (int)*argv[flag_start];
  449. if (dummy == ENDOFSTR) {
  450. fprintf(stdout, "ERROR: Cannot read start entry of morph.\n");
  451. fflush(stdout);
  452. usage();
  453. }
  454. if (sscanf(argv[flag_start], "%lf", &factor) != 1) {
  455. fprintf(stdout, "ERROR: Cannot read start entry of morph.\n");
  456. fflush(stdout);
  457. usage();
  458. }
  459. }
  460. else {
  461. if (ro == ENDOFSTR) {
  462. fprintf(stdout, "ERROR: Cannot read item to match.\n");
  463. fflush(stdout);
  464. usage();
  465. }
  466. if (sscanf(argv[flag_start], "%lf", &factor) != 1) {
  467. fprintf(stdout, "ERROR: Cannot read item to match.\n");
  468. fflush(stdout);
  469. usage();
  470. }
  471. }
  472. break;
  473. case('d'): case('s'): case('m'): case('a'): case('A'):
  474. case('P'): case('q'): case('b'): case('B'):
  475. flag = dummy;
  476. break;
  477. case('R'):
  478. flag = dummy;
  479. ro = (int)*argv[flag_start];
  480. switch (ro) {
  481. case('a'): case('m'): case('s'):
  482. break;
  483. default:
  484. if (!sloom) {
  485. fprintf(stdout, "ERROR: Unknown flag %s.\n", argv[flag_start] - 2);
  486. fflush(stdout);
  487. }
  488. else {
  489. fprintf(stdout, "ERROR: Unknown program mode.\n");
  490. fflush(stdout);
  491. }
  492. usage();
  493. }
  494. break;
  495. case('k'):
  496. flag = dummy;
  497. ro = (int)*argv[flag_start]++;
  498. switch (ro) {
  499. case('k'): case('d'):
  500. if (sscanf(argv[flag_start], "%d", &ifactor) != 1) {
  501. fprintf(stdout, "ERROR: Cannot read cycle count.\n");
  502. fflush(stdout);
  503. usage();
  504. }
  505. break;
  506. default:
  507. fprintf(stdout, "ERROR: Unknown program mode.\n");
  508. fflush(stdout);
  509. usage();
  510. }
  511. break;
  512. case('K'):
  513. flag = dummy;
  514. ro = (int)*argv[flag_start];
  515. switch (ro) {
  516. case('k'): case('d'): case('K'): case('D'): case('c'): case('S'):
  517. break;
  518. default:
  519. if (!sloom) {
  520. fprintf(stdout, "ERROR: Unknown flag %s.\n", argv[flag_start] - 2);
  521. fflush(stdout);
  522. }
  523. else {
  524. fprintf(stdout, "ERROR: Unknown program mode.\n");
  525. fflush(stdout);
  526. }
  527. usage();
  528. }
  529. break;
  530. case('l'):
  531. flag = dummy;
  532. ro = (int)*argv[flag_start];
  533. switch (ro) {
  534. case('a'): case('b'): case('s'):
  535. break;
  536. default:
  537. fprintf(stdout, "ERROR: Unknown program mode.\n");
  538. fflush(stdout);
  539. usage();
  540. }
  541. break;
  542. case('p'):
  543. flag = dummy;
  544. ro = (int)*argv[flag_start]++;
  545. switch (ro) {
  546. case('n'): case('y'): case('N'): case('Y'):
  547. if (sscanf(argv[flag_start], "%lf", &factor) != 1) {
  548. fprintf(stdout, "ERROR: Cannot read parameter value.\n");
  549. fflush(stdout);
  550. usage();
  551. }
  552. errorbnd = 0.0;
  553. break;
  554. default:
  555. fprintf(stdout, "ERROR: Unknown program mode.\n");
  556. fflush(stdout);
  557. usage();
  558. }
  559. break;
  560. case('S'): case('i'): case('o'): case('c'):
  561. flag = dummy;
  562. if (sscanf(argv[flag_start], "%lf", &factor) != 1) {
  563. if (!sloom) {
  564. fprintf(stdout, "Cannot read numerical value with flag 'S'|'i'|'o'|'c'.\n");
  565. fflush(stdout);
  566. }
  567. else {
  568. fprintf(stdout, "ERROR: Cannot read parameter value.\n");
  569. fflush(stdout);
  570. }
  571. usage();
  572. }
  573. ifactor = round(factor);
  574. break;
  575. case('y'):
  576. if (flag_start == bas) {
  577. flag = dummy;
  578. ro = (int)*argv[flag_start];
  579. switch (ro) {
  580. case('z'):
  581. if ((other_params = (double*)malloc(4 * sizeof(double))) == NULL) {
  582. fprintf(stdout, "ERROR: Out of memory.\n");
  583. fflush(stdout);
  584. exit(1);
  585. }
  586. break;
  587. default:
  588. fprintf(stdout, "ERROR: Unknown program mode.\n");
  589. fflush(stdout);
  590. usage();
  591. }
  592. }
  593. else {
  594. k = flag_start - bas - 1;
  595. if (sscanf(argv[flag_start], "%lf", other_params + k) != 1) {
  596. fprintf(stdout, "ERROR: Cannot read parameter %d\n", k + 1);
  597. fflush(stdout);
  598. usage();
  599. }
  600. }
  601. break;
  602. case('I'):
  603. if (flag_start == bas) {
  604. flag = dummy;
  605. ro = (int)*argv[flag_start]++;
  606. switch (ro) {
  607. case(ENDOFSTR):
  608. break;
  609. case('P'):
  610. if ((other_params = (double*)malloc(4 * sizeof(double))) == NULL) {
  611. fprintf(stdout, "ERROR: Out of memory.\n");
  612. fflush(stdout);
  613. exit(1);
  614. }
  615. break;
  616. default:
  617. fprintf(stdout, "ERROR: Unknown program mode.\n");
  618. fflush(stdout);
  619. usage();
  620. }
  621. }
  622. else {
  623. k = flag_start - bas - 1;
  624. if (sscanf(argv[flag_start], "%lf", other_params + k) != 1) {
  625. fprintf(stdout, "ERROR: Cannot read parameter %d\n", k + 1);
  626. fflush(stdout);
  627. usage();
  628. }
  629. }
  630. break;
  631. case('e'):
  632. if (!sloom) {
  633. if (sscanf(argv[flag_start], "%lf", &errorbnd) != 1) {
  634. fprintf(stdout, "ERROR: Cannot read error bound with flag 'e'.\n");
  635. fflush(stdout);
  636. usage();
  637. }
  638. if (errorbnd <= 0.0) {
  639. fprintf(stdout, "ERROR: Error bound impossible.\n");
  640. fflush(stdout);
  641. usage();
  642. }
  643. break;
  644. }
  645. /* fall thro */
  646. default:
  647. if (!sloom) {
  648. fprintf(stdout, "Unknown flag %s.\n", --argv[flag_start]);
  649. fflush(stdout);
  650. usage();
  651. }
  652. else {
  653. if (sscanf(argv[flag_start], "%lf", &other_param) != 1) {
  654. fprintf(stdout, "ERROR: Unknown program parameter '%s'.\n", argv[flag_start]);
  655. fflush(stdout);
  656. usage();
  657. }
  658. if (flag == 'S' || flag == 'p')
  659. errorbnd = other_param;
  660. if (flag == 'M')
  661. ifactor = round(other_param);
  662. }
  663. }
  664. flag_start++;
  665. }
  666. if (flag == 0) {
  667. if (!sloom) {
  668. fprintf(stdout, "ERROR: No action specified.\n");
  669. fflush(stdout);
  670. }
  671. else {
  672. fprintf(stdout, "ERROR: No program mode specified.\n");
  673. fflush(stdout);
  674. }
  675. usage();
  676. }
  677. }
  678. /************************* DO_INFILE ***************************/
  679. int do_infile(char *argv,double **number)
  680. {
  681. int ccnt = 0;
  682. char *p, temp[200];
  683. FILE *fpi;
  684. arraysize = BIGARRAY;
  685. if((fpi = fopen(argv,"r"))==NULL) {
  686. fprintf(stdout,"ERROR: Cannot open infile %s\n",argv);
  687. fflush(stdout);
  688. usage();
  689. }
  690. while(fgets(temp,200,fpi)!=NULL) {
  691. p = temp;
  692. while(strgetfloat(&p,&((*number)[ccnt]))) {
  693. if(++ccnt >= arraysize) {
  694. arraysize += BIGARRAY;
  695. *number = (double *)exrealloc((char *)*number,arraysize*sizeof(double));
  696. }
  697. }
  698. }
  699. *number=(double *)exrealloc((char *)*number,ccnt*sizeof(double));
  700. fclose(fpi);
  701. return(ccnt);
  702. }
  703. /************************* DO_STRINGFILE ***************************/
  704. int do_stringfile(char *argv,char ***stringptr)
  705. {
  706. int ccnt = 0;
  707. char temp[200];
  708. FILE *fpi;
  709. arraysize = BIGARRAY;
  710. if((fpi = fopen(argv,"r"))==NULL) {
  711. fprintf(stdout,"ERROR: Cannot open infile %s\n",argv);
  712. fflush(stdout);
  713. usage();
  714. }
  715. while(fgets(temp,200,fpi)!=NULL) {
  716. if(((*stringptr)[ccnt] = (char *)malloc(strlen(temp)+1)) == NULL) {
  717. fprintf(stdout,"ERROR: No more memory for strings\n");
  718. fflush(stdout);
  719. exit(1);
  720. }
  721. strcpy((*stringptr)[ccnt],temp);
  722. if(++ccnt >= arraysize) {
  723. arraysize += BIGARRAY;
  724. if((*stringptr = (char **)exrealloc((char *)*stringptr,arraysize*sizeof(char *)))== NULL) {
  725. fprintf(stdout,"ERROR: No more memory for strings\n");
  726. fflush(stdout);
  727. exit(1);
  728. }
  729. }
  730. }
  731. fclose(fpi);
  732. return(ccnt);
  733. }
  734. /**************************** FLTEQ *******************************/
  735. int flteq(double f1,double f2,double error_bnd)
  736. {
  737. double upperbnd, lowerbnd;
  738. upperbnd = f2 + error_bnd;
  739. lowerbnd = f2 - error_bnd;
  740. if((f1>upperbnd) || (f1<lowerbnd))
  741. return(0);
  742. return(1);
  743. }
  744. /******************************** LOGO **********************************/
  745. void logo(void)
  746. {
  747. printf("\t ***************************************************\n");
  748. printf("\t * COMPOSERS DESKTOP PROJECT *\n");
  749. printf("\t %s $Revision: 1.1 $\n",PROG);
  750. printf("\t * *\n");
  751. printf("\t * Manipulate 2 columns of numbers (vectors) *\n");
  752. printf("\t * *\n");
  753. printf("\t * by TREVOR WISHART *\n");
  754. printf("\t ***************************************************\n\n");
  755. }
  756. /***************************** EXREALLOC *****************************/
  757. char *exrealloc(char *p,int k)
  758. {
  759. char *q;
  760. if((q = malloc(k))==NULL) {
  761. sprintf(errstr,"ERROR: reallocation of memory failed.\n");
  762. do_error();
  763. }
  764. memcpy(q,p,k);
  765. /*
  766. if((q = realloc(p,k))==NULL) {
  767. sprintf(errstr,"ERROR: reallocation of memory failed.\n");
  768. do_error();
  769. }
  770. */
  771. return(q);
  772. }
  773. /******************************** DO_ADD ****************************/
  774. void do_add(void)
  775. {
  776. int n;
  777. for(n=0;n<cnt;n++)
  778. do_valout(number1[n]+number2[n]);
  779. }
  780. /******************************** DO_MEAN ****************************/
  781. void do_mean(void)
  782. {
  783. int n;
  784. for(n=0;n<cnt;n++)
  785. do_valout((number1[n]+number2[n])/2.0);
  786. }
  787. /******************************** DO_MAX ****************************/
  788. void do_max(void)
  789. {
  790. int n;
  791. for(n=0;n<cnt;n++)
  792. do_valout(max(number1[n],number2[n]));
  793. }
  794. /******************************** DO_MIN ****************************/
  795. void do_min(void)
  796. {
  797. int n;
  798. for(n=0;n<cnt;n++)
  799. do_valout(min(number1[n],number2[n]));
  800. }
  801. /******************************** DO_SUBTRACT ****************************/
  802. void do_subtract(void)
  803. {
  804. int n;
  805. for(n=0;n<cnt;n++)
  806. do_valout(number1[n]-number2[n]);
  807. }
  808. /******************************** DO_MULTIPLY ****************************/
  809. void do_multiply(void)
  810. {
  811. int n;
  812. for(n=0;n<cnt;n++)
  813. do_valout(number1[n]*number2[n]);
  814. }
  815. /******************************** DO_DIVIDE ****************************/
  816. void do_divide(void)
  817. {
  818. int n;
  819. for(n=0;n<cnt;n++) {
  820. if(flteq(number2[n],0.0,FLTERR)) {
  821. if(!sloom) {
  822. fprintf(fp,"INF\n");
  823. fflush(stdout);
  824. } else {
  825. sprintf(errstr,"Division by zero encountered at item %d in 2nd column: Impossible\n",n+1);
  826. do_error();
  827. }
  828. } else
  829. do_valout(number1[n]/number2[n]);
  830. }
  831. }
  832. /******************************** DO_OVERWRITE ****************************/
  833. void do_overwrite(void)
  834. {
  835. int n, m = -1, k = 1;
  836. for(n=0;n<cnt;n++) {
  837. if(n%ifactor)
  838. do_valout(number1[n]);
  839. else {
  840. if(k) {
  841. if(++m >= cnt2) {
  842. fprintf(stdout,"WARNING: Out of values in column2\n");
  843. fflush(stdout);
  844. k = 0;
  845. } else
  846. do_valout(number2[m]);
  847. }
  848. }
  849. }
  850. }
  851. /******************************** DO_INSERT ****************************/
  852. void do_insert(void)
  853. {
  854. int n, m = -1, k = 1;
  855. for(n=0;n<cnt;n++) {
  856. if(k && !(n%ifactor)) {
  857. if(++m >= cnt2) {
  858. fprintf(stdout,"WARNING: Out of values in column2\n");
  859. fflush(stdout);
  860. k = 0;
  861. } else
  862. do_valout(number2[m]);
  863. }
  864. do_valout(number1[n]);
  865. }
  866. }
  867. /******************************** DO_INTERLEAVE ****************************/
  868. void do_interleave(void)
  869. {
  870. int n;
  871. for(n=0;n<cnt;n++) {
  872. if(!sloom) {
  873. fprintf(fp,"%lf\n",number1[n]);
  874. fprintf(fp,"%lf\n",number2[n]);
  875. fflush(stdout);
  876. } else {
  877. fprintf(stdout,"INFO: %lf\n",number1[n]);
  878. fprintf(stdout,"INFO: %lf\n",number2[n]);
  879. }
  880. }
  881. }
  882. /******************************** DO_INTERLEAVE ****************************/
  883. void do_patterned_interleave(void)
  884. {
  885. int n, outcnt[2], incnt[2], range[2], lims[4], which = 0, thiscnt;
  886. char **strs[2];
  887. strs[0] = string0;
  888. strs[1] = string1;
  889. incnt[0] = cnt;
  890. incnt[1] = cnt2;
  891. outcnt[0] = 0;
  892. outcnt[1] = 0;
  893. for(n=0;n<4;n++) {
  894. lims[n] = round(other_params[n]);
  895. if(lims[n] < 0) {
  896. fprintf(stdout,"ERROR: Parameter %d is less than zero: Cannot proceed\n",n+1);
  897. fflush(stdout);
  898. exit(1);
  899. }
  900. }
  901. n = lims[2]; /* change order of params from 01,23 -> 02,13 */
  902. lims[2] = lims[1]; /* so bottom of ranges are 0 and 1 */
  903. lims[1] = n;
  904. range[0] = lims[2] - lims[0]; /* drand over range 0 2, followed by floor, gives ... */
  905. range[1] = lims[3] - lims[1]; /* nums in range 0to0.999 -> 0, nums in range 1to1.999 ->1, and val 2.0 only -> 2 */
  906. /* so vanishingly small probability of generating 2.0 */
  907. range[0]++; /* adding 1 to range, makes equally probable 0,1,or2 will be generated */
  908. range[1]++; /* with vanishingly small probability of generating 3.0 (outside range) */
  909. for(;;) {
  910. thiscnt = (int)(floor(drand48() * range[which]));
  911. if(thiscnt > range[which]) /* safety only : vanishingly small probability of generating number outside range */
  912. thiscnt--;
  913. thiscnt += lims[which];
  914. for(n = 0;n < thiscnt; n++) {
  915. if(outcnt[which] >= incnt[which])
  916. return;
  917. if(!sloom)
  918. fprintf(fp,"%s\n",strs[which][outcnt[which]++]);
  919. else
  920. fprintf(fp,"INFO: %s\n",strs[which][outcnt[which]++]);
  921. fflush(stdout);
  922. }
  923. which = !which;
  924. }
  925. }
  926. /******************************** DO_POWER ****************************/
  927. void do_power(void)
  928. {
  929. int n, is_neg;
  930. double output;
  931. for(n=0;n<cnt;n++) {
  932. is_neg = 0;
  933. if(number1[n]<0.0) {
  934. number1[n] = -number1[n];
  935. is_neg = 1;
  936. }
  937. output = pow(number1[n],number2[n]);
  938. if(is_neg)
  939. output = -output;
  940. do_valout(output);
  941. }
  942. }
  943. /******************************** DO_RANDADD ****************************/
  944. void do_randadd(void)
  945. {
  946. int n;
  947. double sum;
  948. for(n=0;n<cnt;n++) {
  949. sum = ((drand48() * 2.0) - 1.0) * number2[n];
  950. do_valout(number1[n] + sum);
  951. }
  952. }
  953. /******************************** DO_RANDMULT ****************************/
  954. void do_randmult(void)
  955. {
  956. int n;
  957. double sum;
  958. for(n=0;n<cnt;n++) {
  959. sum = drand48() * number2[n];
  960. do_valout(number1[n] * sum);
  961. }
  962. }
  963. /******************************** DO_RANDSCAT ****************************/
  964. void do_randscat(void)
  965. {
  966. int n;
  967. double *diffs, scatter;
  968. if(number2[0]<0.0 || number2[0]>1.0) {
  969. sprintf(errstr,"1st nuber in 2nd column (%lf) is out of range for this option.\n",number2[0]);
  970. do_error();
  971. }
  972. diffs = (double *)exmalloc((cnt-1)*sizeof(double));
  973. for(n=0;n<cnt-1;n++) {
  974. diffs[n] = number1[n+1] - number1[n];
  975. if(number2[n]<0.0 || number2[n]>1.0) {
  976. sprintf(errstr,"COLUMN2[%d] = %lf is out of range for this option.\n",n+1,number2[n]);
  977. do_error();
  978. }
  979. diffs[n] /= 2.0;
  980. }
  981. for(n=1;n<cnt-1;n++) {
  982. scatter = ((drand48() * 2.0) - 1.0) * number2[n-1];
  983. if(scatter > 0.0)
  984. number1[n] += diffs[n] * scatter;
  985. else
  986. number1[n] += diffs[n-1] * scatter;
  987. }
  988. for(n=0;n<cnt;n++)
  989. do_valout(number1[n]);
  990. }
  991. /******************************** DO_SUBSTITUTE ****************************/
  992. void do_substitute(void)
  993. {
  994. int n, m = 0;
  995. for(n=0;n<cnt;n++) {
  996. if(flteq(number1[n],factor,errorbnd)) {
  997. number1[n] = number2[m];
  998. if(++m >= cnt2) {
  999. fprintf(stdout,"WARNING: out of numbers in column2\n");
  1000. fflush(stdout);
  1001. break;
  1002. }
  1003. }
  1004. }
  1005. for(n=0;n<cnt;n++)
  1006. do_valout(number1[n]);
  1007. }
  1008. /******************************** DO_QUANTISE ****************************/
  1009. void do_quantise(void)
  1010. {
  1011. int n, m;
  1012. quantise_sort(number2,&cnt2);
  1013. for(n=0;n<cnt;n++) {
  1014. m = 0;
  1015. while(number1[n] > number2[m]) {
  1016. if(++m>=cnt2)
  1017. break;
  1018. }
  1019. if(m==0) { number1[n] = number2[0]; continue; }
  1020. if(m==cnt2) { number1[n] = number2[cnt2-1]; continue; }
  1021. if(number2[m]-number1[n]<number1[n]-number2[m-1])
  1022. number1[n] = number2[m];
  1023. else
  1024. number1[n] = number2[m-1];
  1025. }
  1026. for(n=0;n<cnt;n++)
  1027. do_valout(number1[n]);
  1028. }
  1029. /*************************** TRUNCATE_VECTORS ************************/
  1030. void truncate_vectors(void)
  1031. {
  1032. int cntdiff;
  1033. if(flag!='S' && flag!='q' && flag!='i'&& flag!='o') {
  1034. if((cntdiff = cnt2 - cnt)>0) {
  1035. if(!sloom && !sloombatch)
  1036. fprintf(stdout,"column2 > column1 : truncating column2.\n");
  1037. else
  1038. fprintf(stdout,"WARNING: column2 > column1 : truncating column2.\n");
  1039. fflush(stdout);
  1040. } else if(cntdiff<0) {
  1041. if(!sloom && !sloombatch)
  1042. fprintf(stdout,"column1 > column2 : truncating column1.\n");
  1043. else
  1044. fprintf(stdout,"WARNING: column1 > column2 : truncating column1.\n");
  1045. fflush(stdout);
  1046. cnt = cnt2;
  1047. }
  1048. }
  1049. }
  1050. /*********************** QUANTISE_SORT *******************************
  1051. *
  1052. * Sort set of doubles into ascending order.
  1053. */
  1054. void quantise_sort(double *scti,int *scatcnt)
  1055. {
  1056. double sct;
  1057. int n, m;
  1058. for(n=0;n<((*scatcnt)-1);n++) {
  1059. for(m=n+1;m<(*scatcnt);m++) {
  1060. if(*(scti+m)<*(scti+n)) {
  1061. sct = *(scti+m);
  1062. *(scti+m) = *(scti+n);
  1063. *(scti+n) = sct;
  1064. }
  1065. }
  1066. }
  1067. for(n=0;n<((*scatcnt)-1);n++) {
  1068. if(flteq(*(scti+n+1),*(scti+n),FLTERR)) {
  1069. for(m = n; m < ((*scatcnt)-1); m++)
  1070. *(scti+m) = *(scti+m+1);
  1071. (*scatcnt)--;
  1072. n--;
  1073. }
  1074. }
  1075. }
  1076. /******************************** CONJOINA ****************************/
  1077. void conjoina(void)
  1078. {
  1079. int n;
  1080. for(n=0;n<cnt;n++)
  1081. fprintf(stdout,"INFO: %lf\n",number1[n]);
  1082. for(n=0;n<cnt2;n++)
  1083. fprintf(stdout,"INFO: %lf\n",number2[n]);
  1084. }
  1085. /******************************** CONJOINB ****************************/
  1086. void conjoinb(void)
  1087. {
  1088. int n;
  1089. for(n=0;n<cnt2;n++)
  1090. fprintf(stdout,"INFO: %lf\n",number2[n]);
  1091. for(n=0;n<cnt;n++)
  1092. fprintf(stdout,"INFO: %lf\n",number1[n]);
  1093. }
  1094. /******************************** CONJOIN_SORT ****************************/
  1095. void conjoin_sort(void)
  1096. {
  1097. int n = 0,m = 0, done = 0;
  1098. while(!done) {
  1099. if(number1[n] <= number2[m]) {
  1100. fprintf(stdout,"INFO: %lf\n",number1[n]);
  1101. if(++n >= cnt)
  1102. done = 1;
  1103. } else {
  1104. fprintf(stdout,"INFO: %lf\n",number2[m]);
  1105. if(++m >= cnt2)
  1106. done = 1;
  1107. }
  1108. } /* Either 1 list is unfinished, or neither list is unfinished */
  1109. /* so print elements in whichever list is still unfinished */
  1110. while(n < cnt) {
  1111. fprintf(stdout,"INFO: %lf\n",number1[n]);
  1112. n++;
  1113. }
  1114. while(m < cnt2) {
  1115. fprintf(stdout,"INFO: %lf\n",number2[m]);
  1116. m++;
  1117. }
  1118. fflush(stdout);
  1119. }
  1120. /******************************** DO_KEEP ****************************/
  1121. void do_keep(void)
  1122. {
  1123. int n, i, warned = 0;
  1124. for(n=0;n<cnt2;n++) {
  1125. i = (int)(round(number2[n]));
  1126. if(i < 1) {
  1127. fprintf(stdout,"ERROR: There is no such position as %lf (%d)\n",number2[n],i);
  1128. fflush(stdout);
  1129. exit(1);
  1130. }
  1131. i--;
  1132. if(i < cnt)
  1133. fprintf(stdout,"INFO: %lf\n",number1[i]);
  1134. else if(!warned) {
  1135. fprintf(stdout,"WARNING: There is no such position as %lf (%d)\n",number2[n],i);
  1136. fflush(stdout);
  1137. warned = 1;
  1138. }
  1139. }
  1140. fflush(stdout);
  1141. }
  1142. /******************************** DO_DEL ****************************/
  1143. void do_del(void)
  1144. {
  1145. int n, m, elimcnt, i, j, *delpos, warned = 0;
  1146. if((delpos = (int *)malloc(cnt2 * sizeof(int)))==NULL) {
  1147. fprintf(stdout,"ERROR: Out of memory.\n");
  1148. fflush(stdout);
  1149. exit(1);
  1150. }
  1151. elimcnt = 0;
  1152. for(n=0;n<cnt2;n++) {
  1153. i = (int)(round(number2[n]));
  1154. if(i < 1) {
  1155. fprintf(stdout,"ERROR: There is no such position as %lf (%d)\n",number2[n],i);
  1156. fflush(stdout);
  1157. exit(1);
  1158. }
  1159. i--;
  1160. if(i < cnt)
  1161. delpos[elimcnt++] = i;
  1162. else if(!warned) {
  1163. fprintf(stdout,"WARNING: There is no such position as %lf (%d)\n",number2[n],i);
  1164. fflush(stdout);
  1165. warned = 1;
  1166. }
  1167. }
  1168. for(n=0;n<elimcnt-1;n++) { /* eliminate duplicates */
  1169. for(m=n+1;m<elimcnt;m++) {
  1170. if(delpos[m] == delpos[n]) { /* if values are equal */
  1171. i = m;
  1172. j = m+1;
  1173. while(j < elimcnt) { /* If there are vals beyond here, shuffle down to eliminate delpos[m] */
  1174. delpos[i] = delpos[j];
  1175. i++;
  1176. j++;
  1177. }
  1178. elimcnt--; /* total number of items reduced by 1 */
  1179. m--; /* comparison cnt stays where it is */
  1180. }
  1181. }
  1182. }
  1183. for(n=0;n<elimcnt-1;n++) { /* sort into ascending order */
  1184. for(m=n+1;m<elimcnt;m++) {
  1185. if(delpos[m] < delpos[n]) {
  1186. i = delpos[m];
  1187. delpos[m] = delpos[n];
  1188. delpos[n] = i;
  1189. }
  1190. }
  1191. }
  1192. i = 0;
  1193. for(n=0;n<cnt;n++) { /* print all values not listed for elimination */
  1194. if(i < elimcnt) {
  1195. if(n != delpos[i])
  1196. fprintf(stdout,"INFO: %lf\n",number1[n]);
  1197. else
  1198. i++;
  1199. } else
  1200. fprintf(stdout,"INFO: %lf\n",number1[n]);
  1201. }
  1202. fflush(stdout);
  1203. }
  1204. /******************************** COMPARE ****************************/
  1205. void compare(void)
  1206. {
  1207. int n;
  1208. double upbnd, lobnd;
  1209. if(factor == 0) {
  1210. for(n = 0;n<cnt;n++) {
  1211. if(number1[n] != number2[n]) {
  1212. if(!sloom && !sloombatch)
  1213. fprintf(stdout,"Columns differ at item %d.\n",n+1);
  1214. else
  1215. fprintf(stdout,"WARNING: Columns differ at item %d.\n",n+1);
  1216. fflush(stdout);
  1217. exit(1);
  1218. }
  1219. }
  1220. if(!sloom && !sloombatch)
  1221. fprintf(stdout,"Columns are the same.\n");
  1222. else
  1223. fprintf(stdout,"WARNING: Columns are the same.\n");
  1224. } else {
  1225. for(n = 0;n<cnt;n++) {
  1226. upbnd = number1[n] + factor;
  1227. lobnd = number1[n] - factor;
  1228. if(number2[n] > upbnd || number2[n] < lobnd) {
  1229. if(!sloom && !sloombatch)
  1230. fprintf(stdout,"Columns differ by more than permitted error at item %d.\n",n+1);
  1231. else
  1232. fprintf(stdout,"WARNING: Columns differ by more than permitted error at item %d.\n",n+1);
  1233. fflush(stdout);
  1234. exit(1);
  1235. }
  1236. }
  1237. if(!sloom && !sloombatch)
  1238. fprintf(stdout,"Columns are equivalent within error range %lf\n",factor);
  1239. else
  1240. fprintf(stdout,"WARNING: Columns are equivalent within error range %lf\n",factor);
  1241. }
  1242. fflush(stdout);
  1243. }
  1244. /******************************** DEL_COPYS ****************************/
  1245. void del_copys(void)
  1246. {
  1247. int n, m, dupl;
  1248. for(n=0;n<cnt;n++) {
  1249. dupl = 0;
  1250. for(m=0;m<cnt2;m++) {
  1251. if(flteq(number1[n],number2[m],FLTERR)) {
  1252. dupl = 1;
  1253. break;
  1254. }
  1255. }
  1256. if(!dupl)
  1257. fprintf(stdout,"INFO: %lf\n",number1[n]);
  1258. }
  1259. fflush(stdout);
  1260. }
  1261. /******************************** INSERT_NON_DUPLS ****************************/
  1262. void insert_non_dupls(void)
  1263. {
  1264. int n, m, k, dupl;
  1265. double temp;
  1266. if((number1 = (double *)realloc((char *)number1,(cnt + cnt2) * sizeof(double))) == NULL) {
  1267. fprintf(stdout,"ERROR: Out of memory.\n");
  1268. fflush(stdout);
  1269. exit(1);
  1270. }
  1271. k = cnt;
  1272. for(m=0;m<cnt2;m++) {
  1273. dupl = 0;
  1274. for(n=0;n<cnt;n++) {
  1275. if(flteq(number1[n],number2[m],FLTERR)) {
  1276. dupl = 1;
  1277. break;
  1278. }
  1279. }
  1280. if(!dupl)
  1281. number1[k++] = number2[m];
  1282. }
  1283. cnt = k;
  1284. for(n=0;n<cnt-1;n++) {
  1285. for(m = n; m<cnt; m++) {
  1286. if(number1[m] < number1[n]) {
  1287. temp = number1[n];
  1288. number1[n] = number1[m];
  1289. number1[m] = temp;
  1290. }
  1291. }
  1292. }
  1293. for(n=0;n<cnt;n++)
  1294. fprintf(stdout,"INFO: %lf\n",number1[n]);
  1295. fflush(stdout);
  1296. }
  1297. /******************************** INSERT_NON_DUPLS ****************************/
  1298. void partition_col(int atpos,int partition_incol)
  1299. {
  1300. double *numbera, *numberb;
  1301. int n;
  1302. if(partition_incol) {
  1303. numbera = number2;
  1304. numberb = number1;
  1305. } else {
  1306. numbera = number1;
  1307. numberb = number2;
  1308. }
  1309. if(atpos) {
  1310. for(n = 0;n < cnt;n++) {
  1311. if(flteq(numbera[n],factor,errorbnd))
  1312. fprintf(stdout,"INFO: %lf\n",numberb[n]);
  1313. }
  1314. } else {
  1315. for(n = 0;n < cnt;n++) {
  1316. if(!flteq(numbera[n],factor,errorbnd))
  1317. fprintf(stdout,"INFO: %lf\n",numberb[n]);
  1318. }
  1319. }
  1320. fflush(stdout);
  1321. }
  1322. /******************************** DO_KEEP_IN_SPANS ****************************/
  1323. void do_keep_in_spans(int is_del)
  1324. {
  1325. int n,m;
  1326. double lo, hi;
  1327. if((cnt2<2) || (cnt2&1)) {
  1328. fprintf(stdout,"ERROR: Values in column two are not paired correctly.\n");
  1329. fflush(stdout);
  1330. exit(1);
  1331. }
  1332. if(!cnt) {
  1333. fprintf(stdout,"ERROR: No Values in column one.\n");
  1334. fflush(stdout);
  1335. exit(1);
  1336. }
  1337. lo = number1[0];
  1338. for(n=1;n<cnt;n++) {
  1339. if(number1[n] < lo) {
  1340. fprintf(stdout,"ERROR: Values in column one are not in ascending order.\n");
  1341. fflush(stdout);
  1342. exit(1);
  1343. }
  1344. lo = number1[n];
  1345. }
  1346. lo = number2[0];
  1347. for(n=1;n<cnt2;n++) {
  1348. if(number2[n] < lo) {
  1349. fprintf(stdout,"ERROR: Values in column two are not in ascending order.\n");
  1350. fflush(stdout);
  1351. exit(1);
  1352. }
  1353. lo = number2[n];
  1354. }
  1355. n = 0;
  1356. m = 0;
  1357. lo = number2[m++];
  1358. hi = number2[m++];
  1359. switch(is_del) {
  1360. case(0):
  1361. do {
  1362. if(number1[n] < lo) /* If below value pair range, ignore */
  1363. ;
  1364. else if(number1[n] <= hi) /* If within value pair range, print */
  1365. fprintf(stdout,"INFO: %lf\n",number1[n]);
  1366. else if(m >= cnt2) /* if above value pair range */
  1367. break; /* if there are no more pairs, finished */
  1368. else {
  1369. lo = number2[m++]; /* else, get next pair */
  1370. hi = number2[m++];
  1371. }
  1372. n++;
  1373. } while(n < cnt);
  1374. break;
  1375. case(1):
  1376. do {
  1377. if(number1[n] < lo) /* If below value pair range, print */
  1378. fprintf(stdout,"INFO: %lf\n",number1[n]);
  1379. else if(number1[n] <= hi) /* If within value pair range, ignore */
  1380. ;
  1381. else if(m >= cnt2) /* if above value pair range */
  1382. break; /* if there are no more pairs, finished with pairs */
  1383. else {
  1384. lo = number2[m++]; /* else, get next pair */
  1385. hi = number2[m++];
  1386. n--; /* but stay where we are in input numbers */
  1387. }
  1388. n++;
  1389. } while(n < cnt);
  1390. while(n < cnt) /* If any vals above last pair, print */
  1391. fprintf(stdout,"INFO: %lf\n",number1[n++]);
  1392. break;
  1393. }
  1394. fflush(stdout);
  1395. }
  1396. /******************************** DO_KEEP_CYCLES ****************************/
  1397. void do_keep_cycles(int do_keep)
  1398. {
  1399. int n, j, m, k;
  1400. int OK = 0, diff, *pos;
  1401. pos = (int *)exmalloc(cnt2 * sizeof(int));
  1402. for(n=0;n<cnt2;n++) {
  1403. if((pos[n] = (int)round(number2[n])) < 1) {
  1404. fprintf(stdout,"ERROR: List positions below 1 do not exist.\n");
  1405. fflush(stdout);
  1406. exit(1);
  1407. }
  1408. if(pos[n] > ifactor) {
  1409. fprintf(stdout,"ERROR: Position %d is outside the cycle length (%d).\n",pos[n],ifactor);
  1410. fflush(stdout);
  1411. exit(1);
  1412. }
  1413. if(n > 0) {
  1414. if((diff = pos[n] - pos[n-1]) < 1) {
  1415. fprintf(stdout,"ERROR: Positions must be in ascending order.\n");
  1416. fflush(stdout);
  1417. exit(1);
  1418. }
  1419. else if(!OK && (diff > 1)) /* if no previous step is greater than 1 */
  1420. OK = 1; /* if this step is > 1, set as OK */
  1421. }
  1422. }
  1423. if(!OK && (pos[0] == 1) && (pos[n-1] == ifactor)) {
  1424. if(do_keep)
  1425. fprintf(stdout,"ERROR: All items will be retained.\n");
  1426. else
  1427. fprintf(stdout,"ERROR: All items will be deleted.\n");
  1428. fflush(stdout);
  1429. exit(1);
  1430. }
  1431. for(n=0;n<cnt2;n++)
  1432. pos[n]--;
  1433. if(do_keep) {
  1434. for(n=0;n<cnt;n+=ifactor) { /* KEEP */
  1435. j = 0;
  1436. for(m=n+pos[0],k=pos[0];k<ifactor;k++,m++) {
  1437. if(m >= cnt)
  1438. break;
  1439. if(k == pos[j]) {
  1440. fprintf(stdout,"INFO: %lf\n",number1[m]);
  1441. if(++j >= cnt2)
  1442. break;
  1443. }
  1444. }
  1445. }
  1446. } else {
  1447. for(n=0;n<cnt;n+=ifactor) { /* DELETE */
  1448. j = 0;
  1449. for(m=n,k=0;k<ifactor;k++,m++) {
  1450. if(m >= cnt)
  1451. break;
  1452. if(j < cnt2) {
  1453. if(k != pos[j])
  1454. fprintf(stdout,"INFO: %lf\n",number1[m]);
  1455. else
  1456. j++;
  1457. } else {
  1458. fprintf(stdout,"INFO: %lf\n",number1[m]);
  1459. }
  1460. }
  1461. }
  1462. }
  1463. fflush(stdout);
  1464. }
  1465. /******************************** DO_MATCHES ****************************/
  1466. void do_matches(void) {
  1467. int n, m, k;
  1468. for(n=0;n<cnt;n++) {
  1469. if(flteq(number1[n],factor,FLTERR)) {
  1470. for(k=0,m=n;k<ifactor;m++,k++)
  1471. fprintf(stdout,"INFO: %lf\n",number2[m]);
  1472. }
  1473. }
  1474. fflush(stdout);
  1475. }
  1476. /******************************** DO_ERROR ****************************/
  1477. void do_error(void) {
  1478. if(!sloom && !sloombatch)
  1479. fprintf(stderr,"%s\n",errstr);
  1480. else {
  1481. fprintf(stdout,"ERROR: %s\n",errstr);
  1482. fflush(stdout);
  1483. }
  1484. exit(1);
  1485. }
  1486. /******************************** DO_VALOUT ****************************/
  1487. void do_valout(double val) {
  1488. if(!sloom && !sloombatch)
  1489. fprintf(fp,"%lf\n",val);
  1490. else
  1491. fprintf(stdout,"INFO: %lf\n",val);
  1492. }
  1493. /******************************** DO_SQUEEZED_PAN ****************************/
  1494. void do_squeezed_pan(void) {
  1495. int n, neg = 1;
  1496. double time_shoulder = other_params[0];
  1497. double start_panpos = other_params[1];
  1498. double end_panpos = other_params[2];
  1499. double end_pantime = other_params[3];
  1500. double lastime;
  1501. double pan_subedj;
  1502. if(time_shoulder < 0.0) {
  1503. fprintf(stdout,"ERROR: half lingertime (%lf) cannot be less than zero.\n",time_shoulder);
  1504. fflush(stdout);
  1505. exit(1);
  1506. }
  1507. if(number1[0] < 0.0) {
  1508. fprintf(stdout,"ERROR: First time value less than zero encountered.\n");
  1509. fflush(stdout);
  1510. exit(1);
  1511. } else if(number1[0] <= time_shoulder) {
  1512. fprintf(stdout,"ERROR: First time (%lf) is too close to zero for half-lingertime %lf\n",number1[0],time_shoulder);
  1513. fflush(stdout);
  1514. exit(1);
  1515. }
  1516. lastime = number1[0];
  1517. for(n = 1; n < cnt; n++) {
  1518. if(number1[n] <= lastime + (2 * time_shoulder)) {
  1519. fprintf(stdout,"ERROR: Too small time step encountered (between %lf and %lf).\n",number1[n],lastime);
  1520. fflush(stdout);
  1521. exit(1);
  1522. }
  1523. }
  1524. if(end_pantime - time_shoulder <= number1[cnt-1]) {
  1525. fprintf(stdout,"ERROR: Too small time step encountered before end pantime (%lf to %lf).\n",number1[cnt -1],end_pantime);
  1526. fflush(stdout);
  1527. exit(1);
  1528. }
  1529. if(flteq(number1[0],0.0,FLTERR))
  1530. fprintf(stdout,"INFO: 0.0 %lf\n",number2[0]);
  1531. else {
  1532. pan_subedj = number2[0] * (7.5/9.0);
  1533. fprintf(stdout,"INFO: 0.0 %lf\n",start_panpos);
  1534. fprintf(stdout,"INFO: %lf %lf\n",number1[0] - time_shoulder,pan_subedj);
  1535. fprintf(stdout,"INFO: %lf %lf\n",number1[0],number2[0]);
  1536. fprintf(stdout,"INFO: %lf %lf\n",number1[0] + time_shoulder,pan_subedj);
  1537. }
  1538. if(number2[0] > 0.0)
  1539. neg = -1;
  1540. for(n = 1; n < cnt; n++) {
  1541. number2[n] *= neg;
  1542. pan_subedj = number2[n] * (7.5/9.0);
  1543. fprintf(stdout,"INFO: %lf %lf\n",number1[n] - time_shoulder,pan_subedj);
  1544. fprintf(stdout,"INFO: %lf %lf\n",number1[n],number2[n]);
  1545. fprintf(stdout,"INFO: %lf %lf\n",number1[n] + time_shoulder,pan_subedj);
  1546. lastime = number1[n];
  1547. neg = -neg;
  1548. }
  1549. fprintf(stdout,"INFO: %lf %lf\n",end_pantime,end_panpos);
  1550. fflush(stdout);
  1551. }
  1552. /************************************** GENERAL *****************************************/
  1553. #ifdef NOTDEF
  1554. void
  1555. initrand48()
  1556. {
  1557. srand(time((time_t *)0));
  1558. }
  1559. double
  1560. drand48()
  1561. {
  1562. return (double)rand()/(double)RAND_MAX;
  1563. }
  1564. #endif
  1565. /******************************** DO_MORPHSEQ ****************************/
  1566. void do_morphseq(void) {
  1567. int n = 0, morphcnt;
  1568. int minlen = min(cnt,cnt2);
  1569. int startmorph = (int)round(factor) - 1;
  1570. int endmorph = ifactor - 1;
  1571. int morphlen = endmorph - startmorph + 1;
  1572. double last1val, last2val, outval, diff1, diff2, trudiff;
  1573. while(n <= startmorph) {
  1574. fprintf(stdout,"INFO: %lf\n",number1[n]);
  1575. n++;
  1576. }
  1577. n--;
  1578. last1val = number1[n];
  1579. last2val = number2[n];
  1580. outval = number1[n];
  1581. n++;
  1582. morphcnt = 1;
  1583. while(n <= endmorph) {
  1584. diff1 = number1[n] - last1val;
  1585. diff2 = number2[n] - last2val;
  1586. last1val = number1[n];
  1587. last2val = number2[n];
  1588. trudiff = ((diff2 - diff1) * ((double)morphcnt/(double)morphlen)) + diff1;
  1589. outval += trudiff;
  1590. fprintf(stdout,"INFO: %lf\n",outval);
  1591. morphcnt++;
  1592. n++;
  1593. }
  1594. while(n < minlen) {
  1595. diff2 = number2[n] - last2val;
  1596. last2val = number2[n];
  1597. outval += diff2;
  1598. fprintf(stdout,"INFO: %lf\n",outval);
  1599. n++;
  1600. }
  1601. fflush(stdout);
  1602. }
  1603. /******************************** DO_MORPH ****************************/
  1604. void do_morph(void) {
  1605. int n = 0, morphcnt;
  1606. int minlen = min(cnt,cnt2);
  1607. int startmorph = (int)round(factor) - 1;
  1608. int endmorph = ifactor - 1;
  1609. int morphlen = endmorph - startmorph + 1;
  1610. while(n <= startmorph) {
  1611. fprintf(stdout,"INFO: %lf\n",number1[n]);
  1612. n++;
  1613. }
  1614. morphcnt = 1;
  1615. while(n < endmorph) {
  1616. fprintf(stdout,"INFO: %lf\n",((number2[n] - number1[n]) * ((double)morphcnt/(double)morphlen)) + number1[n]);
  1617. morphcnt++;
  1618. n++;
  1619. }
  1620. while(n < minlen) {
  1621. fprintf(stdout,"INFO: %lf\n",number2[n]);
  1622. n++;
  1623. }
  1624. fflush(stdout);
  1625. }