static int parse_macro_cmd(const char *p, unsigned char *cmd, unsigned char *val) { char *t; int q = INVALID_NUMBER, c = INVALID_NUMBER, v = INVALID_NUMBER; if (!p || !cmd || !val) goto ret; t = get_token(p); if (t) { c = lookup_macro_token_by_name(t); free(t); } if (c == INVALID_NUMBER) { v = c; goto ret; } p = skip_token(p); /* todo: Q_PLAY */ if (c == Q_PUSH_META) { t = get_token(p); if (t) { q = lookup_macro_token_by_name(t); free(t); } else q = INVALID_NUMBER; if (q == INVALID_NUMBER) { v = q; goto ret; } c |= q; p = skip_token(p); } t = get_token(p); switch (get_macro_arg_type(c)) { case MACRO_ARG_HID: v = lookup_hid_token_by_name(t); break; case MACRO_ARG_META: v = parse_meta_handed(p); break; case MACRO_ARG_DELAY: v = parse_int(t, 0, 255); break; case MACRO_ARG_NONE: v = 0; break; } if (t) free(t); ret: *cmd = (unsigned char)c; *val = (unsigned char)v; return v == INVALID_NUMBER; }
boost::int64_t lazy_entry::int_value() const { TORRENT_ASSERT(m_type == int_t); boost::int64_t val = 0; bool negative = false; if (*m_data.start == '-') negative = true; bdecode_errors::error_code_enum ec = bdecode_errors::no_error; parse_int(m_data.start + negative , m_data.start + m_size, 'e', val, ec); if (ec) return 0; if (negative) val = -val; return val; }
void generator_thread::create_script(QString uuid,QString major, QString minor, int strength){ QString file_name; if(update_minor) file_name = uuid+"/"+major+"/start_beacon_"+major+"_"+minor+".sh"; else file_name = uuid+"/"+minor+"/start_beacon_"+major+"_"+minor+".sh"; //qDebug()<<file_name; QFile file(file_name); if(file.open(QIODevice::ReadWrite)){ QTextStream output_stream(&file); QString command = "#!/bin/bash \nhciconfig hci0 up\nhciconfig hci0 leadv 3 \n"; command.append(create_command_string(uuid)); command.append(parse_int(major.toInt())); command.append(parse_int(minor.toInt())); command.append(QString::number(strength,16)); command.append(" 00"); //qDebug()<<command; output_stream<<command; } file.setPermissions(QFile::ExeGroup | QFile::ReadGroup | QFile::ReadOwner | QFile::ReadOther | QFile::WriteOwner | QFile::ExeUser); }
irom static app_action_t application_function_gpio_status_set(const string_t *src, string_t *dst) { int io, pin; if((parse_int(1, src, &io, 0) == parse_ok) && (parse_int(2, src, &pin, 0) == parse_ok)) { if((io < -1) || (io > io_id_size)) { string_format(dst, "status trigger io %d/%d invalid\n", io, pin); return(app_action_error); } config.status_trigger_io.io = io; config.status_trigger_io.pin = pin; } string_format(dst, "status trigger at io %d/%d (-1 is disabled)\n", config.status_trigger_io.io, config.status_trigger_io.pin); return(app_action_normal); }
static int parse_timeout(redisClient *c, robj *obj, int *p) { int v; if (parse_int(c, obj, &v) != 0) { return -1; } if (v < 0) { addReplyErrorFormat(c, "invalid timeout = %d", v); return -1; } *p = (v == 0) ? 100 : v; return 0; }
int parse_orf_thresh_level(const char *arg, int *x) { int tmp; if (parse_int(arg, &tmp)) { return -1; } if (tmp != 0 && tmp != 1 && tmp != 2) { return -1; } *x = tmp; return 0; }
static int parse_slot(redisClient *c, robj *obj, int *p) { int v; if (parse_int(c, obj, &v) != 0) { return -1; } if (v < 0 || v >= HASH_SLOTS_SIZE) { addReplyErrorFormat(c, "invalid slot number = %d", v); return -1; } *p = v; return 0; }
bool parse_bool(const char *value, bool default_value) { if (!value) return default_value; if (streq(value, "true") || streq(value, "on") || streq(value, "yes")) return true; if (streq(value, "false") || streq(value, "off") || streq(value, "no")) return false; return parse_int(value, default_value); }
/** * Looks up the column index for a range entry. It can be either a column name or a column number. * If it is a column number we subtract 1 because human user starts with index 1 rather than 0. */ size_t csv_tool::lookup_column_index(const std::string &s) { auto it = find_if(csv_data->mcolumns->begin(), csv_data->mcolumns->end(), [s](const meta_column_t &mc) { return mc.title == s; }); if (it != csv_data->mcolumns->end()) { return it - csv_data->mcolumns->begin(); } long long ltmp; bool success = parse_int(s, ltmp); if (success && ltmp <= csv_data->mcolumns->size()) { return ltmp-1; } else { return std::string::npos; } }
static lwm2m_value parse_value(char *message, int message_len, lwm2m_type type) { lwm2m_value value; switch(type) { case INTEGER: { value.int_value = parse_int(message, message_len); break; } case DOUBLE: { value.double_value = parse_double(message, message_len); break; } case OPAQUE: case STRING: { char *string_value = (char *) malloc((size_t) (message_len + 1)); memcpy(string_value, message, (size_t) message_len); string_value[message_len] = '\0'; value.string_value = string_value; break; } case BOOLEAN: { int int_value = parse_int(message, message_len); value.bool_value = int_value ? true : false; break; } case LINK: { lwm2m_link link; link.object_id = parse_int(message + 2, 2); link.instance_id = parse_int(message, 2); value.link_value = link; break; } default: value.int_value = -1; break; } return value; }
static int mput_cmd(char* args) { int i = 0; int rv, k, v; if (th == NULL) { fprintf(stderr, "try `open' before `mput'\n"); return -1; } while (args != NULL && args[0] != '\0') { rv = parse_int(&args, &k); if (rv != 0) { fprintf(stderr, "mput: argument %d not an integer number\n", i); return -1; } rv = parse_int(&args, &v); if (rv != 0) { fprintf(stderr, "mput: value for key %d expected\n", k); return -1; } tapioca_mput(th, &k, sizeof(int), &v, sizeof(int)); i++; } if (i == 0) { fprintf(stderr, "mput: too few arguments\n"); return -1; } rv = tapioca_mput_commit(th); if (rv < 0) { fprintf(stderr, "mput: failed.\n"); return -1; } printf("committed (%d remote requests)\n", rv); return 0; }
static struct AstNode *parse_literal_int(struct DomNode *dom) { long value; if (!dom_node_is_atom(dom)) { return NULL; } if (!parse_int(dom->atom, &value)) { return NULL; } return ast_make_literal_atomic_int(value); }
expr * simple_parser::parse_expr(scanner & s) { builtin_op op; var * v; expr * r; scanner::token token; token = s.scan(); switch (token) { case scanner::LEFT_PAREN: token = s.scan(); if (token != scanner::ID_TOKEN) throw parser_error(); if (m_builtin.find(s.get_id(), op)) { ptr_vector<expr> args; while (true) { expr * arg = parse_expr(s); if (arg) { args.push_back(arg); } else { expr * r = m_manager.mk_app(op.m_family_id, op.m_kind, args.size(), args.c_ptr()); m_exprs.push_back(r); return r; } } } throw parser_error(); case scanner::RIGHT_PAREN: return 0; case scanner::ID_TOKEN: if (m_builtin.find(s.get_id(), op)) { expr * r = m_manager.mk_const(op.m_family_id, op.m_kind); m_exprs.push_back(r); return r; } else if (m_vars.find(s.get_id(), v)) { return v; } throw parser_error(); case scanner::INT_TOKEN: r = parse_int(s.get_number()); m_exprs.push_back(r); return r; case scanner::FLOAT_TOKEN: r = parse_float(s.get_number()); m_exprs.push_back(r); return r; default: throw parser_error(); } }
int cmd_disable(mpd_unused int argc, char **argv, mpd_Connection *conn) { int arg; if( ! parse_int( argv[0], &arg ) || arg <= 0 ) { DIE( "Not a positive integer\n" ); } else { mpd_sendDisableOutputCommand( conn, ( arg - 1 ) ); } mpd_finishCommand( conn ); return cmd_outputs(0, NULL, conn); }
static int try_parse_int(int fd, int *val, const char *pname) { int err; err = parse_int(fd, val); if (err) ERROR("error while parsing %s (%s)", pname, err == -1 ? "end of file" : err == -2 ? "not a number" : "number too big"); return err; }
/* extract substitution tail from the command buffer */ char extract_subst_tail( const char **ibufpp, int *gflagsp, int *snump, const char isglobal ) { const char delimiter = **ibufpp; *gflagsp = *snump = 0; if( delimiter == '\n' ) { stlen = 0; *gflagsp = GPR; return 1; } if( !extract_subst_template( ibufpp, isglobal ) ) return 0; if( **ibufpp == '\n' ) { *gflagsp = GPR; return 1; } if( **ibufpp == delimiter ) ++(*ibufpp); if( **ibufpp >= '1' && **ibufpp <= '9' ) return parse_int( snump, *ibufpp, ibufpp ); if( **ibufpp == 'g' ) { ++(*ibufpp); *gflagsp = GSG; return 1; } return 1; }
int parse_int_mask(uint32_t *val, uint32_t *mask, const char *str2) { char str[MAX_STR_LEN_PROC]; char *mask_str; if (parse_vars(str, sizeof(str), str2)) return -1; mask_str = strchr(str, '&'); if (mask_str == NULL) { set_errf("Missing '&' when parsing mask"); return -2; } *mask_str = 0; if (parse_int(val, str)) return -1; if (parse_int(mask, mask_str + 1)) return -1; return 0; }
/** * Reads the NMEA sentence into the specified structure. * * @param[out] nmea Structure to hold the parsed data. * @param[in] s Start of the string to parse (inclusive). * @param[in] e End of the string to parse (exclusive). * @retval -1 Parameter failure, parsing error. * @retval 0 Success */ static int read(struct nmea_t * nmea, const char * s, const char * e) { struct nmea_rte_t * v; int state = 0; const char * p; if (nmea == NULL || s == NULL || e == NULL) return -1; nmea->type = NMEA_RTE; v = &nmea->sentence.rte; p = find_token_end(s); for (state = -1; state < 13 && s < e; ++state) { switch (state) { case 0: if (parse_int(s, p, &v->n_messages) != p) return -1; break; case 1: if (parse_int(s, p, &v->message_number) != p) return -1; break; case 2: v->message_mode = (s == p) ? NMEA_COMPLETE_ROUTE : *s; break; case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: case 12: if ((unsigned int)(p-s+1) < sizeof(v->waypoint_id[state-3]) && parse_str(s, p, v->waypoint_id[state-3]) != p) return -1; break; default: break; } s = p + 1; p = find_token_end(s); } return 0; }
static void authBasicParse(authScheme * scheme, int n_configured, char *param_str) { if (scheme->scheme_data == NULL) { assert(basicConfig == NULL); /* this is the first param to be found */ scheme->scheme_data = xmalloc(sizeof(auth_basic_config)); memset(scheme->scheme_data, 0, sizeof(auth_basic_config)); basicConfig = scheme->scheme_data; basicConfig->basicAuthRealm = xstrdup("Squid proxy-caching web server"); basicConfig->authenticateChildren = 5; basicConfig->credentialsTTL = 2 * 60 * 60; /* two hours */ } basicConfig = scheme->scheme_data; if (strcasecmp(param_str, "program") == 0) { if (basicConfig->authenticate) wordlistDestroy(&basicConfig->authenticate); parse_wordlist(&basicConfig->authenticate); } else if (strcasecmp(param_str, "children") == 0) { parse_int(&basicConfig->authenticateChildren); } else if (strcasecmp(param_str, "concurrency") == 0) { parse_int(&basicConfig->authenticateConcurrency); } else if (strcasecmp(param_str, "realm") == 0) { parse_eol(&basicConfig->basicAuthRealm); } else if (strcasecmp(param_str, "credentialsttl") == 0) { parse_time_t(&basicConfig->credentialsTTL); } else if (strcasecmp(param_str, "casesensitive") == 0) { parse_onoff(&basicConfig->casesensitive); } else if (strcasecmp(param_str, "blankpassword") == 0) { parse_onoff(&basicConfig->blankpassword); } else if (strcasecmp(param_str, "utf8") == 0) { parse_onoff(&basicConfig->utf8); } else { debug(29, 0) ("unrecognised basic auth scheme parameter '%s'\n", param_str); } }
irom static app_action_t application_function_i2c_read(const string_t *src, string_t *dst) { int size, current; i2c_error_t error; uint8_t bytes[32]; uint32_t start, stop, clocks, spent; if(parse_int(1, src, &size, 0) != parse_ok) { string_cat(dst, "i2c-read: missing byte count\n"); return(app_action_error); } if(size > (int)sizeof(bytes)) { string_format(dst, "i2c-read: read max %d bytes\n", sizeof(bytes)); return(app_action_error); } start = system_get_time(); if((error = i2c_receive(i2c_address, size, bytes)) != i2c_error_ok) { string_cat(dst, "i2c_read"); i2c_error_format_string(dst, error); string_cat(dst, "\n"); i2c_reset(); return(app_action_error); } stop = system_get_time(); string_format(dst, "> i2c_read: read %d bytes from %02x:", size, i2c_address); for(current = 0; current < size; current++) string_format(dst, " %02x", bytes[current]); string_cat(dst, "\n"); clocks = (size + 1) * 20U; spent = (stop - start) * 1000U; string_format(dst, "> transferred %u bytes in %u scl clocks\n", size + 1U, clocks); string_format(dst, "> time spent: %u microseconds, makes %u kHz i2c bus\n", spent / 1000U, 1000000U / (spent / clocks)); return(app_action_normal); }
int main (int argc, char **argv) { float outbuf[BUFSIZE]; float del, att, spd, shape, mod, op; int nsamples, s, i; program_name = argv[0]; if (argc != 7) { usage(); } nsamples = parse_int(argv[1], "nsamples"); del = parse_float(argv[2], "del"); att = parse_float(argv[3], "att"); spd = parse_float(argv[4], "spd"); shape = parse_shape(argv[5], "shape"); mod = parse_float(argv[6], "mod"); op = 0.0f; if (!nsamples) { fprintf(stderr, "%s: nsamples must be nonzero\n", program_name); usage(); } // Seconds * samples/second = samples LfoParams p = {20.0f * 48000.0f, &del, &att, &spd, &shape, &mod, &op}; Lfo *lfo = lfo_create(&p); lfo_trigger(lfo); for (s = 0; s < nsamples;) { // Blank buffer for (i=0; i<BUFSIZE; ++i) outbuf[i] = 0.0f; // Run lfo_run(lfo, outbuf, BUFSIZE); // Write for (i = 0; i < BUFSIZE && s < nsamples; ++i, ++s) printf("%d %f\n", s, outbuf[i]); } lfo_destroy(lfo); return EXIT_SUCCESS; }
int main(int argc,char**argv) { string text_ip=argv[1]; string text_port=argv[2]; int loop=parse_int(argv[3],1<<10); zmq::context_t ctx; zmq::socket_t sock(ctx,ZMQ_REQ); string serv_endpoint="tcp://"+text_ip+":"+text_port; sock.connect(serv_endpoint.c_str()); log_i("connected to %s",serv_endpoint.c_str()); prot::Requester req(sock); <<<<<<< HEAD
irom static app_action_t application_function_ntp_set(const string_t *src, string_t *dst) { int timezone; string_new(static, ip, 32); if((parse_string(1, src, &ip) == parse_ok) && (parse_int(2, src, &timezone, 0) == parse_ok)) { config.ntp_server = ip_addr(string_to_ptr(&ip)); config.ntp_timezone = timezone; sntp_setserver(0, &config.ntp_server); sntp_set_timezone(config.ntp_timezone); } return(application_function_ntp_dump(src, dst)); }
#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; }
static int cmd_select(const char *args) { int s, ret = ERR_INVALID_ARGS; char *t = get_token(args); if (t && !strcmp(t, "any")) s = 0; else s = parse_int(args, 1, 7); if (s != INVALID_NUMBER) { current_select = (unsigned char)s; ret = 0; } if (t) free(t); return ret; }
void update_options_from_argv(const char *argv[]) { int next, flags_pos; for (next = flags_pos = 0; argv[next]; next++) { const char *flag = argv[next]; int value = -1; if (map_enum(&value, commit_order_arg_map, flag)) { opt_commit_order = value; mark_option_seen(&opt_commit_order); continue; } if (map_enum(&value, ignore_space_arg_map, flag)) { opt_ignore_space = value; mark_option_seen(&opt_ignore_space); continue; } if (!strcmp(flag, "--no-notes")) { opt_show_notes = FALSE; mark_option_seen(&opt_show_notes); continue; } if (!prefixcmp(flag, "--show-notes") || !prefixcmp(flag, "--notes")) { opt_show_notes = TRUE; string_ncopy(opt_notes_arg, flag, strlen(flag)); mark_option_seen(&opt_show_notes); continue; } if (!prefixcmp(flag, "-U") && parse_int(&value, flag + 2, 0, 999999) == SUCCESS) { opt_diff_context = value; mark_option_seen(&opt_diff_context); continue; } argv[flags_pos++] = flag; } argv[flags_pos] = NULL; }
void read_clause(Buffer & in, std::ostream& err, sat::solver & solver, sat::literal_vector & lits) { int parsed_lit; int var; lits.reset(); while (true) { parsed_lit = parse_int(in, err); if (parsed_lit == 0) break; var = abs(parsed_lit); SASSERT(var > 0); while (static_cast<unsigned>(var) >= solver.num_vars()) solver.mk_var(); lits.push_back(sat::literal(var, parsed_lit < 0)); } }
/*! @brief バイブレーションのパターンを解析する。 @param[in] data データ @param[in] len パターンの個数 */ static int vibration_parse_pattern(char *data, int len) { // 最大の配列のサイズはVIBRATION_PATTERN_SIZEで指定されている // これ以上のサイズの配列が送られてきた場合にはエラー if (len >= VIBRATION_PATTERN_SIZE) { return VIB_PATTERN_ERROR; } for (int i = 0; i < len; i++) { vib_data[i] = parse_int(data, 2 * i); DBG_LOG(APP_LOG_LEVEL_DEBUG, "vib_data[%d]: %d ", i, (int) vib_data[i]); // pebbleの仕様では、最長で10000までしか設定できない // それ以上のパラメータが送られてきた場合にはエラー if (vib_data[i] > 10000) { return VIB_PATTERN_ERROR; } } return VIB_PATTERN_OK; }
void feature_templates::parse(istream& is, entity_map& entities) { sentence_processors.clear(); entity_processors.clear(); total_features = 1; // An omnipresent feature used in process_sentence string line; vector<string> tokens; while (getline(is, line)) { // Ignore empty lines and lines commented with # if (line.empty() || line[0] == '#') continue; split(line, ' ', tokens); if (tokens.size() < 1) runtime_failure("Bad line '" << line << "' of feature templates file!"); vector<string> token0_parts; split(tokens[0], '/', token0_parts); if (token0_parts.size() < 1 || token0_parts.size() > 2) runtime_failure("Bad feature template description at line '" << line << "' of feature templates file!"); string template_name = token0_parts[0]; int window = token0_parts.size() > 1 ? parse_int(token0_parts[1].c_str(), "feature_template_window") : 0; vector<string> args; for (unsigned i = 1; i < tokens.size(); i++) args.emplace_back(tokens[i]); // Try sentence processor auto* maybe_sentence_processor = sentence_processor::create(template_name); if (maybe_sentence_processor) { if (!maybe_sentence_processor->parse(window, args, entities, &total_features)) runtime_failure("Cannot initialize feature template sentence processor '" << template_name << "' from line '" << line << "' of feature templates file!"); sentence_processors.emplace_back(template_name, maybe_sentence_processor); continue; } // Try entity processor auto* maybe_entity_processor = entity_processor::create(template_name); if (maybe_entity_processor) { if (window) cerr << "Ignoring window of " << window << " specified in entity_processor '" << template_name << "'." << endl; if (!maybe_entity_processor->parse(args, entities)) runtime_failure("Cannot initialize feature template entity processor '" << template_name << "' from line '" << line << "' of feature templates file!"); entity_processors.emplace_back(template_name, maybe_entity_processor); continue; } // Fail runtime_failure("Cannot create feature template '" << template_name << "' from line '" << line << "' of feature templates file!"); } }
int semver_numeric (semver_t *x) { int num; char buf[SLICE_SIZE * 3]; memset(&buf, 0, SLICE_SIZE * 3); if (x->major) concat_num(buf, x->major, NULL); if (x->minor) concat_num(buf, x->minor, NULL); if (x->patch) concat_num(buf, x->patch, NULL); num = parse_int(buf); if(num == -1) return -1; if (x->prerelease) num += char_to_int(x->prerelease); if (x->metadata) num += char_to_int(x->metadata); return num; }