Exemple #1
0
struct lsortedll_st* lsll_init(v_vp_func free_val, i_cvp_cvp_func cmp_val, unsigned opts)
{
	int syserr = -1;

	struct lsortedll_st* lsll = NULL;

	lsll = calloc(1, sizeof(struct lsortedll_st));
	if (! lsll)
	{
		perror("calloc");
		goto EXIT_LABEL;
	}

	lsll->sll = sll_init(free_val, cmp_val, opts);
	if (! lsll->sll)
	{
		goto EXIT_LABEL;
	}

	pthread_mutex_init(&lsll->lock, NULL);

	syserr = 0;

EXIT_LABEL:

	if (syserr)
	{
		lsll_free(lsll);
		lsll = NULL;
	}

	return lsll;
}
// Create a new session
// FIXME - This is currently open to a DoS attack.
//         but since we have a session limit, just for opendias.
char *create_session() {
  char *session_id;

  // Check upper session count limit
  int current_session_count = sll_count( sll_findFirstElement( sessions ) );
  o_log( DEBUGM, "There are currently %d active sessions", current_session_count);
  if( current_session_count > MAX_SESSIONS ) {
    return NULL;
  }

  // Generate session key
  uuid_t uu;
  char *sid = malloc(36+1);
  uuid_generate(uu);
  uuid_unparse(uu, sid);
  session_id = o_strdup(sid);
  free(sid);
  o_log(DEBUGM, "Generated new session: %s", session_id );

  // Create new session structure
  struct session_data *session_element = malloc( sizeof(struct session_data) );
  session_element->last_accessed = time(NULL);
  session_element->session_container = sll_init();

  // Save and return
  sll_insert( sessions, o_strdup(session_id), session_element );
  return session_id;
}
Exemple #3
0
extern char *getTagId(char *tagname) {

  struct simpleLinkedList *vars, *rSet;
  char *sql2, *ret = NULL;
  char *sql = o_printf("SELECT tagid FROM tags WHERE tagname = '%s'", tagname);

  rSet = runquery_db(sql);
  if( rSet != NULL ) {
    ret = o_strdup(readData_db(rSet, "tagid"));
  }
  else {
    o_log(DEBUGM, "no tag was found. Adding a new one.");
    sql2 = o_strdup("INSERT INTO tags (tagname) VALUES (?)");

    vars = sll_init();
    sll_append(vars, DB_TEXT );
    sll_append(vars, tagname );

    runUpdate_db(sql2, vars);
    free(sql2);

    ret = itoa(last_insert(), 10);
  }
  free_recordset( rSet );
  free(sql);

  o_log(DEBUGM, "Using tagid of %s", ret);
  return ret;
}
Exemple #4
0
static char *addNewDoc (int ftype, int getLines, int ppl, int resolution, int pageCount, char *ocrText) {

  char *dateStr = getTimeStr_iso8601();
  char *sql = o_strdup("INSERT INTO docs \
    (depth, lines, ppl, resolution, ocrText, pages, entrydate, filetype) \
    VALUES (8, ?, ?, ?, ?, ?, ?, ?)");

  struct simpleLinkedList *vars = sll_init();
  sll_append(vars, DB_INT) ;
  sll_append(vars, &getLines );
  sll_append(vars, DB_INT );
  sll_append(vars, &ppl );
  sll_append(vars, DB_INT );
  sll_append(vars, &resolution );
  sll_append(vars, DB_TEXT );
  sll_append(vars, ocrText );
  sll_append(vars, DB_INT );
  sll_append(vars, &pageCount );
  sll_append(vars, DB_TEXT );
  sll_append(vars, dateStr );
  sll_append(vars, DB_INT );
  sll_append(vars, &ftype );

  runUpdate_db(sql, vars);
  free(sql);
  free(dateStr);
  free(ocrText);
  return itoa(last_insert(), 10);

}
Exemple #5
0
sSLList *sll_createExtern(fNodeAlloc falloc,fNodeFree ffree) {
	sList *l = (sList*)heapalloc(sizeof(sList));
	if(l == NULL)
		return NULL;
	sll_init((sSLList*)l,falloc,ffree);
	return (sSLList*)l;
}
Exemple #6
0
sSLList *sll_create(void) {
	sList *l = (sList*)heapalloc(sizeof(sList));
	if(l == NULL)
		return NULL;
	sll_init((sSLList*)l,(fNodeAlloc)nodealloc,(fNodeFree)nodefree);
	return (sSLList*)l;
}
Exemple #7
0
/** Initialized explicitly through sll_init() calls */
void test_basic_init (void)
{
	sll_t sll;

	sll_init(&sll);
	
	test_initialization(&sll);
}
Exemple #8
0
extern int updateDocValue (char *docid, char *kkey, char *vvalue) {

  struct simpleLinkedList *vars = sll_init();
  sll_append(vars, DB_TEXT );
  sll_append(vars, vvalue );
  sll_append(vars, DB_TEXT );
  sll_append(vars, docid );

  return doUpdateDocValue(kkey, vars);
}
Exemple #9
0
extern void deleteTag( char *tagid ) {
  char *sql = o_strdup("DELETE FROM tags WHERE tagid = ?");
  int tagid_i = atoi(tagid);

  struct simpleLinkedList *vars = sll_init();
  sll_append(vars, DB_INT );
  sll_append(vars, &tagid_i );

  runUpdate_db(sql, vars);
  free(sql);
}
Exemple #10
0
extern void removeDoc (char *docid) {
  char *sql = o_strdup("DELETE FROM docs WHERE docid = ?");
  int docid_i = atoi(docid);

  struct simpleLinkedList *vars = sll_init();
  sll_append(vars, DB_INT );
  sll_append(vars, &docid_i );

  runUpdate_db(sql, vars);
  free(sql);
}
Exemple #11
0
extern void addLocation(char *location, int role) {

  char *sql = o_strdup("INSERT INTO location_access (location, role) VALUES (?, ?);");
  struct simpleLinkedList *vars = sll_init();
  sll_append(vars, DB_TEXT );
  sll_append(vars, location );
  sll_append(vars, DB_INT );
  sll_append(vars, &role );

  runUpdate_db(sql, vars);
  free(sql);
}
Exemple #12
0
static int addRemoveTagOnDocument (char *sql, char *docid, char *tagid) {

  int rc;
  struct simpleLinkedList *vars = sll_init();
  sll_append(vars, DB_TEXT );
  sll_append(vars, docid );
  sll_append(vars, DB_TEXT );
  sll_append(vars, tagid );

  rc = runUpdate_db(sql, vars);
  free(sql);
  return rc;
}
Exemple #13
0
void create_assembled_pool(Params *p, double **pool, int divpool, double fert)
{
    restart:;
    // define and initialize the ecosystem list
    //-----------------------------------------
    sll ecosys;
    sll_init(&ecosys,(void*)free_species);

    // create the basal resources
    //---------------------------
    basal(p,&ecosys,fert);

    // assemble until the targetted diversity is reached
    //--------------------------------------------------
    int a = divpool + EAM_NBNUT + EAM_NBDET;
    int counter = EAM_STOP_UNSUCCESS_ASSEMBLY*divpool;
    while(ecosys.length != a)
    {
	ParamSP *sp = (void*)species(p,2);
	install_process(p,&ecosys,sp);
	sll_rm(&ecosys,extinct_condition);
	++(p->num);
	--counter;
	if(counter == 0)
	{
	    sll_rm_all(&ecosys);
	    //create_assembled_pool(p, pool, divpool, fert);
	    goto restart;
	}

    }

    // remove the resources
    //---------------------
    sll_rm(&ecosys,is_resource);


    // create the subpool matrix and fill it with the selected species
    //----------------------------------------------------------------
    sllnode *node = ecosys.head;
    for (int i = 0; node != NULL; i++, node = node->next)
    {
        ParamSP sp = *(ParamSP*)(node->data);
	sp.d = EAM_N0SP;
	tr_sp_array(&sp,pool[i]);
    }

    // free the memory
    //----------------
    sll_rm_all(&ecosys);
}
Exemple #14
0
extern void addScanProgress (char *uuid) {

  char *sql = o_strdup("INSERT INTO scan_progress (client_id, status, value) VALUES (?, ?, 0);");

  int t1 = SCAN_IDLE;
  int *t = &t1;

  struct simpleLinkedList *vars = sll_init();
  sll_append(vars, DB_TEXT );
  sll_append(vars, uuid );
  sll_append(vars, DB_INT );
  sll_append(vars, t );

  runUpdate_db(sql, vars);
  free(sql);
}
Exemple #15
0
extern void updateNewScannedPage (int docid, char *ocrText, int page) {

  char *sql = o_strdup("UPDATE docs SET pages = ?, ocrText = ocrText || ? WHERE docid = ?");

  struct simpleLinkedList *vars = sll_init();
  sll_append(vars, DB_INT );
  sll_append(vars, &page );
  sll_append(vars, DB_TEXT );
  sll_append(vars, ocrText );
  sll_append(vars, DB_INT );
  sll_append(vars, &docid );

  runUpdate_db(sql, vars);
  free(sql);

}
Exemple #16
0
extern void updateScanProgress (char *uuid, int status, int value) {

  char *progressUpdate = o_strdup("UPDATE scan_progress \
                                   SET status = ?, \
                                       value = ? \
                                   WHERE client_id = ? ");

  struct simpleLinkedList *vars = sll_init();
  sll_append(vars, DB_INT );
  sll_append(vars, &status );
  sll_append(vars, DB_INT );
  sll_append(vars, &value );
  sll_append(vars, DB_TEXT );
  sll_append(vars, uuid );

  runUpdate_db(progressUpdate, vars);
  free(progressUpdate);

}
Exemple #17
0
extern int setScanParam(char *uuid, int param, char *vvalue) {

  int rc;
  char *sql = o_strdup("INSERT OR REPLACE \
                        INTO scan_params \
                        (client_id, param_option, param_value) \
                        VALUES (?, ?, ?);");

  struct simpleLinkedList *vars = sll_init();
  sll_append(vars, DB_TEXT );
  sll_append(vars, uuid );
  sll_append(vars, DB_INT );
  sll_append(vars, &param );
  sll_append(vars, DB_TEXT );
  sll_append(vars, vvalue );

  rc = runUpdate_db(sql, vars);
  free(sql);

  return rc;
}
Exemple #18
0
void update_config_option( char *option, char *value ) {
  char *sql = o_strdup("update config SET config_value = ? WHERE config_option = ?"); 
  struct simpleLinkedList *vars = sll_init();

	o_log(DEBUGM,"entering update_config_option\n");

  sll_append(vars, DB_TEXT );
  sll_append(vars, value );
  sll_append(vars, DB_TEXT );
  sll_append(vars, option );

  o_log(INFORMATION, "|Attempting to set config option '%s' to '%s'", option, value);
  if( runUpdate_db(sql, vars) ) {
    o_log(INFORMATION, "|    Failed!");
  }
  else {
    o_log(INFORMATION, "|    Successful.");
  }
  free(sql);
	o_log(DEBUGM,"leaving update_config_option\n");
}
Exemple #19
0
struct lnklst_st* new_lstnmods(struct lnklst_st* fkvs)
{
FUNC_ENTER

