void test_statsd_msg__parse_strings(void) { struct brubeck_statsd_msg msg; int i; for(i = 0; test_values[i].metric != NULL; i++) { try_parse(&msg, test_values[i].metric, test_values[i].parse, test_values[i].expected); } #define EXPECT "some.non-float.value" try_parse_set(&msg, "this.is.a.set:" EXPECT "|s|@args", -1, EXPECT); try_parse_set(&msg, "this.is.a.set:" EXPECT "|s", 0, EXPECT); }
bool Resource::Id::tryParse(const std::string& s) { static const char TYPE_A[] = { CMS_TYPE, APP_TYPE }; static const std::string TAG_A[] = { "cms:", "app:" }; for (int k=0; k<2; ++k) { Id v; if (try_parse(s, TAG_A[k], TYPE_A[k], v)) { *this = v; return true; } } DS_DBG_CODE(std::cout << "ERROR ds::resource_id::tryParse() illegal input (" << s << ")" << std::endl); return false; }
int main() { uint64_t id; id = try_parse("232456454545455445454534645656"); id = try_parse("bb"); id = try_parse("324bb"); id = try_parse("3.4"); id = try_parse("+1"); id = try_parse("-134"); id = try_parse("255"); std::cout << id << std::endl; return 0; }
static int parse_terms(char *pmu, char *config, struct perf_event_attr *attr, int recur) { char *format = NULL; char *term; char *newl = strchr(config, '\n'); if (newl) *newl = 0; while ((term = strsep(&config, ",")) != NULL) { char name[30]; int n, val = 1; n = sscanf(term, "%30[^=]=%i", name, &val); if (n < 1) break; if (special_attr(name, val, attr)) continue; free(format); if (read_file(&format, "/sys/devices/%s/format/%s", pmu, name) < 0) { char *alias = NULL; if (recur == 0 && read_file(&alias, "/sys/devices/%s/events/%s", pmu, name) == 0) { if (parse_terms(pmu, alias, attr, 1) < 0) { free(alias); fprintf(stderr, "Cannot parse kernel event alias %s\n", name); break; } free(alias); continue; } fprintf(stderr, "Cannot parse qualifier %s\n", name); break; } bool ok = try_parse(format, "config:%d-%d", val, &attr->config) || try_parse(format, "config:%d", val, &attr->config) || try_parse(format, "config1:%d-%d", val, &attr->config1) || try_parse(format, "config1:%d", val, &attr->config1) || try_parse(format, "config2:%d-%d", val, &attr->config2) || try_parse(format, "config2:%d", val, &attr->config2); if (!ok) { fprintf(stderr, "Cannot parse kernel format %s: %s\n", name, format); break; } } free(format); if (term) return -1; return 0; }
returntype close_connection(omemstream& out, std::vector<std::string_view>& args) { if (args.size() != 1) { out << "[false,\"" << "Expected exactly one argument of type SCALAR." << "\"]"; return ret_arg_err; } // PARAM 1 int connection; if (!try_parse(args[0], connection)) { out << "[false,\"" << "First argument is not of type SCALAR. Expected connection identifier." << "\"]"; return ret_arg_err; } out << "[false,\"" << "NOT IMPLEMENTED." << "\"]"; return ret_not_implemented; }
template<typename T, typename char_t> T parse(const::std::basic_string<char_t>& s) { T result; try_parse(s, result); return result; }
bool parserParser::parse(const string& filename)/*{{{*/ { // open curr_fp = fopen(filename.c_str(), "r"); lineno = 1; this->filename = filename; // read for error recovery fseek(curr_fp, 0, SEEK_END); int f_size = ftell(curr_fp); fseek(curr_fp, 0, SEEK_SET); string compile_string; compile_string.resize(f_size); size_t ret = fread(&compile_string[0], 1, f_size, curr_fp); fseek(curr_fp, 0, SEEK_SET); // init parserCode::init(); string module_name; if (filename.rfind("/") > 0) { module_name = filename.substr( filename.rfind("/")+1 ); } if (filename.rfind(".") > 0) { module_name = module_name.substr( 0, module_name.rfind(".") ); } this->module_name = module_name; name_list_t* vs = g_parser->new_name_list(); vs->push_back("...argv"); parserCode::push_code_stack((char*)module_name.c_str(), vs); code_top->set_argv_on(); // enable argv // parse init(); try { int ret = yyparse(); if (ret != 0) { try_parse(compile_string); if (!g_parser->is_interactive() and !g_parser->is_eval()) { exit(0); } if (g_parser->n_tok > 0) { return false; } else { code_top->clear(); } } } catch(const char* cp) { print("[%s - %d]%s\n", filename.c_str(), lineno, cp); try_parse(compile_string); if (!g_parser->is_interactive() and !g_parser->is_eval()) { exit(0); } return false; } // make binary parserCode::pop_code_stack(); parserCode::Final(filename.c_str()); #ifdef _VM_DEBUG parserCode::dump_final(); #endif // clean up fclose(curr_fp); free_all(); return true; }
static void on_read(uv_stream_t* stream, ssize_t nread, uv_buf_t buf) { printf("on_read: <<%.*s\n>>", (int) nread, buf.base); if (nread < 0) { if (buf.base) free(buf.base); printf("uv_shutdown\n"); uv_shutdown_t* req = (uv_shutdown_t*) malloc(sizeof *req); uv_shutdown(req, stream, after_shutdown); return; } if (nread == 0) { /* Everything OK, but nothing read. */ free(buf.base); return; } if (stream->data == NULL) internal_error("stream->data is null in on_read"); Connection* connection = (Connection*) stream->data; if (connection->server != NULL && connection->server->serverType == WEBSOCK) { http_parser* parser = &connection->parser; printf("parsing as http\n"); int parsed = http_parser_execute(parser, &connection->server->parser_settings, buf.base, nread); if (parser->upgrade) { connection->state = WEBSOCK_DUPLEX_STATE; return; } if (HTTP_PARSER_ERRNO(parser) != HPE_OK) { printf("http parse error: [%s] %s\n", http_errno_name(HTTP_PARSER_ERRNO(parser)), http_errno_description(HTTP_PARSER_ERRNO(parser)) ); // handle parse error return; } if (parsed < nread) { printf("TODO: Handle second message?\n"); } } else { circa_string_append_len(&connection->incomingStr, buf.base, nread); try_parse(&connection->incomingStr, &connection->incomingMsgs); } free(buf.base); }