示例#1
0
static int diary_handle_feed_rss(request_rec *r, diary_conf *conf)
{
    HDF *hdf;
    CSPARSE *cs;
    NEOERR *cs_err;
    STRING cs_err_str;

    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "diary_handle_feed_rss()");

    hdf_init(&hdf);
    hdf_set_value(hdf, "hdf.loadpaths.1", conf->path);
    hdf_set_value(hdf, "diary.title", conf->title);
    hdf_set_value(hdf, "diary.uri", conf->uri);

    cs_err = hdf_read_file(hdf, INDEX_HDF);
    if(cs_err){
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "cannot read index.hdf.");
        hdf_destroy(&hdf);
        return HTTP_INTERNAL_SERVER_ERROR;
    }
    //hdf_dump(hdf, NULL);

    cs_err = cs_init(&cs, hdf);
    if(cs_err){
        string_init(&cs_err_str);
        nerr_error_string(cs_err, &cs_err_str);
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
                      "error at cs_init(): %s", cs_err_str.buf);
        cs_destroy(&cs);
        hdf_destroy(&hdf);
        return HTTP_INTERNAL_SERVER_ERROR;
    }

    cgi_register_strfuncs(cs);

    cs_err = cs_parse_string(cs, strdup(RSS_TMPL), RSS_TMPL_LEN);
    if(cs_err){
        string_init(&cs_err_str);
        nerr_error_string(cs_err, &cs_err_str);
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
                      "error in cs_parse_string(): %s", cs_err_str.buf);
        cs_destroy(&cs);
        hdf_destroy(&hdf);
        return HTTP_INTERNAL_SERVER_ERROR;
    }

    r->content_type = "application/rss+xml";
    cs_render(cs, r, diary_cs_render_cb);

    cs_destroy(&cs);
    hdf_destroy(&hdf);
    return OK;
}
示例#2
0
文件: main.c 项目: ahma88/magro
void savesrc(char* templatedir, char* csfname, char* outfname, HDF* hdf)
{
	CSPARSE* parse;
	NEOERR* err;
	FILE* fp;

	nerr_init();

	chdir(templatedir);
	printf("reading %s ... ", csfname);
    err = cs_init(&parse, hdf);
    if( nerr_match(err, NERR_PASS) ) { printf("NG\n"); nerr_log_error(err); exit(41); } 
	err = cs_parse_file(parse, csfname);
    if( nerr_match(err, NERR_PASS) ) { printf("NG\n"); nerr_log_error(err); exit(42); } 
	printf("OK\n");

	chdir(boot_path);
	chdir(CODEGEN_DIRNAME);
	printf("writing %s ... ", outfname);
	fp = fopen(outfname, "w");
    if( fp != NULL )
	{
		err = cs_render(parse, (void*)fp, render);
		fclose(fp);
    	if( nerr_match(err, NERR_PASS) ) { printf("NG\n"); exit(43); } 
		printf("OK\n");
	}else{
		printf("can't open file");
	}
    cs_destroy(&parse);
}
示例#3
0
int process_template(HDF *hdf, char *infile, char *outfile)
{
  int ret = 0;
  FILE *outfh = NULL;
  NEOERR *err;
  CSPARSE *parse = NULL;

  if (!make_dirs(outfile))
  {
    ret = -1;
  }
  else
  {
    if ((outfh = fopen(outfile, "w")) == NULL)
    {
      perror("fopen");
      ret = -2;
    }
    else
    {
      err = cs_init(&parse, hdf);
      if (err != STATUS_OK)
      {
        nerr_log_error(err);
        ret = -5;
      }
      else
      { 
        err = cs_parse_file(parse, infile);
        if (err != STATUS_OK)
        {
          nerr_log_error(err);
          ret = -6;
        }
        
        err = cs_render(parse, outfh, csoutfunc);  
        if (err != STATUS_OK)
        {
          nerr_log_error(err);
          ret = -7;
        }

        cs_destroy(&parse);
      }
      fclose(outfh);
    }
  }
  return ret;
}
示例#4
0
void formatter_kml_split_output(formatter_split_t *obj, char *filename) {
    FILE *fp = fopen(filename, "w");

    CSPARSE *csparse;
    HDF *hdf;
    NEOERR *err;
    int stage = 0;
    if ((err = hdf_init(&hdf)) != STATUS_OK) {
        goto error;
    }

    coordinate_subset_t *subset = obj->set->first_subset;
    int16_t i = 0;
    while (subset) {
        _cs_set_valuef(hdf, "tracks.%d.colour=%s", i, kml_colours[i % 9]);
        coordinate_t *coordinate = subset->first;
        int16_t j = 0;
        while (coordinate && coordinate != subset->last) {
            _cs_set_valuef(hdf, "tracks.%d.points.%d.lng=%f", i, j, coordinate->lng);
            _cs_set_valuef(hdf, "tracks.%d.points.%d.lat=%f", i, j, coordinate->lat);
            _cs_set_valuef(hdf, "tracks.%d.points.%d.ele=%d", i, j, coordinate->ele);
            _cs_set_valuef(hdf, "tracks.%d.points.%d.time=%f", i, j, coordinate->timestamp);
            coordinate = coordinate->next;
            j++;
        }
        subset = subset->next;
        i++;
    }

    if ((err = cs_init(&csparse, hdf)) != STATUS_OK ||
        (err = cs_parse_file(csparse, "formatter/templates/flight.split.kml.cs.xml")) != STATUS_OK ||
        (err = cs_render(csparse, fp, cs_fwrite)) != STATUS_OK) {
        goto error;
    }

    goto end;

    error:
    nerr_log_error(err);
    goto end;

    end:
    hdf_destroy(&hdf);
    cs_destroy(&csparse);
    fclose(fp);
}
示例#5
0
int main(void)
{
  GC_INIT();
  TF_Buffer *buffer = TF_GetAllOpList();
  Tensorflow__OpList *op_list = tensorflow__op_list__unpack(NULL, buffer->length, buffer->data);

  HDF *hdf;
  hdf_init(&hdf);

  for (int i=0; i<op_list->n_op; i++) {
    struct _Tensorflow__OpDef *op = op_list->op[i];
    char variable[256];
    char value[256];
    snprintf(variable, 256, "Op.%s.name", op->name);
    snprintf(value, 256, "tf-%s", kebab_case(op->name));
    hdf_set_value(hdf, variable, value);
    for (int j=0; j<op->n_input_arg; j++) {
      Tensorflow__OpDef__ArgDef *arg = op->input_arg[j];
      snprintf(variable, 256, "Op.%s.input_arg.%s", op->name, arg->name);
      const char *multiple = arg->number_attr && *arg->number_attr ? "list" : "single";
      hdf_set_value(hdf, variable, multiple);
    };
    for (int j=0; j<op->n_attr; j++) {
      Tensorflow__OpDef__AttrDef *attr = op->attr[j];
      snprintf(variable, 256, "Op.%s.attr.%s", op->name, attr->name);
      snprintf(value, 256, "%s", attr->type);
      hdf_set_value(hdf, variable, value);
    };
    snprintf(variable, 256, "Op.%s.n_output", op->name);
    snprintf(value, 256, "%d", op->n_output_arg);
    hdf_set_value(hdf, variable, value);
  };

  CSPARSE *parse;
  cs_init(&parse, hdf);
  cs_parse_file(parse, "tensorflow.scm.in");
  cs_render(parse, stdout, output);

  cs_destroy(&parse);
  hdf_destroy(&hdf);

  tensorflow__op_list__free_unpacked(op_list, NULL);
  TF_DeleteBuffer(buffer);
}
示例#6
0
static void wiki_output(MMIOT *doc, request_rec *r)
{
    char *title;
    int ret;
    int size;
    char *p;
    wiki_conf *conf;
    list_t *css;
    HDF *hdf;
    CSPARSE *cs;
    int i;

    conf =
        (wiki_conf *) ap_get_module_config(r->per_dir_config,
                                           &wiki_module);
    ret = mkd_compile(doc, MKD_TOC | MKD_AUTOLINK);

    hdf_init(&hdf);

    if(conf->name){
        hdf_set_value(hdf, "wikiname", conf->name);
    }

    title = mkd_doc_title(doc);
    if(title == NULL){
        title = "notitle";
    }
    hdf_set_value(hdf, "title", title);

    for(i=0, css = conf->css; css; i++, css = (list_t *) css->next){
        hdf_set_valuef(hdf, "css.%d=%s", i, (char *)css->data);
    }

    if ((size = mkd_document(doc, &p)) != EOF) {
        hdf_set_value(hdf, "document", p);
    }

    cs_init(&cs, hdf);
    cs_parse_string(cs, strdup(DEFAULT_TEMPLATE), strlen(DEFAULT_TEMPLATE));
    cs_render(cs, r, cs_output);
    hdf_destroy(&hdf);
    cs_destroy(&cs);
}
示例#7
0
int main (int argc, char *argv[])
{
  NEOERR *err;
  CSPARSE *parse;
  HDF *hdf;
  int verbose = 0;
  char *hdf_file, *cs_file;

  if (argc < 3)
  {
    ne_warn ("Usage: cstest [-v] <file.hdf> <file.cs>");
    return -1;
  }

  if (!strcmp(argv[1], "-v"))
  {
    verbose = 1;
    if (argc < 4)
    {
      ne_warn ("Usage: cstest [-v] <file.hdf> <file.cs>");
      return -1;
    }
    hdf_file = argv[2];
    cs_file = argv[3];
  }
  else
  {
    hdf_file = argv[1];
    cs_file = argv[2];
  }

  err = hdf_init(&hdf);
  if (err != STATUS_OK)
  {
    nerr_warn_error(err);
    return -1;
  }
  err = hdf_read_file(hdf, hdf_file);
  if (err != STATUS_OK)
  {
    nerr_warn_error(err);
    return -1;
  }

  printf ("Parsing %s\n", cs_file);
  err = cs_init (&parse, hdf);
  if (err != STATUS_OK)
  {
    nerr_warn_error(err);
    return -1;
  }
  err = cgi_register_strfuncs(parse);
  if (err != STATUS_OK)
  {
    nerr_warn_error(err);
    return -1;
  }

  err = cs_parse_file (parse, cs_file);
  if (err != STATUS_OK)
  {
    err = nerr_pass(err);
    nerr_warn_error(err);
    return -1;
  }

  err = cs_render(parse, NULL, output);
  if (err != STATUS_OK)
  {
    err = nerr_pass(err);
    nerr_warn_error(err);
    return -1;
  }

  if (verbose)
  {
    printf ("\n-----------------------\nCS DUMP\n");
    err = cs_dump(parse, NULL, output);
  }

  cs_destroy (&parse);

  if (verbose)
  {
    printf ("\n-----------------------\nHDF DUMP\n");
    hdf_dump (hdf, NULL);
  }
  hdf_destroy(&hdf);


  return 0;
}
示例#8
0
bool Tree::RenderTemplateInternal(const std::string &tmpl,
                                  bool file_or_string,
                                  std::ostream *out,
                                  std::string *error) const
{
    if (!out) {
        return false;
    }

    CSPARSE *csparse = NULL;
    HDF *hdf = NULL;
    NEOERR *err;

    do {
        err = hdf_init(&hdf);
        if (err != STATUS_OK) {
            break;
        }

        if (!SerializeToHdfInternal(hdf, true)) {
            hdf_destroy(&hdf);
            if (error) {
                *error = "SerializationError: serializing to HDF failed";
            }
            return false;
        }

        err = cs_init(&csparse, hdf);
        if (err != STATUS_OK) {
            break;
        }

        err = cgi_register_strfuncs(csparse);
        if (err != STATUS_OK) {
            break;
        }

        if (file_or_string) {
            err = cs_parse_file(csparse, tmpl.c_str());

        } else {
            char *ctmpl = strdup(tmpl.c_str());
            if (!ctmpl) {
                cs_destroy(&csparse);
                hdf_destroy(&hdf);
                if (error) {
                    *error = "MemoryError: allocating buffer for template";
                }
                break;
            }

            err = cs_parse_string(csparse, ctmpl, tmpl.length());
        }

        if (err != STATUS_OK) {
            break;
        }

        err = cs_render(csparse, out, RenderCallback);
        if (err != STATUS_OK) {
            break;
        }

    } while (false);

    cs_destroy(&csparse);
    hdf_destroy(&hdf);

    if (err != STATUS_OK && error) {
        STRING str;
        string_init(&str);
        nerr_error_string(err, &str);
        *error = str.buf;
        string_clear(&str);
        nerr_ignore(&err);
        return false;
    }

    return true;
}
示例#9
0
static int diary_handle_entry(request_rec *r,
                              diary_conf *conf,
                              const char *filename)
{
    FILE *fp;
    CSPARSE *cs;
    NEOERR *cs_err;
    HDF *hdf;
    MMIOT *doc;
    char *title;
    char *author;
    char *date;
    int size;
    char *p;
    int flag = 0;
    int github_flavoured = conf->github_flavoured;
    calendar_info cal;
    char *theme_path;
    char *theme_file;

    theme_path = apr_pstrcat(r->pool, conf->path, "/themes/", conf->theme, NULL);
    theme_file = apr_pstrcat(r->pool, theme_path, "/index.cst", NULL);

    fp = fopen(filename, "r");
    if(fp == NULL){
        switch (errno) {
        case ENOENT:
            return HTTP_NOT_FOUND;
        case EACCES:
            return HTTP_FORBIDDEN;
        default:
            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
                          "diary_parse_entry error: errno=%d\n", errno);
            return HTTP_INTERNAL_SERVER_ERROR;
        }
    }
    doc = github_flavoured ? gfm_in(fp, 0) : mkd_in(fp, 0);
    fclose(fp);
    if (doc == NULL) {
        return HTTP_INTERNAL_SERVER_ERROR;
    }

    title = mkd_doc_title(doc);
    if(title == NULL){
        title = "notitle";
    }
    date = mkd_doc_date(doc);
    author = mkd_doc_author(doc);

    if(conf->autolink){
        flag = MKD_AUTOLINK;
    }
    mkd_compile(doc, flag);
    if ((size = mkd_document(doc, &p)) == EOF) {
        return HTTP_INTERNAL_SERVER_ERROR;
    }

    hdf_init(&hdf);

    hdf_set_value(hdf, "hdf.loadpaths.1", conf->path);
    hdf_set_value(hdf, "hdf.loadpaths.2", theme_path);

    cs_err = hdf_read_file(hdf, INDEX_HDF);
    if(cs_err){
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "cannot read index.hdf.");
        // TODO: no need to free cs_err and cs_err_str?
        hdf_destroy(&hdf);
        return HTTP_INTERNAL_SERVER_ERROR;
    }

    hdf_set_value(hdf, "diary.title", conf->title);
    hdf_set_value(hdf, "diary.uri", conf->uri);
    hdf_set_value(hdf, "diary.theme", conf->theme);

    hdf_set_value(hdf, "entry.uri", r->uri);
    hdf_set_value(hdf, "entry.title", title);
    hdf_set_value(hdf, "entry.author", author);
    hdf_set_value(hdf, "entry.date", date);
    hdf_set_value(hdf, "entry.desc", p);
    //hdf_dump(hdf, NULL);

    if (conf->calendar) {
        diary_set_calendar_info(&cal, r->args);
        hdf_set_int_value(hdf, "cal.year", cal.year);
        hdf_set_value(hdf, "cal.month", cal.month);
        hdf_set_value(hdf, "cal.day", cal.day);   
        hdf_set_value(hdf, "cal.today", cal.today);
        hdf_set_int_value(hdf, "cal.lastdayofmonth", cal.lastdayofmonth);
        hdf_set_int_value(hdf, "cal.dayofweek_1stdayofmonth", cal.dayofweek_1stdayofmonth);
    }
   
    cs_err = cs_init(&cs, hdf);
    if(cs_err){
        return HTTP_INTERNAL_SERVER_ERROR;
    }
    cgi_register_strfuncs(cs);
    mkd_cleanup(doc);
    cs_parse_file(cs, theme_file);

    r->content_type = "text/html";
    cs_render(cs, r, diary_cs_render_cb);

    hdf_destroy(&hdf);
    cs_destroy(&cs);
    return 0;
}
示例#10
0
static int diary_handle_index(request_rec *r, diary_conf *conf)
{
    HDF *hdf;
    CSPARSE *cs;
    NEOERR *cs_err;
    STRING cs_err_str;
    calendar_info cal;
    char *theme_path;
    char *theme_file;

    theme_path = apr_pstrcat(r->pool, conf->path, "/themes/", conf->theme, NULL);
    theme_file = apr_pstrcat(r->pool, theme_path, "/index.cst", NULL);

    hdf_init(&hdf);
    hdf_set_int_value(hdf, "index", 1);
    hdf_set_value(hdf, "hdf.loadpaths.1", conf->path);
    hdf_set_value(hdf, "hdf.loadpaths.2", theme_path);
    hdf_set_value(hdf, "diary.title", conf->title);
    hdf_set_value(hdf, "diary.uri", conf->uri);
    hdf_set_value(hdf, "diary.theme", conf->theme);

    if (conf->calendar) {
        diary_set_calendar_info(&cal, r->args);
        hdf_set_int_value(hdf, "cal.year", cal.year);
        hdf_set_value(hdf, "cal.month", cal.month);
        hdf_set_value(hdf, "cal.day", cal.day);   
        hdf_set_value(hdf, "cal.today", cal.today);
        hdf_set_int_value(hdf, "cal.lastdayofmonth", cal.lastdayofmonth);
        hdf_set_int_value(hdf, "cal.dayofweek_1stdayofmonth", cal.dayofweek_1stdayofmonth);
    }

    cs_err = hdf_read_file(hdf, INDEX_HDF);
    if(cs_err){
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "cannot read index.hdf.");
        // TODO: no need to free cs_err and cs_err_str?
        hdf_destroy(&hdf);
        return HTTP_INTERNAL_SERVER_ERROR;
    }
    //hdf_dump(hdf, NULL);

    cs_err = cs_init(&cs, hdf);
    if(cs_err){
        string_init(&cs_err_str);
        nerr_error_string(cs_err, &cs_err_str);
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
                      "error at cs_init(): %s", cs_err_str.buf);
        // TODO: no need to free cs_err and cs_err_str?
        cs_destroy(&cs);
        hdf_destroy(&hdf);
        return HTTP_INTERNAL_SERVER_ERROR;
    }
    cgi_register_strfuncs(cs);

    cs_err = cs_parse_file(cs, theme_file);
    if(cs_err){
        string_init(&cs_err_str);
        nerr_error_string(cs_err, &cs_err_str);
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
                      "hoge error in cs_parse_file(): %s", cs_err_str.buf);
        // TODO: no need to free cs_err and cs_err_str?
        cs_destroy(&cs);
        hdf_destroy(&hdf);
        return HTTP_INTERNAL_SERVER_ERROR;
    }

    r->content_type = "text/html";
    cs_render(cs, r, diary_cs_render_cb);

    cs_destroy(&cs);
    hdf_destroy(&hdf);
    return OK;
}
示例#11
0
文件: ltpl.c 项目: adderly/cmoon
NEOERR* ltpl_parse_file(HASH *dbh, HASH *evth,
                        void *lib, char *dir, char *name, HASH *outhash)
{
    char *tp = NULL, *tpl = NULL, *val = NULL;
    HDF *node = NULL, *dhdf = NULL, *child = NULL, *thdf = NULL;
    CSPARSE *cs = NULL;
    STRING str;
    char fname[_POSIX_PATH_MAX], tok[64], *outfile;
    NEOERR* (*data_handler)(HDF *hdf, HASH *dbh, HASH *evth);
    NEOERR *err;
    
    memset(fname, 0x0, sizeof(fname));
    snprintf(fname, sizeof(fname), "%s/%s", dir, name);
    err = hdf_init(&node);
    if (err != STATUS_OK) return nerr_pass(err);
 
    err = hdf_read_file(node, fname);
    if (err != STATUS_OK) return nerr_pass(err);

    child = hdf_obj_child(node);
    while (child != NULL) {
        mtc_dbg("parse node %s", hdf_obj_name(child));
        string_init(&str);

        val = mcs_obj_attr(child, "merge");
        if (val) {
            ULIST *list;
            string_array_split(&list, val, ",", 10);
            ITERATE_MLIST(list) {
                snprintf(fname, sizeof(fname), "%s/%s",
                         dir, neos_strip((char*)list->items[t_rsv_i]));
                err = hdf_init(&dhdf);
                JUMP_NOK(err, wnext);
                err = hdf_read_file(dhdf, fname);
                JUMP_NOK(err, wnext);
                err = hdf_copy(child, NULL, dhdf);
                JUMP_NOK(err, wnext);
            }
            uListDestroy(&list, ULIST_FREE);
        }

        /*
         * can't use dataset directly, because we'll destroy the whole node
         */
        err = hdf_init(&dhdf);
        JUMP_NOK(err, wnext);
        err = hdf_get_node(child, PRE_CFG_DATASET, &thdf);
        JUMP_NOK(err, wnext);
        err = hdf_copy(dhdf, NULL, thdf);
        JUMP_NOK(err, wnext);
        
        err = cs_init(&cs, dhdf);
        JUMP_NOK(err, wnext);

        hdf_set_value(cs->hdf, "hdf.loadpaths.tpl", PATH_TPL);
        hdf_set_value(cs->hdf, "hdf.loadpaths.local", dir);

        err = cgi_register_strfuncs(cs);
        JUMP_NOK(err, wnext);
        err = mcs_register_bitop_functions(cs);
        JUMP_NOK(err, wnext);
        err = mcs_register_mkd_functions(cs);
        JUMP_NOK(err, wnext);
        err = mcs_register_string_uslice(cs);
        JUMP_NOK(err, wnext);

        tpl = hdf_get_value(child, PRE_CFG_LAYOUT, "null.html");
        snprintf(fname, sizeof(fname), "%s/%s", PATH_TPL, tpl);
        err = cs_parse_file(cs, fname);
        JUMP_NOK(err, wnext);

        if (outhash != NULL) {
            /*
             * store template for rend stage use
             */
            hdf_set_value(cs->hdf, PRE_RESERVE"."PRE_CFG_LAYOUT, tpl);
            
            /*
             * strdup the key, baby, because we'll free the hdf later
             */
            err = hash_insert(outhash, (void*)strdup(hdf_obj_name(child)), (void*)cs);
            JUMP_NOK(err, wnext);

            snprintf(tok, sizeof(tok), "%s_hdf", hdf_obj_name(child));
            err = hash_insert(outhash, (void*)strdup(tok), (void*)cs->hdf);
            JUMP_NOK(err, wnext);
        }

        if ((outfile = hdf_get_value(child, PRE_CFG_OUTPUT, NULL)) != NULL) {
            ltpl_prepare_rend(cs->hdf, tpl);
                
            /*
             * get_data
             */
            val = hdf_get_value(child, PRE_CFG_DATAER, NULL);
            if (val != NULL && lib) {
                data_handler = dlsym(lib, val);
                if( (tp = dlerror()) != NULL) {
                    mtc_err("%s", tp);
                    //continue;
                } else {
                    err = (*data_handler)(cs->hdf, dbh, evth);
                    TRACE_NOK(err);
                }
            }

            err = cs_render(cs, &str, mcs_strcb);
            JUMP_NOK(err, wnext);

            /*
             * produce output filename
             */
            val = mcs_hdf_attr(child, PRE_CFG_OUTPUT, "ftime");
            if (val) {
                char tm[LEN_TM];
                mutil_getdatetime(tm, sizeof(tm), val, 0);
                outfile = mstr_repstr(1, outfile, "$ftime$", tm);
            }
            snprintf(fname, sizeof(fname), PATH_DOC"%s", outfile);

            /*
             * output file
             */
            err = mfile_makesure_dir(fname);
            JUMP_NOK(err, wnext);

            err = mcs_str2file(str, fname);
            JUMP_NOK(err, wnext);
#ifdef DEBUG_HDF
            snprintf(fname, sizeof(fname), "%s/hdf.%s",
                     TC_ROOT, hdf_obj_name(child));
            hdf_write_file(child, fname);
#endif
        }

    wnext:
        if (cs != NULL && outhash == NULL)
            cs_destroy(&cs);
        string_clear(&str);
        child = hdf_obj_next(child);
    }
        
    if (node != NULL) hdf_destroy(&node);

    return STATUS_OK;
}
示例#12
0
文件: ltpl.c 项目: kingiol/cmoon
NEOERR* ltpl_parse_file(HASH *dbh, void *lib, char *dir, char *name, HASH *outhash)
{
    char *tp = NULL, *tpl = NULL, *val = NULL;
    HDF *node = NULL, *dhdf = NULL, *child = NULL;
    CSPARSE *cs = NULL;
    STRING str;
    char fname[_POSIX_PATH_MAX], tok[64];
    NEOERR* (*data_handler)(HDF *hdf, HASH *dbh);
    NEOERR *err;
    
    memset(fname, 0x0, sizeof(fname));
    snprintf(fname, sizeof(fname), "%s/%s", dir, name);
    err = hdf_init(&node);
    if (err != STATUS_OK) return nerr_pass(err);

    err = hdf_read_file(node, fname);
    if (err != STATUS_OK) return nerr_pass(err);

    child = hdf_obj_child(node);
    while (child != NULL) {
        mtc_dbg("parse node %s", hdf_obj_name(child));
        string_init(&str);

        val = mutil_obj_attr(child, "merge");
        if (val) {
            snprintf(fname, sizeof(fname), "%s/%s", dir, val);
            err = hdf_init(&dhdf);
            JUMP_NOK(err, wnext);
            err = hdf_read_file(dhdf, fname);
            JUMP_NOK(err, wnext);
            err = hdf_copy(child, NULL, dhdf);
            JUMP_NOK(err, wnext);
        }
        
        err = cs_init(&cs, hdf_get_obj(child, PRE_CFG_DATASET));
        JUMP_NOK(err, wnext);
            
        hdf_set_value(cs->hdf, "hdf.loadpaths.local", dir);

        err = cgi_register_strfuncs(cs);
        JUMP_NOK(err, wnext);
        err = mcs_register_bitop_functions(cs);
        JUMP_NOK(err, wnext);
        err = mcs_register_mkd_functions(cs);
        JUMP_NOK(err, wnext);

        tpl = hdf_get_value(child, PRE_CFG_LAYOUT, "null.html");
        snprintf(fname, sizeof(fname), "%s/%s", PATH_TPL, tpl);
        err = cs_parse_file(cs, fname);
        JUMP_NOK(err, wnext);

        if (outhash != NULL) {
            /*
             * strdup the key, baby, because we'll free the hdf later
             */
            err = hash_insert(outhash, (void*)strdup(hdf_obj_name(child)), (void*)cs);
            JUMP_NOK(err, wnext);
            if (hdf_get_obj(child, PRE_CFG_DATASET)) {
                err = hdf_init(&dhdf);
                JUMP_NOK(err, wnext);
                err = hdf_copy(dhdf, NULL, hdf_get_obj(child, PRE_CFG_DATASET));
                JUMP_NOK(err, wnext);
                snprintf(tok, sizeof(tok), "%s_hdf", hdf_obj_name(child));
                err = hash_insert(outhash, (void*)strdup(tok), (void*)dhdf);
                JUMP_NOK(err, wnext);
            }
        }
            
        if (hdf_get_value(child, PRE_CFG_OUTPUT, NULL) != NULL) {
            ltpl_prepare_rend(hdf_get_obj(child, PRE_CFG_DATASET), tpl);
                
            /*
             * get_data
             */
            val = hdf_get_value(child, PRE_CFG_DATAER, NULL);
            if (val != NULL && lib) {
                data_handler = dlsym(lib, val);
                if( (tp = dlerror()) != NULL) {
                    mtc_err("%s", tp);
                    //continue;
                } else {
                    err = (*data_handler)(hdf_get_obj(child, PRE_CFG_DATASET), dbh);
                    TRACE_NOK(err);
                }
            }
                
            err = cs_render(cs, &str, mcs_strcb);
            JUMP_NOK(err, wnext);
                
            snprintf(fname, sizeof(fname), PATH_DOC"%s",
                     hdf_get_value(child, PRE_CFG_OUTPUT, "null.html"));
            err = mutil_makesure_dir(fname);
            JUMP_NOK(err, wnext);

            err = mcs_str2file(str, fname);
            JUMP_NOK(err, wnext);
#ifdef DEBUG_HDF
            snprintf(fname, sizeof(fname), "%s/hdf.%s",
                     TC_ROOT, hdf_obj_name(child));
            hdf_write_file(child, fname);
#endif
        }

    wnext:
        if (cs != NULL && outhash == NULL)
            cs_destroy(&cs);
        string_clear(&str);
        child = hdf_obj_next(child);
    }
        
    if (node != NULL) hdf_destroy(&node);

    return STATUS_OK;
}
示例#13
0
int main (int argc, char *argv[])
{
  NEOERR *err;
  CSPARSE *parse;
  HDF *hdf;
  int verbose = 0;
  char *hdf_file, *cs_file;
  char c;

  extern char *optarg;

  err = hdf_init(&hdf);
  if (err != STATUS_OK)
  {
    nerr_warn_error(err);
    return -1;
  }

  err = cs_init (&parse, hdf);
  if (err != STATUS_OK) {
    nerr_warn_error(err);
    return -1;
  }

  while ((c = getopt(argc, argv, "Hvh:c:")) != EOF )

    switch (c) {
    case 'h':
      hdf_file=optarg;
      err = hdf_read_file(hdf, hdf_file);
      if (err != STATUS_OK) {
	nerr_warn_error(err);
	return -1;
      }
      break;
    case 'c':
      cs_file=optarg;
      if ( verbose )
	printf ("Parsing %s\n", cs_file);

      err = cs_parse_file (parse, cs_file);
      if (err != STATUS_OK) {
	err = nerr_pass(err);
	nerr_warn_error(err);
	return -1;
      }
      break;
    case 'v':
      verbose=1;
      break;
    case 'H':
      fprintf(stderr, "Usage: %s [-v] [-h <file.hdf>] [-c <file.cs>]\n", argv[0]);
      fprintf(stderr, "     -h <file.hdf> load hdf file file.hdf (multiple allowed)\n");
      fprintf(stderr, "     -c <file.cs>  load cs file file.cs (multiple allowed)\n");
      fprintf(stderr, "     -v            verbose output\n");
      return -1;
      break;
    }


  err = cs_render(parse, NULL, output);
  if (err != STATUS_OK) {
    err = nerr_pass(err);
    nerr_warn_error(err);
    return -1;
  }

  if (verbose) {
    printf ("\n-----------------------\nCS DUMP\n");
    err = cs_dump(parse, NULL, output);
  }

  cs_destroy (&parse);

  if (verbose) {
    printf ("\n-----------------------\nHDF DUMP\n");
    hdf_dump (hdf, NULL);
  }

  return 0;
}