Style * new_style(const char *name, LList *spec) { Style *style; Iterator *it; StrBuf *prebuf; StrBuf *postbuf; style = xmalloc(sizeof(Style)); style->name = xstrdup(name); prebuf = strbuf_new(); postbuf = strbuf_new(); for (it = llist_iterator(spec); iterator_has_next(it); ) { StyleInfo *info = iterator_next(it); if (info->type == STYLEINFO_PRE) { strbuf_append(prebuf, info->value); } else if (info->type == STYLEINFO_POST) { strbuf_prepend(postbuf, info->value); } else if (info->type == STYLEINFO_STYLE) { const Style *add_style = info->value; strbuf_append(prebuf, add_style->pre_string); strbuf_prepend(postbuf, add_style->post_string); } } style->pre_string = strbuf_free_to_string(prebuf); style->post_string = strbuf_free_to_string(postbuf); style->refs = 1; return style; }
static clj_Result read_typed_string(clj_Type type, const wchar_t *prefix, clj_Reader *r) { wint_t c; StringBuffer strbuf; int escape = 0; strbuf_init(&strbuf, 80); // C'mon now, how big is your terminal? strbuf_appends(&strbuf, prefix); while (1) { c = pop_char(r); switch (c) { case WEOF: strbuf_free(&strbuf); reader_error(r, CLJ_UNEXPECTED_EOF); case L'\\': strbuf_append(&strbuf, c); escape = !escape; break; case L'"': strbuf_append(&strbuf, c); if (escape) { escape = 0; break; } else { emit(r, type, strbuf.chars); strbuf_free(&strbuf); return CLJ_MORE; } default: escape = 0; strbuf_append(&strbuf, c); } } }
char *underline(char linechar, const char *str) { size_t i; char *tmp; STRBUF *line; size_t charlen = charlen_utf8(str); if (str[0] == '\0') { tmp = ymalloc(1); tmp[0] = '\0'; return tmp; } line = strbuf_new(); strbuf_append(line, str); strbuf_append(line, "\n"); tmp = ymalloc(charlen); for (i = 0; i < charlen; i++) { tmp[i] = linechar; } strbuf_append_n(line, tmp, charlen); yfree(tmp); strbuf_append(line, "\n\n"); return strbuf_spit(line); }
static FILE * extract_outfile_gen(char **outname_ptr, int w, int h, int bc, int i) { char *inname = *outname_ptr; if (output == NULL || is_directory(output)) { StrBuf *outname; char *inbase; outname = strbuf_new(); if (output != NULL) { strbuf_append(outname, output); if (!ends_with(output, "/")) strbuf_append(outname, "/"); } inbase = strrchr(inname, '/'); inbase = (inbase == NULL ? inname : inbase+1); if (ends_with_nocase(inbase, ".ico") || ends_with_nocase(inbase, ".cur")) { strbuf_append_substring(outname, inbase, 0, strlen(inbase)-4); } else { strbuf_append(outname, inbase); } strbuf_appendf(outname, "_%d_%dx%dx%d.png", i, w, h, bc); *outname_ptr = strbuf_free_to_string(outname); return fopen(*outname_ptr, "wb"); } else if (strcmp(output, "-") == 0) { *outname_ptr = xstrdup(_("(standard out)")); return stdout; } *outname_ptr = xstrdup(output); return fopen(output, "wb"); }
/* * mingled name = 'name' + '_' + type + '_' + 'indent_level' * */ char* vtable_mingle_name(VtableEntry entry) { HEX_ASSERT(entry); HEX_ASSERT(entry->name); Strbuf strbuf = strbuf_create(); HEX_ASSERT(strbuf); char type_str[2]; char indent_level_str[5]; snprintf(type_str, sizeof(type_str), "%u", (unsigned int)entry->type); snprintf(indent_level_str, sizeof(indent_level_str), "%u", entry->indent_level); strbuf_append(strbuf, (const char*)entry->name); strbuf_append(strbuf, "_"); strbuf_append(strbuf, (const char*)type_str); strbuf_append(strbuf, "_"); strbuf_append(strbuf, (const char*)indent_level_str); char *mingled_name = strbuf_cstr(strbuf); HEX_ASSERT(mingled_name); HEX_FREE(strbuf); return mingled_name; }
void get_node_name(strbuf_t *node, strbuf_t *name) { const char *ptr = node->buf+1; strbuf_zero(name); while(*ptr != '>' && !isspace(*ptr)) ptr++; strbuf_append(name, node->buf+1, ptr-node->buf-1); strbuf_append(name, "", 1); }
bool generator_add_simple_unescaped(Generator* self, const char* name, const char* value)/*{{{*/ { if (STATE_IDLE != self->state) { synce_error("Missing call to generator_end_line()"); return false; } strbuf_append(self->buffer, name); strbuf_append_c(self->buffer, ':'); strbuf_append(self->buffer, value); strbuf_append_crlf(self->buffer); return true; }/*}}}*/
bool generator_add_simple_propval(Generator* self, const char* name, CEPROPVAL* propval)/*{{{*/ { bool success = false; if (STATE_IDLE != self->state) { synce_error("Missing call to generator_end_line()"); return false; } switch (propval->propid & 0xffff) { case CEVT_LPWSTR: /* do not add empty strings */ if (propval->val.lpwstr[0]) { strbuf_append(self->buffer, name); strbuf_append_c(self->buffer, ':'); generator_append_escaped_wstr(self, propval->val.lpwstr); strbuf_append_crlf(self->buffer); } success = true; break; default: synce_error("Data type not handled"); break; } return success; }/*}}}*/
char* read_file(char *path) { HEX_ASSERT(path); Strbuf strbuf = strbuf_create(); HEX_ASSERT(strbuf); char c; while( (c = fgetc(_f) ) != EOF ) { if(c == '\n') continue; char s[2]; memset(s, 0, sizeof(s)); snprintf(s, sizeof(s), "%c", c); strbuf_append(strbuf, s); } char *str = strdup(strbuf_cstr(strbuf)); HEX_ASSERT(str); strbuf_free(&strbuf); return str; }
static BOOL stream_chr(stream_t *stream, strbuf_t *buf, char c) { BOOL b = TRUE; ULONG i; while(b) { for(i=stream->p; i<stream->size; i++) { if(stream->buf[i] == c) { b = FALSE; break; } } if(buf && i > stream->p) strbuf_append(buf, stream->buf+stream->p, i-stream->p); stream->p = i; if(stream->p == stream->size) { stream->p = 0; IStream_Read(stream->str, stream->buf, sizeof(stream->buf), &stream->size); if(!stream->size) break; } } return stream->size != 0; }
BOOL next_node(stream_t *stream, strbuf_t *buf) { strbuf_t tmpbuf; /* search through the end of the current node */ strbuf_init(&tmpbuf); if(!find_node_end(stream, &tmpbuf)) { strbuf_free(&tmpbuf); return FALSE; } strbuf_free(&tmpbuf); /* find the beginning of the next node */ if(!stream_chr(stream, NULL, '<')) return FALSE; /* read out the data of the next node */ if(!find_node_end(stream, buf)) return FALSE; strbuf_append(buf, ">", 2); return TRUE; }
bool generator_add_with_type(Generator* self, const char* name, /*{{{*/ const char* type, const char* value) { if (STATE_IDLE != self->state) { synce_error("Missing call to generator_end_line()"); return false; } strbuf_append(self->buffer, name); strbuf_append(self->buffer, ";VALUE="); strbuf_append(self->buffer, type); strbuf_append_c(self->buffer, ':'); generator_append_escaped(self, value); strbuf_append_crlf(self->buffer); return true; }/*}}}*/
void collect(strbuf_t **buf, FILE *stream) { char next[BUFSIZ]; while (!feof(stream)) { size_t size = fread(next, sizeof(char), ASIZE(next), stream); strbuf_append(buf, next, size); } }
void cwt_input_set_text(CWT_WIDGET_PTR widget, const CWT_CHAR *text) { CWT_INPUT_PTR input = (CWT_INPUT_PTR)widget; JQ_ASSERT(widget->wtype == CWT_WT_INPUT); strbuf_clear(input->strbuf); if( strlen(text) <= input->limit ) { strbuf_append(input->strbuf, text); } else { CWT_CHAR *tmp = jq_strndup(text, input->limit); strbuf_append(input->strbuf, tmp); JQ_FREE(tmp); } input->pos = strbuf_length(input->strbuf); cwt_widget_update(widget); }
static clj_Result read_token(clj_Type type, clj_Reader *r, wint_t initch, size_t initial_capacity, char_pred terminates) { wint_t c; StringBuffer strbuf; strbuf_init(&strbuf, initial_capacity); strbuf_append(&strbuf, initch); while (1) { c = pop_char(r); if (WEOF == c || is_clj_whitespace(c) || terminates(c)) { push_char(r, c); emit(r, type, strbuf.chars); strbuf_free(&strbuf); break; } else { strbuf_append(&strbuf, c); } } return ok_read(c); }
/* See section 4.2 of RFC 2616 for header format. */ int http_parse_header(struct http_header **result, const char *header) { const char *p, *q; size_t value_len, value_offset; struct http_header *node, **prev; *result = NULL; prev = result; p = header; while (*p != '\0' && !is_crlf(p)) { /* Get the field name. */ q = p; while (*q != '\0' && is_token_char(*q)) q++; if (*q != ':') { http_header_free(*result); return 400; } node = (struct http_header *) safe_malloc(sizeof(*node)); node->name = mkstr(p, q); node->value = NULL; node->next = NULL; value_len = 0; value_offset = 0; /* Copy the header field value until we hit a CRLF. */ p = q + 1; p = skip_lws(p); for (;;) { q = p; while (*q != '\0' && !is_space_char(*q) && !is_crlf(q)) { /* Section 2.2 of RFC 2616 disallows control characters. */ if (iscntrl((int) (unsigned char) *q)) { http_header_node_free(node); return 400; } q++; } strbuf_append(&node->value, &value_len, &value_offset, p, q - p); p = skip_lws(q); if (is_crlf(p)) break; /* Replace LWS with a single space. */ strbuf_append_str(&node->value, &value_len, &value_offset, " "); } *prev = node; prev = &node->next; p = skip_crlf(p); } return 0; }
static const char *read_quoted_string(const char *s, char **quoted_string) { char *buf = NULL; size_t size = 0, offset = 0; const char *t; while (is_space_char(*s)) s++; if (*s != '"') return NULL; s++; t = s; while (*s != '"') { /* Get a block of normal characters. */ while (*t != '"' && *t != '\\') { /* This is qdtext, which is TEXT except for CTL. */ if (is_ctl_char(*t)) { free(buf); return NULL; } t++; } strbuf_append(&buf, &size, &offset, s, t - s); /* Now possibly handle an escape. */ if (*t == '\\') { t++; /* You can only escape a CHAR, octets 0-127. But we disallow 0. */ if (*t <= 0 || *t > 127) { free(buf); return NULL; } strbuf_append(&buf, &size, &offset, t, 1); t++; } s = t; } s++; *quoted_string = buf; return s; }
void validate_regex(void* val, void* data, xmlChar* key){ struct handler_args* h = data; // fetch the prompt rule char* form_name = get_uri_part(h, QZ_URI_FORM_NAME); struct prompt_rule* rule = fetch_prompt_rule(h, form_name, key); if (rule == NULL) return; // does it have a pattern? if (rule->comp_regex == NULL) return; // is there data there to check? int subject_length = strlen(val); if (subject_length == 0) return; // does the value fit the pattern? const int ovectcount = 30; int ovector[ovectcount]; int rc; rc = pcre_exec(rule->comp_regex, NULL, val, subject_length, 0, 0, ovector, ovectcount); if (rc < 0){ // match failed if (h->data == NULL){ // Then this is the first one, start with an // explanatory note about the failure. static char* regex_failure_hdr = "One or more fields submitted failed validation.\n" "This error should have been caught by the client\n" "before the data was submitted.\n" "You may be able to recover by using your back\n" "button and correcting your data (or may not).\n\n"; h->data = new_strbuf(regex_failure_hdr, 0); content_type(h, "text/plain"); } char* error_msg; asprintf(&error_msg, "attribute %s failed regex_pattern %s rc=%d\n\n", rule->fieldname, rule->regex_pattern, rc); strbuf_append(h->data, new_strbuf(error_msg,0)); fprintf(h->log, "%f %d %s:%d " "fail attribute \"%s\" val [%s] regex_pattern %s rc=%d\n\n", gettime(), h->request_id, __func__, __LINE__, rule->fieldname, (char*)val, rule->regex_pattern, rc); free(error_msg); } }
BOOL next_node(stream_t *stream, strbuf_t *buf) { if(!stream_chr(stream, NULL, '<')) return FALSE; if(!stream_chr(stream, buf, '>')) return FALSE; strbuf_append(buf, ">", 2); return TRUE; }
bool generator_begin_line(Generator* self, const char* name)/*{{{*/ { if (STATE_IDLE != self->state) { synce_error("Missing call to generator_end_line()"); return false; } self->state = STATE_PARAM_OR_VALUE; strbuf_append(self->buffer, name); return true; }/*}}}*/
static unsigned char * rle_decode(unsigned char *data, int datalen, int width, int *decoded_size) { StrBuf *buf, *retbuf; unsigned char *ret, *tmp; int extra, x; buf = strbuf_new (); retbuf = strbuf_new (); extra = 4 - (width % 4); if (extra == 4) extra = 0; for (x = 0; x < datalen; x++) { if (data[x] == 0) { x++; tmp = (unsigned char *) strrepeat ("", 1, (int) data[x]); strbuf_append (buf, tmp, data[x]); free (tmp); } else strbuf_append (buf, &(data[x]), 1); } for (x = 0; x < buf->len; x += width) { if (extra > 0) { tmp = (unsigned char *) strrepeat ("", 1, extra); strbuf_prepend (retbuf, tmp, extra); free (tmp); } strbuf_prepend (retbuf, buf->str + x, width); } ret = retbuf->str; if (decoded_size) *decoded_size = retbuf->len; strbuf_free (retbuf, 0); strbuf_free (buf, 1); return ret; }
BOOL next_node(stream_t *stream, strbuf_t *buf) { /* find the beginning of the next node */ if(!stream_chr(stream, NULL, '<')) return FALSE; /* read out the data of the next node */ if(!find_node_end(stream, buf)) return FALSE; strbuf_append(buf, ">", 2); return TRUE; }
char * backticks(const char *program, char *const args[], int *status) { int child_pipe[2]; int child_pid; FILE *pipe_file; char *line; StrBuf *out; if (pipe(child_pipe) == -1) return NULL; child_pid = fork(); if (child_pid == -1) return NULL; if (child_pid == 0) { if (close(child_pipe[0]) == -1) die_errno(NULL); if (close(STDOUT_FILENO) == -1) die_errno(NULL); if (dup2(child_pipe[1], STDOUT_FILENO) == -1) die_errno(NULL); execvp(program, args); die_errno(NULL); } if (close(child_pipe[1]) == -1) return NULL; pipe_file = fdopen(child_pipe[0], "r"); if (pipe_file == NULL) return NULL; out = strbuf_new(); if (out == NULL) return NULL; while ((line = read_line(pipe_file)) != NULL) { strbuf_append(out, line); free(line); } if (errno != 0) return NULL; if (waitpid(child_pid, status, 0) != child_pid) return NULL; if (fclose(pipe_file) != 0) return NULL; return strbuf_free_to_string(out); }
bool generator_begin_parameter(Generator* self, const char* name)/*{{{*/ { if (STATE_PARAM_OR_VALUE != self->state) { synce_error("Invalid state: %i", self->state); return false; } self->state = STATE_FIRST_PARAM; strbuf_append_c(self->buffer, ';'); strbuf_append(self->buffer, name); strbuf_append_c(self->buffer, '='); return true; }/*}}}*/
int error_append(errorstream_t *error, const char *msg, ...) { error->error_count++; int result = SQLITE_OK; if (msg) { va_list args; va_start(args, msg); result = strbuf_vappend(&error->message, msg, args); va_end(args); if (result == SQLITE_OK) { result = strbuf_append(&error->message, "\n"); } } return result; }
char * strbuf_getline(idnconv_strbuf_t *buf, FILE *fp) { /* * Read a line from FP. */ char s[256]; buf->str[0] = '\0'; while (fgets(s, sizeof(s), fp) != NULL) { if (strbuf_append(buf, s) == NULL) return (NULL); if (strlen(s) < sizeof(s) - 1 || s[sizeof(s) - 2] == '\n') return (buf->str); } if (buf->str[0] != '\0') return (buf->str); return (NULL); }
bool generator_add_parameter_value(Generator* self, const char* value)/*{{{*/ { if (STATE_NEXT_PARAM == self->state) { strbuf_append_c(self->buffer, ','); } else if (STATE_FIRST_PARAM != self->state) { synce_error("Invalid state: %i", self->state); return false; } self->state = STATE_NEXT_PARAM; /* XXX: escape string? */ strbuf_append(self->buffer, value); return true; }/*}}}*/
static char * var_get_display_flags(DCVariable *var) { StrBuf *out; uint32_t c; uint32_t flags; flags = *(uint32_t *) var->value; out = strbuf_new(); for (c = 0; c < display_flag_count; c++) { if (flags & display_flag_details[c].flag) { if (!strbuf_is_empty(out)) strbuf_append_char(out, ' '); strbuf_append(out, display_flag_details[c].name); } } return strbuf_free_to_string(out); }
static int append_quoted_string(char **buf, size_t *size, size_t *offset, const char *s) { const char *t; strbuf_append_str(buf, size, offset, "\""); for (;;) { t = s; while (!((*t >= 0 && *t <= 31) || *t == 127 || *t == '\\')) t++; strbuf_append(buf, size, offset, s, t - s); if (*t == '\0') break; strbuf_sprintf(buf, size, offset, "\\%c", *t); s = t + 1; } strbuf_append_str(buf, size, offset, "\""); return *size; }
static unsigned char * reverse_palette (unsigned char *palette, int palettelen, int *returnsize) { StrBuf *retbuf; unsigned char *ret; int x; retbuf = strbuf_new (); for (x = 0; x < palettelen; x += 4) { unsigned char tmp[4]; tmp[0] = palette[x + 2]; tmp[1] = palette[x + 1]; tmp[2] = palette[x]; tmp[3] = '\0'; strbuf_append (retbuf, tmp, 4); } ret = retbuf->str; if (returnsize) *returnsize = retbuf->len; strbuf_free (retbuf, 0); return ret; }