コード例 #1
0
ファイル: allegrord.cpp プロジェクト: ruthmagnus/audacity
#include "assert.h"
#include "stdlib.h"
#include "stdio.h"
#include "allegro.h"
#include "string.h"
#include "ctype.h"
//#include "memory.h"
#include "trace.h"
#include "strparse.h"
#ifndef EXPERIMENTAL_NOTE_TRACK
#include "allegrord.h"
#endif /* EXPERIMENTAL_NOTE_TRACK */
#define streql(s1, s2) (strcmp(s1, s2) == 0)
#define field_max 80


//Note that this is an #ifdef, not an #ifndef
#ifdef EXPERIMENTAL_NOTE_TRACK

class Alg_reader {
public:
    FILE *file;
    int line_no;
    String_parse line_parser;
    bool line_parser_flag;
    char field[field_max];
    bool error_flag;
    Alg_seq_ptr seq;
    double tsnum;
    double tsden;

    Alg_reader(FILE *a_file, Alg_seq_ptr new_seq);
    void readline();
    Alg_parameters_ptr process_attributes(Alg_parameters_ptr attributes, double time);
    bool parse();
    long parse_chan(char *field);
    long parse_int(char *field);
    int find_real_in(char *field, int n);
    double parse_real(char *field);
    void parse_error(char *field, long offset, char *message);
    double parse_dur(char *field, double base);
    double parse_after_dur(double dur, char *field, int n, double base);
    double parse_loud(char *field);
    long parse_key(char *field);
    double parse_pitch(char *field);
    long parse_after_key(int key, char *field, int n);
    long find_int_in(char *field, int n);
    bool parse_attribute(char *field, Alg_parameter_ptr parm);
    bool parse_val(Alg_parameter_ptr param, char *s, int i);
    bool check_type(char type_char, Alg_parameter_ptr param);
};

#endif /* EXPERIMENTAL_NOTE_TRACK */

void subseq(char *result, char *source, int from, int to)
{
    memcpy(result, source + from, to - from);
    result[to - from] = 0;
}

#ifndef EXPERIMENTAL_NOTE_TRACK
double Allegro_reader::parse_pitch(char *field)
#else /* EXPERIMENTAL_NOTE_TRACK */
double Alg_reader::parse_pitch(char *field)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    if (isdigit(field[1])) {
        char real_string[80];
        int last = find_real_in(field, 1);
        subseq(real_string, field, 1, last);
        return atof(real_string);
    } else {
        return (double) parse_key(field);
    }
}


