int main(int argc, char **argv) { char *trimmed = cstr_trim(cstr_copy(" trim me ")); printf("COPY & TRIM:\n"); printf("\t\"%s\"\n", trimmed); free(trimmed); printf("MERGE:\n"); char *merged = cstr_merge("hello", " world! ", "look", "a", "merged", "string! :)"); printf("\t%s\n", merged); printf("SPLIT (#1: by space character):\n"); cstr_split_t *split = cstr_split(merged, ' ', false); for (int i = 0; i < split->count; i++) printf("\tpiece %i: %s\n", i, split->pieces[i]); cstr_split_free(split); free(merged); printf("SPLIT (#2: by comma + trim):\n"); char *numbers = "1, 2, 3, 4, 5"; printf("\tsplitting this string: %s\n", numbers); split = cstr_split(numbers, ',', true); for (int i = 0; i < split->count; i++) printf("\tpiece %i: \"%s\"\n", i, split->pieces[i]); cstr_split_free(split); printf("REPLACE:\n"); char *hello = "hello world!"; char *heeeo = cstr_replace(hello, 'l', 'e', 2, true); printf("\t%s => %s\n", hello, heeeo); free(heeeo); printf("LOWER & UPPER:\n"); char *mixed = "HeLlO WoRlD"; char *low = cstr_lower(mixed, true); char *up = cstr_upper(low, true); printf("\t%s => %s => %s\n", mixed, low, up); free(low); free(up); }
void cmap_map_deadzone_stick(ini_t *ini, char *ini_key, char *ini_section, cmap_deadzone_t *d) { if (d->left) exit(printf("ERROR: deadzone '%s' has been defined" " twice in section '%s'" DOCU, ini_key, ini_section)); // default is always 0.5 d->left = d->up = d->right = d->down = 0.5 * AXIS_OFFSET_MAX; // read the space separated values char *value = ini_read(ini, ini_section, ini_key); if (!value) return; cstr_split_t *split = cstr_split(value, ' ', true); // factors (left, up, right, down), parsed symmetrically just like // in CSS (eg. padding: ...) double f[] = {0, 0, 0, 0}; switch (split->count) { case 1: f[0] = f[1] = f[2] = f[3] = SPLITVAL2FLOAT(0); break; case 2: f[0] = f[2] = SPLITVAL2FLOAT(0); // left, right f[1] = f[3] = SPLITVAL2FLOAT(1); // up, down break; case 3: f[0] = SPLITVAL2FLOAT(0); // left f[1] = f[3] = SPLITVAL2FLOAT(1); // up, down f[2] = SPLITVAL2FLOAT(2); // right break; case 4: f[0] = SPLITVAL2FLOAT(0); f[1] = SPLITVAL2FLOAT(1); f[2] = SPLITVAL2FLOAT(2); f[3] = SPLITVAL2FLOAT(3); break; default: exit(printf("ERROR: invalid value count for '%s':'%s'!" " Should be 1...4" DOCU, ini_section, ini_key)); } cstr_split_free(split); d->left = f[0] * AXIS_OFFSET_MAX; d->up = f[1] * AXIS_OFFSET_MAX; d->right = f[2] * AXIS_OFFSET_MAX; d->down = f[3] * AXIS_OFFSET_MAX; // TODO: remove key from ini, so we can verify later that there // are no invalid keys }
void ui_init_configs(ui_t *ui) { // user settings ini_t *user = ini_open("data/g2hr.ini", true, false); ini_modify(user, "slotmachine", "enabled", "false", false); ini_modify(user, "slotmachine", "cmd_shutdown", "poweroff", false); ini_modify(user, "slotmachine", "cmd_shutdown", "reboot", false); ini_modify(user, "multiplayer", "time", "3 5 10 15 20 25 30", false); ini_modify(user, "video", "window_width", "640", false); ini_modify(user, "video", "window_height", "480", false); // ui settings ini_t *settings = ini_open(cstr_merge(ui->tk->pref_path, "ui.ini"), false, false); ini_modify(settings, "ui", "update_check_enabled", "ask", false); ini_modify(settings, "video", "fullscreen", "true", false); ini_modify(settings, "video", "menu_upscaling", "linear", false); ini_modify(settings, "video", "ingame_lighting", "dusk", false); ini_modify(settings, "video", "exploding_scores", "true", false); ini_modify(settings, "video", "blood", "true", false); ini_modify(settings, "video", "text_speed", "3", false); ini_modify(settings, "video", "show_names", "true", false); ini_modify(settings, "audio", "3d_sound", "false", false); ini_modify(settings, "audio", "music", "5", false); ini_modify(settings, "audio", "sfx", "5", false); // set values ui->multiplayer_time_values = cstr_split(ini_read(user, "multiplayer", "time"), ' ', false); ui->menu_upscaling_values = cstr_split("nearest linear best", ' ', false); ui->ingame_lighting_values = cstr_split("dusk noon random", ' ', false); ui->text_speed_values = cstr_split("1 2 3 4 5", ' ', false); ui->ini_usersettings = user; ui->ini_settings = settings; }
int main(int argc, char const *argv[]) { cstr s = cstr_create(1024); cstr *array; size_t len, i; cstr_ncat(s, "asasasas", 8); printf("%u %u \n", cstr_len(s), cstr_used(s)); cstr_ncat(s, "asasasas", 8); printf("%s\n", s); printf("%u %u \n", cstr_len(s), cstr_used(s)); array = cstr_split("1 2 3 4 5 ", 11, " ", 1, &len); printf("array len %lu\n", len); for(i = 0; i < len; i++) { printf("array[%lu]:%s\n", i, array[i]); cstr_destroy(array[i]); } jfree(array); cstr_destroy(s); return 0; }
void cmap_map_action(cmap_t *cmap, ini_t *ini, char *str_action, cmap_action_t cmap_action, char is_required) { // transform the str_action in the form of "WALKING_ENTER_CAR" // into the form we actually need to find the config value: // walking/enter-car char *section_key = cstr_replace(str_action, '_', '/', 1, true); cstr_split_t *section_key_split = cstr_split(section_key, '/', false); char *section = cstr_lower(section_key_split->pieces[0], false); char *key = cstr_replace(cstr_lower(section_key_split->pieces[1], false), '_', '-', 0, false); free(section_key); char *value = ini_read(ini, section, key); if (value) { // iterate over all keys and save them to the struct cstr_split_t *value_split = cstr_split(value, ',', true); cmap_state_t *state = section[0] == 'w' ? &cmap->walking : &cmap->driving; for (int i = 0; i < value_split->count; i++) { SDL_GameControllerButton button = SDL_CONTROLLER_BUTTON_INVALID; SDL_GameControllerAxis axis = SDL_CONTROLLER_AXIS_INVALID; char axis_is_positive = 1; char *cmap_value = value_split->pieces[i]; // BUTTONS (there's also SDL_GameControllerGetButtonFromString, // but 'dpup' isn't as readable as DPAD_UP) CONVERT_BUTTON(button, cmap_value, A); CONVERT_BUTTON(button, cmap_value, B); CONVERT_BUTTON(button, cmap_value, X); CONVERT_BUTTON(button, cmap_value, Y); CONVERT_BUTTON(button, cmap_value, LEFTSTICK); CONVERT_BUTTON(button, cmap_value, RIGHTSTICK); CONVERT_BUTTON(button, cmap_value, LEFTSHOULDER); CONVERT_BUTTON(button, cmap_value, RIGHTSHOULDER); CONVERT_BUTTON(button, cmap_value, DPAD_UP); CONVERT_BUTTON(button, cmap_value, DPAD_DOWN); CONVERT_BUTTON(button, cmap_value, DPAD_LEFT); CONVERT_BUTTON(button, cmap_value, DPAD_RIGHT); // AXES CONVERT_STICK(axis, cmap_value, LEFTY, 0, "LEFT", "UP"); CONVERT_STICK(axis, cmap_value, LEFTY, 1, "LEFT", "DOWN"); CONVERT_STICK(axis, cmap_value, LEFTX, 0, "LEFT", "LEFT"); CONVERT_STICK(axis, cmap_value, LEFTX, 1, "LEFT", "RIGHT"); CONVERT_STICK(axis, cmap_value, RIGHTY, 0, "RIGHT", "UP"); CONVERT_STICK(axis, cmap_value, RIGHTY, 1, "RIGHT", "DOWN"); CONVERT_STICK(axis, cmap_value, RIGHTX, 0, "RIGHT", "RIGHT"); CONVERT_STICK(axis, cmap_value, RIGHTX, 1, "RIGHT", "RIGHT"); // TRIGGERS (special case of axis) // (Yes, the names are inconsistent with SDL2. However, they // are consistent with the other axes, first the position on // the game pad and then the name) if (axis == SDL_CONTROLLER_AXIS_INVALID) { if (!strcmp(cmap_value, "LEFTTRIGGER")) axis = SDL_CONTROLLER_AXIS_TRIGGERLEFT; else if (!strcmp(cmap_value, "RIGHTTRIGGER")) axis = SDL_CONTROLLER_AXIS_TRIGGERRIGHT; } // verify if the value could be parsed if (button == SDL_CONTROLLER_BUTTON_INVALID && axis == SDL_CONTROLLER_AXIS_INVALID) exit(printf("ERROR: invalid value '%s' (in '%s':'%s')!" DOCU, cmap_value, section, key)); // actually map the buttons if (button != SDL_CONTROLLER_BUTTON_INVALID) state->buttons[button] = cmap_action; else STATE_AXIS[axis] = cmap_action; } cstr_split_free(value_split); // TODO: remove key from ini, so we can verify later that there // are no invalid keys } if (!value && is_required) exit(printf("ERROR: couldn't find required value for '%s':'%s'" " (case sensitive!). Please add it to the config and try" " again!" DOCU, section, key)); cstr_split_free(section_key_split); }