std::string getOSXSystemLang() { // Get the user's language list (in order of preference) CFArrayRef langs = CFLocaleCopyPreferredLanguages(); if( CFArrayGetCount( langs ) == 0 ) { return "en_US"; } const char *lang_code_raw = CFStringGetCStringPtr( ( CFStringRef )CFArrayGetValueAtIndex( langs, 0 ), kCFStringEncodingUTF8 ); if( !lang_code_raw ) { return "en_US"; } // Convert to the underscore format expected by gettext std::string lang_code( lang_code_raw ); std::replace( lang_code.begin(), lang_code.end(), '-', '_' ); /** * Handle special case for simplified/traditional Chinese. Simplified/Traditional * is actually denoted by the region code in older iterations of the * language codes, whereas now (at least on OS X) region is distinct. * That is, CDDA expects 'zh_CN' but OS X might give 'zh-Hans-CN'. */ if( string_starts_with( lang_code, "zh_Hans" ) ) { return "zh_CN"; } else if( string_starts_with( lang_code, "zh_Hant" ) ) { return "zh_TW"; } return isValidLanguage( lang_code ) ? lang_code : "en_US"; }
static void test_string_starts_with() { CU_ASSERT_TRUE(string_starts_with("#Comentario", "#")); CU_ASSERT_TRUE(string_starts_with("Comentario", "Comen")); CU_ASSERT_FALSE(string_starts_with("Comentario", "comen")); CU_ASSERT_FALSE(string_starts_with("Comentario", "lala")); CU_ASSERT_FALSE(string_starts_with("", "#")); }
/* * @NAME: config_create * @DESC: Crea y devuelve un puntero a una estructura t_config * @PARAMS: * path - path del archivo de configuracion */ t_config *config_create(char *path) { t_config *config = malloc(sizeof(t_config)); config->path = strdup(path); config->properties = dictionary_create(); struct stat stat_file; stat(path, &stat_file); FILE* file = NULL; file = fopen(path, "r"); if (file != NULL) { char* buffer = calloc(1, stat_file.st_size + 1); fread(buffer, stat_file.st_size, 1, file); char** lines = string_split(buffer, "\n"); void add_cofiguration(char *line) { if (!string_starts_with(line, "#")) { char** keyAndValue = string_split(line, "="); dictionary_put(config->properties, keyAndValue[0], keyAndValue[1]); free(keyAndValue[0]); free(keyAndValue); } } string_iterate_lines(lines, add_cofiguration); string_iterate_lines(lines, (void*) free); free(lines); free(buffer); fclose(file); } else {
int main(int argc, char **argv) { char *arg; char *address = NULL; char *language = NULL; bool use_json = false; string_array *languages = NULL; for (int i = 1; i < argc; i++) { arg = argv[i]; if (string_equals(arg, "-h") || string_equals(arg, "--help")) { printf(LIBPOSTAL_USAGE); exit(EXIT_SUCCESS); } else if (string_equals(arg, "--json")) { use_json = true; } else if (address == NULL) { address = arg; } else if (!string_starts_with(arg, "-")) { if (languages == NULL) { languages = string_array_new(); } string_array_push(languages, arg); } } if (address == NULL && (!use_json || isatty(fileno(stdin)))) { log_error(LIBPOSTAL_USAGE); exit(EXIT_FAILURE); } if (!libpostal_setup() || (languages == NULL && !libpostal_setup_language_classifier())) { exit(EXIT_FAILURE); } normalize_options_t options = get_libpostal_default_options(); if (languages != NULL) { options.languages = languages->a; options.num_languages = languages->n; } if (address == NULL) { char *line; while ((line = file_getline(stdin)) != NULL) { print_output(line, options, use_json); free(line); } } else { print_output(address, options, use_json); } if (languages != NULL) { string_array_destroy(languages); } libpostal_teardown(); libpostal_teardown_language_classifier(); }
static void test_string_starts_with() { TEST; if(string_starts_with("Hello world", "Hello") != 1) { FAILURE("Test case failed!\n"); } TEST; if(string_starts_with("Hello world", "Hello") != 1) { FAILURE("Test case failed!\n"); } TEST; if(string_starts_with(" Hello world", " Hello") != 1) { FAILURE("Test case failed!\n"); } }
// ---------------------------------------------------------------- static char * test_starts_or_ends_with() { mu_assert_lf(string_starts_with("abcde", "")); mu_assert_lf(string_starts_with("abcde", "a")); mu_assert_lf(string_starts_with("abcde", "abcd")); mu_assert_lf(string_starts_with("abcde", "abcde")); mu_assert_lf(!string_starts_with("abcde", "abcdef")); mu_assert_lf(string_ends_with("abcde", "", NULL)); mu_assert_lf(string_ends_with("abcde", "e", NULL)); mu_assert_lf(string_ends_with("abcde", "de", NULL)); mu_assert_lf(string_ends_with("abcde", "abcde", NULL)); mu_assert_lf(!string_ends_with("abcde", "0abcde", NULL)); int len = -1; mu_assert_lf(!string_ends_with("abcde", "0abcde", &len)); mu_assert_lf(len == 5); return 0; }
int lengthVecConfig(char * value){ int cont=1; int i; for (i=1;i< strlen(value);i++) { if (string_starts_with(string_substring_from(value,i),",")) cont++; } return cont; }
// This function parses the src_string for the key // To simplify this function, key has to have the = sign in it // for example: html_GET("SSID="); uint8_t html_GET(uint8_t * destination, uint8_t * src, uint8_t * key) { uint8_t * value = 0; if(!src || !key) return FAILURE; uint8_t * cur_ptr = src; // Here we search for the existence of the key in the string while(*cur_ptr != 0) { if(string_starts_with(key,cur_ptr)) { value = &cur_ptr[strlen((char *)key)]; break; } cur_ptr = &cur_ptr[1]; } if(!value) return FAILURE; // With the key found, we must terminate the string cur_ptr = value; uint8_t temp_character = 0; while(cur_ptr) { if(*cur_ptr == '?') break; if(*cur_ptr == 0) break; if(*cur_ptr == '&') break; if(*cur_ptr == ' ') break; cur_ptr++; } temp_character = *cur_ptr; *cur_ptr = 0; if(strlen((char *)value) == 0) return FAILURE; if(destination) { //+1 for null plug if(strlen((char *)value)+1 < MAX_VARCHAR_LENGTH) memcpy(destination, value, strlen((char *)value)+1); else return FAILURE; } *cur_ptr = temp_character; return SUCCESS; }
void test_string() { constexpr size_t NUM_STRINGS = 1024; std::vector<std::string> strings(NUM_STRINGS); std::vector<grnxx::String> refs(NUM_STRINGS); std::vector<grnxx::String> bodies(NUM_STRINGS); for (size_t i = 0; i < NUM_STRINGS; ++i) { std::stringstream stream; stream << i; strings[i] = stream.str(); refs[i] = grnxx::String(strings[i].data(), strings[i].size()); assert(refs[i].is_reference()); bodies[i].assign(refs[i]); assert(bodies[i].is_instance()); } for (size_t i = 0; i < NUM_STRINGS; ++i) { assert(bodies[i].size() == static_cast<size_t>(strings[i].size())); for (size_t j = 0; j < bodies[i].size(); ++j) { assert(bodies[i][j] == strings[i][j]); } for (size_t j = 0; j < NUM_STRINGS; ++j) { assert((bodies[i] == bodies[j]) == (strings[i] == strings[j])); assert((bodies[i] != bodies[j]) == (strings[i] != strings[j])); assert((bodies[i] < bodies[j]) == (strings[i] < strings[j])); assert((bodies[i] > bodies[j]) == (strings[i] > strings[j])); assert((bodies[i] <= bodies[j]) == (strings[i] <= strings[j])); assert((bodies[i] >= bodies[j]) == (strings[i] >= strings[j])); assert((bodies[i] == refs[j]) == (strings[i] == strings[j])); assert((bodies[i] != refs[j]) == (strings[i] != strings[j])); assert((bodies[i] < refs[j]) == (strings[i] < strings[j])); assert((bodies[i] > refs[j]) == (strings[i] > strings[j])); assert((bodies[i] <= refs[j]) == (strings[i] <= strings[j])); assert((bodies[i] >= refs[j]) == (strings[i] >= strings[j])); assert((bodies[i] == strings[j].c_str()) == (strings[i] == strings[j])); assert((bodies[i] != strings[j].c_str()) == (strings[i] != strings[j])); assert((bodies[i] < strings[j].c_str()) == (strings[i] < strings[j])); assert((bodies[i] > strings[j].c_str()) == (strings[i] > strings[j])); assert((bodies[i] <= strings[j].c_str()) == (strings[i] <= strings[j])); assert((bodies[i] >= strings[j].c_str()) == (strings[i] >= strings[j])); assert((refs[i] == bodies[j]) == (strings[i] == strings[j])); assert((refs[i] != bodies[j]) == (strings[i] != strings[j])); assert((refs[i] < bodies[j]) == (strings[i] < strings[j])); assert((refs[i] > bodies[j]) == (strings[i] > strings[j])); assert((refs[i] <= bodies[j]) == (strings[i] <= strings[j])); assert((refs[i] >= bodies[j]) == (strings[i] >= strings[j])); assert((strings[i].c_str() == bodies[j]) == (strings[i] == strings[j])); assert((strings[i].c_str() != bodies[j]) == (strings[i] != strings[j])); assert((strings[i].c_str() < bodies[j]) == (strings[i] < strings[j])); assert((strings[i].c_str() > bodies[j]) == (strings[i] > strings[j])); assert((strings[i].c_str() <= bodies[j]) == (strings[i] <= strings[j])); assert((strings[i].c_str() >= bodies[j]) == (strings[i] >= strings[j])); assert(bodies[i].starts_with(bodies[j]) == string_starts_with(strings[i], strings[j])); assert(bodies[i].starts_with(strings[j].c_str()) == string_starts_with(strings[i], strings[j])); assert(bodies[i].ends_with(bodies[j]) == string_ends_with(strings[i], strings[j])); assert(bodies[i].ends_with(strings[j].c_str()) == string_ends_with(strings[i], strings[j])); } } for (size_t i = 0; i < NUM_STRINGS; ++i) { std::stringstream stream; stream << (i / 2.0); std::string extra_string = stream.str(); strings[i].append(extra_string); bodies[i].append(extra_string.data(), extra_string.size()); assert(bodies[i] == grnxx::String(strings[i].data(), strings[i].size())); } for (size_t i = 0; i < NUM_STRINGS; ++i) { strings[i].append(strings[i]); bodies[i].append(bodies[i]); assert(std::string(bodies[i].data(), bodies[i].size()) == strings[i]); } }
void String__starts_with(caStack* stack) { set_bool(circa_output(stack, 0), string_starts_with(circa_input(stack, 0), as_cstring(circa_input(stack, 1)))); }
NwaRefPtr assemble_nwa(ProcedureMap const & procedures, boost::function<void (Nwa &, State, State)> call_inserter, boost::function<void (Nwa &, State, State, State)> return_inserter) { NwaRefPtr finalnwa = new Nwa(); std::map<std::string, std::set<State> > entries_map; std::map<std::string, std::set<State> > exits_map; //////// // We will set up the states, then fix up the transitions. // First, combine all of the procedures (to get all the states // and transitions.) for (ProcedureMap::const_iterator proc = procedures.begin(); proc != procedures.end(); ++proc) { NwaRefPtr min = minimize_internal_nwa(proc->second); //NwaRefPtr min = proc->second; entries_map[proc->first] = min->getInitialStates(); exits_map[proc->first] = min->getFinalStates(); if (proc->first != "main") { min->clearInitialStates(); min->clearFinalStates(); } finalnwa->combineWith(*min); } //////// // Now set up the transitions // Now, find each of the transitions on a symbol __call__*. These // are the transitions we will replace. Nwa::Internals fake_call_transitions; for (Nwa::InternalIterator trans=finalnwa->beginInternalTrans(); trans != finalnwa->endInternalTrans(); ++trans) { std::string symbol = key2str(trans->second); if (string_starts_with(symbol, call_prefix)) { fake_call_transitions.insert(*trans); } } // Okay, now we have to do two things with each of those // transitions. The simpler one is to remove it. The more // complicated one is add a call and a return transition. The // call transition goes from the source to the entry of the // procedure that corresponds to the transition's symbol. The // return goes from the exit of that procedure to the return // node, with the call node as the predecessor. // // Schematically: // C ------------------> R // __call__foo // turns into // C R // | /\ . // call | | return (C as call predecessor) // | | // V | // entry_foo exit_foo // for (Nwa::Internals::iterator fake_call = fake_call_transitions.begin(); fake_call != fake_call_transitions.end(); ++fake_call) { // Remove the fake call. finalnwa->removeInternalTrans(*fake_call); // Prepare for the call and return transitions Key call_site = fake_call->first; Key return_site = fake_call->third; std::string symbol = key2str(fake_call->second); std::string callee_name = remove_prefix(symbol, call_prefix); assert(entries_map.find(callee_name) != entries_map.end()); assert(exits_map.find(callee_name) != exits_map.end()); std::set<State> const & entries = entries_map[callee_name]; std::set<State> const & exits = exits_map[callee_name]; // I don't think these assertions are necessary for the below // to work, but they do apply in my setting. TODO: consider // removing them when I make this code more general. -Evan // 3/10/11 (Actually change to >= 1 instead of remove // entirely.) assert(entries.size() == 1); // Now add the call transition(s) for (std::set<State>::const_iterator entry = entries.begin(); entry != entries.end(); ++entry) { call_inserter(*finalnwa, call_site, *entry); //finalnwa->addCallTrans(call_site, call_key, *entry); //finalnwa->addInternalTrans(call_site, EPSILON, *entry); } // Now add the return transition(s) for (std::set<State>::const_iterator exit = exits.begin(); exit != exits.end(); ++exit) { return_inserter(*finalnwa, *exit, call_site, return_site); //finalnwa->addReturnTrans(*exit, call_site, return_key, return_site); //finalnwa->addInternalTrans(*exit, EPSILON, return_site); } } // Finally, we remove the __call__ symbols from the automaton. std::set<Symbol> to_remove; for (Nwa::SymbolIterator symiter = finalnwa->beginSymbols(); symiter != finalnwa->endSymbols(); ++symiter) { Symbol symbol = *symiter; if (string_starts_with(key2str(symbol), call_prefix)) { // It shouldn't be used in any transitions. Check that. assert(query::getSources_Sym(*finalnwa, symbol).size() == 0); assert(query::getCallSites_Sym(*finalnwa, symbol).size() == 0); assert(query::getExits_Sym(*finalnwa, symbol).size() == 0); // Now schedule it for removal to_remove.insert(symbol); } } for (std::set<Symbol>::iterator s=to_remove.begin(); s!=to_remove.end(); ++s){ finalnwa->removeSymbol(*s); } return finalnwa; } // end assemble_nwa()
std::string remove_prefix(std::string const & str, std::string const & prefix) { assert(string_starts_with(str, prefix)); return str.substr(prefix.size()); }
int string_starts_with_string(String* source, String* prefix) { return string_starts_with(source, prefix->string); }
int parse_scan_line(type_t *type, boolean *is_counter, char **destformat, char *format) { char buf[strlen(format)]; char *fmt = format; char *c = buf; int size; *is_counter = false; if (!format) return 0; while (*fmt != '\0') { if (*fmt == '%') { *c++ = *fmt++; if (*fmt == '%') { *c++ = *fmt++; continue; } if (*fmt == '*') { *c++ = *fmt++; continue; } if (*fmt == '!') { fmt++; *is_counter = true; } break; } else { *c++ = *fmt++; } } while (isdigit((int) *fmt)) *c++ = *fmt++; if ( string_starts_with(fmt, "[") || string_starts_with(fmt, "s") || string_starts_with(fmt, "c")) { *type = string_type; size = buflen; } else if (string_starts_with(fmt, "hhd") || string_starts_with(fmt, "hhi") || string_starts_with(fmt, "hhn")) { *type = char_type; size = sizeof(char); } else if (string_starts_with(fmt, "hho") || string_starts_with(fmt, "hhu") || string_starts_with(fmt, "hhx")) { *type = uchar_type; size = sizeof(unsigned char); } else if (string_starts_with(fmt, "hd") || string_starts_with(fmt, "hi") || string_starts_with(fmt, "hn")) { *type = short_type; size = sizeof(short); } else if (string_starts_with(fmt, "ho") || string_starts_with(fmt, "hu") || string_starts_with(fmt, "hx")) { *type = ushort_type; size = sizeof(unsigned short); } else if (string_starts_with(fmt, "d") || string_starts_with(fmt, "i") || string_starts_with(fmt, "n")) { *type = int_type; size = sizeof(int); } else if (string_starts_with(fmt, "o") || string_starts_with(fmt, "u") || string_starts_with(fmt, "x")) { *type = uint_type; size = sizeof(unsigned int); } else if (string_starts_with(fmt, "ld") || string_starts_with(fmt, "li") || string_starts_with(fmt, "ln")) { *type = long_type; size = sizeof(long); } else if (string_starts_with(fmt, "lo") || string_starts_with(fmt, "lu") || string_starts_with(fmt, "lx")) { *type = ulong_type; size = sizeof(unsigned long); } else if (string_starts_with(fmt, "Ld") || string_starts_with(fmt, "Li") || string_starts_with(fmt, "Ln") || string_starts_with(fmt, "lld") || string_starts_with(fmt, "lli") || string_starts_with(fmt, "lln")) { *type = longlong_type; size = sizeof(long long); } else if (string_starts_with(fmt, "Lo") || string_starts_with(fmt, "Lu") || string_starts_with(fmt, "Lx") || string_starts_with(fmt, "llo") || string_starts_with(fmt, "llu") || string_starts_with(fmt, "llx")) { *type = ulonglong_type; size = sizeof(unsigned long long); } else if (string_starts_with(fmt, "f") || string_starts_with(fmt, "e") || string_starts_with(fmt, "g")) { *type = float_type; size = sizeof(float); } else if (string_starts_with(fmt, "lf") || string_starts_with(fmt, "le") || string_starts_with(fmt, "lg")) { *type = double_type; size = sizeof(double); } else if (string_starts_with(fmt, "Lf") || string_starts_with(fmt, "Le") || string_starts_with(fmt, "Lg") || string_starts_with(fmt, "llf") || string_starts_with(fmt, "lle") || string_starts_with(fmt, "llg")) { *type = longdouble_type; size = sizeof(long double); } else { *type = invalid_type; size = 0; debugf("cannot determine var type for: %s", format); } while (*fmt != '\0') *c++ = *fmt++; *c = '\0'; *destformat = (char *) smalloc(strlen(buf)+1); strcpy(*destformat, buf); return size; }
// // Function called when a new HTTP request have been recieved. // static void http_handler(struct evhttp_request *request, void *userdata) { struct owl_state* state = userdata; // Setup general response headers struct evkeyvalq *headers = evhttp_request_get_output_headers(request); evhttp_add_header(headers, "Server", USER_AGENT); // Get the requested URI const char* uri = evhttp_request_get_uri(request); const int http_method = evhttp_request_get_command(request); if( http_method != EVHTTP_REQ_GET && http_method != EVHTTP_REQ_PUT && http_method != EVHTTP_REQ_POST) { evhttp_send_error(request, 501, "Not Implemented"); return; } TRACE("Received HTTP request: %s (method %d)\n", uri, http_method); // Keep the request for async usage state->http_request = request; // // Retrieve application state (sync) if(string_starts_with(uri, "/api/state") && http_method == EVHTTP_REQ_GET) { state_action(state); } // // Shutdown owl application (async) else if(string_starts_with(uri, "/api/shutdown") && http_method == EVHTTP_REQ_GET) { shutdown_action(state); } // // Try to login to Spotify (async) else if(string_starts_with(uri, "/api/login") && http_method == EVHTTP_REQ_GET) { char* username = extract_uri_section(2, uri); char* password = extract_uri_section(3, uri); if(username != NULL && password != NULL) { login_to_spotify_action(state, username, password); } else { WARN("Could not extract username and password in order to login to Spotify!\n"); respond_error(request, OWL_HTTO_ERROR_NO_LOGIN_DETAILS, "No username or password given"); } } else if(string_starts_with(uri, "/api/login") && http_method == EVHTTP_REQ_POST) { TRACE("POST LOGIN\n"); get_post_argument(request, "username"); evhttp_send_error(request, 501, "Not Implemented"); } // // Logout from spotify (async) else if(string_starts_with(uri, "/api/logout") && http_method == EVHTTP_REQ_GET) { if(state->state > OWL_STATE_LOGGING_IN && state->state < OWL_STATE_LOGGING_OUT) logout_from_spotify_action(state); else respond_success(request); } // // Clear the entire queue else if(string_starts_with(uri, "/api/queue/clear") && http_method == EVHTTP_REQ_GET) { if(state->state < OWL_STATE_IDLE || state->state > OWL_STATE_PLAYING) { respond_error(request, OWL_HTTP_ERROR_NOT_LOGGED_IN, "Operation not allowed when not logged in"); } else { const int size = queue_size(state->spotify_state->queue); for(int i = 0; i < size; i++) { owl_track *track = pop_from_queue(state->spotify_state->queue); free_track(track); } respond_success(request); } } // // Get the current playback queue (sync) else if(string_starts_with(uri, "/api/queue") && http_method == EVHTTP_REQ_GET) { if(state->state < OWL_STATE_IDLE || state->state > OWL_STATE_PLAYING) { WARN("Operation not allowed at this state (%d)\n", state->state); respond_error(request, OWL_HTTP_ERROR_NOT_LOGGED_IN, "Operation not allowed when not logged in"); } else { respond_with_queue(state->http_request, state->spotify_state->queue); } } // // Serve static file immediately else { // Create the buffer to retrn as content struct evbuffer *content_buffer = evbuffer_new(); // If not a handler this is a static request char *static_file = (char *) malloc(strlen(doc_root) + strlen(uri) + 1); stpcpy(stpcpy(static_file, doc_root), uri); TRACE("Looking for static file: %s\n", static_file); bool file_exists = 1; struct stat st; if(stat(static_file, &st) == -1 || S_ISDIR(st.st_mode)) { file_exists = 0; evhttp_send_error(request, HTTP_NOTFOUND, "Not Found"); } if(file_exists) { const int file_size = st.st_size; FILE *fp = fopen(static_file, "r"); const char* content_type = resolve_content_type(static_file); evbuffer_add_file(content_buffer, fileno(fp), 0, file_size); // will close TRACE("Resolving content type for filename: %s to: %s\n", static_file, content_type); evhttp_add_header(headers, "Content-Type", content_type); evhttp_send_reply(request, HTTP_OK, "OK", content_buffer); } free(static_file); // Send the data evhttp_send_reply(request, HTTP_OK, USER_AGENT, content_buffer); // Free memrory evbuffer_free(content_buffer); } return; }