Exemple #1
0
void generate_file(const char *file_name, int article) {
  parsed_article *pa = parse_file(file_name);

  extend_buffer(article);
  parse_xrefs(pa);
  if (pa != NULL) {
    pa->article = article;
    generate_article(pa);
  }
}
Exemple #2
0
//Navigate the tree from the leaf to the root
int find_path(uint32_t child_index, int unknown_node, FILE* f)
{
	int i, ret, last_path;
	char character;
	
	i = 0;
	last_path = 0;
	
	while (1)
	{	
		//add the read char to the buffer
		character = dictionary[child_index].character;
		decomp_buffer[i] = character;       
				
		//if the last inserted node (its character is not defined!) is equal to the 
		//received symbol, then we set thelast_pathy flag
		if ((i == 0) && (child_index == (array_elem_counter-1)))
           last_path = 1;
				
		//the next node index
		child_index = dictionary[child_index].father_index;
		
		if (child_index == 0)	//root of the tree reached
			break;
		
		if (i == (sizeof decomp_buffer -1))
		{
			// the decomp_buffer is too small
			ret = extend_buffer();
			if (ret < 0)
			{
				fprintf(stderr, "Unable to extend the decompression buffer\n");
				return -1;
			}
		} 
		i++;
	}
		
	//at the first step we don't have any node with unknown character value
    if (unknown_node != 0)
		//there is a child node with an unknown character (it is the last one!)
		dictionary[array_elem_counter-1].character = character;
	
	if (last_path)
		//decomp_buffer[0] has an unspecified value
        decomp_buffer[0] = decomp_buffer[i];    
		
	decomp_buffer[++i] = '\0';
	
	ret = emit_symbols(f);
	
	return 0;
}
static void get_token_sub_part(cst_tokenstream *ts,
			       int charclass,
			       cst_string **buffer,
			       int *buffer_max)
{
    int p;

    for (p=0; ((ts->current_char != TS_EOF) &&
               (ts_charclass(ts->current_char,charclass,ts)) &&
	       (!ts_charclass(ts->current_char,
			      TS_CHARCLASS_SINGLECHAR,ts))); p++)
    {
	if (p >= *buffer_max) extend_buffer(buffer,buffer_max);
	(*buffer)[p] = ts->current_char;
	ts_getc(ts);
    }
    (*buffer)[p] = '\0';
}
const cst_string *ts_get(cst_tokenstream *ts)
{
    /* Get next token */

    /* Skip whitespace */
    get_token_sub_part(ts,
		       TS_CHARCLASS_WHITESPACE,
		       &ts->whitespace,
		       &ts->ws_max);

    /* quoted strings currently ignored */
    ts->token_pos = ts->file_pos - 1;
	
    /* Get prepunctuation */
    if (ts->current_char != TS_EOF &&
        ts_charclass(ts->current_char,TS_CHARCLASS_PREPUNCT,ts))
	get_token_sub_part(ts,
			   TS_CHARCLASS_PREPUNCT,
			   &ts->prepunctuation,
			   &ts->prep_max);
    else if (ts->prepunctuation)
	ts->prepunctuation[0] = '\0';
    /* Get the symbol itself */
    if (ts->current_char != TS_EOF &&
        ts_charclass(ts->current_char,TS_CHARCLASS_SINGLECHAR,ts))
    {
	if (2 >= ts->token_max) extend_buffer(&ts->token,&ts->token_max);
	ts->token[0] = ts->current_char;
	ts->token[1] = '\0';
	ts_getc(ts);
    }
    else
	get_token_sub_part_2(ts,
			     TS_CHARCLASS_WHITESPACE,       /* end class1 */
			     &ts->token,
			     &ts->token_max);
    /* This'll have token *plus* post punctuation in ts->token */
    /* Get postpunctuation */
    if (ts->p_postpunctuationsymbols[0])
        get_token_postpunctuation(ts);

    return ts->token;
}
static void get_token_postpunctuation(cst_tokenstream *ts)
{
    int p,t;

    t = cst_strlen(ts->token);
    for (p=t;
	 (p > 0) && 
	     ((ts->token[p] == '\0') ||
	      (ts_charclass(ts->token[p],TS_CHARCLASS_POSTPUNCT,ts)));
	 p--);

    if (t != p)
    {
	if (t-p >= ts->postp_max) 
	    extend_buffer(&ts->postpunctuation,&ts->postp_max);
	/* Copy postpunctuation from token */
	memmove(ts->postpunctuation,&ts->token[p+1],(t-p));
	/* truncate token at postpunctuation */
	ts->token[p+1] = '\0';
    }
}
const cst_string *ts_get_quoted_token(cst_tokenstream *ts,
					 char quote,
					 char escape)
{
    /* for reading the next quoted token that starts with quote and
       ends with quote, quote may appear only if preceded by escape */
    int l, p;

    /* Hmm can't change quotes within a ts */
    ts->charclass[(unsigned int)quote] |= TS_CHARCLASS_QUOTE;
    ts->charclass[(unsigned int)escape] |= TS_CHARCLASS_QUOTE;

    /* skipping whitespace */
    get_token_sub_part(ts,TS_CHARCLASS_WHITESPACE,
		       &ts->whitespace,
		       &ts->ws_max);
    ts->token_pos = ts->file_pos - 1;
#import "OpenEarsStaticAnalysisToggle.h"
#ifdef STATICANALYZEDEPENDENCIES
#define __clang_analyzer__ 1
#endif
#if !defined(__clang_analyzer__) || defined(STATICANALYZEDEPENDENCIES)
#undef __clang_analyzer__
    if (ts->current_char == quote)
    {   /* go until quote */
	ts_getc(ts);
	l=0;
        for (p=0; ((ts->current_char != TS_EOF) &&
                   (ts->current_char != quote));
             p++)
        {
            if (p >= ts->token_max) 
                extend_buffer(&ts->token,&ts->token_max);
            ts->token[p] = ts->current_char;
            ts_getc(ts);
            if (ts->current_char == escape)
            {
                ts_get(ts);
                if (p >= ts->token_max) 
                    extend_buffer(&ts->token,&ts->token_max);
                ts->token[p] = ts->current_char;
                ts_get(ts);
            }
        }
        ts->token[p] = '\0';
	ts_getc(ts);
    }
#endif    
    else /* its not quotes, like to be careful dont you */
    {    /* treat is as standard token                  */
	/* Get prepunctuation */
	get_token_sub_part(ts,TS_CHARCLASS_PREPUNCT,
			   &ts->prepunctuation,
			   &ts->prep_max);
	/* Get the symbol itself */
	if (!ts_charclass(ts->current_char,TS_CHARCLASS_SINGLECHAR,ts))
	{
	    if (2 >= ts->token_max) extend_buffer(&ts->token,&ts->token_max);
	    ts->token[0] = ts->current_char;
	    ts->token[1] = '\0';
	    ts_getc(ts);
	}
	else
	    get_token_sub_part_2(ts,
				 TS_CHARCLASS_WHITESPACE,    /* end class1 */
				 &ts->token,
				 &ts->token_max);
	/* This'll have token *plus* post punctuation in ts->token */
	/* Get postpunctuation */
	get_token_postpunctuation(ts);
    }

    return ts->token;
}
const unsigned char *ts_get_quoted_token(cst_tokenstream *ts,
					 char quote,
					 char escape)
{
    /* for reading the next quoted token that starts with quote and
       ends with quote, quote may appear only if preceded by escape */
    int l;

    /* Hmm can't change quotes within a ts */
    ts->charclass[(unsigned int)quote] |= TS_CHARCLASS_QUOTE;
    ts->charclass[(unsigned int)escape] |= TS_CHARCLASS_QUOTE;

    /* skipping whitespace */
    get_token_sub_part(ts,TS_CHARCLASS_WHITESPACE,
		       &ts->whitespace,
		       &ts->ws_max);
    ts->token_pos = ts->file_pos - 1;

    if (ts->current_char == quote)
    {   /* go until quote */
	ts_getc(ts);
	l=0;
	while (!ts_eof(ts))
	{
	    get_token_sub_part_2(ts,TS_CHARCLASS_QUOTE,
				 &ts->token,&ts->token_max);
	    if (ts->current_char == escape)
	    {
		ts_getc(ts);
		l = cst_strlen(ts->token);
		if (l+1 >= ts->token_max) 
		    extend_buffer(&ts->token,&ts->token_max);
		ts->token[l] = ts->current_char;
		ts->token[l+1] = '\0';
		ts_getc(ts);
	    }
	    else
		break;
	}
	ts_getc(ts);
    }
    else /* its not quotes, like to be careful dont you */
    {    /* treat is as standard token                  */
	/* Get prepunctuation */
	get_token_sub_part(ts,TS_CHARCLASS_PREPUNCT,
			   &ts->prepunctuation,
			   &ts->prep_max);
	/* Get the symbol itself */
	if (!ts_charclass(ts->current_char,TS_CHARCLASS_SINGLECHAR,ts))
	{
	    if (2 >= ts->token_max) extend_buffer(&ts->token,&ts->token_max);
	    ts->token[0] = ts->current_char;
	    ts->token[1] = '\0';
	    ts_getc(ts);
	}
	else
	    get_token_sub_part_2(ts,
				 TS_CHARCLASS_WHITESPACE,    /* end class1 */
				 &ts->token,
				 &ts->token_max);
	/* This'll have token *plus* post punctuation in ts->token */
	/* Get postpunctuation */
	get_token_postpunctuation(ts);
    }

    return ts->token;
}
Exemple #8
0
func_error_t aitown_db_mysql_write (aitown_db_write_t * request)
{
    func_error_t    ret = FUNC_OK;
    char * querry_buffer = NULL;
    char * iterator;
    size_t querry_buffer_sz;
    size_t querry_computed;

    for (;;) {

        // assume it's our kind
        aitown_db_mysql_t * database = (aitown_db_mysql_t*)request->db;
        DBG_ASSERT (database != NULL);
        DBG_ASSERT (database->con != NULL);

        // approximate our querry size
        size_t db_len = strlen(database->name);
        querry_computed =
                sizeof(MYSQL_Q_INSERT) +
                db_len +
                request->key_sz*2 +
                request->val_sz*2 +
                2;

        // allocate
        ret = extend_buffer(&querry_buffer, &querry_buffer_sz, querry_computed);
        if (ret != FUNC_OK)
            break;
        iterator = querry_buffer;

        // copy first part
        MYSQL_APPEND_LITERAL(iterator,"INSERT INTO ");

        // copy the name of the table
        memcpy (iterator, database->name, db_len );
        iterator += db_len;

        // copy second part
        MYSQL_APPEND_LITERAL(iterator," (Id, MasterValue) VALUES('");

        // copy the key
        iterator += mysql_real_escape_string(
                    database->con, iterator, request->key, request->key_sz);

        // copy separator
        MYSQL_APPEND_LITERAL(iterator,"','");

        // copy the value
        iterator += mysql_real_escape_string(
                    database->con, iterator, request->val, request->val_sz);

        // ending part
        MYSQL_APPEND_LITERAL(iterator,"') ON DUPLICATE KEY UPDATE MasterValue=values(MasterValue);");

        *iterator = 0;

        // perform this querry
        if (mysql_query(database->con, querry_buffer)) {
            err_message (mysql_error(database->con));
            ret = FUNC_GENERIC_ERROR;
            break;
        }

        break;
    }

    if (querry_buffer != NULL) {
        free (querry_buffer);
    }

    // either use the callback or set the value in request
    if (request->kb != NULL) {
        request->kb (
                    request->db,
                    ret,
                    request->user
                    );
    }
    return ret;
}
Exemple #9
0
func_error_t aitown_db_mysql_read (aitown_db_read_t * request)
{
    DBG_ASSERT (request != NULL);

    char * querry_buffer = NULL;
    char * iterator;
    size_t querry_buffer_sz;
    size_t querry_computed;
    MYSQL_RES *result = NULL;

    int vsiz = 0;
    void *vbuf = NULL;

    func_error_t    ret = FUNC_OK;
    for (;;) {

        // assume it's our kind
        aitown_db_mysql_t * database = (aitown_db_mysql_t*)request->db;
        DBG_ASSERT (database != NULL);
        DBG_ASSERT (database->con != NULL);

        // approximate our querry size
        size_t db_len = strlen(database->name);
        querry_computed =
                sizeof(MYSQL_Q_SELECT) +
                db_len +
                request->key_sz*2 +
                2;

        // allocate
        ret = extend_buffer(&querry_buffer, &querry_buffer_sz, querry_computed);
        if (ret != FUNC_OK)
            break;
        iterator = querry_buffer;


        // copy first part
        MYSQL_APPEND_LITERAL(iterator,"SELECT MasterValue FROM ");

        // copy the name of the table
        memcpy (iterator, database->name, db_len);
        iterator += db_len;

        // copy second part
        MYSQL_APPEND_LITERAL(iterator," WHERE Id='");

        // copy the key
        iterator += mysql_real_escape_string(
                    database->con, iterator, request->key, request->key_sz);

        // copy second part
        MYSQL_APPEND_LITERAL(iterator,"';");

        *iterator = 0;

        // perform this querry
        if (mysql_query(database->con, querry_buffer))
        {
            err_message (mysql_error(database->con));
            ret = FUNC_GENERIC_ERROR;
            break;
        }

        // get the result
        result = mysql_store_result(database->con);
        if (result == NULL)
        {
            err_message (mysql_error(database->con));
            ret = FUNC_GENERIC_ERROR;
            break;
        }

        // get characteristics
        MYSQL_ROW row = mysql_fetch_row (result);
        unsigned long *lengths = mysql_fetch_lengths(result);
        if (lengths == NULL) {
            err_message (mysql_error(database->con));
            ret = FUNC_GENERIC_ERROR;
            break;
        }

        // resize
        ret = extend_buffer(&querry_buffer, &querry_buffer_sz, lengths[0]);
        if (ret != FUNC_OK)
            break;
        memcpy(querry_buffer, row[0], lengths[0]);
        vbuf = querry_buffer;
        vsiz = lengths[0];
        querry_buffer = NULL;

        break;
    }

    if (result != NULL) {
        mysql_free_result(result);
    }

    if (querry_buffer != NULL) {
        free (querry_buffer);
    }

    // either use the callback or set the value in request
    if (request->kb == NULL) {
        *request->val = vbuf;
        *request->val_sz = vsiz;
    } else {
        request->kb (
                    request->db,
                    ret,
                    request->user,
                    vbuf,
                    vsiz
                    );
    }
    return ret;
}
Exemple #10
0
func_error_t aitown_db_mysql_open (aitown_db_open_t*data)
{
    DBG_ASSERT (data != NULL);
    DBG_ASSERT (data->db_mng != NULL);

    func_error_t    ret = FUNC_OK;

    aitown_db_mysql_t * new_db = NULL;
    char * querry_buffer = NULL;
    size_t querry_buffer_sz;
    size_t querry_computed;

    aitown_cfg_leaf_t * cfg_mysql_user = NULL;
    aitown_cfg_leaf_t * cfg_mysql_pass = NULL;
    const char * mysql_user;
    const char * mysql_pass;

    for (;;) {

        // a database is required
        if (data->db_name == NULL) {
            err_message ("mysql driver requires a database name");
            ret = FUNC_BAD_INPUT;
            break;
        }

        // get a buffer for our querries
        querry_buffer = (char*)malloc(1024);
        querry_buffer_sz = 1024;
        if (querry_buffer == NULL) {
            ret = FUNC_MEMORY_ERROR;
            break;
        }

        int b_cfg_ok = 0;
        for (;;) {
            aitown_db_mng_t *db_mng = data->db_mng;

            // get root section
            aitown_cfg_sect_t * root_cfg_sect = db_mng->cfg_sect;
            if (root_cfg_sect == NULL)
                break;

            // get databases section
            aitown_cfg_sect_t * cfg_sect_databases =
                    aitown_cfg_get_sect( root_cfg_sect, "databases");
            if (cfg_sect_databases == NULL)
                break;

            // get the section for our database
            aitown_cfg_sect_t * cfg_sect_ourdb =
                    aitown_cfg_get_sect( cfg_sect_databases, data->db_name);
            if (cfg_sect_ourdb == NULL)
                break;

            cfg_mysql_user =
                    aitown_cfg_get_leaf( cfg_sect_ourdb, MYSQL_KEY_USERNAME );
            if ((cfg_mysql_user != NULL) && (cfg_mysql_user->value != NULL))
                break;

            cfg_mysql_pass =
                    aitown_cfg_get_leaf( cfg_sect_ourdb, MYSQL_KEY_PASS );
            if ((cfg_mysql_pass != NULL) && (cfg_mysql_pass->value != NULL))
                break;

            b_cfg_ok = 1;
            break;
        }
        if (!b_cfg_ok) {
            err_message (
                        "mysql driver requires configuration to open a "
                        "database (at least user and password)");
            ret = FUNC_BAD_INPUT;
            break;
        }

        if (*cfg_mysql_user->value == 0) {
            mysql_user = NULL;
        } else {
            mysql_user = cfg_mysql_user->value;
        }
        if (*cfg_mysql_pass->value == 0) {
            mysql_pass = NULL;
        } else {
            mysql_pass = cfg_mysql_pass->value;
        }

        // allocate a new database structure
        new_db = (aitown_db_mysql_t*)malloc (
                    sizeof(aitown_db_mysql_t));
        if (new_db == NULL) {
            ret = FUNC_MEMORY_ERROR;
            break;
        }

        // copy the name
        new_db->name = strdup (data->db_name);

        // start a new connection
        new_db->con = mysql_init(NULL);
        if (new_db->con == NULL) {
            err_message (mysql_error(new_db->con));
            ret = FUNC_GENERIC_ERROR;
            break;
        }

        // attempt to connect
        if (mysql_real_connect(
                new_db->con, data->path_hint,
                mysql_user, mysql_pass,
                NULL, 0, NULL, 0) == NULL)
        {
            err_message (mysql_error(new_db->con));
            ret = FUNC_GENERIC_ERROR;
            break;
        }

        // create database if it does not exist
        querry_computed = sizeof(MYSQL_Q_CREATE_DB) + strlen(data->db_name);
        ret = extend_buffer (&querry_buffer, &querry_buffer_sz, querry_computed);
        if (ret != FUNC_OK) {
            break;
        }
        sprintf (querry_buffer, MYSQL_Q_CREATE_DB, data->db_name);
        if (mysql_query(new_db->con, querry_buffer))
        {
            err_message (mysql_error(new_db->con));
            ret = FUNC_GENERIC_ERROR;
            break;
        }
        mysql_close (new_db->con);

        // attempt to connect
        new_db->con = mysql_init(NULL);
        if (mysql_real_connect(
                new_db->con, data->path_hint,
                cfg_mysql_user->value, cfg_mysql_pass->value,
                data->db_name, 0, NULL, 0) == NULL)
        {
            err_message (mysql_error(new_db->con));
            ret = FUNC_GENERIC_ERROR;
            break;
        }

        // create table if it does not exist
        querry_computed = sizeof(MYSQL_Q_CREATE_TBL) + strlen(data->db_name);
        ret = extend_buffer (&querry_buffer, &querry_buffer_sz, querry_computed);
        if (ret != FUNC_OK) {
            break;
        }
        sprintf (querry_buffer, MYSQL_Q_CREATE_TBL, data->db_name);
        if (mysql_query(new_db->con, querry_buffer))
        {
            err_message (mysql_error(new_db->con));
            ret = FUNC_GENERIC_ERROR;
            break;
        }

        *data->out = (aitown_db_t*)new_db;
        break;
    }

    if (querry_buffer != NULL) {
        free (querry_buffer);
    }

    // clean up in case of error
    if (ret != FUNC_OK) {
        if (new_db != NULL) {
            if (new_db->con != NULL) {
                mysql_close (new_db->con);
            }
            if (new_db->name != NULL) {
                free (new_db->name);
            }
            aitown_db_end (&new_db->header);
            free (new_db);
        }
    }
    return ret;
}