static void __com_val(nvlist_t *post, list_t *list) { struct comment *cur; nvlist_t **comments; uint_t ncomments; int i; /* count the comments */ ncomments = 0; list_for_each(list, cur) ncomments++; comments = malloc(sizeof(nvlist_t *) * ncomments); ASSERT(comments); i = 0; list_for_each(list, cur) { comments[i] = nvl_alloc(); nvl_set_int(comments[i], "commid", cur->id); nvl_set_int(comments[i], "commtime", cur->time); nvl_set_str(comments[i], "commauthor", str_cstr(cur->author)); nvl_set_str(comments[i], "commemail", str_cstr(cur->email)); nvl_set_str(comments[i], "commip", str_cstr(cur->ip)); nvl_set_str(comments[i], "commurl", str_cstr(cur->url)); nvl_set_str(comments[i], "commbody", str_cstr(cur->body)); i++; }
static int medin_readf( FILE *fp, char *buf, int bufsize, int *bufpos, str *line, str *reference, int *fcharset ) { str tmp; char *startptr = NULL, *endptr; int haveref = 0, inref = 0, file_charset = CHARSET_UNKNOWN, m, type = -1; str_init( &tmp ); while ( !haveref && str_fget( fp, buf, bufsize, bufpos, line ) ) { if ( line->data ) { m = xml_getencoding( line ); if ( m!=CHARSET_UNKNOWN ) file_charset = m; } if ( line->data ) { startptr = medin_findstartwrapper( line->data, &type ); } if ( startptr || inref ) { if ( inref ) str_strcat( &tmp, line ); else { str_strcatc( &tmp, startptr ); inref = 1; } endptr = medin_findendwrapper( str_cstr( &tmp ), type ); if ( endptr ) { str_segcpy( reference, str_cstr( &tmp ), endptr ); haveref = 1; } } } str_free( &tmp ); *fcharset = file_charset; return haveref; }
static void check_file(struct val *got, char *fname, bool raw) { const char *pfx = raw ? "raw " : "pretty"; struct str *dumped; char *exp; exp = read_file(fname); if (IS_ERR(exp)) fail("failed to read expected file (%s): %s", fname, xstrerror(PTR_ERR(exp))); trim(exp); fprintf(stderr, "%s exp: %s\n", pfx, exp); dumped = sexpr_dump(got, raw); if (IS_ERR(dumped)) fail("failed to dump val: %s", xstrerror(PTR_ERR(dumped))); fprintf(stderr, "%s got: %s\n", pfx, str_cstr(dumped)); if (strcmp(str_cstr(dumped), exp)) fail("mismatch!"); free(exp); str_putref(dumped); }
/* compare two subindex entries */ static int post_tag_cmp(const void *va, const void *vb) { const struct post_subindex *a = va; const struct post_subindex *b = vb; int ret; ret = strcasecmp(str_cstr(a->name), str_cstr(b->name)); if (ret > 0) return 1; if (ret < 0) return -1; return 0; }
static struct val *__escape(struct val *val, char *(*cvt)(const char*)) { char *out; out = NULL; switch (val->type) { case VT_INT: out = str_of_int(val->i); break; case VT_STR: out = cvt(str_cstr(val->str)); break; case VT_SYM: case VT_CONS: case VT_BOOL: panic("%s called with value type %d", __func__, val->type); } val_putref(val); ASSERT(out); return VAL_ALLOC_CSTR(out); }
/* <MedlineDate>2003 Jan-Feb</MedlineDate> */ static int medin_medlinedate( fields *info, char *p, int level ) { int fstatus; str tmp; str_init( &tmp ); p = str_cpytodelim( &tmp, skip_ws( p ), " \t\n\r", 0 ); if ( str_memerr( &tmp ) ) return BIBL_ERR_MEMERR; if ( str_has_value( &tmp ) ) { fstatus = fields_add( info, "PARTDATE:YEAR", str_cstr( &tmp ), level ); if ( fstatus!=FIELDS_OK ) return BIBL_ERR_MEMERR; } p = str_cpytodelim( &tmp, skip_ws( p ), " \t\n\r", 0 ); if ( str_memerr( &tmp ) ) return BIBL_ERR_MEMERR; if ( str_has_value( &tmp ) ) { str_findreplace( &tmp, "-", "/" ); fstatus = fields_add( info, "PARTDATE:MONTH", str_cstr( &tmp ), level ); if ( fstatus!=FIELDS_OK ) return BIBL_ERR_MEMERR; } (void) str_cpytodelim( &tmp, skip_ws( p ), " \t\n\r", 0 ); if ( str_memerr( &tmp ) ) return BIBL_ERR_MEMERR; if ( str_has_value( &tmp ) ) { fstatus = fields_add( info, "PARTDATE:DAY", str_cstr( &tmp ), level ); if ( fstatus!=FIELDS_OK ) return BIBL_ERR_MEMERR; } str_free( &tmp ); return BIBL_OK; }
static void __tag_val(nvlist_t *post, avl_tree_t *list) { struct post_tag *cur; const char **tags; int ntags; int i; ntags = avl_numnodes(list); tags = malloc(sizeof(char *) * ntags); ASSERT(tags); i = 0; avl_for_each(list, cur) tags[i++] = str_cstr(cur->tag); nvl_set_str_array(post, "tags", (char **) tags, ntags); free(tags); }
struct str *listing(struct post *post, const char *fname) { char path[FILENAME_MAX]; struct str *in; snprintf(path, FILENAME_MAX, "%s/posts/%d/%s", str_cstr(config.data_dir), post->id, fname); in = file_cache_get_cb(path, post->preview ? NULL : revalidate_post, post); if (IS_ERR(in)) goto err; return listing_str(in); err: snprintf(path, FILENAME_MAX, "Failed to read in listing '%d/%s': %s", post->id, fname, xstrerror(PTR_ERR(in))); return STR_DUP(path); }
int blahg_category(struct req *req, char *cat, int page) { int catn; /* wordpress cat name */ catn = atoi(cat); if (catn && ((catn < MIN_CATN) || (catn > MAX_CATN))) return R404(req, NULL); if (catn) { char url[256]; snprintf(url, sizeof(url), "%s/?cat=%s", str_cstr(config.base_url), wordpress_catn[catn]); return R301(req, url); } return __tagcat(req, cat, page, "{catindex}", false); }
/* <Pagination> * <MedlinePgn>12111-6</MedlinePgn> * </Pagination> */ static int medin_pagination( xml *node, fields *info ) { int i, fstatus, status; str sp, ep; char *p, *pp; if ( xml_tag_matches( node, "MedlinePgn" ) && node->value.len ) { strs_init( &sp, &ep, NULL ); p = str_cpytodelim( &sp, xml_value_cstr( node ), "-", 1 ); if ( str_memerr( &sp ) ) return BIBL_ERR_MEMERR; if ( str_has_value( &sp ) ) { fstatus = fields_add( info, "PAGES:START", str_cstr( &sp ), 1 ); if ( fstatus!=FIELDS_OK ) return BIBL_ERR_MEMERR; } (void) str_cpytodelim( &ep, p, "", 0 ); if ( str_memerr( &ep ) ) return BIBL_ERR_MEMERR; if ( str_has_value( &ep ) ) { if ( sp.len > ep.len ) { for ( i=sp.len-ep.len; i<sp.len; ++i ) sp.data[i] = ep.data[i-sp.len+ep.len]; pp = sp.data; } else pp = ep.data; fstatus = fields_add( info, "PAGES:STOP", pp, 1 ); if ( fstatus!=FIELDS_OK ) return BIBL_ERR_MEMERR; } strs_free( &sp, &ep, NULL ); } if ( node->down ) { status = medin_pagination( node->down, info ); if ( status!=BIBL_OK ) return status; } if ( node->next ) { status = medin_pagination( node->next, info ); if ( status!=BIBL_OK ) return status; } return BIBL_OK; }
char* str_destroy_and_copy(Str* s) { char* copy = estrdup(str_cstr(s)); str_destroy(s); return copy; }
int config_load(const char *fname) { struct val *lv; char *raw; srand(time(NULL)); if (fname) { raw = read_file(fname); if (IS_ERR(raw)) return PTR_ERR(raw); lv = sexpr_parse_cstr(raw); free(raw); if (!lv) return -EINVAL; } else { lv = NULL; } config_load_scgi_port(lv); config_load_scgi_threads(lv); config_load_u64(lv, CONFIG_HTML_INDEX_STORIES, &config.html_index_stories, DEFAULT_HTML_INDEX_STORIES); config_load_u64(lv, CONFIG_FEED_INDEX_STORIES, &config.feed_index_stories, DEFAULT_FEED_INDEX_STORIES); config_load_u64(lv, CONFIG_COMMENT_MAX_THINK, &config.comment_max_think, DEFAULT_COMMENT_MAX_THINK); config_load_u64(lv, CONFIG_COMMENT_MIN_THINK, &config.comment_min_think, DEFAULT_COMMENT_MIN_THINK); config_load_u64(lv, CONFIG_COMMENT_CAPTCHA_A, &config.comment_captcha_a, rand()); config_load_u64(lv, CONFIG_COMMENT_CAPTCHA_B, &config.comment_captcha_b, rand()); config_load_str(lv, CONFIG_DATA_DIR, &config.data_dir, DEFAULT_DATA_DIR); config_load_str(lv, CONFIG_WEB_DIR, &config.web_dir, DEFAULT_WEB_DIR); config_load_url(lv, CONFIG_BASE_URL, &config.base_url); config_load_url(lv, CONFIG_BUG_BASE_URL, &config.bug_base_url); config_load_url(lv, CONFIG_WIKI_BASE_URL, &config.wiki_base_url); config_load_url(lv, CONFIG_PHOTO_BASE_URL, &config.photo_base_url); config_load_u64(lv, CONFIG_TAGCLOUD_MIN_SIZE, &config.tagcloud_min_size, DEFAULT_TAGCLOUD_MIN_SIZE); config_load_u64(lv, CONFIG_TAGCLOUD_MAX_SIZE, &config.tagcloud_max_size, DEFAULT_TAGCLOUD_MAX_SIZE); config_load_str(lv, CONFIG_LATEX_BIN, &config.latex_bin, DEFAULT_LATEX_BIN); config_load_str(lv, CONFIG_DVIPS_BIN, &config.dvips_bin, DEFAULT_DVIPS_BIN); config_load_str(lv, CONFIG_CONVERT_BIN, &config.convert_bin, DEFAULT_CONVERT_BIN); config_load_str(lv, CONFIG_TWITTER_USERNAME, &config.twitter_username, NULL); config_load_str(lv, CONFIG_TWITTER_DESCRIPTION, &config.twitter_description, NULL); val_putref(lv); DBG("config.scgi_port = %u", config.scgi_port); DBG("config.scgi_threads = %"PRIu64, config.scgi_threads); DBG("config.html_index_stories = %"PRIu64, config.html_index_stories); DBG("config.feed_index_stories = %"PRIu64, config.feed_index_stories); DBG("config.comment_max_think = %"PRIu64, config.comment_max_think); DBG("config.comment_min_think = %"PRIu64, config.comment_min_think); DBG("config.comment_captcha_a = %"PRIu64, config.comment_captcha_a); DBG("config.comment_captcha_b = %"PRIu64, config.comment_captcha_b); DBG("config.data_dir = %s", str_cstr(config.data_dir)); DBG("config.web_dir = %s", str_cstr(config.web_dir)); DBG("config.base_url = %s", str_cstr(config.base_url)); DBG("config.wiki_base_url = %s", str_cstr(config.wiki_base_url)); DBG("config.bug_base_url = %s", str_cstr(config.bug_base_url)); DBG("config.photo_base_url = %s", str_cstr(config.photo_base_url)); DBG("config.tagcloud_min_size = %"PRIu64, config.tagcloud_min_size); DBG("config.tagcloud_max_size = %"PRIu64, config.tagcloud_max_size); DBG("config.latex_bin = %s", str_cstr(config.latex_bin)); DBG("config.dvips_bin = %s", str_cstr(config.dvips_bin)); DBG("config.convert_bin = %s", str_cstr(config.convert_bin)); DBG("config.twitter_username = %s", str_cstr(config.twitter_username)); DBG("config.twitter_description = %s", str_cstr(config.twitter_description)); return 0; }
//---------------------------------------------------------------------------- qplot_xy_t qplot_read_text( const std::string file, long start, long size, long step, char separator, int xCol, int yCol) { qplot_xy_t data; data.size = 0; qDebug("qplot_read_text(file='%s')", file.c_str()); long cnt = 0; long step_cnt = 0; if (xCol < 0 && yCol < 0) return data; // stupid mission std::ifstream fs(file.c_str()); std::string line; while (std::getline(fs, line)) { if (start != 0) { start--; continue; } if (step_cnt-- != 0) continue; step_cnt = step - 1; if (size != -1 && cnt >= size) break; std::stringstream str_stream(line); std::string cell; std::vector<std::string> cells; cells.clear(); while (std::getline(str_stream, cell, separator)) { //!!! FIXME (use standart trim algoritm next time) str_t str = str_cstr(cell.c_str()); str_t str_trimmed = str_trim(&str); if (separator == ' ') { if (str_size(&str_trimmed)) cells.push_back(str_c(&str_trimmed)); } else cells.push_back(str_c(&str_trimmed)); str_free(&str_trimmed); str_free(&str); } if (yCol < 0) { // xCol >= 0 && yCol < 0 if ((int) cells.size() > xCol) if (cells[yCol].size()) { data.x.push_back(atof(cells[xCol].c_str())); data.y.push_back((double) cnt); cnt++; } } else if (xCol < 0) { // xCol < 0 && yCol >= 0 if ((int) cells.size() > yCol) if (cells[yCol].size()) { data.x.push_back((double) cnt); data.y.push_back(atof(cells[yCol].c_str())); cnt++; } } else { // xCol >= 0 && yCol >= 0 if ((int) cells.size() > xCol && (int) cells.size() > yCol) if (cells[xCol].size() && cells[yCol].size()) { data.x.push_back(atof(cells[xCol].c_str())); data.y.push_back(atof(cells[yCol].c_str())); cnt++; } } } // while (std::getline(fs, line)) data.size = cnt; return data; }