	struct sortedll_st* ports = NULL;
	struct lnklst_st* lstnmods = NULL;		// ports がユニークなので lnklst_t でよい

	ports = sll_init(NULL, cmp_as_long_, SLLOPT_DISALLOW_DUPVAL);
	if (! ports)
	{
		FIRE("sll_init");
	}

	syserr = ll_foreach(fkvs, collect_port_, ports);
	if (syserr)
	{
		FIRE("ll_foreach");
	}

	lstnmods = new_lstnmods_(ports, fkvs);
	if (! lstnmods)
	{
		FIRE("new_lstnmods_");
	}

FUNC_CHECKPOINT

	if (HAS_ERROR())
	{
		ll_free(lstnmods);
		lstnmods = NULL;
	}

	sll_free(ports);
	ports = NULL;

FUNC_LEAVE

	return lstnmods;
}
Exemple #20
0
int main(int argc, char ** argv)
{
	sb_sll * list;
	list = sll_init();
	int i;
	int * element = NULL;
	for (i = 0; i < 100000; i++)
	{
		element = (int *) malloc(sizeof(int));
		*element = i;
		sll_add_front(list, (void *) element);
	}

	while (!sll_empty(list)) {
		element = (int *) sll_front(list);
		printf("element: %d\n", *element);
		sll_remove_front(list);
		free(element);
	}

	sll_destroy(list);
	return EXIT_SUCCESS;
}
Exemple #21
0
void init_session_management( int max_sessions, int max_session_age) {
  sessions = sll_init();
  sll_insert( sessions, "placeholder", "placeholder" );
  MAX_SESSIONS = max_sessions;
  MAX_AGE = max_session_age;;
}
Exemple #22
0
void assembly_run(Params *p, double nfert, double **regpool, int poolsize, char *folder, char *replicate_identity)
{
    // Define and initialize the history and ecosystem lists
    //------------------------------------------------------
    sll history;
    sll_init(&history,(void*)free_seq);
    sll ecosys;
    sll_init(&ecosys,(void*)free_species);

    // create the basal resources
    //---------------------------
    basal(p,&ecosys,nfert);


    // create and initialize the presence matrix (ne pas oublier de mettre les espèces au minimum pour l'invasion)
    //------------------------------------------
    unsigned int **presence = (unsigned int**)mat_alloc(2,poolsize,sizeof(unsigned int));
    for(int i = 0 ; i < poolsize ; ++i)
    {
        presence[0][i] = regpool[i][1];
        presence[1][i] = 0;
    }

    // get the initial values of the sequence
    //---------------------------------------
    seqlevel *seq0 = fill_seq(&ecosys,0,0);
    seq0->success = 0;
    sll_add_head(&history,seq0);

    // assemble until any species can install (or all are already installed)
    //----------------------------------------------------------------------
    int a = poolsize + EAM_NBNUT + EAM_NBDET;                              /* expected ecosystem size */
    int stop_endcycles = poolsize * EAM_STOP_ENDCYCLE;                     /* variable to stop the sequence if infinite */
    int endcycles = 0;                                                     /* 0 if endpoint / 1 if endcycle */ 
    int unsucc = EAM_STOP;                                                 /* check the success of the invader */
    int invasion = 1;                                                      /* invasion number */

    while(ecosys.length != a)
    {
        int b = get_int(p->rng,0,poolsize);                                /* select randomly the number of the species in the regional pool */
        if(presence[1][b] == 0)                                            /* if the species is not already in the ecosystem */
	{
	    ParamSP *sp = (ParamSP*)malloc(sizeof(ParamSP));
	    tr_sp_struct(regpool[b],sp);
	    install_process(p,&ecosys,sp);                                  /* install the species */
	    seqlevel *seq = fill_seq(&ecosys,invasion,presence[0][b]);      /* create and initialize a sequence node */
	    sll_rm(&ecosys,extinct_condition);                              /* remove the extincted species */
	    presence_absence(&ecosys,presence,poolsize);                    /* fill the presence-absence matrix */
	    unsucc = (presence[1][b] == 0) ? unsucc-1 : EAM_STOP;          /* upgrade the counter of unsuccessfull installations */
	    seq->success =  presence[1][b];                                /* fill the success of invader installation in the sequence node */
	    sll_add_head(&history,seq);                                    /* add the sequence node to the assembly history */
	    ++invasion;                                                    /* upgrade the invasion number */
	    --stop_endcycles;                                              /* upgrade the endcycle detector */
	}
	if(unsucc == 0) break;
        if(stop_endcycles == 0)                                              /* break the loop if its an endcycle */
	{
	    endcycles = 1;
	    break;
	}
    }


    // print the outputs : subpool / final community / sequence / abundances history / identity history
    //------------------
    print_final_community(&ecosys,folder,replicate_identity);
    print_id_history(&history,poolsize,folder,replicate_identity);
    print_final_biomasses_history(&history,poolsize,folder,replicate_identity);
    print_sequence(&history,folder,replicate_identity);
    char *buffer = (char*)malloc(100);
    sprintf(buffer, "%s/%s_endcycle.txt",folder,replicate_identity);
    FILE *out = fopen(buffer,"w");
    fprintf(out,"%d",endcycles);
    fclose(out);

    // free the memory
    //----------------
    mat_free((void**)presence,2);
    sll_rm_all(&history);
    sll_rm_all(&ecosys);
    free(buffer);

}