list getTasks(struct error* err) { err->error = NO_ERROR; if(!db) { if(!initializeConnection(NULL)) { exit(1); } } sqlite3_stmt* statement; const char* sql = "select * from tasks"; const char* unused; int rc = sqlite3_prepare(db, sql, -1, &statement, &unused); if(rc != SQLITE_OK) { err->error = DATABASE_SELECT; err->description = sqlite3_errmsg(db); return NULL; } list l = l_new(); while(sqlite3_step(statement) == SQLITE_ROW) { populateList(l, statement); } sqlite3_finalize(statement); return l; }
list getBetweenDate(struct tm start, struct tm end, struct error* err) { err->error = NO_ERROR; if(!db) { if(!initializeConnection(NULL)) { exit(1); } } sqlite3_stmt* statement; const char* sql = "select * from tasks"; const char* unused; int rc = sqlite3_prepare(db, sql, -1, &statement, &unused); if(rc != SQLITE_OK) { err->error = DATABASE_SELECT; err->description = sqlite3_errmsg(db); return NULL; } list l = l_new(); while(sqlite3_step(statement) == SQLITE_ROW) { struct tm s; struct tm e; strptime((char*)sqlite3_column_text(statement, 2), "%a %b %d %H:%M:%S %Y%n", &s); strptime((char*)sqlite3_column_text(statement, 3), "%a %b %d %H:%M:%S %Y%n", &e); if(date_compare(start, s) > 0 || date_compare(end, e) < 0) { continue ; } char* title; char* description; int title_length = strlen((char*)sqlite3_column_text(statement, 0)) + 1; int descr_length = strlen((char*)sqlite3_column_text(statement, 1)) + 1; title = (char*)malloc(title_length*sizeof(char)); description = (char*)malloc(descr_length*sizeof(char)); strcpy(title,(char*)sqlite3_column_text(statement, 0)); strcpy(description, (char*)sqlite3_column_text(statement, 1)); Task t = (Task)malloc(sizeof(struct task)); t->title = title; t->description = description; t->startDay = s; t->dueDay = e; l_push(l, t); } sqlite3_finalize(statement); return l; }
int l_key_file_set_data(LKeyFile *key_file,const char *group,const char *key,const char *value) { KeyValue *p; KeyValue *g=NULL; KeyValue *prev; if(!group) return -1; for(p=key_file->line;p!=NULL;p=p->next) { if(p->value) continue; if(!strcmp(p->key,group)) { g=p; break; } } if(L_UNLIKELY(!key)) { if(!g) return 0; for(p=g->next;p!=NULL;p=g->next) { if(!p->value) break; g->next=p->next; l_keyvalue_free(p); } key_file->line=l_slist_remove(key_file->line,g); l_keyvalue_free(g); key_file->dirty++; return 0; } if(!g) { if(!key) return 0; g=l_new(KeyValue); g->value=0; g->key=l_strdup(group); key_file->line=l_slist_append(key_file->line,g); key_file->dirty++; } for(p=g,prev=NULL;p->next!=NULL;prev=p,p=p->next) { if(!p->next->value) break; if(!p->next->key) continue; if(!strcmp(p->next->key,key)) { if(value) { p=p->next; if(strcmp(p->value,value)) { l_free(p->value); p->value=l_strdup(value); key_file->dirty++; } } else { g=p->next; p->next=g->next; l_keyvalue_free(g); key_file->dirty++; } return 0; } } if(value) { if(prev && !p->key) p=prev; g=l_new(KeyValue); g->key=l_strdup(key); g->value=l_strdup(value); g->next=p->next; p->next=g; key_file->dirty++; } return 0; }
list getByDate(struct tm t, struct error* err, const char* sql) { err->error = NO_ERROR; int seconds = t.tm_sec; int minutes = t.tm_min; int day = t.tm_wday; int hours = t.tm_hour; int month = t.tm_mon; int year = t.tm_year; year += 1900; char* month_str; char* day_str; switch(month) { case 0: month_str = "Jan"; break; case 1: month_str = "Feb"; break; case 2: month_str = "Mar"; break; case 3: month_str = "Apr"; break; case 4: month_str = "May"; break; case 5: month_str = "Jun"; break; case 6: month_str = "Jul"; break; case 7: month_str = "Aug"; break; case 8: month_str = "Sep"; break; case 9: month_str = "Oct"; break; case 10: month_str = "Nov"; break; case 11: month_str = "Dec"; break; default: month_str = "%"; break; } switch(day) { case 0: day_str = "Sun"; break; case 1: day_str = "Mon"; break; case 2: day_str = "Tue"; break; case 3: day_str = "Wed"; break; case 4: day_str = "Thu"; break; case 5: day_str = "Fri"; break; case 6: day_str = "Sat"; break; default: day_str = "%"; break; } char seconds_str[3]; char minutes_str[3]; char hours_str[3]; char year_str[5]; sqlite3_stmt* statement; const char* unused; if(seconds < 0 || seconds > 60) { strcpy(seconds_str,"%"); } else { sprintf(seconds_str, "%d", seconds); } if(minutes < 0 || minutes > 59) { strcpy(minutes_str, "%"); } else { sprintf(minutes_str, "%d", minutes); } if(hours < 0 || hours > 23) { strcpy(hours_str, "%"); } else { sprintf(hours_str, "%d", hours); } if(year < 1900) { strcpy(year_str, "%"); } else { sprintf(year_str, "%d", year); } int rc = sqlite3_prepare(db, sql, -1, &statement, &unused); if(rc != SQLITE_OK) { err->error = DATABASE_SELECT; err->description = sqlite3_errmsg(db); return NULL; } int total_length = strlen(day_str) + 1 /*space*/ + strlen(month_str)+ 1 /*space*/ + 8 /*hh:mm:ss*/+ 1/*space*/ + 4 /*year*/ + 1 /*\0*/; char* value = (char*)malloc(total_length * sizeof(char)); strcpy(value, day_str); strcat(value, " "); strcat(value, month_str); strcat(value, " "); strcat(value, hours_str); strcat(value, ":"); strcat(value, minutes_str); strcat(value, ":"); strcat(value, seconds_str); strcat(value, " "); strcat(value, year_str); sqlite3_bind_text(statement, 1, value, strlen(value), SQLITE_STATIC); list l = l_new(); if(sqlite3_step(statement) != SQLITE_ROW) { err->error = DATABASE_SELECT; err->description = sqlite3_errmsg(db); sqlite3_finalize(statement); return NULL; } populateList(l, statement); while(sqlite3_step(statement) == SQLITE_ROW) { populateList(l, statement); } free(value); return l; }
LKeyFile *l_key_file_load(const char *data,size_t length) { LKeyFile *key_file; KeyValue *kv; const char *end=data; char line[1024]; int count; if(length==-1) length=strlen(data); end=data+length; key_file=l_new0(LKeyFile); key_file->utf8=1; key_file->file=NULL; for(count=0;data<end;count++) { int i; char *p; for(i=0;i<sizeof(line)-1 && data<end;) { int c=*data++; if(c=='\r') continue; if(c=='\n') { break; } line[i++]=c; } line[i]=0; l_str_trim_right(line); if(count==0 && !memcmp(line,"\xef\xbb\xbf",3)) { int len=strlen(line+3)+1; memmove(line,line+3,len); key_file->utf8=1; } for(p=line;isspace(*p);p++); if(p[0]=='[') { char *d=strchr(p+1,']'); if(!d) continue; *d=0; kv=l_new0(KeyValue); kv->key=l_strdup(p+1); } else if(p[0]=='#' || p[0]==';' || !p[0]) { kv=l_new0(KeyValue); kv->value=l_strdup(p); } else { char *d=strchr(p,'='); if(!d) continue; *d++=0; kv=l_new(KeyValue); kv->key=l_strdup(p); for(p=kv->key;*p && !isspace(*p);p++) { } *p=0; for(p=d;!(*p&0x80) && isspace(*p);p++); kv->value=l_strdup(p); } key_file->line=l_slist_append(key_file->line,kv); } return key_file; }