#ifndef EXPERIMENTAL_NOTE_TRACK
Allegro_reader::Allegro_reader(FILE *a_file)
{
    file = a_file; // save the file
    line_parser_flag = false;
    line_no = 0;
    seq = Seq();
    tsnum = 4; // default time signature
    tsden = 4;
}
#else /* EXPERIMENTAL_NOTE_TRACK */
// it is the responsibility of the caller to delete
// the seq
Alg_reader::Alg_reader(FILE *a_file, Alg_seq_ptr new_seq)
{
    file = a_file; // save the file
    line_parser_flag = false;
    line_no = 0;
    tsnum = 4; // default time signature
    tsden = 4;
    seq = new_seq;
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


//Note that this is an #ifdef, not an #ifndef
#ifdef EXPERIMENTAL_NOTE_TRACK
Alg_seq_ptr alg_read(FILE *file, Alg_seq_ptr new_seq)
    // read a sequence from allegro file
{
    if (!new_seq) new_seq = new Alg_seq();
    Alg_reader alg_reader(file, new_seq);
    alg_reader.parse();
    return alg_reader.seq;
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


#ifndef EXPERIMENTAL_NOTE_TRACK
void Allegro_reader::readline()
#else /* EXPERIMENTAL_NOTE_TRACK */
void Alg_reader::readline()
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    char line[256];
    char *line_flag = fgets(line, 256, file);
    line_parser_flag = false;
    if (line_flag) {
        line_parser.init(line);
        line_parser_flag = true;
        error_flag = false;
    }
}


#ifndef EXPERIMENTAL_NOTE_TRACK
void Allegro_reader::process_attributes(Parameters_ptr attributes, double time)
{
    // print "process_attributes:", attributes
    bool ts_flag;
    if (attributes) {
        Parameters_ptr a;
        if (a = Parameters::remove_key(&attributes, "tempor")) {
            double tempo = a->parm.r;
            seq.insert_tempo(tempo, seq.map.time_to_beat(time));
        }
        if (a = Parameters::remove_key(&attributes, "beatr")) {
            double beat = a->parm.r;
            seq.insert_beat(time, beat);
        }
        if (a = Parameters::remove_key(&attributes, "tsnumr")) {
            tsnum = a->parm.r;
            ts_flag = true;
        }
        if (a = Parameters::remove_key(&attributes, "tsdenr")) {
            tsden = a->parm.r;
            ts_flag = true;
        }
        if (ts_flag) {
            seq.set_time_sig(seq.map.time_to_beat(time), tsnum, tsden);
        }
    }
}
#else /* EXPERIMENTAL_NOTE_TRACK */
Alg_parameters_ptr Alg_reader::process_attributes(
        Alg_parameters_ptr attributes, double time)
{
    // print "process_attributes:", attributes
    bool ts_flag = false;
    if (attributes) {
        Alg_parameters_ptr a;
        bool in_seconds = seq->get_units_are_seconds();
        if (a = Alg_parameters::remove_key(&attributes, "tempor")) {
            double tempo = a->parm.r;
            seq->insert_tempo(tempo, seq->get_time_map()->time_to_beat(time));
        }
        if (a = Alg_parameters::remove_key(&attributes, "beatr")) {
            double beat = a->parm.r;
            seq->insert_beat(time, beat);
        }
        if (a = Alg_parameters::remove_key(&attributes, "timesig_numr")) {
            tsnum = a->parm.r;
            ts_flag = true;
        }
        if (a = Alg_parameters::remove_key(&attributes, "timesig_denr")) {
            tsden = a->parm.r;
            ts_flag = true;
        }
        if (ts_flag) {
            seq->set_time_sig(seq->get_time_map()->time_to_beat(time),
            tsnum, tsden);
        }
        if (in_seconds) seq->convert_to_seconds();
    }
    return attributes; // in case it was modified
}
#endif /* EXPERIMENTAL_NOTE_TRACK */

#ifndef EXPERIMENTAL_NOTE_TRACK
bool Allegro_reader::parse()
{
    int voice = 0;
    int key = 60;
    double loud = 100.0;
    double pitch = 60.0;
    double dur = 1.0;
    double time = 0.0;
    readline();
    bool valid = false; // ignore blank lines
    while (line_parser_flag) {
        bool time_flag = false;
        bool next_flag = false;
        double next;
        bool voice_flag = false;
        bool loud_flag = false;
        bool dur_flag = false;
        bool new_pitch_flag = false; // "P" syntax
        double new_pitch = 0.0;
        bool new_key_flag = false;   // "K" syntax
        int new_key = 0;
        bool new_note_flag = false;  // "A"-"G" syntax
        int new_note = 0;
        Parameters_ptr attributes = NULL;
        line_parser.get_nonspace_quoted(field);
        char pk = line_parser.peek();
        if (pk && !isspace(pk)) {
            line_parser.get_nonspace_quoted(field + strlen(field));
        }
        while (field[0]) {
            // print "field", "|";field;"|", "|";line_parser.string;"|", line_parser.pos
            char first = toupper(field[0]);
            if (strchr("ABCDEFGKLPUSIQHW-", first)) {
                valid = true; // it's a note or event
            }
            if (first == 'V') {
                if (voice_flag) {
                    parse_error(field, 0, "Voice specified twice");
                } else {
                    voice = parse_int(field);
                }
                voice_flag = true;
            } else if (first == 'T') {
                if (time_flag) {
                    parse_error(field, 0, "Time specified twice");
                } else {
                    time = parse_dur(field, 0.0);
                }
                time_flag = true;
            } else if (first == 'N') {
                if (next_flag) {
                    parse_error(field, 0, "Next specified twice");
                } else {
                    next = parse_dur(field, time);
                }
                next_flag = true;
            } else if (first == 'K') {
                if (new_key_flag) {
                    parse_error(field, 0, "Key specified twice");
                } else {
                    new_key = parse_key(field);
                    new_key_flag = true;
                }
            } else if (first == 'L') {
                if (loud_flag) {
                    parse_error(field, 0, "Loudness specified twice");
                } else {
                    loud = parse_loud(field);
                }
                loud_flag = true;
            } else if (first == 'P') {
                if (new_note_flag || new_pitch_flag) {
                    parse_error(field, 0, "Pitch specified twice");
                } else {
                    new_pitch = parse_pitch(field);
                    new_pitch_flag = true;
                }
            } else if (first == 'U') {
                if (dur_flag) {
                    parse_error(field, 0, "Dur specified twice");
                } else {
                    dur = parse_dur(field, time);
                    dur_flag = true;
                }
            } else if (strchr("SIQHW", first)) {
                if (dur_flag) {
                    parse_error(field, 0, "Dur specified twice");
                } else {
                    // prepend 'U' to field, copy EOS too
                    memmove(field + 1, field, strlen(field) + 1);
                    field[0] = 'U';
                    dur = parse_dur(field, time);
                    dur_flag = true;
                }
            } else if (strchr("ABCDEFG", first)) {
                if (new_note_flag || new_pitch_flag) {
                    parse_error(field, 0, "Pitch specified twice");
                } else {
                    // prepend 'K' to field, copy EOS too
                    memmove(field + 1, field, strlen(field) + 1);
                    field[0] = 'K';
                    new_note = parse_key(field);
                    new_note_flag = true;
                }
            } else if (first == '-') {
                Parameter parm;
                if (parse_attribute(field, &parm)) { // enter attribute-value pair
                    attributes = new Parameters(attributes);
                    attributes->parm = parm;
                    parm.s = NULL; // protect string from deletion by destructor
                }
            } else {
                parse_error(field, 0, "Unknown field");
            }

            if (error_flag) {
                field[0] = 0; // exit the loop
            } else {
                line_parser.get_nonspace_quoted(field);
                pk = line_parser.peek();
                if (pk && !isspace(pk)) {
                    line_parser.get_nonspace_quoted(field + strlen(field));
                }
            }
        }
        // a case analysis:
        // Key < 128 counts as both key and pitch
        // A-G implies pitch AND key unless key given too
        //   K60 P60 -- both are specified, use 'em
        //   K60 P60 C4 -- overconstrained, an error
        //   K60 C4 -- overconstrained
        //   K60 -- OK, pitch is 60
        //   C4 P60 -- over constrained
        //   P60 -- OK, key is from before, pitch is 60
        //   C4 -- OK, key is 60, pitch is 60
        //   <nothing> -- OK, key and pitch from before
        //   K200 with P60 ok, pitch is 60
        //   K200 with neither P60 nor C4 uses 
        //       pitch from before
        // figure out what the key/instance is:
        if (new_key_flag) { // it was directly specified
            key = new_key;
            if (key < 128 && new_note_flag) {
                parse_error("", 0, "Pitch specified twice");
            }
        } else if (new_note_flag) { // "A"-"G" used
            key = new_note;
        }
        if (new_pitch_flag) {
            pitch = new_pitch;
        } else if (key < 128) {
            pitch = key;
        }
        // now we've acquired new parameters
        // if (it is a note, then enter the note
        if (valid) {
            // change tempo or beat
            process_attributes(attributes, time);
            // if there's a duration or pitch, make a note:
            if (new_pitch_flag || dur_flag || new_note_flag) {
                new_key_flag = false;
                new_pitch_flag = false;
                Allegro_note_ptr note_ptr = new Allegro_note;
                note_ptr->chan = voice;
                note_ptr->time = time;
                note_ptr->dur = dur;
                note_ptr->key = key;
                note_ptr->pitch = pitch;
                note_ptr->loud = loud;
                note_ptr->parameters = attributes;
                seq.add_event(note_ptr); // sort later
            } else {
                int update_key = -1;
                // key or pitch must appear explicitly; otherwise
                //    update applies to channel
                if (new_key_flag || new_pitch_flag) {
                    update_key = key;
                }
                if (loud_flag) {
                    Allegro_update_ptr new_upd = new Allegro_update;
                    new_upd->chan = voice;
                    new_upd->time = time;
                    new_upd->key = update_key;
                    new_upd->parameter.set_attr(symbol_table.insert_string("loudr"));
                    new_upd->parameter.r = pitch;
                    seq.add_event(new_upd);
                }
                if (attributes) {
                    while (attributes) {
                        Allegro_update_ptr new_upd = new Allegro_update;
                        new_upd->chan = voice;
                        new_upd->time = time;
                        new_upd->key = update_key;
                        new_upd->parameter = attributes->parm;
                        seq.add_event(new_upd);
                        Parameters_ptr p = attributes;
                        attributes = attributes->next;
                        delete p;
                    }
                }
            }
            if (next_flag) {
                time = time + next;
            } else if (dur_flag) {
                time = time + dur;
            }
        }
        readline();
    }
    //print "Finished reading score"
    if (!error_flag) {
        seq.convert_to_seconds(); // make sure format is correct
        // seq.notes.sort('event_greater_than');
    }
    // print "parse returns error_flag", error_flag
    return error_flag;
}
#else /* EXPERIMENTAL_NOTE_TRACK */
bool Alg_reader::parse()
{
    int voice = 0;
    int key = 60;
    double loud = 100.0;
    double pitch = 60.0;
    double dur = 1.0;
    double time = 0.0;
    int track_num = 0;
    seq->convert_to_seconds();
    //seq->set_real_dur(0.0); // just in case it's not initialized already
    readline();
    bool valid = false; // ignore blank lines
    while (line_parser_flag) {
        bool time_flag = false;
        bool next_flag = false;
        double next;
        bool voice_flag = false;
        bool loud_flag = false;
        bool dur_flag = false;
        bool new_pitch_flag = false; // "P" syntax
        double new_pitch = 0.0;
        bool new_key_flag = false;   // "K" syntax
        int new_key = 0;
        bool new_note_flag = false;  // "A"-"G" syntax
        int new_note = 0;
        Alg_parameters_ptr attributes = NULL;
        if (line_parser.peek() == '#') {
            // look for #track
            line_parser.get_nonspace_quoted(field);
            if (streql(field, "#track")) {
                line_parser.get_nonspace_quoted(field); // number
                track_num = parse_int(field - 1);
                seq->add_track(track_num);
            }
            // maybe we have a comment
        } else {
            // we must have a track to insert into
            if (seq->tracks() == 0) seq->add_track(0);
            line_parser.get_nonspace_quoted(field);
            char pk = line_parser.peek();
            // attributes are parsed as two adjacent nonspace_quoted tokens
            // so we have to conditionally call get_nonspace_quoted() again
            if (pk && !isspace(pk)) {
                line_parser.get_nonspace_quoted(field + strlen(field));
            }
            while (field[0]) {
                char first = toupper(field[0]);
                if (strchr("ABCDEFGKLPUSIQHW-", first)) {
                    valid = true; // it's a note or event
                }
                if (first == 'V') {
                    if (voice_flag) {
                        parse_error(field, 0, "Voice specified twice");
                    } else {
                        voice = parse_chan(field);
                    }
                    voice_flag = true;
                } else if (first == 'T') {
                    if (time_flag) {
                        parse_error(field, 0, "Time specified twice");
                    } else {
                        time = parse_dur(field, 0.0);
                    }
                    time_flag = true;
                } else if (first == 'N') {
                    if (next_flag) {
                        parse_error(field, 0, "Next specified twice");
                    } else {
                        next = parse_dur(field, time);
                    }
                    next_flag = true;
                } else if (first == 'K') {
                    if (new_key_flag) {
                        parse_error(field, 0, "Key specified twice");
                    } else {
                        new_key = parse_key(field);
                        new_key_flag = true;
                    }
                } else if (first == 'L') {
                    if (loud_flag) {
                        parse_error(field, 0, "Loudness specified twice");
                    } else {
                        loud = parse_loud(field);
                    }
                    loud_flag = true;
                } else if (first == 'P') {
                    if (new_note_flag || new_pitch_flag) {
                        parse_error(field, 0, "Pitch specified twice");
                    } else {
                        new_pitch = parse_pitch(field);
                        new_pitch_flag = true;
                    }
                } else if (first == 'U') {
                    if (dur_flag) {
                        parse_error(field, 0, "Dur specified twice");
                    } else {
                        dur = parse_dur(field, time);
                        dur_flag = true;
                    }
                } else if (strchr("SIQHW", first)) {
                    if (dur_flag) {
                        parse_error(field, 0, "Dur specified twice");
                    } else {
                        // prepend 'U' to field, copy EOS too
                        memmove(field + 1, field, strlen(field) + 1);
                        field[0] = 'U';
                        dur = parse_dur(field, time);
                        dur_flag = true;
                    }
                } else if (strchr("ABCDEFG", first)) {
                    if (new_note_flag || new_pitch_flag) {
                        parse_error(field, 0, "Pitch specified twice");
                    } else {
                        // prepend 'K' to field, copy EOS too
                        memmove(field + 1, field, strlen(field) + 1);
                        field[0] = 'K';
                        new_note = parse_key(field);
                        new_note_flag = true;
                    }
                } else if (first == '-') {
                    Alg_parameter parm;
                    if (parse_attribute(field, &parm)) { // enter attribute-value pair
                        attributes = new Alg_parameters(attributes);
                        attributes->parm = parm;
                        parm.s = NULL; // protect string from deletion by destructor
                    }
                } else {
                    parse_error(field, 0, "Unknown field");
                }

                if (error_flag) {
                    field[0] = 0; // exit the loop
                } else {
                    line_parser.get_nonspace_quoted(field);
                    pk = line_parser.peek();
                    // attributes are parsed as two adjacent nonspace_quoted 
                    // tokens so we have to conditionally call 
                    // get_nonspace_quoted() again
                    if (pk && !isspace(pk)) {
                        line_parser.get_nonspace_quoted(field + strlen(field));
                    }
                }
            }
            // a case analysis:
            // Key < 128 counts as both key and pitch
            // A-G implies pitch AND key unless key given too
            //   K60 P60 -- both are specified, use 'em
            //   K60 P60 C4 -- overconstrained, an error
            //   K60 C4 -- overconstrained
            //   K60 -- OK, pitch is 60
            //   C4 P60 -- over constrained
            //   P60 -- OK, key is from before, pitch is 60
            //   C4 -- OK, key is 60, pitch is 60
            //   <nothing> -- OK, key and pitch from before
            //   K200 with P60 ok, pitch is 60
            //   K200 with neither P60 nor C4 uses 
            //       pitch from before
            // figure out what the key/instance is:
            if (new_key_flag) { // it was directly specified
                key = new_key;
                if (key < 128 && new_note_flag) {
                    parse_error("", 0, "Pitch specified twice");
                }
            } else if (new_note_flag) { // "A"-"G" used
                key = new_note;
            }
            if (new_pitch_flag) {
                pitch = new_pitch;
            } else if (key < 128) {
                pitch = key;
            }
            // now we've acquired new parameters
            // if (it is a note, then enter the note
            if (valid) {
                // change tempo or beat
                attributes = process_attributes(attributes, time);
                // if there's a duration or pitch, make a note:
                if (new_pitch_flag || dur_flag || new_note_flag) {
                    new_key_flag = false;
                    new_pitch_flag = false;
                    Alg_note_ptr note_ptr = new Alg_note;
                    note_ptr->chan = voice;
                    note_ptr->time = time;
                    note_ptr->dur = dur;
                    note_ptr->set_identifier(key);
                    note_ptr->pitch = pitch;
                    note_ptr->loud = loud;
                    note_ptr->parameters = attributes;
                    seq->add_event(note_ptr, track_num); // sort later
                    if (seq->get_real_dur() < (time + dur)) seq->set_real_dur(time + dur);
                } else {
                    int update_key = -1;
                    // key or pitch must appear explicitly; otherwise
                    //    update applies to channel
                    if (new_key_flag || new_pitch_flag) {
                        update_key = key;
                    }
                    if (loud_flag) {
                        Alg_update_ptr new_upd = new Alg_update;
                        new_upd->chan = voice;
                        new_upd->time = time;
                        new_upd->set_identifier(update_key);
                        new_upd->parameter.set_attr(symbol_table.insert_string("loudr"));
                        new_upd->parameter.r = pitch;
                        seq->add_event(new_upd, track_num);
                        if (seq->get_real_dur() < time) seq->set_real_dur(time);
                    }
                    if (attributes) {
                        while (attributes) {
                            Alg_update_ptr new_upd = new Alg_update;
                            new_upd->chan = voice;
                            new_upd->time = time;
                            new_upd->set_identifier(update_key);
                            new_upd->parameter = attributes->parm;
                            seq->add_event(new_upd, track_num);
                            Alg_parameters_ptr p = attributes;
                            attributes = attributes->next;
                            p->parm.s = NULL; // so we don't delete the string
                            delete p;
                        }
                    }
                }
                if (next_flag) {
                    time = time + next;
                } else if (dur_flag) {
                    time = time + dur;
                }
            }
        }
        readline();
    }
    //print "Finished reading score"
    if (!error_flag) {
        seq->convert_to_seconds(); // make sure format is correct
        // seq->notes.sort('event_greater_than');
    }
    // real_dur is valid, translate to beat_dur
    seq->set_beat_dur((seq->get_time_map())->time_to_beat(seq->get_real_dur()));
    // print "parse returns error_flag", error_flag
    return error_flag;
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


//Note that this is an #ifdef, not an #ifndef
#ifdef EXPERIMENTAL_NOTE_TRACK
long Alg_reader::parse_chan(char *field)
{
    char *int_string = field + 1;
    char *msg = "Integer or - expected";
    char *p = int_string;
    char c;
    // check that all chars in int_string are digits or '-':
    while (c = *p++) {
        if (!isdigit(c) && c != '-') {
            parse_error(field, p - field - 1, msg);
            return 0;
        }
    }
    p--; // p now points to end-of-string character
    if (p - int_string == 0) {
        // bad: string length is zero
        parse_error(field, 1, msg);
        return 0;
    }
    if (p - int_string == 1 && int_string[0] == '-') {
        // special case: entire string is "-", interpret as -1
        return -1;
    }
    return atoi(int_string);
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


#ifndef EXPERIMENTAL_NOTE_TRACK
long Allegro_reader::parse_int(char *field)
{
    char int_string[field_max];
    strcpy(int_string, field + 1);
    char *msg = "Integer expected";
    char *p = int_string;
    char c;
    while (c = *p++) {
        if (!isdigit(c)) {
            parse_error(field, 1, msg);
            return 0;
        }
    }
    if (strlen(int_string) < 1) {
        parse_error(field, 1, msg);
        return 0;
    }
    return atoi(int_string);
}
#else /* EXPERIMENTAL_NOTE_TRACK */
long Alg_reader::parse_int(char *field)
{
    char *int_string = field + 1;
    char *msg = "Integer expected";
    char *p = int_string;
    char c;
    // check that all chars in int_string are digits:
    while (c = *p++) {
        if (!isdigit(c)) {
            parse_error(field, p - field - 1, msg);
            return 0;
        }
    }
    p--; // p now points to end-of-string character
    if (p - int_string == 0) {
        // bad: string length is zero
        parse_error(field, 1, msg);
        return 0;
    }
    return atoi(int_string);
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


#ifndef EXPERIMENTAL_NOTE_TRACK
int Allegro_reader::find_real_in(char *field, int n)
#else /* EXPERIMENTAL_NOTE_TRACK */
int Alg_reader::find_real_in(char *field, int n)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    // scans from offset n to the end of a real constant
    bool decimal = false;
    int len = strlen(field);
    for (int i = n; i < len; i++) {
        char c = field[i];
        if (!isdigit(c)) {
            if (c == '.' && !decimal) {
                decimal = true;
            } else {
                return i;
            }
        }
    }
    return strlen(field);
}


#ifndef EXPERIMENTAL_NOTE_TRACK
double Allegro_reader::parse_real(char *field)
#else /* EXPERIMENTAL_NOTE_TRACK */
double Alg_reader::parse_real(char *field)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    char real_string[80];
    char *msg = "Real expected";
    bool decimal = false;
    int last = find_real_in(field, 1);
    subseq(real_string, field, 1, last);
    if (last <= 1 || last < (int) strlen(field)) {
       parse_error(field, 1, msg);
       return 0;
    }
    return atof(real_string);
}


#ifndef EXPERIMENTAL_NOTE_TRACK
void Allegro_reader::parse_error(char *field, long offset, char *message)
#else /* EXPERIMENTAL_NOTE_TRACK */
void Alg_reader::parse_error(char *field, long offset, char *message)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    int position = line_parser.pos - strlen(field) + offset;
    error_flag = true;
    puts(line_parser.string);
    for (int i = 0; i < position; i++) {
        putc(' ', stdout);
    }
    putc('^', stdout);
    printf("    %s\n", message);
}


double duration_lookup[] = { 0.25, 0.5, 1.0, 2.0, 4.0 };


#ifndef EXPERIMENTAL_NOTE_TRACK
double Allegro_reader::parse_dur(char *field, double base)
{
    char *msg = "Duration expected";
    char real_string[80];
    char *durs = "SIQHW";
    char *p;
    int last;
    double dur;
    if (strlen(field) < 2) {
        // fall through to error message
        return -1;
    } else if (isdigit(field[1])) {
        last = find_real_in(field, 1);
        subseq(real_string, field, 1, last);
        dur = atof(real_string);
        // convert dur from seconds to beats
        dur = seq.map.time_to_beat(base + dur) - seq.map.time_to_beat(base);
    } else if (p = strchr(durs, field[1])) {
        dur = duration_lookup[p - durs];
        last = 2;
    } else {
        parse_error(field, 1, msg);
        return 0;
    }
    dur = parse_after_dur(dur, field, last, base);
    dur = seq.map.beat_to_time(seq.map.time_to_beat(base) + dur) - base;
    return dur;
}
#else /* EXPERIMENTAL_NOTE_TRACK */
double Alg_reader::parse_dur(char *field, double base)
{
    char *msg = "Duration expected";
    char real_string[80];
    char *durs = "SIQHW";
    char *p;
    int last;
    double dur;
    if (strlen(field) < 2) {
        // fall through to error message
        return -1;
    } else if (isdigit(field[1])) {
        last = find_real_in(field, 1);
        subseq(real_string, field, 1, last);
        dur = atof(real_string);
        // convert dur from seconds to beats
        dur = seq->get_time_map()->time_to_beat(base + dur) - 
              seq->get_time_map()->time_to_beat(base);
    } else if (p = strchr(durs, field[1])) {
        dur = duration_lookup[p - durs];
        last = 2;
    } else {
        parse_error(field, 1, msg);
        return 0;
    }
    dur = parse_after_dur(dur, field, last, base);
    dur = seq->get_time_map()->beat_to_time(
              seq->get_time_map()->time_to_beat(base) + dur) - base;
    return dur;
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


#ifndef EXPERIMENTAL_NOTE_TRACK
double Allegro_reader::parse_after_dur(double dur, char *field, int n, double base)
{
    char a_string[80];
    if ((int) strlen(field) == n) {
        return dur;
    }
    if (field[n] == 'T') {
        return parse_after_dur(dur * 2/3, field, n + 1, base);
    }
    if (field[n] == '.') {
        return parse_after_dur(dur * 1.5, field, n + 1, base);
    }
    if (isdigit(field[n])) {
        int last = find_real_in(field, n);
        subseq(a_string, field, n, last);
        double f = atof(a_string);
        return parse_after_dur(dur * f, field, last, base);
    }
    if (field[n] == '+') {
        subseq(a_string, field, n + 1, -1);
        return dur + parse_dur(a_string, 
                               seq.map.beat_to_time(
                                   seq.map.time_to_beat(base) + dur));
    }
    parse_error(field, n, "Unexpected character in duration");
    return dur;
}
#else /* EXPERIMENTAL_NOTE_TRACK */
double Alg_reader::parse_after_dur(double dur, char *field, int n, double base)
{
    char a_string[80];
    if ((int) strlen(field) == n) {
        return dur;
    }
    if (field[n] == 'T') {
        return parse_after_dur(dur * 2/3, field, n + 1, base);
    }
    if (field[n] == '.') {
        return parse_after_dur(dur * 1.5, field, n + 1, base);
    }
    if (isdigit(field[n])) {
        int last = find_real_in(field, n);
        subseq(a_string, field, n, last);
        double f = atof(a_string);
        return parse_after_dur(dur * f, field, last, base);
    }
    if (field[n] == '+') {
        subseq(a_string, field, n + 1, -1);
        return dur + parse_dur(
                a_string, seq->get_time_map()->beat_to_time(
                              seq->get_time_map()->time_to_beat(base) + dur));
    }
    parse_error(field, n, "Unexpected character in duration");
    return dur;
}
コード例 #2
0
ファイル: allegrord.cpp プロジェクト: ruthmagnus/audacity
bool Allegro_reader::parse_val(Parameter_ptr param, char *s, int i)
{
    int len = (int) strlen(s);
    if (i >= len) {
        return false;
    }
    if (s[i] == '"') {
        if (!check_type('s', param)) {
            return false;
        }
        char *r = new char[len - i];
        subseq(r, s, i + 1, len - 1);
        param->s = r;
    } else if (s[i] == '\'') {
        if (!check_type('a', param)) {
            return false;
        }
        char r[80];
        subseq(r, s, i + 1, len - 1);
        param->a = symbol_table.insert_string(r);
    } else if (param->attr_type() == 'l') {
        if (streql(s + i, "true") || streql(s + i, "t")) {
            param->l = true;
        } else if (streql(s + i, "false") || streql(s + i, "nil")) {
            param->l = false;
        } else return false;
    } else if (isdigit(s[i])) {
        int pos = i + 1;
        bool period = false;
        while (pos < len) {
            if (isdigit(s[pos])) {
                ;
            } else if (!period && s[pos] == '.') {
                period = true;
            } else {
                parse_error(s, pos, "Unexpected char in number");
                return false;
            }
            pos = pos + 1;
        }
        char r[80];
        subseq(r, s, i, len);
        if (period) {
            if (!check_type('r', param)) {
                return false;
            }
            param->r = atof(r);
        } else {
            if (param->attr_type() == 'r') {
                param->r = atoi(r);
            } else if (!check_type('i', param)) {
                return false;
            } else {
                param->i = atoi(r);
            }
        }
    }
    return true;
}
コード例 #3
0
ファイル: html.c プロジェクト: JamesTFarrington/roswell
LVal parse_attr(char* str) {
  int i,j;
  char *name,*val;
  struct Cons tmp,*ret=&tmp,*cur=ret;
  struct opts* p;
  tmp.next=(LVal)NULL;
  for(i=0;str[i]!='\0';++i) {
    name=NULL,val=NULL;
    /*skip space*/
    int pos=position_char_not(" \t\r\n",&str[i]);
    if(pos!=-1) {
      i+=pos;
    }else continue;
    /*attr name */
    pos=position_char("\"'>/= \t\r\n",&str[i]);
    if(pos!=-1)
      name=subseq(&str[i],0,pos),i+=pos;
    /*skip space*/
    pos=position_char_not(" \t\r\n",&str[i]);
    if(pos!=-1)
      i+=pos;
    if(!(name && str[i]=='='))
      continue;
    cur->next=(LVal)attralloc();;
    p=(struct opts*)firstp((LVal)cur->next);
    p->name=name;
    /*skip space*/
    pos=position_char_not(" \t\r\n",&str[++i]);
    if(pos!=-1) {
      i+=pos;
      switch(str[i]) {
      case '\'':
        j=position_char("'",&str[i+1]);
        if(j!=-1) {
          val=subseq(&str[i+1],0,j);
          i+=j+1;
        }
        break;
      case '"':
        for(j=1;str[i+j]!='"';++j)
          if(str[i+j]=='\\')
            ++j;
        val=subseq(&str[i],1,j);
        i+=j;
        break;
      default:
        pos=position_char(" \t\r\n",&str[i]);
        val=subseq(&str[i],0,pos);
        break;
      }
      p->value=val;
    }
    cur=(struct Cons *)cur->next;
  }
  return (LVal)ret->next;
}
コード例 #4
0
ファイル: html.c プロジェクト: JamesTFarrington/roswell
LVal filter_sbcl_uri(LVal v) {
  char* str=subseq(firsts(v),-3,0);
  if(strcmp(str,"bz2")==0 ||
     strcmp(str,"msi")==0) {
    char* u=uname();
    char* m=uname_m();
    char *third,*fourth;
    char *m2;
    int i;
    char* tmp=file_namestring(q(firsts(v)));
    LVal ret= split_string(tmp,"-");
    s(tmp);
    third=firsts(nthcdr(2,ret));
    fourth=firsts(nthcdr(3,ret));
    if(strcmp(third,"x86")==0 &&
       strcmp(fourth,"64")==0) {
      m2=q("x86-64");
      i=4;
    }else {
      m2=q(third);
      i=3;
    }
    i=(strcmp(m2,m)==0 && strcmp(firsts(nthcdr(i,ret)),u)==0);

    s(m2),s(str),s(m),s(u),sL(ret);
    return i?toNumber(1):0;
  }
  s(str);
  return 0;
}
コード例 #5
0
ファイル: allegrord.cpp プロジェクト: ruthmagnus/audacity
#include "assert.h"
#include "stdlib.h"
#include "stdio.h"
#include "allegro.h"
#include "string.h"
#include "ctype.h"
//#include "memory.h"
#include "trace.h"
#include "strparse.h"
#ifndef EXPERIMENTAL_NOTE_TRACK
#include "allegrord.h"
#endif /* EXPERIMENTAL_NOTE_TRACK */
#define streql(s1, s2) (strcmp(s1, s2) == 0)
#define field_max 80


//Note that this is an #ifdef, not an #ifndef
#ifdef EXPERIMENTAL_NOTE_TRACK

class Alg_reader {
public:
    FILE *file;
    int line_no;
    String_parse line_parser;
    bool line_parser_flag;
    char field[field_max];
    bool error_flag;
    Alg_seq_ptr seq;
    double tsnum;
    double tsden;

    Alg_reader(FILE *a_file, Alg_seq_ptr new_seq);
    void readline();
    Alg_parameters_ptr process_attributes(Alg_parameters_ptr attributes, double time);
    bool parse();
    long parse_chan(char *field);
    long parse_int(char *field);
    int find_real_in(char *field, int n);
    double parse_real(char *field);
    void parse_error(char *field, long offset, char *message);
    double parse_dur(char *field, double base);
    double parse_after_dur(double dur, char *field, int n, double base);
    double parse_loud(char *field);
    long parse_key(char *field);
    double parse_pitch(char *field);
    long parse_after_key(int key, char *field, int n);
    long find_int_in(char *field, int n);
    bool parse_attribute(char *field, Alg_parameter_ptr parm);
    bool parse_val(Alg_parameter_ptr param, char *s, int i);
    bool check_type(char type_char, Alg_parameter_ptr param);
};

#endif /* EXPERIMENTAL_NOTE_TRACK */

void subseq(char *result, char *source, int from, int to)
{
    memcpy(result, source + from, to - from);
    result[to - from] = 0;
}

#ifndef EXPERIMENTAL_NOTE_TRACK
double Allegro_reader::parse_pitch(char *field)
#else /* EXPERIMENTAL_NOTE_TRACK */
double Alg_reader::parse_pitch(char *field)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    if (isdigit(field[1])) {
        char real_string[80];
        int last = find_real_in(field, 1);
        subseq(real_string, field, 1, last);
        return atof(real_string);
    } else {
        return (double) parse_key(field);
    }
}


#ifndef EXPERIMENTAL_NOTE_TRACK
Allegro_reader::Allegro_reader(FILE *a_file)
{
    file = a_file; // save the file
    line_parser_flag = false;
    line_no = 0;
    seq = Seq();
    tsnum = 4; // default time signature
    tsden = 4;
}
#else /* EXPERIMENTAL_NOTE_TRACK */
// it is the responsibility of the caller to delete
// the seq
Alg_reader::Alg_reader(FILE *a_file, Alg_seq_ptr new_seq)
{
    file = a_file; // save the file
    line_parser_flag = false;
    line_no = 0;
    tsnum = 4; // default time signature
    tsden = 4;
    seq = new_seq;
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


//Note that this is an #ifdef, not an #ifndef
#ifdef EXPERIMENTAL_NOTE_TRACK
Alg_seq_ptr alg_read(FILE *file, Alg_seq_ptr new_seq)
    // read a sequence from allegro file
{
    if (!new_seq) new_seq = new Alg_seq();
    Alg_reader alg_reader(file, new_seq);
    alg_reader.parse();
    return alg_reader.seq;
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


#ifndef EXPERIMENTAL_NOTE_TRACK
void Allegro_reader::readline()
#else /* EXPERIMENTAL_NOTE_TRACK */
void Alg_reader::readline()
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    char line[256];
    char *line_flag = fgets(line, 256, file);
    line_parser_flag = false;
    if (line_flag) {
        line_parser.init(line);
        line_parser_flag = true;
        error_flag = false;
    }
}


#ifndef EXPERIMENTAL_NOTE_TRACK
void Allegro_reader::process_attributes(Parameters_ptr attributes, double time)
{
    // print "process_attributes:", attributes
    bool ts_flag;
    if (attributes) {
        Parameters_ptr a;
        if (a = Parameters::remove_key(&attributes, "tempor")) {
            double tempo = a->parm.r;
            seq.insert_tempo(tempo, seq.map.time_to_beat(time));
        }
        if (a = Parameters::remove_key(&attributes, "beatr")) {
            double beat = a->parm.r;
            seq.insert_beat(time, beat);
        }
        if (a = Parameters::remove_key(&attributes, "tsnumr")) {
            tsnum = a->parm.r;
            ts_flag = true;
        }
        if (a = Parameters::remove_key(&attributes, "tsdenr")) {
            tsden = a->parm.r;
            ts_flag = true;
        }
        if (ts_flag) {
            seq.set_time_sig(seq.map.time_to_beat(time), tsnum, tsden);
        }
    }
}
#else /* EXPERIMENTAL_NOTE_TRACK */
Alg_parameters_ptr Alg_reader::process_attributes(
        Alg_parameters_ptr attributes, double time)
{
    // print "process_attributes:", attributes
    bool ts_flag = false;
    if (attributes) {
        Alg_parameters_ptr a;
        bool in_seconds = seq->get_units_are_seconds();
        if (a = Alg_parameters::remove_key(&attributes, "tempor")) {
            double tempo = a->parm.r;
            seq->insert_tempo(tempo, seq->get_time_map()->time_to_beat(time));
        }
        if (a = Alg_parameters::remove_key(&attributes, "beatr")) {
            double beat = a->parm.r;
            seq->insert_beat(time, beat);
        }
        if (a = Alg_parameters::remove_key(&attributes, "timesig_numr")) {
            tsnum = a->parm.r;
            ts_flag = true;
        }
        if (a = Alg_parameters::remove_key(&attributes, "timesig_denr")) {
            tsden = a->parm.r;
            ts_flag = true;
        }
        if (ts_flag) {
            seq->set_time_sig(seq->get_time_map()->time_to_beat(time),
            tsnum, tsden);
        }
        if (in_seconds) seq->convert_to_seconds();
    }
    return attributes; // in case it was modified
}
#endif /* EXPERIMENTAL_NOTE_TRACK */

#ifndef EXPERIMENTAL_NOTE_TRACK
bool Allegro_reader::parse()
{
    int voice = 0;
    int key = 60;
    double loud = 100.0;
    double pitch = 60.0;
    double dur = 1.0;
    double time = 0.0;
    readline();
    bool valid = false; // ignore blank lines
    while (line_parser_flag) {
        bool time_flag = false;
        bool next_flag = false;
        double next;
        bool voice_flag = false;
        bool loud_flag = false;
        bool dur_flag = false;
        bool new_pitch_flag = false; // "P" syntax
        double new_pitch = 0.0;
        bool new_key_flag = false;   // "K" syntax
        int new_key = 0;
        bool new_note_flag = false;  // "A"-"G" syntax
        int new_note = 0;
        Parameters_ptr attributes = NULL;
        line_parser.get_nonspace_quoted(field);
        char pk = line_parser.peek();
        if (pk && !isspace(pk)) {
            line_parser.get_nonspace_quoted(field + strlen(field));
        }
        while (field[0]) {
            // print "field", "|";field;"|", "|";line_parser.string;"|", line_parser.pos
            char first = toupper(field[0]);
            if (strchr("ABCDEFGKLPUSIQHW-", first)) {
                valid = true; // it's a note or event
            }
            if (first == 'V') {
                if (voice_flag) {
                    parse_error(field, 0, "Voice specified twice");
                } else {
                    voice = parse_int(field);
                }
                voice_flag = true;
            } else if (first == 'T') {
                if (time_flag) {
                    parse_error(field, 0, "Time specified twice");
                } else {
                    time = parse_dur(field, 0.0);
                }
                time_flag = true;
            } else if (first == 'N') {
                if (next_flag) {
                    parse_error(field, 0, "Next specified twice");
                } else {
                    next = parse_dur(field, time);
                }
                next_flag = true;
            } else if (first == 'K') {
                if (new_key_flag) {
                    parse_error(field, 0, "Key specified twice");
                } else {
                    new_key = parse_key(field);
                    new_key_flag = true;
                }
            } else if (first == 'L') {
                if (loud_flag) {
                    parse_error(field, 0, "Loudness specified twice");
                } else {
                    loud = parse_loud(field);
                }
                loud_flag = true;
            } else if (first == 'P') {
                if (new_note_flag || new_pitch_flag) {
                    parse_error(field, 0, "Pitch specified twice");
                } else {
                    new_pitch = parse_pitch(field);
                    new_pitch_flag = true;
                }
            } else if (first == 'U') {
                if (dur_flag) {
                    parse_error(field, 0, "Dur specified twice");
                } else {
                    dur = parse_dur(field, time);
                    dur_flag = true;
                }
            } else if (strchr("SIQHW", first)) {
                if (dur_flag) {
                    parse_error(field, 0, "Dur specified twice");
                } else {
                    // prepend 'U' to field, copy EOS too
                    memmove(field + 1, field, strlen(field) + 1);
                    field[0] = 'U';
                    dur = parse_dur(field, time);
                    dur_flag = true;
                }
            } else if (strchr("ABCDEFG", first)) {
                if (new_note_flag || new_pitch_flag) {
                    parse_error(field, 0, "Pitch specified twice");
                } else {
                    // prepend 'K' to field, copy EOS too
                    memmove(field + 1, field, strlen(field) + 1);
                    field[0] = 'K';
                    new_note = parse_key(field);
                    new_note_flag = true;
                }
            } else if (first == '-') {
                Parameter parm;
                if (parse_attribute(field, &parm)) { // enter attribute-value pair
                    attributes = new Parameters(attributes);
                    attributes->parm = parm;
                    parm.s = NULL; // protect string from deletion by destructor
                }
            } else {
                parse_error(field, 0, "Unknown field");
            }

            if (error_flag) {
                field[0] = 0; // exit the loop
            } else {
                line_parser.get_nonspace_quoted(field);
                pk = line_parser.peek();
                if (pk && !isspace(pk)) {
                    line_parser.get_nonspace_quoted(field + strlen(field));
                }
            }
        }
        // a case analysis:
        // Key < 128 counts as both key and pitch
        // A-G implies pitch AND key unless key given too
        //   K60 P60 -- both are specified, use 'em
        //   K60 P60 C4 -- overconstrained, an error
        //   K60 C4 -- overconstrained
        //   K60 -- OK, pitch is 60
        //   C4 P60 -- over constrained
        //   P60 -- OK, key is from before, pitch is 60
        //   C4 -- OK, key is 60, pitch is 60
        //   <nothing> -- OK, key and pitch from before
        //   K200 with P60 ok, pitch is 60
        //   K200 with neither P60 nor C4 uses 
        //       pitch from before
        // figure out what the key/instance is:
        if (new_key_flag) { // it was directly specified
            key = new_key;
            if (key < 128 && new_note_flag) {
                parse_error("", 0, "Pitch specified twice");
            }
        } else if (new_note_flag) { // "A"-"G" used
            key = new_note;
        }
        if (new_pitch_flag) {
            pitch = new_pitch;
        } else if (key < 128) {
            pitch = key;
        }
        // now we've acquired new parameters
        // if (it is a note, then enter the note
        if (valid) {
            // change tempo or beat
            process_attributes(attributes, time);
            // if there's a duration or pitch, make a note:
            if (new_pitch_flag || dur_flag || new_note_flag) {
                new_key_flag = false;
                new_pitch_flag = false;
                Allegro_note_ptr note_ptr = new Allegro_note;
                note_ptr->chan = voice;
                note_ptr->time = time;
                note_ptr->dur = dur;
                note_ptr->key = key;
                note_ptr->pitch = pitch;
                note_ptr->loud = loud;
                note_ptr->parameters = attributes;
                seq.add_event(note_ptr); // sort later
            } else {
                int update_key = -1;
                // key or pitch must appear explicitly; otherwise
                //    update applies to channel
                if (new_key_flag || new_pitch_flag) {
                    update_key = key;
                }
                if (loud_flag) {
                    Allegro_update_ptr new_upd = new Allegro_update;
                    new_upd->chan = voice;
                    new_upd->time = time;
                    new_upd->key = update_key;
                    new_upd->parameter.set_attr(symbol_table.insert_string("loudr"));
                    new_upd->parameter.r = pitch;
                    seq.add_event(new_upd);
                }
                if (attributes) {
                    while (attributes) {
                        Allegro_update_ptr new_upd = new Allegro_update;
                        new_upd->chan = voice;
                        new_upd->time = time;
                        new_upd->key = update_key;
                        new_upd->parameter = attributes->parm;
                        seq.add_event(new_upd);
                        Parameters_ptr p = attributes;
                        attributes = attributes->next;
                        delete p;
                    }
                }
            }
            if (next_flag) {
                time = time + next;
            } else if (dur_flag) {
                time = time + dur;
            }
        }
        readline();
    }
    //print "Finished reading score"
    if (!error_flag) {
        seq.convert_to_seconds(); // make sure format is correct
        // seq.notes.sort('event_greater_than');
    }
    // print "parse returns error_flag", error_flag
    return error_flag;
}
#else /* EXPERIMENTAL_NOTE_TRACK */
bool Alg_reader::parse()
{
    int voice = 0;
    int key = 60;
    double loud = 100.0;
    double pitch = 60.0;
    double dur = 1.0;
    double time = 0.0;
    int track_num = 0;
    seq->convert_to_seconds();
    //seq->set_real_dur(0.0); // just in case it's not initialized already
    readline();
    bool valid = false; // ignore blank lines
    while (line_parser_flag) {
        bool time_flag = false;
        bool next_flag = false;
        double next;
        bool voice_flag = false;
        bool loud_flag = false;
        bool dur_flag = false;
        bool new_pitch_flag = false; // "P" syntax
        double new_pitch = 0.0;
        bool new_key_flag = false;   // "K" syntax
        int new_key = 0;
        bool new_note_flag = false;  // "A"-"G" syntax
        int new_note = 0;
        Alg_parameters_ptr attributes = NULL;
        if (line_parser.peek() == '#') {
            // look for #track
            line_parser.get_nonspace_quoted(field);
            if (streql(field, "#track")) {
                line_parser.get_nonspace_quoted(field); // number
                track_num = parse_int(field - 1);
                seq->add_track(track_num);
            }
            // maybe we have a comment
        } else {
            // we must have a track to insert into
            if (seq->tracks() == 0) seq->add_track(0);
            line_parser.get_nonspace_quoted(field);
            char pk = line_parser.peek();
            // attributes are parsed as two adjacent nonspace_quoted tokens
            // so we have to conditionally call get_nonspace_quoted() again
            if (pk && !isspace(pk)) {
                line_parser.get_nonspace_quoted(field + strlen(field));
            }
            while (field[0]) {
                char first = toupper(field[0]);
                if (strchr("ABCDEFGKLPUSIQHW-", first)) {
                    valid = true; // it's a note or event
                }
                if (first == 'V') {
                    if (voice_flag) {
                        parse_error(field, 0, "Voice specified twice");
                    } else {
                        voice = parse_chan(field);
                    }
                    voice_flag = true;
                } else if (first == 'T') {
                    if (time_flag) {
                        parse_error(field, 0, "Time specified twice");
                    } else {
                        time = parse_dur(field, 0.0);
                    }
                    time_flag = true;
                } else if (first == 'N') {
                    if (next_flag) {
                        parse_error(field, 0, "Next specified twice");
                    } else {
                        next = parse_dur(field, time);
                    }
                    next_flag = true;
                } else if (first == 'K') {
                    if (new_key_flag) {
                        parse_error(field, 0, "Key specified twice");
                    } else {
                        new_key = parse_key(field);
                        new_key_flag = true;
                    }
                } else if (first == 'L') {
                    if (loud_flag) {
                        parse_error(field, 0, "Loudness specified twice");
                    } else {
                        loud = parse_loud(field);
                    }
                    loud_flag = true;
                } else if (first == 'P') {
                    if (new_note_flag || new_pitch_flag) {
                        parse_error(field, 0, "Pitch specified twice");
                    } else {
                        new_pitch = parse_pitch(field);
                        new_pitch_flag = true;
                    }
                } else if (first == 'U') {
                    if (dur_flag) {
                        parse_error(field, 0, "Dur specified twice");
                    } else {
                        dur = parse_dur(field, time);
                        dur_flag = true;
                    }
                } else if (strchr("SIQHW", first)) {
                    if (dur_flag) {
                        parse_error(field, 0, "Dur specified twice");
                    } else {
                        // prepend 'U' to field, copy EOS too
                        memmove(field + 1, field, strlen(field) + 1);
                        field[0] = 'U';
                        dur = parse_dur(field, time);
                        dur_flag = true;
                    }
                } else if (strchr("ABCDEFG", first)) {
                    if (new_note_flag || new_pitch_flag) {
                        parse_error(field, 0, "Pitch specified twice");
                    } else {
                        // prepend 'K' to field, copy EOS too
                        memmove(field + 1, field, strlen(field) + 1);
                        field[0] = 'K';
                        new_note = parse_key(field);
                        new_note_flag = true;
                    }
                } else if (first == '-') {
                    Alg_parameter parm;
                    if (parse_attribute(field, &parm)) { // enter attribute-value pair
                        attributes = new Alg_parameters(attributes);
                        attributes->parm = parm;
                        parm.s = NULL; // protect string from deletion by destructor
                    }
                } else {
                    parse_error(field, 0, "Unknown field");
                }

                if (error_flag) {
                    field[0] = 0; // exit the loop
                } else {
                    line_parser.get_nonspace_quoted(field);
                    pk = line_parser.peek();
                    // attributes are parsed as two adjacent nonspace_quoted 
                    // tokens so we have to conditionally call 
                    // get_nonspace_quoted() again
                    if (pk && !isspace(pk)) {
                        line_parser.get_nonspace_quoted(field + strlen(field));
                    }
                }
            }
            // a case analysis:
            // Key < 128 counts as both key and pitch
            // A-G implies pitch AND key unless key given too
            //   K60 P60 -- both are specified, use 'em
            //   K60 P60 C4 -- overconstrained, an error
            //   K60 C4 -- overconstrained
            //   K60 -- OK, pitch is 60
            //   C4 P60 -- over constrained
            //   P60 -- OK, key is from before, pitch is 60
            //   C4 -- OK, key is 60, pitch is 60
            //   <nothing> -- OK, key and pitch from before
            //   K200 with P60 ok, pitch is 60
            //   K200 with neither P60 nor C4 uses 
            //       pitch from before
            // figure out what the key/instance is:
            if (new_key_flag) { // it was directly specified
                key = new_key;
                if (key < 128 && new_note_flag) {
                    parse_error("", 0, "Pitch specified twice");
                }
            } else if (new_note_flag) { // "A"-"G" used
                key = new_note;
            }
            if (new_pitch_flag) {
                pitch = new_pitch;
            } else if (key < 128) {
                pitch = key;
            }
            // now we've acquired new parameters
            // if (it is a note, then enter the note
            if (valid) {
                // change tempo or beat
                attributes = process_attributes(attributes, time);
                // if there's a duration or pitch, make a note:
                if (new_pitch_flag || dur_flag || new_note_flag) {
                    new_key_flag = false;
                    new_pitch_flag = false;
                    Alg_note_ptr note_ptr = new Alg_note;
                    note_ptr->chan = voice;
                    note_ptr->time = time;
                    note_ptr->dur = dur;
                    note_ptr->set_identifier(key);
                    note_ptr->pitch = pitch;
                    note_ptr->loud = loud;
                    note_ptr->parameters = attributes;
                    seq->add_event(note_ptr, track_num); // sort later
                    if (seq->get_real_dur() < (time + dur)) seq->set_real_dur(time + dur);
                } else {
                    int update_key = -1;
                    // key or pitch must appear explicitly; otherwise
                    //    update applies to channel
                    if (new_key_flag || new_pitch_flag) {
                        update_key = key;
                    }
                    if (loud_flag) {
                        Alg_update_ptr new_upd = new Alg_update;
                        new_upd->chan = voice;
                        new_upd->time = time;
                        new_upd->set_identifier(update_key);
                        new_upd->parameter.set_attr(symbol_table.insert_string("loudr"));
                        new_upd->parameter.r = pitch;
                        seq->add_event(new_upd, track_num);
                        if (seq->get_real_dur() < time) seq->set_real_dur(time);
                    }
                    if (attributes) {
                        while (attributes) {
                            Alg_update_ptr new_upd = new Alg_update;
                            new_upd->chan = voice;
                            new_upd->time = time;
                            new_upd->set_identifier(update_key);
                            new_upd->parameter = attributes->parm;
                            seq->add_event(new_upd, track_num);
                            Alg_parameters_ptr p = attributes;
                            attributes = attributes->next;
                            p->parm.s = NULL; // so we don't delete the string
                            delete p;
                        }
                    }
                }
                if (next_flag) {
                    time = time + next;
                } else if (dur_flag) {
                    time = time + dur;
                }
            }
        }
        readline();
    }
    //print "Finished reading score"
    if (!error_flag) {
        seq->convert_to_seconds(); // make sure format is correct
        // seq->notes.sort('event_greater_than');
    }
    // real_dur is valid, translate to beat_dur
    seq->set_beat_dur((seq->get_time_map())->time_to_beat(seq->get_real_dur()));
    // print "parse returns error_flag", error_flag
    return error_flag;
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


//Note that this is an #ifdef, not an #ifndef
#ifdef EXPERIMENTAL_NOTE_TRACK
long Alg_reader::parse_chan(char *field)
{
    char *int_string = field + 1;
    char *msg = "Integer or - expected";
    char *p = int_string;
    char c;
    // check that all chars in int_string are digits or '-':
    while (c = *p++) {
        if (!isdigit(c) && c != '-') {
            parse_error(field, p - field - 1, msg);
            return 0;
        }
    }
    p--; // p now points to end-of-string character
    if (p - int_string == 0) {
        // bad: string length is zero
        parse_error(field, 1, msg);
        return 0;
    }
    if (p - int_string == 1 && int_string[0] == '-') {
        // special case: entire string is "-", interpret as -1
        return -1;
    }
    return atoi(int_string);
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


#ifndef EXPERIMENTAL_NOTE_TRACK
long Allegro_reader::parse_int(char *field)
{
    char int_string[field_max];
    strcpy(int_string, field + 1);
    char *msg = "Integer expected";
    char *p = int_string;
    char c;
    while (c = *p++) {
        if (!isdigit(c)) {
            parse_error(field, 1, msg);
            return 0;
        }
    }
    if (strlen(int_string) < 1) {
        parse_error(field, 1, msg);
        return 0;
    }
    return atoi(int_string);
}
#else /* EXPERIMENTAL_NOTE_TRACK */
long Alg_reader::parse_int(char *field)
{
    char *int_string = field + 1;
    char *msg = "Integer expected";
    char *p = int_string;
    char c;
    // check that all chars in int_string are digits:
    while (c = *p++) {
        if (!isdigit(c)) {
            parse_error(field, p - field - 1, msg);
            return 0;
        }
    }
    p--; // p now points to end-of-string character
    if (p - int_string == 0) {
        // bad: string length is zero
        parse_error(field, 1, msg);
        return 0;
    }
    return atoi(int_string);
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


#ifndef EXPERIMENTAL_NOTE_TRACK
int Allegro_reader::find_real_in(char *field, int n)
#else /* EXPERIMENTAL_NOTE_TRACK */
int Alg_reader::find_real_in(char *field, int n)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    // scans from offset n to the end of a real constant
    bool decimal = false;
    int len = strlen(field);
    for (int i = n; i < len; i++) {
        char c = field[i];
        if (!isdigit(c)) {
            if (c == '.' && !decimal) {
                decimal = true;
            } else {
                return i;
            }
        }
    }
    return strlen(field);
}


#ifndef EXPERIMENTAL_NOTE_TRACK
double Allegro_reader::parse_real(char *field)
#else /* EXPERIMENTAL_NOTE_TRACK */
double Alg_reader::parse_real(char *field)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    char real_string[80];
    char *msg = "Real expected";
    bool decimal = false;
    int last = find_real_in(field, 1);
    subseq(real_string, field, 1, last);
    if (last <= 1 || last < (int) strlen(field)) {
       parse_error(field, 1, msg);
       return 0;
    }
    return atof(real_string);
}


#ifndef EXPERIMENTAL_NOTE_TRACK
void Allegro_reader::parse_error(char *field, long offset, char *message)
#else /* EXPERIMENTAL_NOTE_TRACK */
void Alg_reader::parse_error(char *field, long offset, char *message)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    int position = line_parser.pos - strlen(field) + offset;
    error_flag = true;
    puts(line_parser.string);
    for (int i = 0; i < position; i++) {
        putc(' ', stdout);
    }
    putc('^', stdout);
    printf("    %s\n", message);
}


double duration_lookup[] = { 0.25, 0.5, 1.0, 2.0, 4.0 };


#ifndef EXPERIMENTAL_NOTE_TRACK
double Allegro_reader::parse_dur(char *field, double base)
{
    char *msg = "Duration expected";
    char real_string[80];
    char *durs = "SIQHW";
    char *p;
    int last;
    double dur;
    if (strlen(field) < 2) {
        // fall through to error message
        return -1;
    } else if (isdigit(field[1])) {
        last = find_real_in(field, 1);
        subseq(real_string, field, 1, last);
        dur = atof(real_string);
        // convert dur from seconds to beats
        dur = seq.map.time_to_beat(base + dur) - seq.map.time_to_beat(base);
    } else if (p = strchr(durs, field[1])) {
        dur = duration_lookup[p - durs];
        last = 2;
    } else {
        parse_error(field, 1, msg);
        return 0;
    }
    dur = parse_after_dur(dur, field, last, base);
    dur = seq.map.beat_to_time(seq.map.time_to_beat(base) + dur) - base;
    return dur;
}
#else /* EXPERIMENTAL_NOTE_TRACK */
double Alg_reader::parse_dur(char *field, double base)
{
    char *msg = "Duration expected";
    char real_string[80];
    char *durs = "SIQHW";
    char *p;
    int last;
    double dur;
    if (strlen(field) < 2) {
        // fall through to error message
        return -1;
    } else if (isdigit(field[1])) {
        last = find_real_in(field, 1);
        subseq(real_string, field, 1, last);
        dur = atof(real_string);
        // convert dur from seconds to beats
        dur = seq->get_time_map()->time_to_beat(base + dur) - 
              seq->get_time_map()->time_to_beat(base);
    } else if (p = strchr(durs, field[1])) {
        dur = duration_lookup[p - durs];
        last = 2;
    } else {
        parse_error(field, 1, msg);
        return 0;
    }
    dur = parse_after_dur(dur, field, last, base);
    dur = seq->get_time_map()->beat_to_time(
              seq->get_time_map()->time_to_beat(base) + dur) - base;
    return dur;
}
コード例 #6
0
ファイル: cmd-run.c プロジェクト: JamesTFarrington/roswell
char* determin_impl(char* impl) {
  char* version=NULL;
  int pos;
  cond_printf(1,"determin_impl:%s\n",impl);
  if(impl && (pos=position_char("/",impl))!=-1) {
    version=subseq(impl,pos+1,0);
    impl=subseq(impl,0,pos);
  }else {
    if(!impl)
      impl=get_opt("default.lisp",1);
    if(impl) {
      char* opt=s_cat(q(impl),q("."),q("version"),NULL);
      version=get_opt(opt,1);
      s(opt);
    }
    if(!impl)
      impl=DEFAULT_IMPL;
    impl=q(impl);
    if(version)
      version=q(version);
  }
  if(!version&&strcmp(impl,DEFAULT_IMPL)!=0) {
    cond_printf(1,"once!%s,%s\n",impl,version);
    if(!version)
      s(version);
    version=q("system");
  }

  if(!(impl && version)) {
    char* cmd=cat(which(argv_orig[0]),verbose>0?(verbose>1?" -v -v":" -v"):""," setup",NULL);
    char* ret;
    if(impl) s(impl);
    impl=q(DEFAULT_IMPL);
    cond_printf(1,"cmd:%s\n",cmd);
    ret=system_(cmd);
    cond_printf(1,"ret:%s\n",ret);
    s(ret);
    char* path=s_cat(configdir(),q("config"),NULL);
    global_opt=load_opts(path),s(path);;
    version=get_opt(DEFAULT_IMPL".version",0);
  }
  return s_cat(impl,q("/"),version,NULL);
}
コード例 #7
0
ファイル: allegrord.cpp プロジェクト: ruthmagnus/audacity
#include "assert.h"
#include "stdlib.h"
#include "stdio.h"
#include "allegro.h"
#include "string.h"
#include "ctype.h"
//#include "memory.h"
#include "trace.h"
#include "strparse.h"
#ifndef EXPERIMENTAL_NOTE_TRACK
#include "allegrord.h"
#endif /* EXPERIMENTAL_NOTE_TRACK */
#define streql(s1, s2) (strcmp(s1, s2) == 0)
#define field_max 80


//Note that this is an #ifdef, not an #ifndef
#ifdef EXPERIMENTAL_NOTE_TRACK

class Alg_reader {
public:
    FILE *file;
    int line_no;
    String_parse line_parser;
    bool line_parser_flag;
    char field[field_max];
    bool error_flag;
    Alg_seq_ptr seq;
    double tsnum;
    double tsden;

    Alg_reader(FILE *a_file, Alg_seq_ptr new_seq);
    void readline();
    Alg_parameters_ptr process_attributes(Alg_parameters_ptr attributes, double time);
    bool parse();
    long parse_chan(char *field);
    long parse_int(char *field);
    int find_real_in(char *field, int n);
    double parse_real(char *field);
    void parse_error(char *field, long offset, char *message);
    double parse_dur(char *field, double base);
    double parse_after_dur(double dur, char *field, int n, double base);
    double parse_loud(char *field);
    long parse_key(char *field);
    double parse_pitch(char *field);
    long parse_after_key(int key, char *field, int n);
    long find_int_in(char *field, int n);
    bool parse_attribute(char *field, Alg_parameter_ptr parm);
    bool parse_val(Alg_parameter_ptr param, char *s, int i);
    bool check_type(char type_char, Alg_parameter_ptr param);
};

#endif /* EXPERIMENTAL_NOTE_TRACK */

void subseq(char *result, char *source, int from, int to)
{
    memcpy(result, source + from, to - from);
    result[to - from] = 0;
}

#ifndef EXPERIMENTAL_NOTE_TRACK
double Allegro_reader::parse_pitch(char *field)
#else /* EXPERIMENTAL_NOTE_TRACK */
double Alg_reader::parse_pitch(char *field)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    if (isdigit(field[1])) {
        char real_string[80];
        int last = find_real_in(field, 1);
        subseq(real_string, field, 1, last);
        return atof(real_string);
    } else {
        return (double) parse_key(field);
    }
}
コード例 #8
0
ファイル: download.c プロジェクト: dtpeters/roswell
static size_t header_callback(char *buffer, size_t size,size_t nitems, int *opt) {
  int pos=-1,pos2,code=0;
  if(strncmp("HTTP",buffer,nitems<4?nitems:4)==0) {
    pos=position_char(" ",buffer);
    if(pos!=-1 && (pos2=position_char_not("0123456789",&buffer[pos+1]))!=-1) {
      char *num=subseq(&buffer[pos+1],0,pos2);
      code=atoi(num),s(num);
      if(verbose)
        fprintf(c_out, "http response:%d\n",code);
    }
    if(400<=code)
      return 0; /*invoke error for curl*/
  }else if(strncmp("content-length:",downcase(buffer),nitems<15?nitems:15)==0) {
    pos=position_char(" ",buffer);
    if(pos!=-1 && (pos2=position_char_not("0123456789",&buffer[pos+1]))!=-1) {
      char *num=subseq(&buffer[pos+1],0,pos2);
      code=atoi(num),s(num);
      content_length=code;
    }
  }
  return nitems * size;
}
コード例 #9
0
ファイル: opt.c プロジェクト: neuroradiology/roswell
struct opts* load_opts(const char* path) {
  FILE* fp;
  char buf[1024];
  struct opts opt;
  struct opts *cur=&opt;
  opt.next=NULL;

  if((fp=fopen(path,"r"))==NULL)
    return NULL;

  while(fgets(buf,1024,fp) !=NULL) {
    int i,mode,last;
    cur->next=(struct opts*)alloc(sizeof(struct opts));
    cur=cur->next;
    cur->type=OPT_VOID;
    cur->value=NULL;
    cur->name=NULL;
    cur->next=NULL;
    for(i=0,mode=0,last=0;i<1024&&buf[i]!='\0';++i) {
      if(buf[i]=='\t'||buf[i]=='\n') {
        switch (mode++) {
        case 0:
          cur->name=subseq(buf,last,i);
          break;
        case 1:
          cur->type=buf[i-1]-'0';
          break;
        case 2:
          cur->value=subseq(buf,last,i);
          break;
        }
        last=i+1;
      }
    }
  }
  fclose (fp);
  return opt.next;
}
コード例 #10
0
ファイル: allegrord.cpp プロジェクト: ruthmagnus/audacity
#include "assert.h"
#include "stdlib.h"
#include "stdio.h"
#include "allegro.h"
#include "string.h"
#include "ctype.h"
//#include "memory.h"
#include "trace.h"
#include "strparse.h"
#ifndef EXPERIMENTAL_NOTE_TRACK
#include "allegrord.h"
#endif /* EXPERIMENTAL_NOTE_TRACK */
#define streql(s1, s2) (strcmp(s1, s2) == 0)
#define field_max 80


//Note that this is an #ifdef, not an #ifndef
#ifdef EXPERIMENTAL_NOTE_TRACK

class Alg_reader {
public:
    FILE *file;
    int line_no;
    String_parse line_parser;
    bool line_parser_flag;
    char field[field_max];
    bool error_flag;
    Alg_seq_ptr seq;
    double tsnum;
    double tsden;

    Alg_reader(FILE *a_file, Alg_seq_ptr new_seq);
    void readline();
    Alg_parameters_ptr process_attributes(Alg_parameters_ptr attributes, double time);
    bool parse();
    long parse_chan(char *field);
    long parse_int(char *field);
    int find_real_in(char *field, int n);
    double parse_real(char *field);
    void parse_error(char *field, long offset, char *message);
    double parse_dur(char *field, double base);
    double parse_after_dur(double dur, char *field, int n, double base);
    double parse_loud(char *field);
    long parse_key(char *field);
    double parse_pitch(char *field);
    long parse_after_key(int key, char *field, int n);
    long find_int_in(char *field, int n);
    bool parse_attribute(char *field, Alg_parameter_ptr parm);
    bool parse_val(Alg_parameter_ptr param, char *s, int i);
    bool check_type(char type_char, Alg_parameter_ptr param);
};

#endif /* EXPERIMENTAL_NOTE_TRACK */

void subseq(char *result, char *source, int from, int to)
{
    memcpy(result, source + from, to - from);
    result[to - from] = 0;
}

#ifndef EXPERIMENTAL_NOTE_TRACK
double Allegro_reader::parse_pitch(char *field)
#else /* EXPERIMENTAL_NOTE_TRACK */
double Alg_reader::parse_pitch(char *field)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    if (isdigit(field[1])) {
        char real_string[80];
        int last = find_real_in(field, 1);
        subseq(real_string, field, 1, last);
        return atof(real_string);
    } else {
        return (double) parse_key(field);
    }
}


