allegrord.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. #include "debug.h"
  2. #include "stdlib.h"
  3. #include "string.h"
  4. #include "ctype.h"
  5. #include "trace.h"
  6. #include <string>
  7. #include <fstream>
  8. #include <algorithm>
  9. #include "strparse.h"
  10. #include "allegro.h"
  11. #include "algrd_internal.h"
  12. using namespace std;
  13. #define streql(s1, s2) (strcmp(s1, s2) == 0)
  14. #define field_max 80
  15. class Alg_reader {
  16. public:
  17. istream *file;
  18. string input_line;
  19. int line_no;
  20. String_parse line_parser;
  21. bool line_parser_flag;
  22. string field;
  23. bool error_flag;
  24. Alg_seq_ptr seq;
  25. double tsnum;
  26. double tsden;
  27. Alg_reader(istream *a_file, Alg_seq_ptr new_seq);
  28. void readline();
  29. Alg_parameters_ptr process_attributes(Alg_parameters_ptr attributes,
  30. double time);
  31. bool parse();
  32. long parse_chan(string &field);
  33. long parse_int(string &field);
  34. int find_real_in(string &field, int n);
  35. double parse_real(string &field);
  36. void parse_error(string &field, long offset, const char *message);
  37. double parse_dur(string &field, double base);
  38. double parse_after_dur(double dur, string &field, int n, double base);
  39. double parse_loud(string &field);
  40. long parse_key(string &field);
  41. double parse_pitch(string &field);
  42. long parse_after_key(int key, string &field, int n);
  43. long find_int_in(string &field, int n);
  44. bool parse_attribute(string &field, Alg_parameter_ptr parm);
  45. bool parse_val(Alg_parameter_ptr param, string &s, int i);
  46. bool check_type(char type_char, Alg_parameter_ptr param);
  47. };
  48. double Alg_reader::parse_pitch(string &field)
  49. {
  50. if (isdigit(field[1])) {
  51. int last = find_real_in(field, 1);
  52. string real_string = field.substr(1, last - 1);
  53. return atof(real_string.c_str());
  54. } else {
  55. return (double) parse_key(field);
  56. }
  57. }
  58. // it is the responsibility of the caller to delete
  59. // the seq
  60. Alg_reader::Alg_reader(istream *a_file, Alg_seq_ptr new_seq)
  61. {
  62. file = a_file; // save the file
  63. line_parser_flag = false;
  64. line_no = 0;
  65. tsnum = 4; // default time signature
  66. tsden = 4;
  67. seq = new_seq;
  68. }
  69. Alg_error alg_read(istream &file, Alg_seq_ptr new_seq)
  70. // read a sequence from allegro file
  71. {
  72. assert(new_seq);
  73. Alg_reader alg_reader(&file, new_seq);
  74. bool err = alg_reader.parse();
  75. return (err ? alg_error_syntax : alg_no_error);
  76. }
  77. void Alg_reader::readline()
  78. {
  79. // a word about memory management: this Alg_reader has a
  80. // member variable input_line that holds a line of input
  81. // it is reused for each line. input_line is parsed by
  82. // line_parser, which holds a reference to input_line
  83. line_parser_flag = false;
  84. if (getline(*file, input_line)) {
  85. line_parser.init(&input_line);
  86. line_parser_flag = true;
  87. error_flag = false;
  88. }
  89. }
  90. Alg_parameters_ptr Alg_reader::process_attributes(
  91. Alg_parameters_ptr attributes, double time)
  92. {
  93. // print "process_attributes:", attributes
  94. bool ts_flag = false;
  95. if (attributes) {
  96. Alg_parameters_ptr a;
  97. bool in_seconds = seq->get_units_are_seconds();
  98. if ((a = Alg_parameters::remove_key(&attributes, "tempor"))) {
  99. double tempo = a->parm.r;
  100. seq->insert_tempo(tempo, seq->get_time_map()->time_to_beat(time));
  101. }
  102. if ((a = Alg_parameters::remove_key(&attributes, "beatr"))) {
  103. double beat = a->parm.r;
  104. seq->insert_beat(time, beat);
  105. }
  106. if ((a = Alg_parameters::remove_key(&attributes, "timesig_numr"))) {
  107. tsnum = a->parm.r;
  108. ts_flag = true;
  109. }
  110. if ((a = Alg_parameters::remove_key(&attributes, "timesig_denr"))) {
  111. tsden = a->parm.r;
  112. ts_flag = true;
  113. }
  114. if (ts_flag) {
  115. seq->set_time_sig(seq->get_time_map()->time_to_beat(time),
  116. tsnum, tsden);
  117. }
  118. if (in_seconds) seq->convert_to_seconds();
  119. }
  120. return attributes; // in case it was modified
  121. }
  122. bool Alg_reader::parse()
  123. {
  124. int voice = 0;
  125. int key = 60;
  126. double loud = 100.0;
  127. double pitch = 60.0;
  128. double dur = 1.0;
  129. double time = 0.0;
  130. int track_num = 0;
  131. seq->convert_to_seconds();
  132. //seq->set_real_dur(0.0); // just in case it's not initialized already
  133. readline();
  134. bool valid = false; // ignore blank lines
  135. while (line_parser_flag) {
  136. bool time_flag = false;
  137. bool next_flag = false;
  138. double next;
  139. bool voice_flag = false;
  140. bool loud_flag = false;
  141. bool dur_flag = false;
  142. bool new_pitch_flag = false; // "P" syntax or "A"-"G" syntax
  143. double new_pitch = 0.0;
  144. bool new_key_flag = false; // "K" syntax
  145. int new_key = 0;
  146. Alg_parameters_ptr attributes = NULL;
  147. if (line_parser.peek() == '#') {
  148. // look for #track
  149. line_parser.get_nonspace_quoted(field);
  150. if (streql(field.c_str(), "#track")) {
  151. line_parser.get_nonspace_quoted(field); // number
  152. field.insert(0, " "); // need char at beginning because
  153. // parse_int ignores the first character of the argument
  154. track_num = parse_int(field);
  155. seq->add_track(track_num);
  156. }
  157. // maybe we have a sequence or track name
  158. line_parser.get_remainder(field);
  159. // if there is a non-space character after #track n then
  160. // use it as sequence or track name. Note that because we
  161. // skip over spaces, a sequence or track name cannot begin
  162. // with leading blanks. Another decision is that the name
  163. // must be at time zero
  164. if (field.length() > 0) {
  165. // insert the field as sequence name or track name
  166. Alg_update_ptr update = new Alg_update;
  167. update->chan = -1;
  168. update->time = 0;
  169. update->set_identifier(-1);
  170. // sequence name is whatever is on track 0
  171. // other tracks have track names
  172. const char *attr =
  173. (track_num == 0 ? "seqnames" : "tracknames");
  174. update->parameter.set_attr(symbol_table.insert_string(attr));
  175. update->parameter.s = heapify(field.c_str());
  176. seq->add_event(update, track_num);
  177. }
  178. } else {
  179. // we must have a track to insert into
  180. if (seq->tracks() == 0) seq->add_track(0);
  181. line_parser.get_nonspace_quoted(field);
  182. char pk = line_parser.peek();
  183. // attributes are parsed as two adjacent nonspace_quoted tokens
  184. // so we have to conditionally call get_nonspace_quoted() again
  185. if (pk && !isspace(pk)) {
  186. string field2;
  187. line_parser.get_nonspace_quoted(field2);
  188. field.append(field2);
  189. }
  190. while (field[0]) {
  191. char first = toupper(field[0]);
  192. if (strchr("ABCDEFGKLPUSIQHW-", first)) {
  193. valid = true; // it's a note or event
  194. }
  195. if (first == 'V') {
  196. if (voice_flag) {
  197. parse_error(field, 0, "Voice specified twice");
  198. } else {
  199. voice = parse_chan(field);
  200. }
  201. voice_flag = true;
  202. } else if (first == 'T') {
  203. if (time_flag) {
  204. parse_error(field, 0, "Time specified twice");
  205. } else {
  206. time = parse_dur(field, 0.0);
  207. }
  208. time_flag = true;
  209. } else if (first == 'N') {
  210. if (next_flag) {
  211. parse_error(field, 0, "Next specified twice");
  212. } else {
  213. next = parse_dur(field, time);
  214. }
  215. next_flag = true;
  216. } else if (first == 'K') {
  217. if (new_key_flag) {
  218. parse_error(field, 0, "Key specified twice");
  219. } else {
  220. new_key = parse_key(field);
  221. new_key_flag = true;
  222. }
  223. } else if (first == 'L') {
  224. if (loud_flag) {
  225. parse_error(field, 0, "Loudness specified twice");
  226. } else {
  227. loud = parse_loud(field);
  228. }
  229. loud_flag = true;
  230. } else if (first == 'P') {
  231. if (new_pitch_flag) {
  232. parse_error(field, 0, "Pitch specified twice");
  233. } else {
  234. new_pitch = parse_pitch(field);
  235. new_pitch_flag = true;
  236. }
  237. } else if (first == 'U') {
  238. if (dur_flag) {
  239. parse_error(field, 0, "Dur specified twice");
  240. } else {
  241. dur = parse_dur(field, time);
  242. dur_flag = true;
  243. }
  244. } else if (strchr("SIQHW", first)) {
  245. if (dur_flag) {
  246. parse_error(field, 0, "Dur specified twice");
  247. } else {
  248. // prepend 'U' to field, copy EOS too
  249. field.insert(0, 1, 'U');
  250. dur = parse_dur(field, time);
  251. dur_flag = true;
  252. }
  253. } else if (strchr("ABCDEFG", first)) {
  254. if (new_pitch_flag) {
  255. parse_error(field, 0, "Pitch specified twice");
  256. } else {
  257. // prepend 'P' to field
  258. field.insert(0, 1, 'P');
  259. new_pitch = parse_pitch(field);
  260. new_pitch_flag = true;
  261. }
  262. } else if (first == '-') {
  263. Alg_parameter parm;
  264. if (parse_attribute(field, &parm)) { // enter attribute-value pair
  265. attributes = new Alg_parameters(attributes);
  266. attributes->parm = parm;
  267. parm.s = NULL; // protect string from deletion by destructor
  268. }
  269. } else {
  270. parse_error(field, 0, "Unknown field");
  271. }
  272. if (error_flag) {
  273. field[0] = 0; // exit the loop
  274. } else {
  275. line_parser.get_nonspace_quoted(field);
  276. pk = line_parser.peek();
  277. // attributes are parsed as two adjacent nonspace_quoted
  278. // tokens so we have to conditionally call
  279. // get_nonspace_quoted() again
  280. if (pk && !isspace(pk)) {
  281. string field2;
  282. line_parser.get_nonspace_quoted(field2);
  283. field.append(field2);
  284. }
  285. }
  286. }
  287. // a case analysis:
  288. // Key < 128 implies pitch unless pitch is explicitly given
  289. // Pitch implies Key unless key is explicitly given,
  290. // Pitch is rounded to nearest integer to determine the Key
  291. // if necessary, so MIDI files will lose the pitch fraction
  292. // A-G is a Pitch specification (therefore it implies Key)
  293. // K60 P60 -- both are specified, use 'em
  294. // K60 P60 C4 -- overconstrained, an error
  295. // K60 C4 -- OK, but K60 is already implied by C4
  296. // K60 -- OK, pitch is 60
  297. // C4 P60 -- over constrained
  298. // P60 -- OK, key is 60
  299. // P60.1 -- OK, key is 60
  300. // C4 -- OK, key is 60, pitch is 60
  301. // <nothing> -- OK, key and pitch from before
  302. // K200 P60 -- ok, pitch is 60
  303. // K200 with neither P60 nor C4 uses
  304. // pitch from before
  305. // figure out what the key/instance is:
  306. if (new_key_flag) { // it was directly specified
  307. key = new_key;
  308. } else if (new_pitch_flag) {
  309. // pitch was specified, but key was not; get key from pitch
  310. key = (int) (new_pitch + 0.5); // round to integer key number
  311. }
  312. if (new_pitch_flag) {
  313. pitch = new_pitch;
  314. } else if (key < 128 && new_key_flag) {
  315. // no explicit pitch, but key < 128, so it implies pitch
  316. pitch = key;
  317. new_pitch_flag = true;
  318. }
  319. // now we've acquired new parameters
  320. // if (it is a note, then enter the note
  321. if (valid) {
  322. // change tempo or beat
  323. attributes = process_attributes(attributes, time);
  324. // if there's a duration or pitch, make a note:
  325. if (new_pitch_flag || dur_flag) {
  326. Alg_note_ptr note_ptr = new Alg_note;
  327. note_ptr->chan = voice;
  328. note_ptr->time = time;
  329. note_ptr->dur = dur;
  330. note_ptr->set_identifier(key);
  331. note_ptr->pitch = pitch;
  332. note_ptr->loud = loud;
  333. note_ptr->parameters = attributes;
  334. seq->add_event(note_ptr, track_num); // sort later
  335. if (seq->get_real_dur() < (time + dur)) seq->set_real_dur(time + dur);
  336. } else {
  337. int update_key = -1;
  338. // key must appear explicitly; otherwise
  339. // update applies to channel
  340. if (new_key_flag) {
  341. update_key = key;
  342. }
  343. if (loud_flag) {
  344. Alg_update_ptr new_upd = new Alg_update;
  345. new_upd->chan = voice;
  346. new_upd->time = time;
  347. new_upd->set_identifier(update_key);
  348. new_upd->parameter.set_attr(symbol_table.insert_string("loudr"));
  349. new_upd->parameter.r = pitch;
  350. seq->add_event(new_upd, track_num);
  351. if (seq->get_real_dur() < time) seq->set_real_dur(time);
  352. }
  353. if (attributes) {
  354. while (attributes) {
  355. Alg_update_ptr new_upd = new Alg_update;
  356. new_upd->chan = voice;
  357. new_upd->time = time;
  358. new_upd->set_identifier(update_key);
  359. new_upd->parameter = attributes->parm;
  360. seq->add_event(new_upd, track_num);
  361. Alg_parameters_ptr p = attributes;
  362. attributes = attributes->next;
  363. p->parm.s = NULL; // so we don't delete the string
  364. delete p;
  365. }
  366. }
  367. }
  368. if (next_flag) {
  369. time = time + next;
  370. } else if (dur_flag || new_pitch_flag) { // a note: incr by dur
  371. time = time + dur;
  372. }
  373. }
  374. }
  375. readline();
  376. }
  377. if (!error_flag) { // why not convert even if there was an error? -RBD
  378. seq->convert_to_seconds(); // make sure format is correct
  379. }
  380. // real_dur is valid, translate to beat_dur
  381. seq->set_beat_dur((seq->get_time_map())->time_to_beat(seq->get_real_dur()));
  382. return error_flag;
  383. }
  384. long Alg_reader::parse_chan(string &field)
  385. {
  386. const char *int_string = field.c_str() + 1;
  387. const char *msg = "Integer or - expected";
  388. const char *p = int_string;
  389. char c;
  390. // check that all chars in int_string are digits or '-':
  391. while ((c = *p++)) {
  392. if (!isdigit(c) && c != '-') {
  393. parse_error(field, p - field.c_str() - 1, msg);
  394. return 0;
  395. }
  396. }
  397. p--; // p now points to end-of-string character
  398. if (p - int_string == 0) {
  399. // bad: string length is zero
  400. parse_error(field, 1, msg);
  401. return 0;
  402. }
  403. if (p - int_string == 1 && int_string[0] == '-') {
  404. // special case: entire string is "-", interpret as -1
  405. return -1;
  406. }
  407. return atoi(int_string);
  408. }
  409. long Alg_reader::parse_int(string &field)
  410. {
  411. const char *int_string = field.c_str() + 1;
  412. const char *msg = "Integer expected";
  413. const char *p = int_string;
  414. char c;
  415. // check that all chars in int_string are digits:
  416. while ((c = *p++)) {
  417. if (!isdigit(c)) {
  418. parse_error(field, p - field.c_str() - 1, msg);
  419. return 0;
  420. }
  421. }
  422. p--; // p now points to end-of-string character
  423. if (p - int_string == 0) {
  424. // bad: string length is zero
  425. parse_error(field, 1, msg);
  426. return 0;
  427. }
  428. return atoi(int_string);
  429. }
  430. int Alg_reader::find_real_in(string &field, int n)
  431. {
  432. // scans from offset n to the end of a real constant
  433. bool decimal = false;
  434. int len = field.length();
  435. for (int i = n; i < len; i++) {
  436. char c = field[i];
  437. if (!isdigit(c)) {
  438. if (c == '.' && !decimal) {
  439. decimal = true;
  440. } else {
  441. return i;
  442. }
  443. }
  444. }
  445. return field.length();
  446. }
  447. double Alg_reader::parse_real(string &field)
  448. {
  449. const char *msg = "Real expected";
  450. int last = find_real_in(field, 1);
  451. string real_string = field.substr(1, last - 1);
  452. if (last <= 1 || last < (int) field.length()) {
  453. parse_error(field, 1, msg);
  454. return 0;
  455. }
  456. return atof(real_string.c_str());
  457. }
  458. void Alg_reader::parse_error(string &field, long offset, const char *message)
  459. {
  460. int position = line_parser.pos - field.length() + offset;
  461. error_flag = true;
  462. puts(line_parser.str->c_str());
  463. for (int i = 0; i < position; i++) {
  464. putc(' ', stdout);
  465. }
  466. putc('^', stdout);
  467. printf(" %s\n", message);
  468. }
  469. double duration_lookup[] = { 0.25, 0.5, 1.0, 2.0, 4.0 };
  470. double Alg_reader::parse_dur(string &field, double base)
  471. {
  472. const char *msg = "Duration expected";
  473. const char *durs = "SIQHW";
  474. const char *p;
  475. int last;
  476. double dur;
  477. if (field.length() < 2) {
  478. // fall through to error message
  479. return -1;
  480. } else if (isdigit(field[1])) {
  481. last = find_real_in(field, 1);
  482. string real_string = field.substr(1, last - 1);
  483. dur = atof(real_string.c_str());
  484. // convert dur from seconds to beats
  485. dur = seq->get_time_map()->time_to_beat(base + dur) -
  486. seq->get_time_map()->time_to_beat(base);
  487. } else if ((p = strchr(durs, toupper(field[1])))) {
  488. dur = duration_lookup[p - durs];
  489. last = 2;
  490. } else {
  491. parse_error(field, 1, msg);
  492. return 0;
  493. }
  494. dur = parse_after_dur(dur, field, last, base);
  495. dur = seq->get_time_map()->beat_to_time(
  496. seq->get_time_map()->time_to_beat(base) + dur) - base;
  497. return dur;
  498. }
  499. double Alg_reader::parse_after_dur(double dur, string &field,
  500. int n, double base)
  501. {
  502. if ((int) field.length() == n) {
  503. return dur;
  504. }
  505. if (toupper(field[n]) == 'T') {
  506. return parse_after_dur(dur * 2/3, field, n + 1, base);
  507. }
  508. if (field[n] == '.') {
  509. return parse_after_dur(dur * 1.5, field, n + 1, base);
  510. }
  511. if (isdigit(field[n])) {
  512. int last = find_real_in(field, n);
  513. string a_string = field.substr(n, last - n);
  514. double f = atof(a_string.c_str());
  515. return parse_after_dur(dur * f, field, last, base);
  516. }
  517. if (field[n] == '+') {
  518. string a_string = field.substr(n + 1);
  519. return dur + parse_dur(
  520. a_string, seq->get_time_map()->beat_to_time(
  521. seq->get_time_map()->time_to_beat(base) + dur));
  522. }
  523. parse_error(field, n, "Unexpected character in duration");
  524. return dur;
  525. }
  526. struct loud_lookup_struct {
  527. const char *str;
  528. int val;
  529. } loud_lookup[] = { {"FFF", 127}, {"FF", 120}, {"F", 110}, {"MF", 100},
  530. {"MP", 90}, {"P", 80}, {"PP", 70}, {"PPP", 60},
  531. {NULL, 0} };
  532. double Alg_reader::parse_loud(string &field)
  533. {
  534. const char *msg = "Loudness expected";
  535. if (isdigit(field[1])) {
  536. return parse_int(field);
  537. } else {
  538. string dyn = field.substr(1);
  539. transform(dyn.begin(), dyn.end(), dyn.begin(), ::toupper);
  540. for (int i = 0; loud_lookup[i].str; i++) {
  541. if (streql(loud_lookup[i].str, dyn.c_str())) {
  542. return (double) loud_lookup[i].val;
  543. }
  544. }
  545. }
  546. parse_error(field, 1, msg);
  547. return 100.0;
  548. }
  549. int key_lookup[] = {21, 23, 12, 14, 16, 17, 19};
  550. // the field can be K<number> or K[A-G]<number> or P[A-G]<number>
  551. // (this can be called from parse_pitch() to handle [A-G])
  552. // Notice that the routine ignores the first character: K or P
  553. //
  554. long Alg_reader::parse_key(string &field)
  555. {
  556. const char *msg = "Pitch expected";
  557. const char *pitches = "ABCDEFG";
  558. const char *p;
  559. if (isdigit(field[1])) {
  560. // This routine would not have been called if field = "P<number>"
  561. // so it must be "K<number>" so <number> must be an integer.
  562. return parse_int(field);
  563. } else if ((p = strchr(pitches, toupper(field[1])))) {
  564. long key = key_lookup[p - pitches];
  565. key = parse_after_key(key, field, 2);
  566. return key;
  567. }
  568. parse_error(field, 1, msg);
  569. return 0;
  570. }
  571. long Alg_reader::parse_after_key(int key, string &field, int n)
  572. {
  573. if ((int) field.length() == n) {
  574. return key;
  575. }
  576. char c = toupper(field[n]);
  577. if (c == 'S') {
  578. return parse_after_key(key + 1, field, n + 1);
  579. }
  580. if (c == 'F') {
  581. return parse_after_key(key - 1, field, n + 1);
  582. }
  583. if (isdigit(field[n])) {
  584. int last = find_int_in(field, n);
  585. string octave = field.substr(n, last - n);
  586. int oct = atoi(octave.c_str());
  587. return parse_after_key(key + oct * 12, field, last);
  588. }
  589. parse_error(field, n, "Unexpected character in pitch");
  590. return key;
  591. }
  592. long Alg_reader::find_int_in(string &field, int n)
  593. {
  594. while ((int) field.length() > n && isdigit(field[n])) {
  595. n = n + 1;
  596. }
  597. return n;
  598. }
  599. bool Alg_reader::parse_attribute(string &field, Alg_parameter_ptr param)
  600. {
  601. int i = 1;
  602. while (i < (int) field.length()) {
  603. if (field[i] == ':') {
  604. string attr = field.substr(1, i - 1);
  605. char type_char = field[i - 1];
  606. if (strchr("iarsl", type_char)) {
  607. param->set_attr(symbol_table.insert_string(attr.c_str()));
  608. parse_val(param, field, i + 1);
  609. } else {
  610. parse_error(field, 0, "attribute needs to end with typecode: i,a,r,s, or l");
  611. }
  612. return !error_flag;
  613. }
  614. i = i + 1;
  615. }
  616. return false;
  617. }
  618. bool Alg_reader::parse_val(Alg_parameter_ptr param, string &s, int i)
  619. {
  620. int len = (int) s.length();
  621. if (i >= len) {
  622. return false;
  623. }
  624. if (s[i] == '"') {
  625. if (!check_type('s', param)) {
  626. return false;
  627. }
  628. // note: (len - i) includes 2 quote characters but no EOS character
  629. // so total memory to allocate is (len - i) - 1
  630. char *r = new char[(len - i) - 1];
  631. strncpy(r, s.c_str() + i + 1, (len - i) - 2);
  632. r[(len - i) - 2] = 0; // terminate the string
  633. param->s = r;
  634. } else if (s[i] == '\'') {
  635. if (!check_type('a', param)) {
  636. return false;
  637. }
  638. string r = s.substr(i + 1, len - i - 2);
  639. param->a = symbol_table.insert_string(r.c_str());
  640. } else if (param->attr_type() == 'l') {
  641. if (streql(s.c_str() + i, "true") ||
  642. streql(s.c_str() + i, "t")) {
  643. param->l = true;
  644. } else if (streql(s.c_str() + i, "false") ||
  645. streql(s.c_str() + i, "nil")) {
  646. param->l = false;
  647. } else return false;
  648. } else if (isdigit(s[i]) || s[i] == '-' || s[i] == '.') {
  649. int pos = i;
  650. bool period = false;
  651. if (s[pos] == '-') {
  652. pos++;
  653. }
  654. while (pos < len) {
  655. if (isdigit(s[pos])) {
  656. ;
  657. } else if (!period && s[pos] == '.') {
  658. period = true;
  659. } else {
  660. parse_error(s, pos, "Unexpected char in number");
  661. return false;
  662. }
  663. pos = pos + 1;
  664. }
  665. string r = s.substr(i, len - i);
  666. if (period) {
  667. if (!check_type('r', param)) {
  668. return false;
  669. }
  670. param->r = atof(r.c_str());
  671. } else {
  672. if (param->attr_type() == 'r') {
  673. param->r = atoi(r.c_str());
  674. } else if (!check_type('i', param)) {
  675. return false;
  676. } else {
  677. param->i = atoi(r.c_str());
  678. }
  679. }
  680. } else {
  681. parse_error(s, i, "invalid value");
  682. return false;
  683. }
  684. return true;
  685. }
  686. bool Alg_reader::check_type(char type_char, Alg_parameter_ptr param)
  687. {
  688. return param->attr_type() == type_char;
  689. }
  690. //duration_lookup = {"S": 0.5, "I": 0.5, "Q": 1, "H": 2, "W": 4}
  691. //key_lookup = {"C": 12, "D": 14, "E": 16, "F": 17, "G": 19, "A": 21, "B": 23}
  692. /*
  693. def test():
  694. reader = Alg_reader(open("data\\test.gro", "r"))
  695. reader.parse()
  696. score = reader->seq.notes
  697. print "score:", score
  698. reader = nil
  699. */