/** * @brief tokenize string into array of string by delimiters * @param str input string * @param delimiters delimiters to break the string according to * @return vector of strings */ std::vector<std::string> string_tokenize(const std::string &str, const std::string& delimiter) { std::string::size_type start (0); std::vector<std::string> tokens; std::string token; std::string::size_type end = str.find(delimiter); while ( end != std::string::npos ) { if ( end != start ) token = str.substr(start, end - start); else token.clear(); // trim spaces string_trim(token); if ( !token.empty() ) tokens.push_back(token); // next token start = end + delimiter.length(); end = str.find(delimiter, start ); } if ( start != (str.length() - 1) ) { // We have another token which is not delimited token = str.substr(start); tokens.push_back(token); } return tokens; }
/* * Render the layout with the file content passed as a partial with the key * "content". */ static void render_with_layout(FILE *in_fp, FILE *out_fp, struct layout *layouts, int num_layouts, ctache_data_t *file_data) { char *content = read_file_contents(in_fp); size_t content_len = strlen(content); ctache_data_t *content_data; content_data = ctache_data_create_string(content, content_len); ctache_data_hash_table_set(file_data, "content", content_data); ctache_data_t *layout_data = ctache_data_hash_table_get(file_data, LAYOUT); char *str = strdup(ctache_data_string_buffer(layout_data)); char *layout_name = string_trim(str); free(str); char *layout = get_layout_content(layouts, num_layouts, layout_name); if (layout == NULL) { fprintf(stderr, "ERROR: Layout not found: \"%s\"\n", layout_name); abort(); } size_t layout_len = strlen(layout); ctache_render_string(layout, layout_len, out_fp, file_data, ESCAPE_HTML, DELIM_BEGIN, DELIM_END); free(content); free(layout_name); }
int zsock_get_type(char *type) { type = string_trim(type); if (strcmp(type, "ZMQ_REQ") == 0) { return ZMQ_REQ; } else if (strcmp(type, "ZMQ_REP") == 0) { return ZMQ_REP; } else if (strcmp(type, "ZMQ_DEALER") == 0) { return ZMQ_DEALER; } else if (strcmp(type, "ZMQ_ROUTER") == 0) { return ZMQ_ROUTER; } else if (strcmp(type, "ZMQ_PUB") == 0) { return ZMQ_PUB; } else if (strcmp(type, "ZMQ_SUB") == 0) { return ZMQ_SUB; } else if (strcmp(type, "ZMQ_PUSH") == 0) { return ZMQ_PUSH; } else if (strcmp(type, "ZMQ_PULL") == 0) { return ZMQ_PULL; } else if (strcmp(type, "ZMQ_PAIR") == 0) { return ZMQ_PAIR; } else { return -1; } }
void showList(void) { FILE *pfile; pfile = fopen("/proc/partitions", "r"); if (pfile == NULL) { fprintf(stderr, "ERROR: unable to read /proc-filesystem\n"); exit(EXIT_FAILURE); } printf("DEVICES:\n" ); char buf[256]; char *dev; fgets(buf, sizeof(buf)/sizeof(buf[0]), pfile); /* dummy - first line/header */ while(fgets(buf, sizeof(buf)/sizeof(buf[0]), pfile)) { dev = strrchr(buf, ' '); if (dev == NULL) continue; dev = string_trim(dev); printf("\t%s\n", dev); free(dev); } fclose(pfile); }
static int command_delete_list (int argc, char *argv[], ProgEnv *env) { FILE *input; char line[256]; input = fopen (argv[0], "r"); if (!input) { fprintf (stderr, "delete-list: Cannot open input file '%s'\n", argv[0]); return 1; } while (fgets (line, sizeof line, input)) { char *p; p = string_trim (line); if ('\0' != *p) if (!sb_trie_delete (env->sb_trie, (const TrieChar *) p)) fprintf (stderr, "No entry '%s'. Not deleted.\n", p); } fclose (input); return 1; }
mulk_type_return_t read_metalink_list_from_text_file(const char *filename) { FILE *file; char buf[MAX_LINE_LENGTH]; mulk_type_return_t ret = MULK_RET_OK; if ((file = fopen(filename, "r")) == NULL) return MULK_RET_FILE_ERR; for (;;) { if (!fgets(buf, MAX_LINE_LENGTH, file)) break; if (feof(file)) break; string_trim(buf); /* skip empty lines and comments */ if (!strlen(buf) || *buf == '#') continue; MULK_NOTE((_("Add Metalink file to download coming from text file: %s\n"), buf)); if ((ret = mulk_add_new_metalink_file(buf)) != MULK_RET_OK) break; } fclose(file); return ret; }
String StringUtil::Trim(CStrRef input, TrimType type /* = TrimBoth */, CStrRef charlist /* = k_HPHP_TRIM_CHARLIST */) { if (input.empty()) return input; int len = input.size(); char *ret = string_trim(input.data(), len, charlist.data(), charlist.length(), type); return String(ret, len, AttachString); }
static int open_nist_file(sphinx_wave2feat_t *wtf, char const *infile, FILE **out_fh, int detect_endian) { char nist[7]; lineiter_t *li; FILE *fh; if ((fh = fopen(infile, "rb")) == NULL) { E_ERROR_SYSTEM("Failed to open %s", infile); return -1; } if (fread(&nist, 1, 7, fh) != 7) { E_ERROR_SYSTEM("Failed to read NIST header"); fclose(fh); return -1; } /* Is this actually a NIST file? */ if (0 != strncmp(nist, "NIST_1A", 7)) { fclose(fh); return FALSE; } /* Rewind, parse lines. */ fseek(fh, 0, SEEK_SET); for (li = lineiter_start(fh); li; li = lineiter_next(li)) { char **words; int nword; string_trim(li->buf, STRING_BOTH); if (strlen(li->buf) == 0) { lineiter_free(li); break; } nword = str2words(li->buf, NULL, 0); if (nword != 3) continue; words = (char **)ckd_calloc(nword, sizeof(*words)); str2words(li->buf, words, nword); if (0 == strcmp(words[0], "sample_rate")) { cmd_ln_set_float32_r(wtf->config, "-samprate", atof_c(words[2])); } if (0 == strcmp(words[0], "channel_count")) { cmd_ln_set_int32_r(wtf->config, "-nchans", atoi(words[2])); } if (detect_endian && 0 == strcmp(words[0], "sample_byte_format")) { cmd_ln_set_str_r(wtf->config, "-input_endian", (0 == strcmp(words[2], "10")) ? "big" : "little"); } ckd_free(words); } fseek(fh, 1024, SEEK_SET); if (out_fh) *out_fh = fh; else fclose(fh); return TRUE; }
//電卓関数モードで中間か逆ポーランドか選択可能 void calc(char* formula) { printf("\tCalc Exec : Start\n"); //エラーフラグのクリア error_flag = 0; #ifdef _Debug_enable //デバッグ用 printf("\t\tDebug : calc -> mode set search start\n"); #endif char tmp_buffer[BUFFER_LENGTH]; my_strcpy(tmp_buffer,formula); string_trim(tmp_buffer); //コマンド部分に関する記述 if(string_match(tmp_buffer,"set")) { printf("\tCalc Exec : detected \"set\"\n"); if(string_match(tmp_buffer,"mode=IN")) { mode = IN_MODE; printf("\tCalc Exec : set Calc mode = IN_MODE\n"); return; } else if(string_match(tmp_buffer,"mode=RPN")) { mode = RPN_MODE; printf("\tCalc Exec : set Calc mode = RPN_MODE\n"); return; } else { printf("\tCalc Exec : Please retry set mode.\n"); return; } } else if(string_match(tmp_buffer,"quit")||string_match(tmp_buffer,"exit")) { mode = EXIT_MODE; return; } #ifdef _Debug_enable //デバッグ用 printf("\t\tDebug : calc -> mode = %d, fromula = \"%s\"\n",mode,formula); #endif //計算関数呼び出し switch(mode) { case RPN_MODE : rpn_calc(formula); break; case IN_MODE : in_calc(formula); break; } }
DynamicParMetis::EdgeSource DynamicParMetis::stringToEdgeSource( std::string s ) { string_to_upper( s ); string_trim( s ); if( s == "EDGES_FROM_FOREST" ) return PARMETIS_EDGES_FROM_FOREST; else if( s == "EDGES_FROM_EDGE_WEIGHTS" ) return PARMETIS_EDGES_FROM_EDGE_WEIGHTS; else WALBERLA_ABORT( "Illegal ParMetis weights usage specified (" << s << ")! Valid choices are: \"EDGES_FROM_FOREST\" or \"EDGES_FROM_EDGE_WEIGHTS\"" ); }
int main(void) { char *text = " Text to trim...with spaces... "; char *trim = string_trim(text); printf ( "text: >%s<\n", text ); printf ( "trim: >%s<\n", trim ); free(trim); exit (EXIT_SUCCESS); }
//デバッグ用 //中間モードの関数(実際は逆ポーランドに変換) void in_calc(char* formula) { //中間置方だと空白があった場合に支障を来すことがあるので空白を削除 printf("\tIN Calc Exec : formula = \"%s\"\n",formula); string_trim(formula); printf("\tIN Calc : trim formula \"%s\"\n",formula); //RPNの形式へ変換 convert_in_formula_to_rpn_formula(formula); //RPNで計算実行 rpn_calc(formula); }
/* read only long option */ mulk_type_return_t read_option_from_text_file(const char *filename) { FILE *file; char buf[MAX_LINE_LENGTH], *option, *value; int option_index = 0; mulk_type_return_t ret = MULK_RET_OK; if ((file = fopen(filename, "r")) == NULL) return MULK_RET_FILE_ERR; for (;;) { if (!fgets(buf, MAX_LINE_LENGTH, file)) break; if (feof(file)) break; string_trim(buf); /* skip empty lines and comments */ if (!strlen(buf) || *buf == '#') continue; option = buf; if ((value = strchr(buf, '=')) != NULL) { *value = 0; value++; if (!*value) value = NULL; } else value = NULL; MULK_NOTE((_("Add option coming from text file: %s%s%s\n"), option, value ? "=" : "", value ? value : "")); if ((ret = mulk_find_long_option(option, &option_index)) != MULK_RET_OK) break; if (!strcmp(option, OPT_OPTION_FILE)) { fprintf(stderr, _("\nERROR: option not valid inside an option file\n\n")); ret = MULK_RET_OPTION_ERR; break; } if ((ret = mulk_set_option(option_index, value)) != MULK_RET_OK) break; } fclose(file); return ret; }
static void test_string_trim() { char *string; string = string_duplicate("Hola"); string_trim(&string); CU_ASSERT_STRING_EQUAL(string, "Hola"); free(string); string = string_duplicate(" Hola"); string_trim(&string); CU_ASSERT_STRING_EQUAL(string, "Hola"); free(string); string = string_duplicate("Hola "); string_trim(&string); CU_ASSERT_STRING_EQUAL(string, "Hola"); free(string); string = string_duplicate(" Hola "); string_trim(&string); CU_ASSERT_STRING_EQUAL(string, "Hola"); free(string); }
lineiter_t * lineiter_next(lineiter_t *li) { if (!li->clean) return lineiter_next_plain(li); for (li = lineiter_next_plain(li); li; li = lineiter_next_plain(li)) { if (li->buf && li->buf[0] != '#') { li->buf = string_trim(li->buf, STRING_BOTH); break; } } return li; }
DynamicParMetis::WeightsToUse DynamicParMetis::stringToWeightsToUse( std::string s ) { string_to_upper( s ); string_trim( s ); if( s == "NO_WEIGHTS" ) return PARMETIS_NO_WEIGHTS; else if( s == "EDGE_WEIGHTS" ) return PARMETIS_EDGE_WEIGHTS; else if( s == "VERTEX_WEIGHTS" ) return PARMETIS_VERTEX_WEIGHTS; else if( s == "BOTH_WEIGHTS" ) return PARMETIS_BOTH_WEIGHTS; else WALBERLA_ABORT( "Illegal ParMetis weights usage specified (" << s << ")! Valid choices are: \"NO_WEIGHTS\", \"EDGE_WEIGHTS\", \"VERTEX_WEIGHTS\", or \"BOTH_WEIGHTS\"." ); }
static bool split_argv_string(const char *argv[SIZEOF_ARG], int *argc, char *cmd, bool remove_quotes) { while (*cmd && *argc < SIZEOF_ARG) { char *arg = parse_arg(&cmd, remove_quotes); if (!arg) break; argv[(*argc)++] = arg; cmd = string_trim(cmd); } if (*argc < SIZEOF_ARG) argv[*argc] = NULL; return *argc < SIZEOF_ARG; }
char *make_pgpname (char *dir) { int n; char pgpname[MAXLINE]; if (dir == NULL) return NULL; if (!(n = string_trim (dir, pgpname))) return NULL; if (n > (MAXLINE - 7)) return NULL; strcat (pgpname, "/.pgp/"); return strdup (pgpname); }
DynamicParMetis::Algorithm DynamicParMetis::stringToAlgorithm( std::string s ) { string_to_upper( s ); string_trim( s ); if( s == "PART_GEOM_KWAY" ) return PARMETIS_PART_GEOM_KWAY; else if( s == "PART_GEOM" ) return PARMETIS_PART_GEOM; else if( s == "PART_KWAY" ) return PARMETIS_PART_KWAY; else if( s == "ADAPTIVE_REPART" ) return PARMETIS_ADAPTIVE_REPART; else if( s == "REFINE_KWAY" ) return PARMETIS_REFINE_KWAY; else WALBERLA_ABORT( "Illegal ParMetis algorithm specified (" << s << ")! Valid choices are: \"PART_GEOM_KWAY\", \"PART_GEOM\", \"PART_KWAY\", \"ADAPTIVE_REPART\", or \"REFINE_KWAY\"." ); }
int main(int argc, char *argv[]) { sbthread_t *threads[10]; cmd_ln_t *config; int i; E_INFO("Processing chan3.raw in 10 threads\n"); if ((config = cmd_ln_parse_r(NULL, fe_args, 0, NULL, FALSE)) == NULL) return -1; err_set_callback(err_threaded_cb, NULL); pthread_key_create(&logfp_index, NULL); pthread_setspecific(logfp_index, (void*)stderr); for (i = 0; i < 10; ++i) { config = cmd_ln_retain(config); threads[i] = sbthread_start(config, process, (void *)(long)i); } for (i = 0; i < 10; ++i) { int rv; rv = sbthread_wait(threads[i]); E_INFO("Thread %d exited with status %d\n", i, rv); sbthread_free(threads[i]); } /* Now check to make sure they all created logfiles with the * correct contents. */ for (i = 0; i < 10; ++i) { char logfile[16], line[256]; FILE *logfh; sprintf(logfile, "%03d.log", i); TEST_ASSERT(logfh = fopen(logfile, "r")); while (fgets(line, sizeof(line), logfh)) { string_trim(line, STRING_BOTH); printf("%s: |%s|\n", logfile, line); /* total number of frames in audio file is 1436, but there are only 1290 voiced */ TEST_EQUAL(0, strcmp(line, "INFO: test_tls_log.c(61): nfr = 1290")); } fclose(logfh); } cmd_ln_free_r(config); return 0; }
lineiter_t * lineiter_start_clean(FILE *fh) { lineiter_t *li; li = lineiter_start(fh); if (li == NULL) return li; li->clean = TRUE; if (li->buf && li->buf[0] == '#') { li = lineiter_next(li); } else { string_trim(li->buf, STRING_BOTH); } return li; }
bool cOverworld_Manager::New(std::string name) { string_trim(name, ' '); // no name given if (name.empty()) { return 0; } // name already exists if (Get_from_Path(name)) { return 0; } cOverworld* overworld = new cOverworld(); overworld->New(name); objects.push_back(overworld); return 1; }
char *normalize_string_utf8(char *str, uint64_t options) { int utf8proc_options = UTF8PROC_OPTIONS_BASE | UTF8PROC_IGNORE | UTF8PROC_NLF2LF | UTF8PROC_STRIPCC; uint8_t *utf8proc_normalized = NULL; bool have_utf8proc_options = false; if (options & NORMALIZE_STRING_TRIM) { string_trim(str); } if (options & NORMALIZE_STRING_DECOMPOSE) { have_utf8proc_options = true; utf8proc_options |= UTF8PROC_OPTIONS_NFD; } if (options & NORMALIZE_STRING_STRIP_ACCENTS) { have_utf8proc_options = true; utf8proc_options |= UTF8PROC_OPTIONS_STRIP_ACCENTS; } if (options & NORMALIZE_STRING_LOWERCASE) { have_utf8proc_options = true; utf8proc_options |= UTF8PROC_OPTIONS_LOWERCASE; } char *normalized = NULL; if (have_utf8proc_options) { utf8proc_map((uint8_t *)str, 0, &utf8proc_normalized, utf8proc_options); normalized = (char *)utf8proc_normalized; str = normalized; } if (options & NORMALIZE_STRING_REPLACE_HYPHENS) { string_replace(str, '-', ' '); normalized = str; } return normalized; }
static int command_add_list (int argc, char *argv[], ProgEnv *env) { FILE *input; char line[256]; input = fopen (argv[0], "r"); if (!input) { fprintf (stderr, "add-list: Cannot open input file '%s'\n", argv[0]); return 1; } while (fgets (line, sizeof line, input)) { char *key, *data; TrieData data_val; key = string_trim (line); if ('\0' != *key) { /* find key boundary */ for (data = key; *data && !strchr ("\t,", *data); ++data) ; /* mark key ending and find data begin */ if ('\0' != *data) { *data++ = '\0'; while (isspace (*data)) ++data; } /* decode data */ data_val = ('\0' != *data) ? atoi (data) : TRIE_DATA_ERROR; /* store the key */ if (!sb_trie_store (env->sb_trie, (const TrieChar *) key, data_val)) fprintf (stderr, "Failed to add key '%s' with data %d.\n", key, data_val); } } fclose (input); return 1; }
static std::string getMIMETypeFromHttpHeader(const std::string& http_header) { std::string mimetype; size_t pos = http_header.find("\nContent-Type: "); if (pos != std::string::npos) { pos += 15; size_t epos = http_header.find("\n", pos); if (epos != std::string::npos) { std::string ct = http_header.substr(pos, epos - pos); size_t charsetpos = ct.find("charset="); if (charsetpos != std::string::npos) { mimetype = ct.substr(0, charsetpos); size_t split_pos = mimetype.find(';'); if (split_pos != std::string::npos) { mimetype = mimetype.substr(0, split_pos); } string_trim(mimetype); std::transform(mimetype.begin(), mimetype.end(), mimetype.begin(), (int(*)(int))tolower); } } } return mimetype; }
static void ngrams_raw_read_order(ngram_raw_t ** raw_ngrams, lineiter_t ** li, hash_table_t * wid, logmath_t * lmath, uint32 count, int order, int order_max) { char expected_header[20]; uint32 i; sprintf(expected_header, "\\%d-grams:", order); while ((*li = lineiter_next(*li))) { string_trim((*li)->buf, STRING_BOTH); if (strcmp((*li)->buf, expected_header) == 0) break; } *raw_ngrams = (ngram_raw_t *) ckd_calloc(count, sizeof(ngram_raw_t)); for (i = 0; i < count; i++) { read_ngram_instance(li, wid, lmath, order, order_max, &((*raw_ngrams)[i])); } //sort raw ngrams that was read ngram_comparator(NULL, &order); //setting up order in comparator qsort(*raw_ngrams, count, sizeof(ngram_raw_t), &ngram_comparator); }
char *string_trim_spaces(char *s) { return string_trim(s, isspace); }
pfc::string8 provider_lyricsplugin::lookup_one(unsigned p_index, const metadb_handle_ptr & p_meta, threaded_process_status & p_status, abort_callback & p_abort) { // Regular Expression Class CRegexpT<char> regexp; MatchResult match; // Buffer pfc::string8 buff; try { // Init fetcher curl_wrapper_simple fetcher(&m_config_item); // Clear buff buff.reset(); const metadb_handle_ptr & p = p_meta; if (p.is_empty()) { return ""; } pfc::string8_fast artist, title; static_api_ptr_t<titleformat_compiler> compiler; service_ptr_t<titleformat_object> script; file_info_impl info; p->get_info(info); // Get count of artists t_size count = info.meta_get_count_by_name("artist"); // Get TITLE compiler->compile_safe(script, "[%title%]"); p->format_title(NULL, title, script, NULL); bool found = false; // Iterate through all artists listed for (int j = 0; j < count && !found; j++) { // Get Artist artist = info.meta_get("artist", j); // Fetching from HTTP // Set HTTP Address pfc::string8_fast url("http://www.squirrelscript.net/mediamonkey/Lyricator/lyrics.php?artist="); // URL = http://www.squirrelscript.net/mediamonkey/Lyricator/lyrics.php?artist=<Artist>&title=<Title> url += fetcher.quote(artist); url += "&title="; url += fetcher.quote(title); // Get it now try { fetcher.fetch(url, buff); } catch (pfc::exception & e) { console_error(e.what()); continue; } catch (...) { continue; } const char * regex_lyricbox = "<div\\s+id\\s*?=\\s*?\"lyrics\"\\s*?>[\\r\\n]*(.*?)[\\r\\n]*</div>"; // expression for extract lyrics regexp.Compile(regex_lyricbox, SINGLELINE); // match MatchResult result = regexp.Match(buff.get_ptr()); // Get Group if (result.IsMatched()) { int nStart = result.GetGroupStart(1); int nEnd = result.GetGroupEnd(1); int index; pfc::string8_fast lyric(buff.get_ptr() + nStart, nEnd - nStart); convert_html_to_plain(lyric); index = lyric.find_first("www.tunerankings.com"); if (index == 0) { continue; } else if (index != -1) { lyric.remove_chars(index, 20); } if (string_trim(lyric).get_length() > 0) { return lyric; } } } } catch (pfc::exception & e) { console_error(e.what()); return ""; } catch (...) { return ""; } return ""; }
int main(int argc, char *argv[]) { if (argc < 2) return 1; if (!strcmp(argv[1], "string_join")) { char *foo = string_join("bar", "baz", "quux", NULL); if (strcmp(foo, "barbazquux") != 0) { printf("%s != barbazquux\n", foo); return 1; } foo = string_join("hello", NULL); if (strcmp(foo, "hello") != 0) { printf("%s != hello\n", foo); return 1; } return 0; } else if (!strcmp(argv[1], "fread_line")) { FILE *fp = fopen(TESTDATADIR "/_fread_line.txt", "r"); char *line; size_t len; if (fp == NULL) { perror("Failed to open " TESTDATADIR "/_fread_line.txt"); return 1; } line = fread_line(fp, &len); printf("len = %d orig = %d\n", len, strlen("Hello world!\n")); if (strcmp(line, "Hello world!\n") != 0) { printf("'%s' != 'Hello world!\\n'\n", line); return 1; } ckd_free(line); line = fread_line(fp, &len); /* A line of exactly 127 characters. */ printf("len = %d orig = %d\n", len, strlen("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456\n")); if (strcmp(line, "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456\n") != 0) { printf("'%s' != '123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456\\n'\n", line); return 1; } ckd_free(line); /* A very long line. */ line = fread_line(fp, &len); printf("len = %d orig = %d\n", len, strlen("All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. \n")); if (strcmp(line, "All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. \n") != 0) { printf("'%s' != 'All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. All work and no play makes Jack a very dull boy. \\n'\n", line); return 1; } ckd_free(line); line = fread_line(fp, &len); if (line != NULL) { printf("%p != NULL\n", line); return 1; } } else if (!strcmp(argv[1], "string_trim")) { char *foo = ckd_salloc("\t foo bar baz \n"); string_trim(foo, STRING_BOTH); if (strcmp(foo, "foo bar baz") != 0) { printf("'%s' != 'foo bar baz'\n", foo); return 1; } string_trim(foo, STRING_BOTH); if (strcmp(foo, "foo bar baz") != 0) { printf("'%s' != 'foo bar baz'\n", foo); return 1; } strcpy(foo, "foo\nbar\n\n"); string_trim(foo, STRING_END); if (strcmp(foo, "foo\nbar") != 0) { printf("'%s' != 'foo\\nbar'\n", foo); return 1; } strcpy(foo, " \t \t foobar\n"); string_trim(foo, STRING_START); if (strcmp(foo, "foobar\n") != 0) { printf("'%s' != 'foobar\\n'\n", foo); return 1; } } else if (!strcmp(argv[1], "str2words")) { char *line = ckd_salloc(" foo bar baz argh"); char **words; int n; n = str2words(line, NULL, 0); if (n != 4) { printf("%d != 4\n", n); return 1; } words = ckd_calloc(n, sizeof(*words)); n = str2words(line, words, n); if (n != 4) { printf("%d != 4\n", n); return 1; } if (strcmp(words[0], "foo") != 0 || strcmp(words[1], "bar") != 0 || strcmp(words[2], "baz") != 0 || strcmp(words[3], "argh") != 0) { printf("%s, %s, %s, %s != foo, bar, baz, argh\n", words[0], words[1], words[2], words[3]); return 1; } return 0; } else if (!strcmp(argv[1], "nextword")) { char *line = ckd_salloc(" \tfoo bar\nbaz argh"); char *word; const char *delim = " \t\n"; char delimfound; int n; n = nextword(line, delim, &word, &delimfound); if (strcmp(word, "foo") != 0) { printf("%s != foo\n", word); return 1; } if (delimfound != ' ') { printf("didn't find ' '\n"); return 1; } word[n] = delimfound; line = word + n; n = nextword(line, delim, &word, &delimfound); if (strcmp(word, "bar") != 0) { printf("%s != bar\n", word); return 1; } if (delimfound != '\n') { printf("didn't find '\\n'\n"); return 1; } word[n] = delimfound; line = word + n; n = nextword(line, delim, &word, &delimfound); if (strcmp(word, "baz") != 0) { printf("%s != baz\n", word); return 1; } if (delimfound != ' ') { printf("didn't find ' '\n"); return 1; } word[n] = delimfound; line = word + n; n = nextword(line, delim, &word, &delimfound); if (strcmp(word, "argh") != 0) { printf("%s != argh\n", word); return 1; } if (delimfound != '\0') { printf("didn't find NUL\n"); return 1; } word[n] = delimfound; line = word + n; n = nextword(line, delim, &word, &delimfound); if (n != -1) { printf("didn't get -1 at end of string\n"); } line = ckd_salloc("FOO!"); n = nextword(line, delim, &word, &delimfound); if (strcmp(word, "FOO!") != 0) { printf("%s != FOO!\n", word); return 1; } if (delimfound != '\0') { printf("didn't find NUL\n"); return 1; } return 0; } return 0; }
void alldone(int estatus) { char *ptr; #if defined(WIN32) || defined(WIN64) struct _stat x; #else struct stat x; #endif if (preprocessonly == 0 && strlen(out1) > 0) { (void) unlink((const char *) out1); } (void) unlink(TMP_FILE1); (void) unlink(TMP_FILE2); if (!buzzed && seedy && !analyze && !export_ast && !s_trail && !preprocessonly && depth > 0) { printf("seed used: %d\n", SeedUsed); } if (!buzzed && xspin && (analyze || s_trail)) { if (estatus) { printf("spin: %d error(s) - aborting\n", estatus); } else { printf("Exit-Status 0\n"); } } if (buzzed && replay && !has_code && !estatus) { extern QH *qh; QH *j; int i; char *tmp = (char *) emalloc(strlen("spin -t") + strlen(pan_runtime) + strlen(Fname->name) + 8); pan_runtime = (char *) emalloc(2048); /* more than enough */ sprintf(pan_runtime, "-n%d ", SeedUsed); if (jumpsteps) { sprintf(&pan_runtime[strlen(pan_runtime)], "-j%d ", jumpsteps); } if (trailfilename) { sprintf(&pan_runtime[strlen(pan_runtime)], "-k%s ", *trailfilename); } if (cutoff) { sprintf(&pan_runtime[strlen(pan_runtime)], "-u%d ", cutoff); } for (i = 1; i <= PreCnt; i++) { strcat(pan_runtime, PreArg[i]); strcat(pan_runtime, " "); } for (j = qh; j; j = j->nxt) { sprintf(&pan_runtime[strlen(pan_runtime)], "-q%d ", j->n); } if (strcmp(PreProc, CPP) != 0) { sprintf(&pan_runtime[strlen(pan_runtime)], "\"-P%s\" ", PreProc); } /* -oN options 1..5 are ignored in simulations */ if (old_priority_rules) strcat(pan_runtime, "-o6 "); if (!implied_semis) strcat(pan_runtime, "-o7 "); if (no_print) strcat(pan_runtime, "-b "); if (no_wrapup) strcat(pan_runtime, "-B "); if (columns == 1) strcat(pan_runtime, "-c "); if (columns == 2) strcat(pan_runtime, "-M "); if (seedy == 1) strcat(pan_runtime, "-h "); if (like_java == 1) strcat(pan_runtime, "-J "); if (old_scope_rules) strcat(pan_runtime, "-O "); if (notabs) strcat(pan_runtime, "-T "); if (verbose&1) strcat(pan_runtime, "-g "); if (verbose&2) strcat(pan_runtime, "-l "); if (verbose&4) strcat(pan_runtime, "-p "); if (verbose&8) strcat(pan_runtime, "-r "); if (verbose&16) strcat(pan_runtime, "-s "); if (verbose&32) strcat(pan_runtime, "-v "); if (verbose&64) strcat(pan_runtime, "-w "); if (m_loss) strcat(pan_runtime, "-m "); sprintf(tmp, "spin -t %s %s", pan_runtime, Fname->name); estatus = e_system(1, tmp); /* replay */ exit(estatus); /* replay without c_code */ } if (buzzed && (!replay || has_code) && !estatus) { char *tmp, *tmp2 = NULL, *P_X; char *C_X = (buzzed == 2) ? "-O" : ""; if (replay && strlen(pan_comptime) == 0) { #if defined(WIN32) || defined(WIN64) P_X = "pan"; #else P_X = "./pan"; #endif if (stat(P_X, (struct stat *)&x) < 0) { goto recompile; /* no executable pan for replay */ } tmp = (char *) emalloc(8 + strlen(P_X) + strlen(pan_runtime) +4); /* the final +4 is too allow adding " &" in some cases */ sprintf(tmp, "%s %s", P_X, pan_runtime); goto runit; } #if defined(WIN32) || defined(WIN64) P_X = "-o pan pan.c && pan"; #else P_X = "-o pan pan.c && ./pan"; #endif /* swarm and biterate randomization additions */ if (!replay && itsr) /* iterative search refinement */ { if (!strstr(pan_comptime, "-DBITSTATE")) { add_comptime("-DBITSTATE"); } if (!strstr(pan_comptime, "-DPUTPID")) { add_comptime("-DPUTPID"); } if (!strstr(pan_comptime, "-DT_RAND") && !strstr(pan_comptime, "-DT_REVERSE")) { add_runtime("-T0 "); /* controls t_reverse */ } if (!strstr(pan_runtime, "-P") /* runtime flag */ || pan_runtime[2] < '0' || pan_runtime[2] > '1') /* no -P0 or -P1 */ { add_runtime("-P0 "); /* controls p_reverse */ } if (!strstr(pan_runtime, "-w")) { add_runtime("-w18 "); /* -w18 = 256K */ } else { char nv[32]; int x; x = omit_str(pan_runtime, "-w"); if (x >= 0) { sprintf(nv, "-w%d ", x); add_runtime(nv); /* added spaces */ } } if (!strstr(pan_runtime, "-h")) { add_runtime("-h0 "); /* 0..499 */ /* leave 2 spaces for increments up to -h499 */ } else if (!strstr(pan_runtime, "-hash")) { char nv[32]; int x; x = omit_str(pan_runtime, "-h"); if (x >= 0) { sprintf(nv, "-h%d ", x%500); add_runtime(nv); /* added spaces */ } } if (!strstr(pan_runtime, "-k")) { add_runtime("-k1 "); /* 1..3 */ } else { char nv[32]; int x; x = omit_str(pan_runtime, "-k"); if (x >= 0) { sprintf(nv, "-k%d ", x%4); add_runtime(nv); /* added spaces */ } } if (strstr(pan_runtime, "-p_rotate")) { char nv[32]; int x; x = omit_str(pan_runtime, "-p_rotate"); if (x < 0) { x = 0; } sprintf(nv, "-p_rotate%d ", x%256); add_runtime(nv); /* added spaces */ } else if (strstr(pan_runtime, "-p_permute") == 0) { add_runtime("-p_rotate0 "); } if (strstr(pan_runtime, "-RS")) { (void) omit_str(pan_runtime, "-RS"); } /* need room for at least 10 digits */ add_runtime("-RS1234567890 "); change_rs(pan_runtime); } recompile: if (strstr(PreProc, "cpp")) /* unix/linux */ { strcpy(PreProc, "gcc"); /* need compiler */ } else if ((tmp = strstr(PreProc, "-E")) != NULL) { *tmp = '\0'; /* strip preprocessing flags */ } final_fiddle(); tmp = (char *) emalloc(8 + /* account for alignment */ strlen(PreProc) + strlen(C_X) + strlen(pan_comptime) + strlen(P_X) + strlen(pan_runtime) + strlen(" -p_reverse & ")); tmp2 = tmp; /* P_X ends with " && ./pan " */ sprintf(tmp, "%s %s %s %s %s", PreProc, C_X, pan_comptime, P_X, pan_runtime); if (!replay) { if (itsr < 0) /* swarm only */ { strcat(tmp, " &"); /* after ./pan */ itsr = -itsr; /* now same as biterate */ } /* do compilation first * split cc command from run command * leave cc in tmp, and set tmp2 to run */ if ((ptr = strstr(tmp, " && ")) != NULL) { tmp2 = ptr + 4; /* first run */ *ptr = '\0'; } } if (has_ltl) { (void) unlink("_spin_nvr.tmp"); } #ifdef PC /* make sure that if compilation fails we do not continue */ (void) unlink("./pan.exe"); #else (void) unlink("./pan"); #endif runit: estatus = e_system(1, tmp); /* compile */ if (replay || estatus < 0) { goto skipahead; } /* !replay */ if (itsr == 0) /* single run */ { estatus = e_system(1, tmp2); } else if (itsr > 0) /* iterative search refinement */ { int is_swarm = 0; if (tmp2 != tmp) /* swarm: did only compilation so far */ { tmp = tmp2; /* now we point to the run command */ estatus = e_system(1, tmp); /* first run */ } itsr--; /* count down */ /* the following are added back randomly later */ (void) omit_str(tmp, "-p_reverse"); /* replaced by spaces */ (void) omit_str(tmp, "-p_normal"); if (strstr(tmp, " &") != NULL) { (void) omit_str(tmp, " &"); is_swarm = 1; } /* increase -w every itsr_n-th run */ if ((itsr_n > 0 && (itsr == 0 || (itsr%itsr_n) != 0)) || (change_param(tmp, "-w", 36) >= 0)) /* max 4G bit statespace */ { (void) change_param(tmp, "-h", 500); /* hash function 0.499 */ (void) change_param(tmp, "-p_rotate", 256); /* if defined */ (void) change_param(tmp, "-k", 4); /* nr bits per state 0->1,1,2,3 */ (void) change_param(tmp, "-T", 2); /* with or without t_reverse*/ (void) change_param(tmp, "-P", 2); /* -P 0..1 != p_reverse */ change_rs(tmp); /* change random seed */ string_trim(tmp); if (rand()%5 == 0) /* 20% of all runs */ { strcat(tmp, " -p_reverse "); /* at end, so this overrides -p_rotateN, if there */ /* but -P0..1 disable this in 50% of the cases */ /* so its really activated in 10% of all runs */ } else if (rand()%6 == 0) /* override p_rotate and p_reverse */ { strcat(tmp, " -p_normal "); } if (is_swarm) { strcat(tmp, " &"); } goto runit; } } skipahead: (void) unlink("pan.b"); (void) unlink("pan.c"); (void) unlink("pan.h"); (void) unlink("pan.m"); (void) unlink("pan.p"); (void) unlink("pan.t"); } exit(estatus); }