#ifndef EXPERIMENTAL_NOTE_TRACK
Allegro_reader::Allegro_reader(FILE *a_file)
{
    file = a_file; // save the file
    line_parser_flag = false;
    line_no = 0;
    seq = Seq();
    tsnum = 4; // default time signature
    tsden = 4;
}
#else /* EXPERIMENTAL_NOTE_TRACK */
// it is the responsibility of the caller to delete
// the seq
Alg_reader::Alg_reader(FILE *a_file, Alg_seq_ptr new_seq)
{
    file = a_file; // save the file
    line_parser_flag = false;
    line_no = 0;
    tsnum = 4; // default time signature
    tsden = 4;
    seq = new_seq;
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


//Note that this is an #ifdef, not an #ifndef
#ifdef EXPERIMENTAL_NOTE_TRACK
Alg_seq_ptr alg_read(FILE *file, Alg_seq_ptr new_seq)
    // read a sequence from allegro file
{
    if (!new_seq) new_seq = new Alg_seq();
    Alg_reader alg_reader(file, new_seq);
    alg_reader.parse();
    return alg_reader.seq;
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


#ifndef EXPERIMENTAL_NOTE_TRACK
void Allegro_reader::readline()
#else /* EXPERIMENTAL_NOTE_TRACK */
void Alg_reader::readline()
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    char line[256];
    char *line_flag = fgets(line, 256, file);
    line_parser_flag = false;
    if (line_flag) {
        line_parser.init(line);
        line_parser_flag = true;
        error_flag = false;
    }
}


#ifndef EXPERIMENTAL_NOTE_TRACK
void Allegro_reader::process_attributes(Parameters_ptr attributes, double time)
{
    // print "process_attributes:", attributes
    bool ts_flag;
    if (attributes) {
        Parameters_ptr a;
        if (a = Parameters::remove_key(&attributes, "tempor")) {
            double tempo = a->parm.r;
            seq.insert_tempo(tempo, seq.map.time_to_beat(time));
        }
        if (a = Parameters::remove_key(&attributes, "beatr")) {
            double beat = a->parm.r;
            seq.insert_beat(time, beat);
        }
        if (a = Parameters::remove_key(&attributes, "tsnumr")) {
            tsnum = a->parm.r;
            ts_flag = true;
        }
        if (a = Parameters::remove_key(&attributes, "tsdenr")) {
            tsden = a->parm.r;
            ts_flag = true;
        }
        if (ts_flag) {
            seq.set_time_sig(seq.map.time_to_beat(time), tsnum, tsden);
        }
    }
}
#else /* EXPERIMENTAL_NOTE_TRACK */
Alg_parameters_ptr Alg_reader::process_attributes(
        Alg_parameters_ptr attributes, double time)
{
    // print "process_attributes:", attributes
    bool ts_flag = false;
    if (attributes) {
        Alg_parameters_ptr a;
        bool in_seconds = seq->get_units_are_seconds();
        if (a = Alg_parameters::remove_key(&attributes, "tempor")) {
            double tempo = a->parm.r;
            seq->insert_tempo(tempo, seq->get_time_map()->time_to_beat(time));
        }
        if (a = Alg_parameters::remove_key(&attributes, "beatr")) {
            double beat = a->parm.r;
            seq->insert_beat(time, beat);
        }
        if (a = Alg_parameters::remove_key(&attributes, "timesig_numr")) {
            tsnum = a->parm.r;
            ts_flag = true;
        }
        if (a = Alg_parameters::remove_key(&attributes, "timesig_denr")) {
            tsden = a->parm.r;
            ts_flag = true;
        }
        if (ts_flag) {
            seq->set_time_sig(seq->get_time_map()->time_to_beat(time),
            tsnum, tsden);
        }
        if (in_seconds) seq->convert_to_seconds();
    }
    return attributes; // in case it was modified
}
#endif /* EXPERIMENTAL_NOTE_TRACK */

#ifndef EXPERIMENTAL_NOTE_TRACK
bool Allegro_reader::parse()
{
    int voice = 0;
    int key = 60;
    double loud = 100.0;
    double pitch = 60.0;
    double dur = 1.0;
    double time = 0.0;
    readline();
    bool valid = false; // ignore blank lines
    while (line_parser_flag) {
        bool time_flag = false;
        bool next_flag = false;
        double next;
        bool voice_flag = false;
        bool loud_flag = false;
        bool dur_flag = false;
        bool new_pitch_flag = false; // "P" syntax
        double new_pitch = 0.0;
        bool new_key_flag = false;   // "K" syntax
        int new_key = 0;
        bool new_note_flag = false;  // "A"-"G" syntax
        int new_note = 0;
        Parameters_ptr attributes = NULL;
        line_parser.get_nonspace_quoted(field);
        char pk = line_parser.peek();
        if (pk && !isspace(pk)) {
            line_parser.get_nonspace_quoted(field + strlen(field));
        }
        while (field[0]) {
            // print "field", "|";field;"|", "|";line_parser.string;"|", line_parser.pos
            char first = toupper(field[0]);
            if (strchr("ABCDEFGKLPUSIQHW-", first)) {
                valid = true; // it's a note or event
            }
            if (first == 'V') {
                if (voice_flag) {
                    parse_error(field, 0, "Voice specified twice");
                } else {
                    voice = parse_int(field);
                }
                voice_flag = true;
            } else if (first == 'T') {
                if (time_flag) {
                    parse_error(field, 0, "Time specified twice");
                } else {
                    time = parse_dur(field, 0.0);
                }
                time_flag = true;
            } else if (first == 'N') {
                if (next_flag) {
                    parse_error(field, 0, "Next specified twice");
                } else {
                    next = parse_dur(field, time);
                }
                next_flag = true;
            } else if (first == 'K') {
                if (new_key_flag) {
                    parse_error(field, 0, "Key specified twice");
                } else {
                    new_key = parse_key(field);
                    new_key_flag = true;
                }
            } else if (first == 'L') {
                if (loud_flag) {
                    parse_error(field, 0, "Loudness specified twice");
                } else {
                    loud = parse_loud(field);
                }
                loud_flag = true;
            } else if (first == 'P') {
                if (new_note_flag || new_pitch_flag) {
                    parse_error(field, 0, "Pitch specified twice");
                } else {
                    new_pitch = parse_pitch(field);
                    new_pitch_flag = true;
                }
            } else if (first == 'U') {
                if (dur_flag) {
                    parse_error(field, 0, "Dur specified twice");
                } else {
                    dur = parse_dur(field, time);
                    dur_flag = true;
                }
            } else if (strchr("SIQHW", first)) {
                if (dur_flag) {
                    parse_error(field, 0, "Dur specified twice");
                } else {
                    // prepend 'U' to field, copy EOS too
                    memmove(field + 1, field, strlen(field) + 1);
                    field[0] = 'U';
                    dur = parse_dur(field, time);
                    dur_flag = true;
                }
            } else if (strchr("ABCDEFG", first)) {
                if (new_note_flag || new_pitch_flag) {
                    parse_error(field, 0, "Pitch specified twice");
                } else {
                    // prepend 'K' to field, copy EOS too
                    memmove(field + 1, field, strlen(field) + 1);
                    field[0] = 'K';
                    new_note = parse_key(field);
                    new_note_flag = true;
                }
            } else if (first == '-') {
                Parameter parm;
                if (parse_attribute(field, &parm)) { // enter attribute-value pair
                    attributes = new Parameters(attributes);
                    attributes->parm = parm;
                    parm.s = NULL; // protect string from deletion by destructor
                }
            } else {
                parse_error(field, 0, "Unknown field");
            }

            if (error_flag) {
                field[0] = 0; // exit the loop
            } else {
                line_parser.get_nonspace_quoted(field);
                pk = line_parser.peek();
                if (pk && !isspace(pk)) {
                    line_parser.get_nonspace_quoted(field + strlen(field));
                }
            }
        }
        // a case analysis:
        // Key < 128 counts as both key and pitch
        // A-G implies pitch AND key unless key given too
        //   K60 P60 -- both are specified, use 'em
        //   K60 P60 C4 -- overconstrained, an error
        //   K60 C4 -- overconstrained
        //   K60 -- OK, pitch is 60
        //   C4 P60 -- over constrained
        //   P60 -- OK, key is from before, pitch is 60
        //   C4 -- OK, key is 60, pitch is 60
        //   <nothing> -- OK, key and pitch from before
        //   K200 with P60 ok, pitch is 60
        //   K200 with neither P60 nor C4 uses 
        //       pitch from before
        // figure out what the key/instance is:
        if (new_key_flag) { // it was directly specified
            key = new_key;
            if (key < 128 && new_note_flag) {
                parse_error("", 0, "Pitch specified twice");
            }
        } else if (new_note_flag) { // "A"-"G" used
            key = new_note;
        }
        if (new_pitch_flag) {
            pitch = new_pitch;
        } else if (key < 128) {
            pitch = key;
        }
        // now we've acquired new parameters
        // if (it is a note, then enter the note
        if (valid) {
            // change tempo or beat
            process_attributes(attributes, time);
            // if there's a duration or pitch, make a note:
            if (new_pitch_flag || dur_flag || new_note_flag) {
                new_key_flag = false;
                new_pitch_flag = false;
                Allegro_note_ptr note_ptr = new Allegro_note;
                note_ptr->chan = voice;
                note_ptr->time = time;
                note_ptr->dur = dur;
                note_ptr->key = key;
                note_ptr->pitch = pitch;
                note_ptr->loud = loud;
                note_ptr->parameters = attributes;
                seq.add_event(note_ptr); // sort later
            } else {
                int update_key = -1;
                // key or pitch must appear explicitly; otherwise
                //    update applies to channel
                if (new_key_flag || new_pitch_flag) {
                    update_key = key;
                }
                if (loud_flag) {
                    Allegro_update_ptr new_upd = new Allegro_update;
                    new_upd->chan = voice;
                    new_upd->time = time;
                    new_upd->key = update_key;
                    new_upd->parameter.set_attr(symbol_table.insert_string("loudr"));
                    new_upd->parameter.r = pitch;
                    seq.add_event(new_upd);
                }
                if (attributes) {
                    while (attributes) {
                        Allegro_update_ptr new_upd = new Allegro_update;
                        new_upd->chan = voice;
                        new_upd->time = time;
                        new_upd->key = update_key;
                        new_upd->parameter = attributes->parm;
                        seq.add_event(new_upd);
                        Parameters_ptr p = attributes;
                        attributes = attributes->next;
                        delete p;
                    }
                }
            }
            if (next_flag) {
                time = time + next;
            } else if (dur_flag) {
                time = time + dur;
            }
        }
        readline();
    }
    //print "Finished reading score"
    if (!error_flag) {
        seq.convert_to_seconds(); // make sure format is correct
        // seq.notes.sort('event_greater_than');
    }
    // print "parse returns error_flag", error_flag
    return error_flag;
}
#else /* EXPERIMENTAL_NOTE_TRACK */
bool Alg_reader::parse()
{
    int voice = 0;
    int key = 60;
    double loud = 100.0;
    double pitch = 60.0;
    double dur = 1.0;
    double time = 0.0;
    int track_num = 0;
    seq->convert_to_seconds();
    //seq->set_real_dur(0.0); // just in case it's not initialized already
    readline();
    bool valid = false; // ignore blank lines
    while (line_parser_flag) {
        bool time_flag = false;
        bool next_flag = false;
        double next;
        bool voice_flag = false;
        bool loud_flag = false;
        bool dur_flag = false;
        bool new_pitch_flag = false; // "P" syntax
        double new_pitch = 0.0;
        bool new_key_flag = false;   // "K" syntax
        int new_key = 0;
        bool new_note_flag = false;  // "A"-"G" syntax
        int new_note = 0;
        Alg_parameters_ptr attributes = NULL;
        if (line_parser.peek() == '#') {
            // look for #track
            line_parser.get_nonspace_quoted(field);
            if (streql(field, "#track")) {
                line_parser.get_nonspace_quoted(field); // number
                track_num = parse_int(field - 1);
                seq->add_track(track_num);
            }
            // maybe we have a comment
        } else {
            // we must have a track to insert into
            if (seq->tracks() == 0) seq->add_track(0);
            line_parser.get_nonspace_quoted(field);
            char pk = line_parser.peek();
            // attributes are parsed as two adjacent nonspace_quoted tokens
            // so we have to conditionally call get_nonspace_quoted() again
            if (pk && !isspace(pk)) {
                line_parser.get_nonspace_quoted(field + strlen(field));
            }
            while (field[0]) {
                char first = toupper(field[0]);
                if (strchr("ABCDEFGKLPUSIQHW-", first)) {
                    valid = true; // it's a note or event
                }
                if (first == 'V') {
                    if (voice_flag) {
                        parse_error(field, 0, "Voice specified twice");
                    } else {
                        voice = parse_chan(field);
                    }
                    voice_flag = true;
                } else if (first == 'T') {
                    if (time_flag) {
                        parse_error(field, 0, "Time specified twice");
                    } else {
                        time = parse_dur(field, 0.0);
                    }
                    time_flag = true;
                } else if (first == 'N') {
                    if (next_flag) {
                        parse_error(field, 0, "Next specified twice");
                    } else {
                        next = parse_dur(field, time);
                    }
                    next_flag = true;
                } else if (first == 'K') {
                    if (new_key_flag) {
                        parse_error(field, 0, "Key specified twice");
                    } else {
                        new_key = parse_key(field);
                        new_key_flag = true;
                    }
                } else if (first == 'L') {
                    if (loud_flag) {
                        parse_error(field, 0, "Loudness specified twice");
                    } else {
                        loud = parse_loud(field);
                    }
                    loud_flag = true;
                } else if (first == 'P') {
                    if (new_note_flag || new_pitch_flag) {
                        parse_error(field, 0, "Pitch specified twice");
                    } else {
                        new_pitch = parse_pitch(field);
                        new_pitch_flag = true;
                    }
                } else if (first == 'U') {
                    if (dur_flag) {
                        parse_error(field, 0, "Dur specified twice");
                    } else {
                        dur = parse_dur(field, time);
                        dur_flag = true;
                    }
                } else if (strchr("SIQHW", first)) {
                    if (dur_flag) {
                        parse_error(field, 0, "Dur specified twice");
                    } else {
                        // prepend 'U' to field, copy EOS too
                        memmove(field + 1, field, strlen(field) + 1);
                        field[0] = 'U';
                        dur = parse_dur(field, time);
                        dur_flag = true;
                    }
                } else if (strchr("ABCDEFG", first)) {
                    if (new_note_flag || new_pitch_flag) {
                        parse_error(field, 0, "Pitch specified twice");
                    } else {
                        // prepend 'K' to field, copy EOS too
                        memmove(field + 1, field, strlen(field) + 1);
                        field[0] = 'K';
                        new_note = parse_key(field);
                        new_note_flag = true;
                    }
                } else if (first == '-') {
                    Alg_parameter parm;
                    if (parse_attribute(field, &parm)) { // enter attribute-value pair
                        attributes = new Alg_parameters(attributes);
                        attributes->parm = parm;
                        parm.s = NULL; // protect string from deletion by destructor
                    }
                } else {
                    parse_error(field, 0, "Unknown field");
                }

                if (error_flag) {
                    field[0] = 0; // exit the loop
                } else {
                    line_parser.get_nonspace_quoted(field);
                    pk = line_parser.peek();
                    // attributes are parsed as two adjacent nonspace_quoted 
                    // tokens so we have to conditionally call 
                    // get_nonspace_quoted() again
                    if (pk && !isspace(pk)) {
                        line_parser.get_nonspace_quoted(field + strlen(field));
                    }
                }
            }
            // a case analysis:
            // Key < 128 counts as both key and pitch
            // A-G implies pitch AND key unless key given too
            //   K60 P60 -- both are specified, use 'em
            //   K60 P60 C4 -- overconstrained, an error
            //   K60 C4 -- overconstrained
            //   K60 -- OK, pitch is 60
            //   C4 P60 -- over constrained
            //   P60 -- OK, key is from before, pitch is 60
            //   C4 -- OK, key is 60, pitch is 60
            //   <nothing> -- OK, key and pitch from before
            //   K200 with P60 ok, pitch is 60
            //   K200 with neither P60 nor C4 uses 
            //       pitch from before
            // figure out what the key/instance is:
            if (new_key_flag) { // it was directly specified
                key = new_key;
                if (key < 128 && new_note_flag) {
                    parse_error("", 0, "Pitch specified twice");
                }
            } else if (new_note_flag) { // "A"-"G" used
                key = new_note;
            }
            if (new_pitch_flag) {
                pitch = new_pitch;
            } else if (key < 128) {
                pitch = key;
            }
            // now we've acquired new parameters
            // if (it is a note, then enter the note
            if (valid) {
                // change tempo or beat
                attributes = process_attributes(attributes, time);
                // if there's a duration or pitch, make a note:
                if (new_pitch_flag || dur_flag || new_note_flag) {
                    new_key_flag = false;
                    new_pitch_flag = false;
                    Alg_note_ptr note_ptr = new Alg_note;
                    note_ptr->chan = voice;
                    note_ptr->time = time;
                    note_ptr->dur = dur;
                    note_ptr->set_identifier(key);
                    note_ptr->pitch = pitch;
                    note_ptr->loud = loud;
                    note_ptr->parameters = attributes;
                    seq->add_event(note_ptr, track_num); // sort later
                    if (seq->get_real_dur() < (time + dur)) seq->set_real_dur(time + dur);
                } else {
                    int update_key = -1;
                    // key or pitch must appear explicitly; otherwise
                    //    update applies to channel
                    if (new_key_flag || new_pitch_flag) {
                        update_key = key;
                    }
                    if (loud_flag) {
                        Alg_update_ptr new_upd = new Alg_update;
                        new_upd->chan = voice;
                        new_upd->time = time;
                        new_upd->set_identifier(update_key);
                        new_upd->parameter.set_attr(symbol_table.insert_string("loudr"));
                        new_upd->parameter.r = pitch;
                        seq->add_event(new_upd, track_num);
                        if (seq->get_real_dur() < time) seq->set_real_dur(time);
                    }
                    if (attributes) {
                        while (attributes) {
                            Alg_update_ptr new_upd = new Alg_update;
                            new_upd->chan = voice;
                            new_upd->time = time;
                            new_upd->set_identifier(update_key);
                            new_upd->parameter = attributes->parm;
                            seq->add_event(new_upd, track_num);
                            Alg_parameters_ptr p = attributes;
                            attributes = attributes->next;
                            p->parm.s = NULL; // so we don't delete the string
                            delete p;
                        }
                    }
                }
                if (next_flag) {
                    time = time + next;
                } else if (dur_flag) {
                    time = time + dur;
                }
            }
        }
        readline();
    }
    //print "Finished reading score"
    if (!error_flag) {
        seq->convert_to_seconds(); // make sure format is correct
        // seq->notes.sort('event_greater_than');
    }
    // real_dur is valid, translate to beat_dur
    seq->set_beat_dur((seq->get_time_map())->time_to_beat(seq->get_real_dur()));
    // print "parse returns error_flag", error_flag
    return error_flag;
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


//Note that this is an #ifdef, not an #ifndef
#ifdef EXPERIMENTAL_NOTE_TRACK
long Alg_reader::parse_chan(char *field)
{
    char *int_string = field + 1;
    char *msg = "Integer or - expected";
    char *p = int_string;
    char c;
    // check that all chars in int_string are digits or '-':
    while (c = *p++) {
        if (!isdigit(c) && c != '-') {
            parse_error(field, p - field - 1, msg);
            return 0;
        }
    }
    p--; // p now points to end-of-string character
    if (p - int_string == 0) {
        // bad: string length is zero
        parse_error(field, 1, msg);
        return 0;
    }
    if (p - int_string == 1 && int_string[0] == '-') {
        // special case: entire string is "-", interpret as -1
        return -1;
    }
    return atoi(int_string);
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


#ifndef EXPERIMENTAL_NOTE_TRACK
long Allegro_reader::parse_int(char *field)
{
    char int_string[field_max];
    strcpy(int_string, field + 1);
    char *msg = "Integer expected";
    char *p = int_string;
    char c;
    while (c = *p++) {
        if (!isdigit(c)) {
            parse_error(field, 1, msg);
            return 0;
        }
    }
    if (strlen(int_string) < 1) {
        parse_error(field, 1, msg);
        return 0;
    }
    return atoi(int_string);
}
#else /* EXPERIMENTAL_NOTE_TRACK */
long Alg_reader::parse_int(char *field)
{
    char *int_string = field + 1;
    char *msg = "Integer expected";
    char *p = int_string;
    char c;
    // check that all chars in int_string are digits:
    while (c = *p++) {
        if (!isdigit(c)) {
            parse_error(field, p - field - 1, msg);
            return 0;
        }
    }
    p--; // p now points to end-of-string character
    if (p - int_string == 0) {
        // bad: string length is zero
        parse_error(field, 1, msg);
        return 0;
    }
    return atoi(int_string);
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


#ifndef EXPERIMENTAL_NOTE_TRACK
int Allegro_reader::find_real_in(char *field, int n)
#else /* EXPERIMENTAL_NOTE_TRACK */
int Alg_reader::find_real_in(char *field, int n)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    // scans from offset n to the end of a real constant
    bool decimal = false;
    int len = strlen(field);
    for (int i = n; i < len; i++) {
        char c = field[i];
        if (!isdigit(c)) {
            if (c == '.' && !decimal) {
                decimal = true;
            } else {
                return i;
            }
        }
    }
    return strlen(field);
}


#ifndef EXPERIMENTAL_NOTE_TRACK
double Allegro_reader::parse_real(char *field)
#else /* EXPERIMENTAL_NOTE_TRACK */
double Alg_reader::parse_real(char *field)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    char real_string[80];
    char *msg = "Real expected";
    bool decimal = false;
    int last = find_real_in(field, 1);
    subseq(real_string, field, 1, last);
    if (last <= 1 || last < (int) strlen(field)) {
       parse_error(field, 1, msg);
       return 0;
    }
    return atof(real_string);
}
コード例 #11
0
ファイル: allegrord.cpp プロジェクト: ruthmagnus/audacity
bool Allegro_reader::parse_attribute(char *field, Parameter_ptr param)
{
    int i = 1;
    while (i < (int) strlen(field)) {
        if (field[i] == ':') {
            char attr[80];
            subseq(attr, field, 1, i);
            char type_char = field[i - 1];
            if (strchr("iarsl", type_char)) {
                param->set_attr(symbol_table.insert_string(attr));
                parse_val(param, field, i + 1);
            }
            return !error_flag;
        }
        i = i + 1;
    }
    return false;
}
コード例 #12
0
int sbcl_bin_expand(struct install_options* param) {
  char* impl=param->impl;
  char* version=q(param->version);
  int ret;
  char* home=configdir();
  char* arch= arch_(param);
  char* archive=cat(impl,"-",version,"-",arch,".msi",NULL);
  char* log_path=cat(home,"impls",SLASH,"log",SLASH,impl,"-",version,"-",arch,SLASH,"install.log",NULL);
  char* dist_path;
  int pos=position_char("-",impl);
  if(pos!=-1) {
    impl=subseq(impl,0,pos);
  }else
    impl=q(impl);
  dist_path=cat(home,"src",SLASH,impl,"-",version,"-",arch,SLASH,NULL);
  printf("Extracting the msi archive. %s to %s\n",archive,dist_path);
  archive=s_cat(q(home),q("archives"),q(SLASH),archive,NULL);
  delete_directory(dist_path,1);
  ensure_directories_exist(dist_path);
  ensure_directories_exist(log_path);
  if(dist_path[strlen(dist_path)-1]=='\\')
    dist_path[strlen(dist_path)-1]='\0';

  char* cmd=cat("msiexec.exe /a \"",
                archive,
                "\" targetdir=\"",
                dist_path,
                "\" /qn /lv ",
                "\"",
                log_path,
                "\"",
                NULL);
  cmd=cat("cmd /c \"",cmd,"\"",NULL);
  cond_printf(1,"msiexeccmd:%s\n",cmd);
  ret=System(cmd);
  s(impl);
  s(dist_path);
  s(log_path);
  s(archive);
  s(cmd),s(home),s(version),s(arch);
  return !ret;
}
コード例 #13
0
ファイル: allegrord.cpp プロジェクト: ruthmagnus/audacity
#include "assert.h"
#include "stdlib.h"
#include "stdio.h"
#include "allegro.h"
#include "string.h"
#include "ctype.h"
//#include "memory.h"
#include "trace.h"
#include "strparse.h"
#ifndef EXPERIMENTAL_NOTE_TRACK
#include "allegrord.h"
#endif /* EXPERIMENTAL_NOTE_TRACK */
#define streql(s1, s2) (strcmp(s1, s2) == 0)
#define field_max 80


//Note that this is an #ifdef, not an #ifndef
#ifdef EXPERIMENTAL_NOTE_TRACK

class Alg_reader {
public:
    FILE *file;
    int line_no;
    String_parse line_parser;
    bool line_parser_flag;
    char field[field_max];
    bool error_flag;
    Alg_seq_ptr seq;
    double tsnum;
    double tsden;

    Alg_reader(FILE *a_file, Alg_seq_ptr new_seq);
    void readline();
    Alg_parameters_ptr process_attributes(Alg_parameters_ptr attributes, double time);
    bool parse();
    long parse_chan(char *field);
    long parse_int(char *field);
    int find_real_in(char *field, int n);
    double parse_real(char *field);
    void parse_error(char *field, long offset, char *message);
    double parse_dur(char *field, double base);
    double parse_after_dur(double dur, char *field, int n, double base);
    double parse_loud(char *field);
    long parse_key(char *field);
    double parse_pitch(char *field);
    long parse_after_key(int key, char *field, int n);
    long find_int_in(char *field, int n);
    bool parse_attribute(char *field, Alg_parameter_ptr parm);
    bool parse_val(Alg_parameter_ptr param, char *s, int i);
    bool check_type(char type_char, Alg_parameter_ptr param);
};

#endif /* EXPERIMENTAL_NOTE_TRACK */

void subseq(char *result, char *source, int from, int to)
{
    memcpy(result, source + from, to - from);
    result[to - from] = 0;
}

#ifndef EXPERIMENTAL_NOTE_TRACK
double Allegro_reader::parse_pitch(char *field)
#else /* EXPERIMENTAL_NOTE_TRACK */
double Alg_reader::parse_pitch(char *field)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    if (isdigit(field[1])) {
        char real_string[80];
        int last = find_real_in(field, 1);
        subseq(real_string, field, 1, last);
        return atof(real_string);
    } else {
        return (double) parse_key(field);
    }
}


#ifndef EXPERIMENTAL_NOTE_TRACK
Allegro_reader::Allegro_reader(FILE *a_file)
{
    file = a_file; // save the file
    line_parser_flag = false;
    line_no = 0;
    seq = Seq();
    tsnum = 4; // default time signature
    tsden = 4;
}
#else /* EXPERIMENTAL_NOTE_TRACK */
// it is the responsibility of the caller to delete
// the seq
Alg_reader::Alg_reader(FILE *a_file, Alg_seq_ptr new_seq)
{
    file = a_file; // save the file
    line_parser_flag = false;
    line_no = 0;
    tsnum = 4; // default time signature
    tsden = 4;
    seq = new_seq;
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


//Note that this is an #ifdef, not an #ifndef
#ifdef EXPERIMENTAL_NOTE_TRACK
Alg_seq_ptr alg_read(FILE *file, Alg_seq_ptr new_seq)
    // read a sequence from allegro file
{
    if (!new_seq) new_seq = new Alg_seq();
    Alg_reader alg_reader(file, new_seq);
    alg_reader.parse();
    return alg_reader.seq;
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


#ifndef EXPERIMENTAL_NOTE_TRACK
void Allegro_reader::readline()
#else /* EXPERIMENTAL_NOTE_TRACK */
void Alg_reader::readline()
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    char line[256];
    char *line_flag = fgets(line, 256, file);
    line_parser_flag = false;
    if (line_flag) {
        line_parser.init(line);
        line_parser_flag = true;
        error_flag = false;
    }
}


#ifndef EXPERIMENTAL_NOTE_TRACK
void Allegro_reader::process_attributes(Parameters_ptr attributes, double time)
{
    // print "process_attributes:", attributes
    bool ts_flag;
    if (attributes) {
        Parameters_ptr a;
        if (a = Parameters::remove_key(&attributes, "tempor")) {
            double tempo = a->parm.r;
            seq.insert_tempo(tempo, seq.map.time_to_beat(time));
        }
        if (a = Parameters::remove_key(&attributes, "beatr")) {
            double beat = a->parm.r;
            seq.insert_beat(time, beat);
        }
        if (a = Parameters::remove_key(&attributes, "tsnumr")) {
            tsnum = a->parm.r;
            ts_flag = true;
        }
        if (a = Parameters::remove_key(&attributes, "tsdenr")) {
            tsden = a->parm.r;
            ts_flag = true;
        }
        if (ts_flag) {
            seq.set_time_sig(seq.map.time_to_beat(time), tsnum, tsden);
        }
    }
}
#else /* EXPERIMENTAL_NOTE_TRACK */
Alg_parameters_ptr Alg_reader::process_attributes(
        Alg_parameters_ptr attributes, double time)
{
    // print "process_attributes:", attributes
    bool ts_flag = false;
    if (attributes) {
        Alg_parameters_ptr a;
        bool in_seconds = seq->get_units_are_seconds();
        if (a = Alg_parameters::remove_key(&attributes, "tempor")) {
            double tempo = a->parm.r;
            seq->insert_tempo(tempo, seq->get_time_map()->time_to_beat(time));
        }
        if (a = Alg_parameters::remove_key(&attributes, "beatr")) {
            double beat = a->parm.r;
            seq->insert_beat(time, beat);
        }
        if (a = Alg_parameters::remove_key(&attributes, "timesig_numr")) {
            tsnum = a->parm.r;
            ts_flag = true;
        }
        if (a = Alg_parameters::remove_key(&attributes, "timesig_denr")) {
            tsden = a->parm.r;
            ts_flag = true;
        }
        if (ts_flag) {
            seq->set_time_sig(seq->get_time_map()->time_to_beat(time),
            tsnum, tsden);
        }
        if (in_seconds) seq->convert_to_seconds();
    }
    return attributes; // in case it was modified
}
#endif /* EXPERIMENTAL_NOTE_TRACK */

#ifndef EXPERIMENTAL_NOTE_TRACK
bool Allegro_reader::parse()
{
    int voice = 0;
    int key = 60;
    double loud = 100.0;
    double pitch = 60.0;
    double dur = 1.0;
    double time = 0.0;
    readline();
    bool valid = false; // ignore blank lines
    while (line_parser_flag) {
        bool time_flag = false;
        bool next_flag = false;
        double next;
        bool voice_flag = false;
        bool loud_flag = false;
        bool dur_flag = false;
        bool new_pitch_flag = false; // "P" syntax
        double new_pitch = 0.0;
        bool new_key_flag = false;   // "K" syntax
        int new_key = 0;
        bool new_note_flag = false;  // "A"-"G" syntax
        int new_note = 0;
        Parameters_ptr attributes = NULL;
        line_parser.get_nonspace_quoted(field);
        char pk = line_parser.peek();
        if (pk && !isspace(pk)) {
            line_parser.get_nonspace_quoted(field + strlen(field));
        }
        while (field[0]) {
            // print "field", "|";field;"|", "|";line_parser.string;"|", line_parser.pos
            char first = toupper(field[0]);
            if (strchr("ABCDEFGKLPUSIQHW-", first)) {
                valid = true; // it's a note or event
            }
            if (first == 'V') {
                if (voice_flag) {
                    parse_error(field, 0, "Voice specified twice");
                } else {
                    voice = parse_int(field);
                }
                voice_flag = true;
            } else if (first == 'T') {
                if (time_flag) {
                    parse_error(field, 0, "Time specified twice");
                } else {
                    time = parse_dur(field, 0.0);
                }
                time_flag = true;
            } else if (first == 'N') {
                if (next_flag) {
                    parse_error(field, 0, "Next specified twice");
                } else {
                    next = parse_dur(field, time);
                }
                next_flag = true;
            } else if (first == 'K') {
                if (new_key_flag) {
                    parse_error(field, 0, "Key specified twice");
                } else {
                    new_key = parse_key(field);
                    new_key_flag = true;
                }
            } else if (first == 'L') {
                if (loud_flag) {
                    parse_error(field, 0, "Loudness specified twice");
                } else {
                    loud = parse_loud(field);
                }
                loud_flag = true;
            } else if (first == 'P') {
                if (new_note_flag || new_pitch_flag) {
                    parse_error(field, 0, "Pitch specified twice");
                } else {
                    new_pitch = parse_pitch(field);
                    new_pitch_flag = true;
                }
            } else if (first == 'U') {
                if (dur_flag) {
                    parse_error(field, 0, "Dur specified twice");
                } else {
                    dur = parse_dur(field, time);
                    dur_flag = true;
                }
            } else if (strchr("SIQHW", first)) {
                if (dur_flag) {
                    parse_error(field, 0, "Dur specified twice");
                } else {
                    // prepend 'U' to field, copy EOS too
                    memmove(field + 1, field, strlen(field) + 1);
                    field[0] = 'U';
                    dur = parse_dur(field, time);
                    dur_flag = true;
                }
            } else if (strchr("ABCDEFG", first)) {
                if (new_note_flag || new_pitch_flag) {
                    parse_error(field, 0, "Pitch specified twice");
                } else {
                    // prepend 'K' to field, copy EOS too
                    memmove(field + 1, field, strlen(field) + 1);
                    field[0] = 'K';
                    new_note = parse_key(field);
                    new_note_flag = true;
                }
            } else if (first == '-') {
                Parameter parm;
                if (parse_attribute(field, &parm)) { // enter attribute-value pair
                    attributes = new Parameters(attributes);
                    attributes->parm = parm;
                    parm.s = NULL; // protect string from deletion by destructor
                }
            } else {
                parse_error(field, 0, "Unknown field");
            }

            if (error_flag) {
                field[0] = 0; // exit the loop
            } else {
                line_parser.get_nonspace_quoted(field);
                pk = line_parser.peek();
                if (pk && !isspace(pk)) {
                    line_parser.get_nonspace_quoted(field + strlen(field));
                }
            }
        }
        // a case analysis:
        // Key < 128 counts as both key and pitch
        // A-G implies pitch AND key unless key given too
        //   K60 P60 -- both are specified, use 'em
        //   K60 P60 C4 -- overconstrained, an error
        //   K60 C4 -- overconstrained
        //   K60 -- OK, pitch is 60
        //   C4 P60 -- over constrained
        //   P60 -- OK, key is from before, pitch is 60
        //   C4 -- OK, key is 60, pitch is 60
        //   <nothing> -- OK, key and pitch from before
        //   K200 with P60 ok, pitch is 60
        //   K200 with neither P60 nor C4 uses 
        //       pitch from before
        // figure out what the key/instance is:
        if (new_key_flag) { // it was directly specified
            key = new_key;
            if (key < 128 && new_note_flag) {
                parse_error("", 0, "Pitch specified twice");
            }
        } else if (new_note_flag) { // "A"-"G" used
            key = new_note;
        }
        if (new_pitch_flag) {
            pitch = new_pitch;
        } else if (key < 128) {
            pitch = key;
        }
        // now we've acquired new parameters
        // if (it is a note, then enter the note
        if (valid) {
            // change tempo or beat
            process_attributes(attributes, time);
            // if there's a duration or pitch, make a note:
            if (new_pitch_flag || dur_flag || new_note_flag) {
                new_key_flag = false;
                new_pitch_flag = false;
                Allegro_note_ptr note_ptr = new Allegro_note;
                note_ptr->chan = voice;
                note_ptr->time = time;
                note_ptr->dur = dur;
                note_ptr->key = key;
                note_ptr->pitch = pitch;
                note_ptr->loud = loud;
                note_ptr->parameters = attributes;
                seq.add_event(note_ptr); // sort later
            } else {
                int update_key = -1;
                // key or pitch must appear explicitly; otherwise
                //    update applies to channel
                if (new_key_flag || new_pitch_flag) {
                    update_key = key;
                }
                if (loud_flag) {
                    Allegro_update_ptr new_upd = new Allegro_update;
                    new_upd->chan = voice;
                    new_upd->time = time;
                    new_upd->key = update_key;
                    new_upd->parameter.set_attr(symbol_table.insert_string("loudr"));
                    new_upd->parameter.r = pitch;
                    seq.add_event(new_upd);
                }
                if (attributes) {
                    while (attributes) {
                        Allegro_update_ptr new_upd = new Allegro_update;
                        new_upd->chan = voice;
                        new_upd->time = time;
                        new_upd->key = update_key;
                        new_upd->parameter = attributes->parm;
                        seq.add_event(new_upd);
                        Parameters_ptr p = attributes;
                        attributes = attributes->next;
                        delete p;
                    }
                }
            }
            if (next_flag) {
                time = time + next;
            } else if (dur_flag) {
                time = time + dur;
            }
        }
        readline();
    }
    //print "Finished reading score"
    if (!error_flag) {
        seq.convert_to_seconds(); // make sure format is correct
        // seq.notes.sort('event_greater_than');
    }
    // print "parse returns error_flag", error_flag
    return error_flag;
}
#else /* EXPERIMENTAL_NOTE_TRACK */
bool Alg_reader::parse()
{
    int voice = 0;
    int key = 60;
    double loud = 100.0;
    double pitch = 60.0;
    double dur = 1.0;
    double time = 0.0;
    int track_num = 0;
    seq->convert_to_seconds();
    //seq->set_real_dur(0.0); // just in case it's not initialized already
    readline();
    bool valid = false; // ignore blank lines
    while (line_parser_flag) {
        bool time_flag = false;
        bool next_flag = false;
        double next;
        bool voice_flag = false;
        bool loud_flag = false;
        bool dur_flag = false;
        bool new_pitch_flag = false; // "P" syntax
        double new_pitch = 0.0;
        bool new_key_flag = false;   // "K" syntax
        int new_key = 0;
        bool new_note_flag = false;  // "A"-"G" syntax
        int new_note = 0;
        Alg_parameters_ptr attributes = NULL;
        if (line_parser.peek() == '#') {
            // look for #track
            line_parser.get_nonspace_quoted(field);
            if (streql(field, "#track")) {
                line_parser.get_nonspace_quoted(field); // number
                track_num = parse_int(field - 1);
                seq->add_track(track_num);
            }
            // maybe we have a comment
        } else {
            // we must have a track to insert into
            if (seq->tracks() == 0) seq->add_track(0);
            line_parser.get_nonspace_quoted(field);
            char pk = line_parser.peek();
            // attributes are parsed as two adjacent nonspace_quoted tokens
            // so we have to conditionally call get_nonspace_quoted() again
            if (pk && !isspace(pk)) {
                line_parser.get_nonspace_quoted(field + strlen(field));
            }
            while (field[0]) {
                char first = toupper(field[0]);
                if (strchr("ABCDEFGKLPUSIQHW-", first)) {
                    valid = true; // it's a note or event
                }
                if (first == 'V') {
                    if (voice_flag) {
                        parse_error(field, 0, "Voice specified twice");
                    } else {
                        voice = parse_chan(field);
                    }
                    voice_flag = true;
                } else if (first == 'T') {
                    if (time_flag) {
                        parse_error(field, 0, "Time specified twice");
                    } else {
                        time = parse_dur(field, 0.0);
                    }
                    time_flag = true;
                } else if (first == 'N') {
                    if (next_flag) {
                        parse_error(field, 0, "Next specified twice");
                    } else {
                        next = parse_dur(field, time);
                    }
                    next_flag = true;
                } else if (first == 'K') {
                    if (new_key_flag) {
                        parse_error(field, 0, "Key specified twice");
                    } else {
                        new_key = parse_key(field);
                        new_key_flag = true;
                    }
                } else if (first == 'L') {
                    if (loud_flag) {
                        parse_error(field, 0, "Loudness specified twice");
                    } else {
                        loud = parse_loud(field);
                    }
                    loud_flag = true;
                } else if (first == 'P') {
                    if (new_note_flag || new_pitch_flag) {
                        parse_error(field, 0, "Pitch specified twice");
                    } else {
                        new_pitch = parse_pitch(field);
                        new_pitch_flag = true;
                    }
                } else if (first == 'U') {
                    if (dur_flag) {
                        parse_error(field, 0, "Dur specified twice");
                    } else {
                        dur = parse_dur(field, time);
                        dur_flag = true;
                    }
                } else if (strchr("SIQHW", first)) {
                    if (dur_flag) {
                        parse_error(field, 0, "Dur specified twice");
                    } else {
                        // prepend 'U' to field, copy EOS too
                        memmove(field + 1, field, strlen(field) + 1);
                        field[0] = 'U';
                        dur = parse_dur(field, time);
                        dur_flag = true;
                    }
                } else if (strchr("ABCDEFG", first)) {
                    if (new_note_flag || new_pitch_flag) {
                        parse_error(field, 0, "Pitch specified twice");
                    } else {
                        // prepend 'K' to field, copy EOS too
                        memmove(field + 1, field, strlen(field) + 1);
                        field[0] = 'K';
                        new_note = parse_key(field);
                        new_note_flag = true;
                    }
                } else if (first == '-') {
                    Alg_parameter parm;
                    if (parse_attribute(field, &parm)) { // enter attribute-value pair
                        attributes = new Alg_parameters(attributes);
                        attributes->parm = parm;
                        parm.s = NULL; // protect string from deletion by destructor
                    }
                } else {
                    parse_error(field, 0, "Unknown field");
                }

                if (error_flag) {
                    field[0] = 0; // exit the loop
                } else {
                    line_parser.get_nonspace_quoted(field);
                    pk = line_parser.peek();
                    // attributes are parsed as two adjacent nonspace_quoted 
                    // tokens so we have to conditionally call 
                    // get_nonspace_quoted() again
                    if (pk && !isspace(pk)) {
                        line_parser.get_nonspace_quoted(field + strlen(field));
                    }
                }
            }
            // a case analysis:
            // Key < 128 counts as both key and pitch
            // A-G implies pitch AND key unless key given too
            //   K60 P60 -- both are specified, use 'em
            //   K60 P60 C4 -- overconstrained, an error
            //   K60 C4 -- overconstrained
            //   K60 -- OK, pitch is 60
            //   C4 P60 -- over constrained
            //   P60 -- OK, key is from before, pitch is 60
            //   C4 -- OK, key is 60, pitch is 60
            //   <nothing> -- OK, key and pitch from before
            //   K200 with P60 ok, pitch is 60
            //   K200 with neither P60 nor C4 uses 
            //       pitch from before
            // figure out what the key/instance is:
            if (new_key_flag) { // it was directly specified
                key = new_key;
                if (key < 128 && new_note_flag) {
                    parse_error("", 0, "Pitch specified twice");
                }
            } else if (new_note_flag) { // "A"-"G" used
                key = new_note;
            }
            if (new_pitch_flag) {
                pitch = new_pitch;
            } else if (key < 128) {
                pitch = key;
            }
            // now we've acquired new parameters
            // if (it is a note, then enter the note
            if (valid) {
                // change tempo or beat
                attributes = process_attributes(attributes, time);
                // if there's a duration or pitch, make a note:
                if (new_pitch_flag || dur_flag || new_note_flag) {
                    new_key_flag = false;
                    new_pitch_flag = false;
                    Alg_note_ptr note_ptr = new Alg_note;
                    note_ptr->chan = voice;
                    note_ptr->time = time;
                    note_ptr->dur = dur;
                    note_ptr->set_identifier(key);
                    note_ptr->pitch = pitch;
                    note_ptr->loud = loud;
                    note_ptr->parameters = attributes;
                    seq->add_event(note_ptr, track_num); // sort later
                    if (seq->get_real_dur() < (time + dur)) seq->set_real_dur(time + dur);
                } else {
                    int update_key = -1;
                    // key or pitch must appear explicitly; otherwise
                    //    update applies to channel
                    if (new_key_flag || new_pitch_flag) {
                        update_key = key;
                    }
                    if (loud_flag) {
                        Alg_update_ptr new_upd = new Alg_update;
                        new_upd->chan = voice;
                        new_upd->time = time;
                        new_upd->set_identifier(update_key);
                        new_upd->parameter.set_attr(symbol_table.insert_string("loudr"));
                        new_upd->parameter.r = pitch;
                        seq->add_event(new_upd, track_num);
                        if (seq->get_real_dur() < time) seq->set_real_dur(time);
                    }
                    if (attributes) {
                        while (attributes) {
                            Alg_update_ptr new_upd = new Alg_update;
                            new_upd->chan = voice;
                            new_upd->time = time;
                            new_upd->set_identifier(update_key);
                            new_upd->parameter = attributes->parm;
                            seq->add_event(new_upd, track_num);
                            Alg_parameters_ptr p = attributes;
                            attributes = attributes->next;
                            p->parm.s = NULL; // so we don't delete the string
                            delete p;
                        }
                    }
                }
                if (next_flag) {
                    time = time + next;
                } else if (dur_flag) {
                    time = time + dur;
                }
            }
        }
        readline();
    }
    //print "Finished reading score"
    if (!error_flag) {
        seq->convert_to_seconds(); // make sure format is correct
        // seq->notes.sort('event_greater_than');
    }
    // real_dur is valid, translate to beat_dur
    seq->set_beat_dur((seq->get_time_map())->time_to_beat(seq->get_real_dur()));
    // print "parse returns error_flag", error_flag
    return error_flag;
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


//Note that this is an #ifdef, not an #ifndef
#ifdef EXPERIMENTAL_NOTE_TRACK
long Alg_reader::parse_chan(char *field)
{
    char *int_string = field + 1;
    char *msg = "Integer or - expected";
    char *p = int_string;
    char c;
    // check that all chars in int_string are digits or '-':
    while (c = *p++) {
        if (!isdigit(c) && c != '-') {
            parse_error(field, p - field - 1, msg);
            return 0;
        }
    }
    p--; // p now points to end-of-string character
    if (p - int_string == 0) {
        // bad: string length is zero
        parse_error(field, 1, msg);
        return 0;
    }
    if (p - int_string == 1 && int_string[0] == '-') {
        // special case: entire string is "-", interpret as -1
        return -1;
    }
    return atoi(int_string);
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


#ifndef EXPERIMENTAL_NOTE_TRACK
long Allegro_reader::parse_int(char *field)
{
    char int_string[field_max];
    strcpy(int_string, field + 1);
    char *msg = "Integer expected";
    char *p = int_string;
    char c;
    while (c = *p++) {
        if (!isdigit(c)) {
            parse_error(field, 1, msg);
            return 0;
        }
    }
    if (strlen(int_string) < 1) {
        parse_error(field, 1, msg);
        return 0;
    }
    return atoi(int_string);
}
#else /* EXPERIMENTAL_NOTE_TRACK */
long Alg_reader::parse_int(char *field)
{
    char *int_string = field + 1;
    char *msg = "Integer expected";
    char *p = int_string;
    char c;
    // check that all chars in int_string are digits:
    while (c = *p++) {
        if (!isdigit(c)) {
            parse_error(field, p - field - 1, msg);
            return 0;
        }
    }
    p--; // p now points to end-of-string character
    if (p - int_string == 0) {
        // bad: string length is zero
        parse_error(field, 1, msg);
        return 0;
    }
    return atoi(int_string);
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


#ifndef EXPERIMENTAL_NOTE_TRACK
int Allegro_reader::find_real_in(char *field, int n)
#else /* EXPERIMENTAL_NOTE_TRACK */
int Alg_reader::find_real_in(char *field, int n)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    // scans from offset n to the end of a real constant
    bool decimal = false;
    int len = strlen(field);
    for (int i = n; i < len; i++) {
        char c = field[i];
        if (!isdigit(c)) {
            if (c == '.' && !decimal) {
                decimal = true;
            } else {
                return i;
            }
        }
    }
    return strlen(field);
}


#ifndef EXPERIMENTAL_NOTE_TRACK
double Allegro_reader::parse_real(char *field)
#else /* EXPERIMENTAL_NOTE_TRACK */
double Alg_reader::parse_real(char *field)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    char real_string[80];
    char *msg = "Real expected";
    bool decimal = false;
    int last = find_real_in(field, 1);
    subseq(real_string, field, 1, last);
    if (last <= 1 || last < (int) strlen(field)) {
       parse_error(field, 1, msg);
       return 0;
    }
    return atof(real_string);
}


#ifndef EXPERIMENTAL_NOTE_TRACK
void Allegro_reader::parse_error(char *field, long offset, char *message)
#else /* EXPERIMENTAL_NOTE_TRACK */
void Alg_reader::parse_error(char *field, long offset, char *message)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    int position = line_parser.pos - strlen(field) + offset;
    error_flag = true;
    puts(line_parser.string);
    for (int i = 0; i < position; i++) {
        putc(' ', stdout);
    }
    putc('^', stdout);
    printf("    %s\n", message);
}


double duration_lookup[] = { 0.25, 0.5, 1.0, 2.0, 4.0 };


#ifndef EXPERIMENTAL_NOTE_TRACK
double Allegro_reader::parse_dur(char *field, double base)
{
    char *msg = "Duration expected";
    char real_string[80];
    char *durs = "SIQHW";
    char *p;
    int last;
    double dur;
    if (strlen(field) < 2) {
        // fall through to error message
        return -1;
    } else if (isdigit(field[1])) {
        last = find_real_in(field, 1);
        subseq(real_string, field, 1, last);
        dur = atof(real_string);
        // convert dur from seconds to beats
        dur = seq.map.time_to_beat(base + dur) - seq.map.time_to_beat(base);
    } else if (p = strchr(durs, field[1])) {
        dur = duration_lookup[p - durs];
        last = 2;
    } else {
        parse_error(field, 1, msg);
        return 0;
    }
    dur = parse_after_dur(dur, field, last, base);
    dur = seq.map.beat_to_time(seq.map.time_to_beat(base) + dur) - base;
    return dur;
}
#else /* EXPERIMENTAL_NOTE_TRACK */
double Alg_reader::parse_dur(char *field, double base)
{
    char *msg = "Duration expected";
    char real_string[80];
    char *durs = "SIQHW";
    char *p;
    int last;
    double dur;
    if (strlen(field) < 2) {
        // fall through to error message
        return -1;
    } else if (isdigit(field[1])) {
        last = find_real_in(field, 1);
        subseq(real_string, field, 1, last);
        dur = atof(real_string);
        // convert dur from seconds to beats
        dur = seq->get_time_map()->time_to_beat(base + dur) - 
              seq->get_time_map()->time_to_beat(base);
    } else if (p = strchr(durs, field[1])) {
        dur = duration_lookup[p - durs];
        last = 2;
    } else {
        parse_error(field, 1, msg);
        return 0;
    }
    dur = parse_after_dur(dur, field, last, base);
    dur = seq->get_time_map()->beat_to_time(
              seq->get_time_map()->time_to_beat(base) + dur) - base;
    return dur;
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


#ifndef EXPERIMENTAL_NOTE_TRACK
double Allegro_reader::parse_after_dur(double dur, char *field, int n, double base)
{
    char a_string[80];
    if ((int) strlen(field) == n) {
        return dur;
    }
    if (field[n] == 'T') {
        return parse_after_dur(dur * 2/3, field, n + 1, base);
    }
    if (field[n] == '.') {
        return parse_after_dur(dur * 1.5, field, n + 1, base);
    }
    if (isdigit(field[n])) {
        int last = find_real_in(field, n);
        subseq(a_string, field, n, last);
        double f = atof(a_string);
        return parse_after_dur(dur * f, field, last, base);
    }
    if (field[n] == '+') {
        subseq(a_string, field, n + 1, -1);
        return dur + parse_dur(a_string, 
                               seq.map.beat_to_time(
                                   seq.map.time_to_beat(base) + dur));
    }
    parse_error(field, n, "Unexpected character in duration");
    return dur;
}
#else /* EXPERIMENTAL_NOTE_TRACK */
double Alg_reader::parse_after_dur(double dur, char *field, int n, double base)
{
    char a_string[80];
    if ((int) strlen(field) == n) {
        return dur;
    }
    if (field[n] == 'T') {
        return parse_after_dur(dur * 2/3, field, n + 1, base);
    }
    if (field[n] == '.') {
        return parse_after_dur(dur * 1.5, field, n + 1, base);
    }
    if (isdigit(field[n])) {
        int last = find_real_in(field, n);
        subseq(a_string, field, n, last);
        double f = atof(a_string);
        return parse_after_dur(dur * f, field, last, base);
    }
    if (field[n] == '+') {
        subseq(a_string, field, n + 1, -1);
        return dur + parse_dur(
                a_string, seq->get_time_map()->beat_to_time(
                              seq->get_time_map()->time_to_beat(base) + dur));
    }
    parse_error(field, n, "Unexpected character in duration");
    return dur;
}
#endif /* EXPERIMENTAL_NOTE_TRACK */

#ifndef EXPERIMENTAL_NOTE_TRACK
static struct {
#else /* EXPERIMENTAL_NOTE_TRACK */
struct loud_lookup_struct {
#endif /* EXPERIMENTAL_NOTE_TRACK */
    char *str;
    int val;
} loud_lookup[] = { {"FFF", 127}, {"FF", 120}, {"F", 110}, {"MF", 100}, 
                    {"MP", 90}, {"P", 80}, {"PP", 70}, {"PPP", 60}, 
                    {NULL, 0} };


#ifndef EXPERIMENTAL_NOTE_TRACK
double Allegro_reader::parse_loud(char *field)
{
    char *msg = "Loudness expected";
    if (isdigit(field[1])) {
        return parse_int(field);
    } else {
        double loud = 0.0;
        char dyn[field_max];
        strcpy(dyn, field + 1);
        char *p = dyn;
        while (*p) {
            if (isupper(*p)) *p = toupper(*p);
        }
        for (int i = 0; loud_lookup[i].str; i++) {
            if (streql(loud_lookup[i].str, dyn)) {
                return (double) loud_lookup[i].val;
            }
        }
    }
    parse_error(field, 1, msg);
    return 100.0;
}
#else /* EXPERIMENTAL_NOTE_TRACK */
double Alg_reader::parse_loud(char *field)
{
    char *msg = "Loudness expected";
    if (isdigit(field[1])) {
        return parse_int(field);
    } else {
        double loud = 0.0;
        char dyn[field_max];
        strcpy(dyn, field + 1);
        char *p = dyn;
        while (*p) {
            if (islower(*p)) *p = toupper(*p);
        p++;
        }
        for (int i = 0; loud_lookup[i].str; i++) {
            if (streql(loud_lookup[i].str, dyn)) {
                return (double) loud_lookup[i].val;
            }
        }
    }
    parse_error(field, 1, msg);
    return 100.0;
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


int key_lookup[] = {21, 23, 12, 14, 16, 17, 19};

#ifndef EXPERIMENTAL_NOTE_TRACK
long Allegro_reader::parse_key(char *field)
#else /* EXPERIMENTAL_NOTE_TRACK */
long Alg_reader::parse_key(char *field)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    char *msg = "Pitch expected";
    char *pitches = "ABCDEFG";
    char *p;
    if (isdigit(field[1])) {
        return parse_int(field);
    } else if (p = strchr(pitches, field[1])) {
        long key = key_lookup[p - pitches];
        key = parse_after_key(key, field, 2);
        return key;
    }
    parse_error(field, 1, msg);
    return 0;
}


#ifndef EXPERIMENTAL_NOTE_TRACK
long Allegro_reader::parse_after_key(int key, char *field, int n)
#else /* EXPERIMENTAL_NOTE_TRACK */
long Alg_reader::parse_after_key(int key, char *field, int n)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    char octave[20];
    if ((int) strlen(field) == n) {
        return key;
    }
    char c = toupper(field[n]);
    if (c == 'S') {
        return parse_after_key(key + 1, field, n + 1);
    }
    if (c == 'F') {
        return parse_after_key(key - 1, field, n + 1);
    }
    if (isdigit(c)) {
        int last = find_int_in(field, n);
        subseq(octave, field, n, last);
        int oct = atoi(octave);
        return parse_after_key(key + oct * 12, field, last);
    }
    parse_error(field, n, "Unexpected character in pitch");
    return key;
}


#ifndef EXPERIMENTAL_NOTE_TRACK
long Allegro_reader::find_int_in(char *field, int n)
#else /* EXPERIMENTAL_NOTE_TRACK */
long Alg_reader::find_int_in(char *field, int n)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    while ((int) strlen(field) > n && isdigit(field[n])) {
        n = n + 1;
    }
    return n;
}


#ifndef EXPERIMENTAL_NOTE_TRACK
bool Allegro_reader::parse_attribute(char *field, Parameter_ptr param)
{
    int i = 1;
    while (i < (int) strlen(field)) {
        if (field[i] == ':') {
            char attr[80];
            subseq(attr, field, 1, i);
            char type_char = field[i - 1];
            if (strchr("iarsl", type_char)) {
                param->set_attr(symbol_table.insert_string(attr));
                parse_val(param, field, i + 1);
            }
            return !error_flag;
        }
        i = i + 1;
    }
    return false;
}
#else /* EXPERIMENTAL_NOTE_TRACK */
bool Alg_reader::parse_attribute(char *field, Alg_parameter_ptr param)
{
    int i = 1;
    while (i < (int) strlen(field)) {
        if (field[i] == ':') {
            char attr[80];
            subseq(attr, field, 1, i);
            char type_char = field[i - 1];
            if (strchr("iarsl", type_char)) {
                param->set_attr(symbol_table.insert_string(attr));
                parse_val(param, field, i + 1);
            } else {
                parse_error(field, 0, "attribute needs to end with typecode: i,a,r,s, or l");
            }
            return !error_flag;
        }
        i = i + 1;
    }
    return false;
}
コード例 #14
0
ファイル: allegrord.cpp プロジェクト: ruthmagnus/audacity
#include "assert.h"
#include "stdlib.h"
#include "stdio.h"
#include "allegro.h"
#include "string.h"
#include "ctype.h"
//#include "memory.h"
#include "trace.h"
#include "strparse.h"
#ifndef EXPERIMENTAL_NOTE_TRACK
#include "allegrord.h"
#endif /* EXPERIMENTAL_NOTE_TRACK */
#define streql(s1, s2) (strcmp(s1, s2) == 0)
#define field_max 80


//Note that this is an #ifdef, not an #ifndef
#ifdef EXPERIMENTAL_NOTE_TRACK

class Alg_reader {
public:
    FILE *file;
    int line_no;
    String_parse line_parser;
    bool line_parser_flag;
    char field[field_max];
    bool error_flag;
    Alg_seq_ptr seq;
    double tsnum;
    double tsden;

    Alg_reader(FILE *a_file, Alg_seq_ptr new_seq);
    void readline();
    Alg_parameters_ptr process_attributes(Alg_parameters_ptr attributes, double time);
    bool parse();
    long parse_chan(char *field);
    long parse_int(char *field);
    int find_real_in(char *field, int n);
    double parse_real(char *field);
    void parse_error(char *field, long offset, char *message);
    double parse_dur(char *field, double base);
    double parse_after_dur(double dur, char *field, int n, double base);
    double parse_loud(char *field);
    long parse_key(char *field);
    double parse_pitch(char *field);
    long parse_after_key(int key, char *field, int n);
    long find_int_in(char *field, int n);
    bool parse_attribute(char *field, Alg_parameter_ptr parm);
    bool parse_val(Alg_parameter_ptr param, char *s, int i);
    bool check_type(char type_char, Alg_parameter_ptr param);
};

#endif /* EXPERIMENTAL_NOTE_TRACK */

void subseq(char *result, char *source, int from, int to)
{
    memcpy(result, source + from, to - from);
    result[to - from] = 0;
}

#ifndef EXPERIMENTAL_NOTE_TRACK
double Allegro_reader::parse_pitch(char *field)
#else /* EXPERIMENTAL_NOTE_TRACK */
double Alg_reader::parse_pitch(char *field)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    if (isdigit(field[1])) {
        char real_string[80];
        int last = find_real_in(field, 1);
        subseq(real_string, field, 1, last);
        return atof(real_string);
    } else {
        return (double) parse_key(field);
    }
}


#ifndef EXPERIMENTAL_NOTE_TRACK
Allegro_reader::Allegro_reader(FILE *a_file)
{
    file = a_file; // save the file
    line_parser_flag = false;
    line_no = 0;
    seq = Seq();
    tsnum = 4; // default time signature
    tsden = 4;
}
#else /* EXPERIMENTAL_NOTE_TRACK */
// it is the responsibility of the caller to delete
// the seq
Alg_reader::Alg_reader(FILE *a_file, Alg_seq_ptr new_seq)
{
    file = a_file; // save the file
    line_parser_flag = false;
    line_no = 0;
    tsnum = 4; // default time signature
    tsden = 4;
    seq = new_seq;
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


//Note that this is an #ifdef, not an #ifndef
#ifdef EXPERIMENTAL_NOTE_TRACK
Alg_seq_ptr alg_read(FILE *file, Alg_seq_ptr new_seq)
    // read a sequence from allegro file
{
    if (!new_seq) new_seq = new Alg_seq();
    Alg_reader alg_reader(file, new_seq);
    alg_reader.parse();
    return alg_reader.seq;
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


#ifndef EXPERIMENTAL_NOTE_TRACK
void Allegro_reader::readline()
#else /* EXPERIMENTAL_NOTE_TRACK */
void Alg_reader::readline()
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    char line[256];
    char *line_flag = fgets(line, 256, file);
    line_parser_flag = false;
    if (line_flag) {
        line_parser.init(line);
        line_parser_flag = true;
        error_flag = false;
    }
}


#ifndef EXPERIMENTAL_NOTE_TRACK
void Allegro_reader::process_attributes(Parameters_ptr attributes, double time)
{
    // print "process_attributes:", attributes
    bool ts_flag;
    if (attributes) {
        Parameters_ptr a;
        if (a = Parameters::remove_key(&attributes, "tempor")) {
            double tempo = a->parm.r;
            seq.insert_tempo(tempo, seq.map.time_to_beat(time));
        }
        if (a = Parameters::remove_key(&attributes, "beatr")) {
            double beat = a->parm.r;
            seq.insert_beat(time, beat);
        }
        if (a = Parameters::remove_key(&attributes, "tsnumr")) {
            tsnum = a->parm.r;
            ts_flag = true;
        }
        if (a = Parameters::remove_key(&attributes, "tsdenr")) {
            tsden = a->parm.r;
            ts_flag = true;
        }
        if (ts_flag) {
            seq.set_time_sig(seq.map.time_to_beat(time), tsnum, tsden);
        }
    }
}
#else /* EXPERIMENTAL_NOTE_TRACK */
Alg_parameters_ptr Alg_reader::process_attributes(
        Alg_parameters_ptr attributes, double time)
{
    // print "process_attributes:", attributes
    bool ts_flag = false;
    if (attributes) {
        Alg_parameters_ptr a;
        bool in_seconds = seq->get_units_are_seconds();
        if (a = Alg_parameters::remove_key(&attributes, "tempor")) {
            double tempo = a->parm.r;
            seq->insert_tempo(tempo, seq->get_time_map()->time_to_beat(time));
        }
        if (a = Alg_parameters::remove_key(&attributes, "beatr")) {
            double beat = a->parm.r;
            seq->insert_beat(time, beat);
        }
        if (a = Alg_parameters::remove_key(&attributes, "timesig_numr")) {
            tsnum = a->parm.r;
            ts_flag = true;
        }
        if (a = Alg_parameters::remove_key(&attributes, "timesig_denr")) {
            tsden = a->parm.r;
            ts_flag = true;
        }
        if (ts_flag) {
            seq->set_time_sig(seq->get_time_map()->time_to_beat(time),
            tsnum, tsden);
        }
        if (in_seconds) seq->convert_to_seconds();
    }
    return attributes; // in case it was modified
}
#endif /* EXPERIMENTAL_NOTE_TRACK */

#ifndef EXPERIMENTAL_NOTE_TRACK
bool Allegro_reader::parse()
{
    int voice = 0;
    int key = 60;
    double loud = 100.0;
    double pitch = 60.0;
    double dur = 1.0;
    double time = 0.0;
    readline();
    bool valid = false; // ignore blank lines
    while (line_parser_flag) {
        bool time_flag = false;
        bool next_flag = false;
        double next;
        bool voice_flag = false;
        bool loud_flag = false;
        bool dur_flag = false;
        bool new_pitch_flag = false; // "P" syntax
        double new_pitch = 0.0;
        bool new_key_flag = false;   // "K" syntax
        int new_key = 0;
        bool new_note_flag = false;  // "A"-"G" syntax
        int new_note = 0;
        Parameters_ptr attributes = NULL;
        line_parser.get_nonspace_quoted(field);
        char pk = line_parser.peek();
        if (pk && !isspace(pk)) {
            line_parser.get_nonspace_quoted(field + strlen(field));
        }
        while (field[0]) {
            // print "field", "|";field;"|", "|";line_parser.string;"|", line_parser.pos
            char first = toupper(field[0]);
            if (strchr("ABCDEFGKLPUSIQHW-", first)) {
                valid = true; // it's a note or event
            }
            if (first == 'V') {
                if (voice_flag) {
                    parse_error(field, 0, "Voice specified twice");
                } else {
                    voice = parse_int(field);
                }
                voice_flag = true;
            } else if (first == 'T') {
                if (time_flag) {
                    parse_error(field, 0, "Time specified twice");
                } else {
                    time = parse_dur(field, 0.0);
                }
                time_flag = true;
            } else if (first == 'N') {
                if (next_flag) {
                    parse_error(field, 0, "Next specified twice");
                } else {
                    next = parse_dur(field, time);
                }
                next_flag = true;
            } else if (first == 'K') {
                if (new_key_flag) {
                    parse_error(field, 0, "Key specified twice");
                } else {
                    new_key = parse_key(field);
                    new_key_flag = true;
                }
            } else if (first == 'L') {
                if (loud_flag) {
                    parse_error(field, 0, "Loudness specified twice");
                } else {
                    loud = parse_loud(field);
                }
                loud_flag = true;
            } else if (first == 'P') {
                if (new_note_flag || new_pitch_flag) {
                    parse_error(field, 0, "Pitch specified twice");
                } else {
                    new_pitch = parse_pitch(field);
                    new_pitch_flag = true;
                }
            } else if (first == 'U') {
                if (dur_flag) {
                    parse_error(field, 0, "Dur specified twice");
                } else {
                    dur = parse_dur(field, time);
                    dur_flag = true;
                }
            } else if (strchr("SIQHW", first)) {
                if (dur_flag) {
                    parse_error(field, 0, "Dur specified twice");
                } else {
                    // prepend 'U' to field, copy EOS too
                    memmove(field + 1, field, strlen(field) + 1);
                    field[0] = 'U';
                    dur = parse_dur(field, time);
                    dur_flag = true;
                }
            } else if (strchr("ABCDEFG", first)) {
                if (new_note_flag || new_pitch_flag) {
                    parse_error(field, 0, "Pitch specified twice");
                } else {
                    // prepend 'K' to field, copy EOS too
                    memmove(field + 1, field, strlen(field) + 1);
                    field[0] = 'K';
                    new_note = parse_key(field);
                    new_note_flag = true;
                }
            } else if (first == '-') {
                Parameter parm;
                if (parse_attribute(field, &parm)) { // enter attribute-value pair
                    attributes = new Parameters(attributes);
                    attributes->parm = parm;
                    parm.s = NULL; // protect string from deletion by destructor
                }
            } else {
                parse_error(field, 0, "Unknown field");
            }

            if (error_flag) {
                field[0] = 0; // exit the loop
            } else {
                line_parser.get_nonspace_quoted(field);
                pk = line_parser.peek();
                if (pk && !isspace(pk)) {
                    line_parser.get_nonspace_quoted(field + strlen(field));
                }
            }
        }
        // a case analysis:
        // Key < 128 counts as both key and pitch
        // A-G implies pitch AND key unless key given too
        //   K60 P60 -- both are specified, use 'em
        //   K60 P60 C4 -- overconstrained, an error
        //   K60 C4 -- overconstrained
        //   K60 -- OK, pitch is 60
        //   C4 P60 -- over constrained
        //   P60 -- OK, key is from before, pitch is 60
        //   C4 -- OK, key is 60, pitch is 60
        //   <nothing> -- OK, key and pitch from before
        //   K200 with P60 ok, pitch is 60
        //   K200 with neither P60 nor C4 uses 
        //       pitch from before
        // figure out what the key/instance is:
        if (new_key_flag) { // it was directly specified
            key = new_key;
            if (key < 128 && new_note_flag) {
                parse_error("", 0, "Pitch specified twice");
            }
        } else if (new_note_flag) { // "A"-"G" used
            key = new_note;
        }
        if (new_pitch_flag) {
            pitch = new_pitch;
        } else if (key < 128) {
            pitch = key;
        }
        // now we've acquired new parameters
        // if (it is a note, then enter the note
        if (valid) {
            // change tempo or beat
            process_attributes(attributes, time);
            // if there's a duration or pitch, make a note:
            if (new_pitch_flag || dur_flag || new_note_flag) {
                new_key_flag = false;
                new_pitch_flag = false;
                Allegro_note_ptr note_ptr = new Allegro_note;
                note_ptr->chan = voice;
                note_ptr->time = time;
                note_ptr->dur = dur;
                note_ptr->key = key;
                note_ptr->pitch = pitch;
                note_ptr->loud = loud;
                note_ptr->parameters = attributes;
                seq.add_event(note_ptr); // sort later
            } else {
                int update_key = -1;
                // key or pitch must appear explicitly; otherwise
                //    update applies to channel
                if (new_key_flag || new_pitch_flag) {
                    update_key = key;
                }
                if (loud_flag) {
                    Allegro_update_ptr new_upd = new Allegro_update;
                    new_upd->chan = voice;
                    new_upd->time = time;
                    new_upd->key = update_key;
                    new_upd->parameter.set_attr(symbol_table.insert_string("loudr"));
                    new_upd->parameter.r = pitch;
                    seq.add_event(new_upd);
                }
                if (attributes) {
                    while (attributes) {
                        Allegro_update_ptr new_upd = new Allegro_update;
                        new_upd->chan = voice;
                        new_upd->time = time;
                        new_upd->key = update_key;
                        new_upd->parameter = attributes->parm;
                        seq.add_event(new_upd);
                        Parameters_ptr p = attributes;
                        attributes = attributes->next;
                        delete p;
                    }
                }
            }
            if (next_flag) {
                time = time + next;
            } else if (dur_flag) {
                time = time + dur;
            }
        }
        readline();
    }
    //print "Finished reading score"
    if (!error_flag) {
        seq.convert_to_seconds(); // make sure format is correct
        // seq.notes.sort('event_greater_than');
    }
    // print "parse returns error_flag", error_flag
    return error_flag;
}
#else /* EXPERIMENTAL_NOTE_TRACK */
bool Alg_reader::parse()
{
    int voice = 0;
    int key = 60;
    double loud = 100.0;
    double pitch = 60.0;
    double dur = 1.0;
    double time = 0.0;
    int track_num = 0;
    seq->convert_to_seconds();
    //seq->set_real_dur(0.0); // just in case it's not initialized already
    readline();
    bool valid = false; // ignore blank lines
    while (line_parser_flag) {
        bool time_flag = false;
        bool next_flag = false;
        double next;
        bool voice_flag = false;
        bool loud_flag = false;
        bool dur_flag = false;
        bool new_pitch_flag = false; // "P" syntax
        double new_pitch = 0.0;
        bool new_key_flag = false;   // "K" syntax
        int new_key = 0;
        bool new_note_flag = false;  // "A"-"G" syntax
        int new_note = 0;
        Alg_parameters_ptr attributes = NULL;
        if (line_parser.peek() == '#') {
            // look for #track
            line_parser.get_nonspace_quoted(field);
            if (streql(field, "#track")) {
                line_parser.get_nonspace_quoted(field); // number
                track_num = parse_int(field - 1);
                seq->add_track(track_num);
            }
            // maybe we have a comment
        } else {
            // we must have a track to insert into
            if (seq->tracks() == 0) seq->add_track(0);
            line_parser.get_nonspace_quoted(field);
            char pk = line_parser.peek();
            // attributes are parsed as two adjacent nonspace_quoted tokens
            // so we have to conditionally call get_nonspace_quoted() again
            if (pk && !isspace(pk)) {
                line_parser.get_nonspace_quoted(field + strlen(field));
            }
            while (field[0]) {
                char first = toupper(field[0]);
                if (strchr("ABCDEFGKLPUSIQHW-", first)) {
                    valid = true; // it's a note or event
                }
                if (first == 'V') {
                    if (voice_flag) {
                        parse_error(field, 0, "Voice specified twice");
                    } else {
                        voice = parse_chan(field);
                    }
                    voice_flag = true;
                } else if (first == 'T') {
                    if (time_flag) {
                        parse_error(field, 0, "Time specified twice");
                    } else {
                        time = parse_dur(field, 0.0);
                    }
                    time_flag = true;
                } else if (first == 'N') {
                    if (next_flag) {
                        parse_error(field, 0, "Next specified twice");
                    } else {
                        next = parse_dur(field, time);
                    }
                    next_flag = true;
                } else if (first == 'K') {
                    if (new_key_flag) {
                        parse_error(field, 0, "Key specified twice");
                    } else {
                        new_key = parse_key(field);
                        new_key_flag = true;
                    }
                } else if (first == 'L') {
                    if (loud_flag) {
                        parse_error(field, 0, "Loudness specified twice");
                    } else {
                        loud = parse_loud(field);
                    }
                    loud_flag = true;
                } else if (first == 'P') {
                    if (new_note_flag || new_pitch_flag) {
                        parse_error(field, 0, "Pitch specified twice");
                    } else {
                        new_pitch = parse_pitch(field);
                        new_pitch_flag = true;
                    }
                } else if (first == 'U') {
                    if (dur_flag) {
                        parse_error(field, 0, "Dur specified twice");
                    } else {
                        dur = parse_dur(field, time);
                        dur_flag = true;
                    }
                } else if (strchr("SIQHW", first)) {
                    if (dur_flag) {
                        parse_error(field, 0, "Dur specified twice");
                    } else {
                        // prepend 'U' to field, copy EOS too
                        memmove(field + 1, field, strlen(field) + 1);
                        field[0] = 'U';
                        dur = parse_dur(field, time);
                        dur_flag = true;
                    }
                } else if (strchr("ABCDEFG", first)) {
                    if (new_note_flag || new_pitch_flag) {
                        parse_error(field, 0, "Pitch specified twice");
                    } else {
                        // prepend 'K' to field, copy EOS too
                        memmove(field + 1, field, strlen(field) + 1);
                        field[0] = 'K';
                        new_note = parse_key(field);
                        new_note_flag = true;
                    }
                } else if (first == '-') {
                    Alg_parameter parm;
                    if (parse_attribute(field, &parm)) { // enter attribute-value pair
                        attributes = new Alg_parameters(attributes);
                        attributes->parm = parm;
                        parm.s = NULL; // protect string from deletion by destructor
                    }
                } else {
                    parse_error(field, 0, "Unknown field");
                }

                if (error_flag) {
                    field[0] = 0; // exit the loop
                } else {
                    line_parser.get_nonspace_quoted(field);
                    pk = line_parser.peek();
                    // attributes are parsed as two adjacent nonspace_quoted 
                    // tokens so we have to conditionally call 
                    // get_nonspace_quoted() again
                    if (pk && !isspace(pk)) {
                        line_parser.get_nonspace_quoted(field + strlen(field));
                    }
                }
            }
            // a case analysis:
            // Key < 128 counts as both key and pitch
            // A-G implies pitch AND key unless key given too
            //   K60 P60 -- both are specified, use 'em
            //   K60 P60 C4 -- overconstrained, an error
            //   K60 C4 -- overconstrained
            //   K60 -- OK, pitch is 60
            //   C4 P60 -- over constrained
            //   P60 -- OK, key is from before, pitch is 60
            //   C4 -- OK, key is 60, pitch is 60
            //   <nothing> -- OK, key and pitch from before
            //   K200 with P60 ok, pitch is 60
            //   K200 with neither P60 nor C4 uses 
            //       pitch from before
            // figure out what the key/instance is:
            if (new_key_flag) { // it was directly specified
                key = new_key;
                if (key < 128 && new_note_flag) {
                    parse_error("", 0, "Pitch specified twice");
                }
            } else if (new_note_flag) { // "A"-"G" used
                key = new_note;
            }
            if (new_pitch_flag) {
                pitch = new_pitch;
            } else if (key < 128) {
                pitch = key;
            }
            // now we've acquired new parameters
            // if (it is a note, then enter the note
            if (valid) {
                // change tempo or beat
                attributes = process_attributes(attributes, time);
                // if there's a duration or pitch, make a note:
                if (new_pitch_flag || dur_flag || new_note_flag) {
                    new_key_flag = false;
                    new_pitch_flag = false;
                    Alg_note_ptr note_ptr = new Alg_note;
                    note_ptr->chan = voice;
                    note_ptr->time = time;
                    note_ptr->dur = dur;
                    note_ptr->set_identifier(key);
                    note_ptr->pitch = pitch;
                    note_ptr->loud = loud;
                    note_ptr->parameters = attributes;
                    seq->add_event(note_ptr, track_num); // sort later
                    if (seq->get_real_dur() < (time + dur)) seq->set_real_dur(time + dur);
                } else {
                    int update_key = -1;
                    // key or pitch must appear explicitly; otherwise
                    //    update applies to channel
                    if (new_key_flag || new_pitch_flag) {
                        update_key = key;
                    }
                    if (loud_flag) {
                        Alg_update_ptr new_upd = new Alg_update;
                        new_upd->chan = voice;
                        new_upd->time = time;
                        new_upd->set_identifier(update_key);
                        new_upd->parameter.set_attr(symbol_table.insert_string("loudr"));
                        new_upd->parameter.r = pitch;
                        seq->add_event(new_upd, track_num);
                        if (seq->get_real_dur() < time) seq->set_real_dur(time);
                    }
                    if (attributes) {
                        while (attributes) {
                            Alg_update_ptr new_upd = new Alg_update;
                            new_upd->chan = voice;
                            new_upd->time = time;
                            new_upd->set_identifier(update_key);
                            new_upd->parameter = attributes->parm;
                            seq->add_event(new_upd, track_num);
                            Alg_parameters_ptr p = attributes;
                            attributes = attributes->next;
                            p->parm.s = NULL; // so we don't delete the string
                            delete p;
                        }
                    }
                }
                if (next_flag) {
                    time = time + next;
                } else if (dur_flag) {
                    time = time + dur;
                }
            }
        }
        readline();
    }
    //print "Finished reading score"
    if (!error_flag) {
        seq->convert_to_seconds(); // make sure format is correct
        // seq->notes.sort('event_greater_than');
    }
    // real_dur is valid, translate to beat_dur
    seq->set_beat_dur((seq->get_time_map())->time_to_beat(seq->get_real_dur()));
    // print "parse returns error_flag", error_flag
    return error_flag;
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


//Note that this is an #ifdef, not an #ifndef
#ifdef EXPERIMENTAL_NOTE_TRACK
long Alg_reader::parse_chan(char *field)
{
    char *int_string = field + 1;
    char *msg = "Integer or - expected";
    char *p = int_string;
    char c;
    // check that all chars in int_string are digits or '-':
    while (c = *p++) {
        if (!isdigit(c) && c != '-') {
            parse_error(field, p - field - 1, msg);
            return 0;
        }
    }
    p--; // p now points to end-of-string character
    if (p - int_string == 0) {
        // bad: string length is zero
        parse_error(field, 1, msg);
        return 0;
    }
    if (p - int_string == 1 && int_string[0] == '-') {
        // special case: entire string is "-", interpret as -1
        return -1;
    }
    return atoi(int_string);
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


#ifndef EXPERIMENTAL_NOTE_TRACK
long Allegro_reader::parse_int(char *field)
{
    char int_string[field_max];
    strcpy(int_string, field + 1);
    char *msg = "Integer expected";
    char *p = int_string;
    char c;
    while (c = *p++) {
        if (!isdigit(c)) {
            parse_error(field, 1, msg);
            return 0;
        }
    }
    if (strlen(int_string) < 1) {
        parse_error(field, 1, msg);
        return 0;
    }
    return atoi(int_string);
}
#else /* EXPERIMENTAL_NOTE_TRACK */
long Alg_reader::parse_int(char *field)
{
    char *int_string = field + 1;
    char *msg = "Integer expected";
    char *p = int_string;
    char c;
    // check that all chars in int_string are digits:
    while (c = *p++) {
        if (!isdigit(c)) {
            parse_error(field, p - field - 1, msg);
            return 0;
        }
    }
    p--; // p now points to end-of-string character
    if (p - int_string == 0) {
        // bad: string length is zero
        parse_error(field, 1, msg);
        return 0;
    }
    return atoi(int_string);
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


#ifndef EXPERIMENTAL_NOTE_TRACK
int Allegro_reader::find_real_in(char *field, int n)
#else /* EXPERIMENTAL_NOTE_TRACK */
int Alg_reader::find_real_in(char *field, int n)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    // scans from offset n to the end of a real constant
    bool decimal = false;
    int len = strlen(field);
    for (int i = n; i < len; i++) {
        char c = field[i];
        if (!isdigit(c)) {
            if (c == '.' && !decimal) {
                decimal = true;
            } else {
                return i;
            }
        }
    }
    return strlen(field);
}


#ifndef EXPERIMENTAL_NOTE_TRACK
double Allegro_reader::parse_real(char *field)
#else /* EXPERIMENTAL_NOTE_TRACK */
double Alg_reader::parse_real(char *field)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    char real_string[80];
    char *msg = "Real expected";
    bool decimal = false;
    int last = find_real_in(field, 1);
    subseq(real_string, field, 1, last);
    if (last <= 1 || last < (int) strlen(field)) {
       parse_error(field, 1, msg);
       return 0;
    }
    return atof(real_string);
}


#ifndef EXPERIMENTAL_NOTE_TRACK
void Allegro_reader::parse_error(char *field, long offset, char *message)
#else /* EXPERIMENTAL_NOTE_TRACK */
void Alg_reader::parse_error(char *field, long offset, char *message)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    int position = line_parser.pos - strlen(field) + offset;
    error_flag = true;
    puts(line_parser.string);
    for (int i = 0; i < position; i++) {
        putc(' ', stdout);
    }
    putc('^', stdout);
    printf("    %s\n", message);
}


double duration_lookup[] = { 0.25, 0.5, 1.0, 2.0, 4.0 };


#ifndef EXPERIMENTAL_NOTE_TRACK
double Allegro_reader::parse_dur(char *field, double base)
{
    char *msg = "Duration expected";
    char real_string[80];
    char *durs = "SIQHW";
    char *p;
    int last;
    double dur;
    if (strlen(field) < 2) {
        // fall through to error message
        return -1;
    } else if (isdigit(field[1])) {
        last = find_real_in(field, 1);
        subseq(real_string, field, 1, last);
        dur = atof(real_string);
        // convert dur from seconds to beats
        dur = seq.map.time_to_beat(base + dur) - seq.map.time_to_beat(base);
    } else if (p = strchr(durs, field[1])) {
        dur = duration_lookup[p - durs];
        last = 2;
    } else {
        parse_error(field, 1, msg);
        return 0;
    }
    dur = parse_after_dur(dur, field, last, base);
    dur = seq.map.beat_to_time(seq.map.time_to_beat(base) + dur) - base;
    return dur;
}
#else /* EXPERIMENTAL_NOTE_TRACK */
double Alg_reader::parse_dur(char *field, double base)
{
    char *msg = "Duration expected";
    char real_string[80];
    char *durs = "SIQHW";
    char *p;
    int last;
    double dur;
    if (strlen(field) < 2) {
        // fall through to error message
        return -1;
    } else if (isdigit(field[1])) {
        last = find_real_in(field, 1);
        subseq(real_string, field, 1, last);
        dur = atof(real_string);
        // convert dur from seconds to beats
        dur = seq->get_time_map()->time_to_beat(base + dur) - 
              seq->get_time_map()->time_to_beat(base);
    } else if (p = strchr(durs, field[1])) {
        dur = duration_lookup[p - durs];
        last = 2;
    } else {
        parse_error(field, 1, msg);
        return 0;
    }
    dur = parse_after_dur(dur, field, last, base);
    dur = seq->get_time_map()->beat_to_time(
              seq->get_time_map()->time_to_beat(base) + dur) - base;
    return dur;
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


#ifndef EXPERIMENTAL_NOTE_TRACK
double Allegro_reader::parse_after_dur(double dur, char *field, int n, double base)
{
    char a_string[80];
    if ((int) strlen(field) == n) {
        return dur;
    }
    if (field[n] == 'T') {
        return parse_after_dur(dur * 2/3, field, n + 1, base);
    }
    if (field[n] == '.') {
        return parse_after_dur(dur * 1.5, field, n + 1, base);
    }
    if (isdigit(field[n])) {
        int last = find_real_in(field, n);
        subseq(a_string, field, n, last);
        double f = atof(a_string);
        return parse_after_dur(dur * f, field, last, base);
    }
    if (field[n] == '+') {
        subseq(a_string, field, n + 1, -1);
        return dur + parse_dur(a_string, 
                               seq.map.beat_to_time(
                                   seq.map.time_to_beat(base) + dur));
    }
    parse_error(field, n, "Unexpected character in duration");
    return dur;
}
#else /* EXPERIMENTAL_NOTE_TRACK */
double Alg_reader::parse_after_dur(double dur, char *field, int n, double base)
{
    char a_string[80];
    if ((int) strlen(field) == n) {
        return dur;
    }
    if (field[n] == 'T') {
        return parse_after_dur(dur * 2/3, field, n + 1, base);
    }
    if (field[n] == '.') {
        return parse_after_dur(dur * 1.5, field, n + 1, base);
    }
    if (isdigit(field[n])) {
        int last = find_real_in(field, n);
        subseq(a_string, field, n, last);
        double f = atof(a_string);
        return parse_after_dur(dur * f, field, last, base);
    }
    if (field[n] == '+') {
        subseq(a_string, field, n + 1, -1);
        return dur + parse_dur(
                a_string, seq->get_time_map()->beat_to_time(
                              seq->get_time_map()->time_to_beat(base) + dur));
    }
    parse_error(field, n, "Unexpected character in duration");
    return dur;
}
#endif /* EXPERIMENTAL_NOTE_TRACK */

#ifndef EXPERIMENTAL_NOTE_TRACK
static struct {
#else /* EXPERIMENTAL_NOTE_TRACK */
struct loud_lookup_struct {
#endif /* EXPERIMENTAL_NOTE_TRACK */
    char *str;
    int val;
} loud_lookup[] = { {"FFF", 127}, {"FF", 120}, {"F", 110}, {"MF", 100}, 
                    {"MP", 90}, {"P", 80}, {"PP", 70}, {"PPP", 60}, 
                    {NULL, 0} };


#ifndef EXPERIMENTAL_NOTE_TRACK
double Allegro_reader::parse_loud(char *field)
{
    char *msg = "Loudness expected";
    if (isdigit(field[1])) {
        return parse_int(field);
    } else {
        double loud = 0.0;
        char dyn[field_max];
        strcpy(dyn, field + 1);
        char *p = dyn;
        while (*p) {
            if (isupper(*p)) *p = toupper(*p);
        }
        for (int i = 0; loud_lookup[i].str; i++) {
            if (streql(loud_lookup[i].str, dyn)) {
                return (double) loud_lookup[i].val;
            }
        }
    }
    parse_error(field, 1, msg);
    return 100.0;
}
#else /* EXPERIMENTAL_NOTE_TRACK */
double Alg_reader::parse_loud(char *field)
{
    char *msg = "Loudness expected";
    if (isdigit(field[1])) {
        return parse_int(field);
    } else {
        double loud = 0.0;
        char dyn[field_max];
        strcpy(dyn, field + 1);
        char *p = dyn;
        while (*p) {
            if (islower(*p)) *p = toupper(*p);
        p++;
        }
        for (int i = 0; loud_lookup[i].str; i++) {
            if (streql(loud_lookup[i].str, dyn)) {
                return (double) loud_lookup[i].val;
            }
        }
    }
    parse_error(field, 1, msg);
    return 100.0;
}
#endif /* EXPERIMENTAL_NOTE_TRACK */


int key_lookup[] = {21, 23, 12, 14, 16, 17, 19};

#ifndef EXPERIMENTAL_NOTE_TRACK
long Allegro_reader::parse_key(char *field)
#else /* EXPERIMENTAL_NOTE_TRACK */
long Alg_reader::parse_key(char *field)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    char *msg = "Pitch expected";
    char *pitches = "ABCDEFG";
    char *p;
    if (isdigit(field[1])) {
        return parse_int(field);
    } else if (p = strchr(pitches, field[1])) {
        long key = key_lookup[p - pitches];
        key = parse_after_key(key, field, 2);
        return key;
    }
    parse_error(field, 1, msg);
    return 0;
}


#ifndef EXPERIMENTAL_NOTE_TRACK
long Allegro_reader::parse_after_key(int key, char *field, int n)
#else /* EXPERIMENTAL_NOTE_TRACK */
long Alg_reader::parse_after_key(int key, char *field, int n)
#endif /* EXPERIMENTAL_NOTE_TRACK */
{
    char octave[20];
    if ((int) strlen(field) == n) {
        return key;
    }
    char c = toupper(field[n]);
    if (c == 'S') {
        return parse_after_key(key + 1, field, n + 1);
    }
    if (c == 'F') {
        return parse_after_key(key - 1, field, n + 1);
    }
    if (isdigit(c)) {
        int last = find_int_in(field, n);
        subseq(octave, field, n, last);
        int oct = atoi(octave);
        return parse_after_key(key + oct * 12, field, last);
    }
    parse_error(field, n, "Unexpected character in pitch");
    return key;
}
コード例 #15
0
ファイル: cmd-install.c プロジェクト: neuroradiology/roswell
int install_help(int argc,char **argv,struct sub_command* cmd) {
  if(argc==1) {
    cond_printf(0,"Usage: %s install impl [OPTIONS]\n\nFor more details on impl specific options, type:\n %s help install impl\n\n"
                "Candidates impls for installation are:\n",argv_orig[0],argv_orig[0]);
    char* install=lispdir();
    LVal d=directory(install),v=d;
    for(;v;v=Next(v)) {
      char* str=firsts(v);
      if(str[strlen(str)-1]!='/') {
        int p=position_char(".",str);
        if(p!=-1) {
          char *sub=subseq(str,0,p);
          if(p>=8/*strlen("install-")*/ && strncmp(str,"install-",8)==0)
            printf("%s\n",sub+8);
          s(sub);
        }
      }
    }
    sL(d);
  }else if(argc==2) {
    int i,j,argc_;
    char** tmp;
    char* install_ros=s_cat2(lispdir(),q("install.ros"));
    tmp=(char**)alloc(sizeof(char*)*(argc+9));
    i=0;
    tmp[i++]=q("--");
    tmp[i++]=install_ros;
    tmp[i++]=q("help");
    tmp[i++]=q(argv[1]);
    for(j=2;j<argc;tmp[i++]=q(argv[j++]));
    argc_=i;
    for(i=0;i<argc_;i+=proccmd(argc_-i,&tmp[i],top_options,top_commands));
    for(j=0;j<argc_;s(tmp[j++]));
    dealloc(tmp);
  }
  return 0;
}
コード例 #16
0
ファイル: cmd-install.c プロジェクト: neuroradiology/roswell
int cmd_install(int argc,char **argv,struct sub_command* cmd) {
  install_cmds *cmds=NULL;
  struct install_options param;
  quicklisp=1;
  param.os=uname();
  param.arch=uname_m();
  param.arch_in_archive_name=0;
  param.expand_path=NULL;
  if(argc!=1) {
    int ret=1,k;
    for(k=1;k<argc;++k) {
      int i,pos;
      param.impl=argv[k];
      pos=position_char("/",param.impl);
      if(pos!=-1) {
        param.version=subseq(param.impl,pos+1,0);
        param.impl=subseq(param.impl,0,pos);
      }else {
        param.version=NULL;
        param.impl=q(param.impl);
      }

      for(install_impl=NULL,i=0;i<sizeof(impls_to_install)/sizeof(struct install_impls*);++i) {
        struct install_impls* j=impls_to_install[i];
        if(strcmp(param.impl,j->name)==0) {
          install_impl=j;
        }
      }
      if(install_impl) {
        for(cmds=install_impl->call;*cmds&&ret;++cmds)
          ret=(*cmds)(&param);
        if(ret) { // after install latest installed impl/version should be default for 'run'
          struct opts* opt=global_opt;
          struct opts** opts=&opt;
          char* home=configdir();
          char* path=cat(home,"config",NULL);
          char* v=cat(param.impl,".version",NULL);
          char* version=param.version;
          if(!install_impl->util) {
            int i;
            for(i=0;version[i]!='\0';++i)
              if(version[i]=='-')
                version[i]='\0';
            set_opt(opts,"default.lisp",param.impl,0);
            set_opt(opts,v,version,0);
            save_opts(path,opt);
          }
          s(home),s(path),s(v);
        }
      }else {
        char* lisp_path=lispdir();
        int i,j,argc_;
        char** tmp;
        char* install_ros=s_cat2(lisp_path,q("install.ros"));
        if(verbose&1) {
          fprintf(stderr,"%s is not implemented internal. %s argc:%d\n",param.impl,install_ros,argc);
          for(i=0;i<argc;++i)
            fprintf(stderr,"%s:",argv[i]);
          fprintf(stderr,"\n");
        }
        tmp=(char**)alloc(sizeof(char*)*(argc+9));
        i=0;
        tmp[i++]=q("--");
        tmp[i++]=install_ros;
        tmp[i++]=q("install");
        tmp[i++]=q(argv[1]);
        for(j=2;j<argc;tmp[i++]=q(argv[j++]));
        argc_=i;
        if(verbose&1) {
          int j;
          fprintf(stderr,"argc_=%d",argc_);
          for(j=0;j<argc_;++j)
            fprintf(stderr,"argv[%d]=%s,",j,tmp[j]);
        }
        for(i=0;i<argc_;i+=proccmd(argc_-i,&tmp[i],top_options,top_commands));
        for(j=0;j<argc_;s(tmp[j++]));
        dealloc(tmp);
        return 0;
      }
      if(param.version)s(param.version);
      s(param.impl),s(param.arch),s(param.os);
      s(param.expand_path);
      if(!ret)
        exit(EXIT_FAILURE);
    }
  }else {
    char* tmp[]={"help","install"};
    proccmd(2,tmp,top_options,top_commands);
    exit(EXIT_SUCCESS);
  }
  return 0;
}
コード例 #17
0
ファイル: cmd-run.c プロジェクト: JamesTFarrington/roswell
int cmd_run_star(int argc,char **argv,struct sub_command* cmd) {
  int ret=1;
  char* config=configdir();
  set_opt(&local_opt,"quicklisp",s_escape_string(cat(config,"impls",SLASH,"ALL",SLASH,"ALL",SLASH,"quicklisp",SLASH,NULL)),0);
  set_opt(&local_opt,"argv0",argv_orig[0],0);
  set_opt(&local_opt,"wargv0",which(argv_orig[0]),0);
  set_opt(&local_opt,"homedir",config,0);
  if(rc) {
    char* init=s_cat(configdir(),q("init.lisp"),NULL);
#ifdef _WIN32
    char* etc="";
#else
    char* etc="/etc/rosrc";
#endif
    char* current=get_opt("program",0);
    char *path,*would;
    if(file_exist_p(init)) {
      path=cat("(:load \"",init,"\")",NULL);
      would=cat(path,current?current:"",NULL);
      s(current);
      set_opt(&local_opt,"program",would,0);
      s(path);
    }
    s(init);
    current=get_opt("program",0);
    if(file_exist_p(etc)) {
      path=cat("(:load \"",etc,"\")",NULL);
      would=cat(path,current?current:"",NULL);
      set_opt(&local_opt,"program",would,0);
    }
  }
  char*lisp=get_opt("lisp",1);
  if(!lisp)
    lisp=get_opt("*lisp",0);
  set_opt(&local_opt,"impl",determin_impl(lisp),0);
  char** arg=NULL;
  int i;
  char* wrap=get_opt("wrap",1);
  {
    struct sub_command cmd;
    int i;
    char *_= get_opt("impl",0);
    i=position_char("/",_);
    cmd.name=subseq(_,0,i);
    cmd.short_name=subseq(_,i+1,0);
    for(i=0;i<sizeof(impls_to_run)/sizeof(struct run_impl_t);++i)
      if(strcmp(impls_to_run[i].name,cmd.name)==0) {
        arg=impls_to_run[i].impl(argc,argv,&cmd);
        break;
      }
    s((char*)cmd.name),s((char*)cmd.short_name);
  }
  if(wrap)
    arg[0]=q(wrap);
  if(arg && file_exist_p(arg[1])) {
    char* opts=sexp_opts(local_opt);
    setenv("ROS_OPTS",opts,1);
    if(verbose&1 ||testing) {
      fprintf(stderr,"args ");
      for(i=0;arg[i]!=NULL;++i)
        fprintf(stderr,"%s ",arg[i]);
      fprintf(stderr,"\nROS_OPTS %s\n",getenv("ROS_OPTS"));
      if(testing)
        s(opts),exit(EXIT_SUCCESS);
    }
    s(opts);
#ifdef _WIN32
    {
      char* cmd=q(arg[wrap?0:1]);
      for(i=wrap?1:2;arg[i]!=NULL;++i) {
        cmd=s_cat(cmd,q(" "),q("\""),escape_string(arg[i]),q("\""),NULL);
      }
      SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
      exit(System(cmd));
      s(cmd);
    }
#else
    execvp(arg[wrap?0:1],&(arg[wrap?0:1]));
#endif
  }else
    fprintf(stderr,"%s is not installed.stop.\n",get_opt("impl",0));

  s(config);
  return ret;
}
コード例 #18
0
ファイル: html.c プロジェクト: JamesTFarrington/roswell
LVal parse_tags(FILE* fp,LVal before,int mode) {
  LVal current=tagalloc();
  char str[2]={'\0','\0'};
  int c,i=0;
  char* buf=q("");
  switch(mode) {
  case 0: /* wait for '<' */
    ((struct tag*)firstp(current))->type=0;
    while((c=fgetc(fp))!=EOF) {
      if(c=='<') {
        if(strlen(buf)==0) {
          tagfree(current);
          s(buf);
          return parse_tags(fp,before,1);
        }else {
          ((struct Cons*)current)->next=parse_tags(fp,current,1);
          s(buf);
          return current;
        }
      }else
        str[0]=c,buf=s_cat2(buf,q(str));
    }
    break;
  case 1: /* wait for '>' */
    ((struct tag*)firstp(current))->type=0;
    while((c=fgetc(fp))!=EOF) {
      if(i==0) {
        if(c=='/')
          ((struct tag*)firstp(current))->type=2;
        else {
          ((struct tag*)firstp(current))->type=1;
          str[0]=c;
          buf=s_cat2(buf,q(str));
        }
        ++i;
        continue;
      }
      if(c=='>') {
        char *buf2;
        if(((struct tag*)firstp(current))->type==2) {
          int pos=position_char(" \t\r\n",buf);
          if(pos!=-1) {
            buf2=subseq(buf,0,pos);
            s(buf);
            buf=buf2;
            ((struct tag*)firstp(current))->name=q(buf);
          }else {
            ((struct tag*)firstp(current))->name=buf;
            buf=q("");
          }
        }else if(((struct tag*)firstp(current))->type==1) {
          int pos=position_char(" \t\r\n",buf);
          if(pos!=-1) {
            ((struct tag*)firstp(current))->name=subseq(buf,0,pos);
            buf2=subseq(buf,pos,0);
            ((struct tag*)firstp(current))->attr=(struct Cons*)parse_attr(buf2);
            s(buf);
            buf=buf2;
          }else {
            ((struct tag*)firstp(current))->name=buf;
            buf=q("");
          }
        }
        if(strcmp(((struct tag*)firstp(current))->name,"script")==0) {
          ((struct Cons*)current)->next=parse_tags(fp,current,2);
        }else{
          ((struct Cons*)current)->next=parse_tags(fp,current,0);
        }
        s(buf);
        return current;
      }else {
        str[0]=c;
        buf=s_cat2(buf,q(str));
      }
      ++i;
    }
    break;
  case 2: /* wait for '</' */
    ((struct tag*)firstp(current))->type=0;
    while((c=fgetc(fp))!=EOF) {
      if(c=='<') {
        if((c=fgetc(fp))!=EOF && c=='/') {
          ungetc('/',fp);
          if(strlen(buf)==0) {
            tagfree(current);
            s(buf);
            return parse_tags(fp,current,1);
          }else {
            ((struct Cons*)current)->next=parse_tags(fp,current,1);
            s(buf);
            return current;
          }
        }
        ungetc('/',fp);
      }else {
        str[0]=c;
        buf=s_cat2(buf,q(str));
      }
    }
    break;
  }
  s(buf);
  return current;
}