void do_cache_set(void) { struct string *key = string_create(); read_into_string(key); struct cache_entry *entry = cache_lookup(key); if (entry == NULL) { // There's no existing entry for this key. Find a free slot to put // a new entry in. entry = find_free_slot(); } if (entry == NULL) { // No free slots, tell the client the cache is full :-( write(STDOUT_FILENO, &kCacheFull, sizeof(kCacheFull)); return; } write(STDOUT_FILENO, &kFound, sizeof(kFound)); entry->key = key; if (entry->value == NULL) { entry->value = string_create(); } read_into_string(entry->value); entry->lifetime = read_int(); }
static Boolean ReadMakefile( const char *fname, /* makefile to read */ Boolean isSystem) /* system makefile */ { FILE *stream; string_t name; if (!strcmp(fname, "-")) { Var_Set(sMAKEFILE, sNULL, VAR_GLOBAL); Parse_File(string_create("(stdin)"), stdin); } else { string_t sfname = string_create(fname); if (!isSystem) { name = Dir_FindFile(sfname, dirSearchPath); } else { name = Dir_FindFile(sfname, parseIncPath); if (name == (string_t) NULL) name = Dir_FindFile(sfname, sysIncPath); } string_deref(sfname); if (name == (string_t) NULL || (stream = fopen(name->data, "r")) == NULL) return(FALSE); /* * set the MAKEFILE variable desired by System V fans -- the * placement of the setting here means it gets set to the last * makefile specified, as it is set by SysV make. */ Var_Set(sMAKEFILE, name, VAR_GLOBAL); Parse_File(name, stream); (void)fclose(stream); } return(TRUE); }
void string_resize(StringData** data, int newLength) { if (*data == NULL) { *data = string_create(newLength); return; } // Perform the same check as touch() if ((*data)->refCount == 1) { INCREMENT_STAT(StringResizeInPlace); // Modify in-place *data = (StringData*) realloc(*data, sizeof(StringData) + newLength + 1); (*data)->length = newLength; return; } INCREMENT_STAT(StringResizeCreate); StringData* oldData = *data; StringData* newData = string_create(newLength); memcpy(newData->str, oldData->str, oldData->length + 1); decref(oldData); *data = newData; }
json_object_object_foreach(jobj, key, val) { char *value; size_t value_len = 0; int array_len; const enum json_type inner_type = json_object_get_type(val); switch (inner_type) { case json_type_boolean: kvp_add_simple((struct _kvp_store *)jenc, key, kvp_val_create(NULL, json_object_get_boolean(val) ? 1 : 0)); break; case json_type_double: kvp_add_simple((struct _kvp_store *)jenc, key, kvp_val_create(NULL, json_object_get_double(val))); break; case json_type_int: kvp_add_simple((struct _kvp_store *)jenc, key, kvp_val_create(NULL, json_object_get_int(val))); break; case json_type_string: value = string_create(&value_len, snprintf(NULL, 0, "%s", json_object_get_string(val))); sprintf(value, "%s", json_object_get_string(val)); kvp_add_simple((struct _kvp_store *)jenc, key, kvp_val_create(value, 0)); string_free(value, &value_len); break; case json_type_object: value = string_create(&value_len, snprintf(NULL, 0, "%s", json_object_get_string(val))); sprintf(value, "%s", json_object_get_string(val)); kvp_add_simple((struct _kvp_store *)jenc, key, kvp_val_create(value, 0)); string_free(value, &value_len); break; case json_type_array: array_len = json_object_array_length(val); for (int i = 0; i < array_len; i++) { json_object *array_member = json_object_array_get_idx(val, i); if (!is_error(array_member)) { value = string_create(&value_len, snprintf(NULL, 0, "%s", json_object_get_string(array_member))); sprintf(value, "%s", json_object_get_string(array_member)); kvp_add_array((struct _kvp_store *)jenc, key, kvp_val_create(value, 0)); string_free(value, &value_len); } } break; case json_type_null: kvp_add_simple((struct _kvp_store *)jenc, key, kvp_val_create("", 0)); break; default: MYERROR("What other JSON type?"); break; } }
char* string_initialize(caValue* value, int length) { make(TYPES.string, value); StringData* data = string_create(length); value->value_data.ptr = data; return data->str; }
static void rest_extract_token(t_rest *const rest, struct _memory_struct *const out_header) { if ((*out_header).memory) { char *cookie_params = NULL; char *header_line = strtok((*out_header).memory, "\n"); while (header_line != NULL) { if (strncmp(header_line, "Set-Cookie:", 11) == 0) { cookie_params = strtok(header_line, ": "); /*remove "Set-Cookie:" */ cookie_params = strtok(NULL, "; "); while (cookie_params != NULL) { if (strlen(cookie_params)) { rest->common.auth_token = string_create( &rest->common.auth_token_len, strlen(cookie_params)); strcpy(rest->common.auth_token, cookie_params); break; } cookie_params = strtok(NULL, "; "); } break; } header_line = strtok(NULL, "\n"); } } }
void cmd_insert (char *cp) { Line *line; String *string; if (range_single (cp, &cp, &cur_position) < 0) return; /* see where to insert */ if (*cp == ';') /* if ;, insert the remaining string before current line */ { cur_position.offset = 0; string = string_create (strlen (cp + 1), cp + 1); string_concat (string, 1, "\n"); buffer_dirty (cur_position.buffer, 1); line_insert (cur_position.buffer, cur_position.line, string); return; } if (!eoltest (cp)) return; /* otherwise, that's all there should be */ /* Read tty input until eof into the current buffer just before the current line */ cur_position.offset = 0; while ((string = jnl_readprompt ("\r\n >")) != NULL) { string_concat (string, 1, "\n"); /* put line terminator on string */ buffer_dirty (cur_position.buffer, 1); line_insert (cur_position.buffer, cur_position.line, string); /* insert line just before current line */ } }
static scheme_object* read_string(vm* context, FILE* in) { string_t* str = string_create(); //we read until the first unescaped double quote character int c; while((c = getc(in)) != '"') { if(c == '\\') { //this is an escaped character c = getc(in); if(c == 'n') { c = '\n'; } else if(c == 't') { c = '\t'; } } if(c == EOF) { fprintf(stderr, "Unexpected termination of a string literal"); exit(1); } string_append_char(str, c); } return make_string(context, str); }
void set_string(caValue* value, const char* s, int length) { make(TYPES.string, value); StringData* data = string_create(length); memcpy(data->str, s, length); data->str[length] = 0; value->value_data.ptr = data; }
void set_string(caValue* value, const char* s) { make(TYPES.string, value); int length = strlen(s); StringData* data = string_create(length); memcpy(data->str, s, length + 1); value->value_data.ptr = data; }
static void *rest_get_auth_token(void *const thread_args) { t_rest *const rest = thread_args; /* length + name=&password=*/ rest->common.parameters = string_create(&rest->common.parameters_len, rest->cookie.username_len + rest->cookie.password_len + 17); if (rest->common.parameters != NULL) { strcpy(rest->common.parameters, "name="); strcat(rest->common.parameters, rest->cookie.username); strcat(rest->common.parameters, "&password="******"POST"); pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0); rest->common.easy_handle = curl_easy_init(); rest->common.multi_handle = curl_multi_init(); if (rest->common.easy_handle == NULL) { MYERROR("Cannot init curl."); ctw_cleanup_request(&rest->common, NULL, NULL); } else { struct curl_slist *slist = NULL; struct _memory_struct out_content; struct _memory_struct out_header; FILE *fp = ctw_prepare(&rest->common, slist, &out_content, NULL); out_header.memory = getbytes(1); out_header.size = 0; struct _cb_val *cb_val = getbytes(sizeof(struct _cb_val)); cb_val->mem = &out_header; cb_val->ctw = (struct _ctw *)rest; curl_easy_setopt(rest->common.easy_handle, CURLOPT_HEADERFUNCTION, ctw_write_mem_cb); curl_easy_setopt(rest->common.easy_handle, CURLOPT_WRITEHEADER, (void *)cb_val); ctw_thread_perform(&rest->common); rest_process_auth_data(rest, &out_header); string_free(out_header.memory, &out_header.size); string_free(out_content.memory, &out_content.size); ctw_cleanup_request(&rest->common, fp, slist); } return NULL; }
void test_string(void) { string_t str; TEST_EXPECT(string_create(&str)); test_empty_string(&str); string_clear(&str); test_empty_string(&str); string_destroy(&str); TEST_EXPECT(string_create(&str)); test_empty_string(&str); string_destroy(&str); test_append_c_str(); }
// Creates a hard duplicate of a string. Starts off with 1 ref. StringData* string_duplicate(StringData* original) { INCREMENT_STAT(StringDuplicate); StringData* dup = string_create(original->length); memcpy(dup->str, original->str, original->length + 1); return dup; }
void irc_unqueue(irc_t *irc) { irc_queued_t *entry; size_t events = 0; /* Only when enough time has passed */ if (difftime(time(NULL), irc->lastunqueue) <= IRC_FLOOD_INTERVAL) { irc_manager_wake(irc->manager); return; } while (events != IRC_FLOOD_LINES && (entry = list_shift(irc->queue))) { const size_t targetlen = string_length(entry->target); const char *target = string_contents(entry->target); const irc_command_func_t *func = &irc_commands[entry->command]; /* If there is a payload we use the extended call */ if (entry->payload) { size_t payloadlen = string_length(entry->payload); const char *payload = string_contents(entry->payload); size_t corelen = func->baselen + targetlen + 63; /* 63 is MAX_HOST_LENGTH */ /* Split payload for 512 byte IRC line limit */ while (corelen + payloadlen > 512) { char truncate[512]; size_t size = sizeof(truncate) - corelen; strncpy(truncate, payload, size); truncate[size] = '\0'; func->extended(irc, target, truncate); /* Flood protection */ if (++events == IRC_FLOOD_LINES) { /* Construct a new partial payload */ char *move = string_move(entry->payload); entry->payload = string_create(move + (payload - move) + size); free(move); list_prepend(irc->queue, entry); break; } payloadlen -= size; payload += size; } func->extended(irc, target, payload); events++; string_destroy(entry->payload); } else { /* Otherwise we do a standard call */ func->standard(irc, target); events++; } string_destroy(entry->target); free(entry); } /* Flood protection */ if (events == IRC_FLOOD_LINES) { irc->lastunqueue = time(NULL); irc_manager_wake(irc->manager); } }
static void irc_enqueue_standard(irc_t *irc, const char *target, irc_command_t command) { irc_queued_t *entry = malloc(sizeof(*entry)); entry->target = string_create(target); entry->payload = NULL; entry->command = command; list_push(irc->queue, entry); }
void rest_command(t_rest *const rest, const t_symbol *const sel, const int argc, t_atom *argv) { const char *const req_type = sel->s_name; char path[MAXPDSTRING]; if (rest->common.locked) { post("rest object is performing request and locked."); return; } memset(rest->common.req_type, 0x00, REQUEST_TYPE_LEN); if (argc == 0) { return; } rest->common.locked = 1; strncpy(rest->common.req_type, req_type, REQUEST_TYPE_LEN - 1); if (ctw_check_request_type(rest->common.req_type) != 0){ pd_error(rest, "Request method %s not supported.", rest->common.req_type); rest->common.locked = 0; return; } atom_string(argv, path, MAXPDSTRING); rest->common.complete_url = string_create(&rest->common.complete_url_len, rest->common.base_url_len + strlen(path) + 1); if (rest->common.base_url != NULL) { strcpy(rest->common.complete_url, rest->common.base_url); } strcat(rest->common.complete_url, path); if (argc > 1) { char parameters[MAXPDSTRING]; atom_string(argv + 1, parameters, MAXPDSTRING); if (strlen(parameters)) { char *cleaned_parameters; size_t memsize = 0; cleaned_parameters = string_remove_backslashes(parameters, &memsize); rest->common.parameters = string_create(&rest->common.parameters_len, memsize + 1); strcpy(rest->common.parameters, cleaned_parameters); freebytes(cleaned_parameters, memsize); } } ctw_thread_exec((struct _ctw *)rest, ctw_exec); }
String *jnl_readprompt (const char *prompt) { char *buff, header[16], *p; String *string; uLong strln; /* If recovery file open, read from it, otherwise read from terminal */ if (recover_file == NULL) goto readterminal; if (fgets (header, sizeof header, recover_file) == NULL) goto closerecovery; if (header[0] != '*') goto badrecovery; if (strcmp (header + 1, "EOF\n") == 0) { string = NULL; goto writejournal; } strln = strtoul (header + 1, &p, 10); if (*p != '\n') goto badrecovery; buff = malloc (strln + 1); if (fread (buff, strln, 1, recover_file) == 0) goto closerecovery; string = string_create (strln, buff); buff[strln] = 0; outfmt (strlen (prompt) + strlen (buff), "%s%s\n", prompt, buff); free (buff); goto writejournal; badrecovery: outerr (strlen (header), "bad recovery file format at %s\n", header); closerecovery: fclose (recover_file); recover_file = NULL; /* Read from terminal screen after flushing any output */ readterminal: output (); string = os_readprompt (prompt); /* Write journal record */ writejournal: if (journal_file != NULL) { if (string == NULL) fprintf (journal_file, "*EOF\n"); else { strln = string_getlen (string); fprintf (journal_file, "*%u\n", strln); fwrite (string_getval (string), strln, 1, journal_file); } } /* Return pointer to string (NULL for eof) */ return (string); }
Source_multi_or* parse_source_multi_or( char** input, ParserState* state ) { Source* operands[PARSER_BUFFER_SIZE]; int i = 0; if (**input != '(') return 0; *input += 1; do { if (i == PARSER_BUFFER_SIZE) break; operands[i] = parse_source( input, state ); if (**input != ';') break; *input += 1; // sizeof(char) } while (operands[i++]); if (i == 0) return 0; if (**input != ')') return 0; *input += 1; Source_multi_or* retval = alloct(Source_multi_or); /// retval+ String* ns = string_create(operands, sizeof(Source*) * i); /// ns+ source_multi_or_init( retval, ns ); release(ns); /// ns- return retval; }
static void irc_enqueue_extended(irc_t *irc, const char *target, string_t *payload, irc_command_t command) { irc_queued_t *entry = malloc(sizeof(*entry)); const char *contents = string_contents(payload); string_reassociate(payload, irc_color_parse_code(contents)); entry->target = string_create(target); entry->payload = payload; entry->command = command; list_push(irc->queue, entry); }
static void ctw_set_cert_path(struct _ctw *common, char *directory) { size_t i; common->cert_path = string_create(&common->cert_path_len, strlen(directory) + 11); strcpy(common->cert_path, directory); for(i = 0; i < strlen(common->cert_path); i++) { if (common->cert_path[i] == '/') { common->cert_path[i] = '\\'; } } strcat(common->cert_path, "\\cacert.pem"); }
static void test1() { String* a = string_create(""); String* b = string_create("Hello"); String* c = string_create("World"); printf("a = \"%s\"\n", a->text); printf("b = \"%s\"\n", b->text); printf("c = \"%s\"\n", c->text); assert(strcmp(a->text, "") == 0); assert(strcmp(b->text, "Hello") == 0); assert(strcmp(c->text, "World") == 0); string_append(a, b->text); string_append(b, c->text); string_append(c, a->text); printf("a = a+b = \"%s\"\n", a->text); printf("b = b+c = \"%s\"\n", b->text); printf("c = c+a = \"%s\"\n", c->text); assert(strcmp(a->text, "Hello") == 0); assert(strcmp(b->text, "HelloWorld") == 0); assert(strcmp(c->text, "WorldHello") == 0); string_prepend(a, b->text); string_prepend(b, c->text); string_prepend(c, a->text); printf("a = b+a = \"%s\"\n", a->text); printf("b = c+b = \"%s\"\n", b->text); printf("c = a+c = \"%s\"\n", c->text); assert(strcmp(a->text, "HelloWorldHello") == 0); assert(strcmp(b->text, "WorldHelloHelloWorld") == 0); assert(strcmp(c->text, "HelloWorldHelloWorldHello") == 0); string_destroy(a); string_destroy(b); string_destroy(c); }
void app_usbd_serial_num_generate(void) { char serial_number_string[SERIAL_NUMBER_STRING_SIZE + 1]; const uint16_t serial_num_high_bytes = (uint16_t)NRF_FICR->DEVICEADDR[1] | 0xC000; // The masking makes the address match the Random Static BLE address. const uint32_t serial_num_low_bytes = NRF_FICR->DEVICEADDR[0]; (void)snprintf(serial_number_string, SERIAL_NUMBER_STRING_SIZE + 1, "%04"PRIX16"%08"PRIX32, serial_num_high_bytes, serial_num_low_bytes); string_create(serial_number_string); }
static void set_caption_string(MainLoop *ml) { String *cap; VideoWindow *vw = ml->vw; char *template; char *fullpath; int i; int literal_start, literal_mode; int maxlen = vw->full_width / 7; /* XXX */ if ((cap = string_create()) == NULL) return; if ((template = config_get_str(ml->uidata->c, "/enfle/plugins/ui/normal/caption_template")) == NULL)
args_t arg_parse(const char* line_raw) { args_t self; int numargs = 0; const char *end, *last; /* First pass to count how many args... */ end = line_raw; while(*end && isspace(*end)) ++end; //Skip initial space... for(;;) { while(*end && !isspace(*end)) ++end; //Go to end of argument ++numargs; while(*end && isspace(*end)) ++end; //Go to end of space after argument if(!*end) break; } self.num = numargs; // One more # args than spaces. self.arg = malloc(sizeof(arg_t)*numargs); /* Second pass to assign args. (Lemee alone, is more efficient than a linked list!) */ last = line_raw; numargs = 0; //Now numargs is which current arg. end = line_raw; while(*end && isspace(*end)) ++end; //Skip initial space... for(;;) { arg_t* nextarg = self.arg + numargs;; const char* isbinary; while(*end && !isspace(*end)) ++end; //Go to end of argument isbinary = strchr(last,'='); //Is there a value? //Make sure not to pass end in our search for = if(isbinary && (isbinary < end)) { nextarg->name = string_ncreate(last,isbinary-last); nextarg->value = string_ncreate(isbinary+1,end-isbinary-1); } else { nextarg->name = string_ncreate(last,end-last); nextarg->value = string_create(NULL); } ++numargs; while(*end && isspace(*end)) ++end; //Go to end of space after argument if(!*end) break; last = end; } return self; }
static char *ctw_set_param(void *x, t_atom *arg, size_t *string_len, char *error_msg) { char temp[MAXPDSTRING]; char *string; if (arg[0].a_type != A_SYMBOL) { pd_error(x, "%s", error_msg); return NULL; } atom_string(arg, temp, MAXPDSTRING); string = string_create(string_len, strlen(temp)); if (string == NULL) { return NULL; } strcpy(string, temp); return string; }
static void test2() { String* a = string_create(""); int i; for (i=1; i<=10000; ++i) { string_append(a, "a"); assert(a->length == i); assert(strlen(a->text) == i); } for (i=0; i<10000; ++i) assert(a->text[i] == 'a'); assert(a->text[i] == '\0'); string_destroy(a); }
static void test_append_c_str() { string_t s; TEST_EXPECT(string_create(&s)); TEST_EXPECT(string_append_c_str(&s, "")); TEST_EXPECT(!strcmp(string_c_str(&s), "")); TEST_EXPECT(string_size(&s) == 0); TEST_EXPECT(string_append_c_str(&s, "abc")); TEST_EXPECT(!strcmp(string_c_str(&s), "abc")); TEST_EXPECT(string_size(&s) == 3); TEST_EXPECT(string_append_c_str(&s, "123")); TEST_EXPECT(!strcmp(string_c_str(&s), "abc123")); TEST_EXPECT(string_size(&s) == 6); string_destroy(&s); }
static void ctw_set_file(void *x, int argc, t_atom *argv) { struct _ctw *common = x; t_symbol *filename; char buf[MAXPDSTRING]; string_free(common->out_file, &common->out_file_len); if (argc == 0) { return; } filename = atom_getsymbol(argv); if (filename == 0) { pd_error(x, "not a filename"); return; } canvas_makefilename(common->x_canvas, filename->s_name, buf, MAXPDSTRING); common->out_file = string_create(&(common->out_file_len), strlen(buf)); strcpy(common->out_file, buf); }
Template_multi_and* parse_template_multi_and( char** input, ParserState* state ) { Template* operands[PARSER_BUFFER_SIZE]; int i = 0; do { if (i == PARSER_BUFFER_SIZE) break; operands[i] = parse_source( input, state ); if (**input != ' ') break; *input += 1; // sizeof(char) } while (operands[i++]); if (i == 0) return 0; Template_multi_and* retval = alloct(Template_multi_and); /// retval+ String* ns = string_create(operands, sizeof(Template*) * i); /// ns+ template_multi_and_init( retval, ns ); release(ns); /// ns- return retval; }
static scheme_object* read_symbol(vm* context, FILE* in) { string_t* str = string_create(); int c = getc(in); while(is_initial(c) || isdigit(c) || c == '+' || c == '-') { string_append_char(str, c); c = getc(in); } if(!is_delimiter(c)) { fprintf(stderr, "symbol not folowed by delimiter\n"); exit(1); } ungetc(c, in); scheme_object* rval = make_symbol(context, string_cstring(str)); string_free(str); return rval; }