static FITEM *build_remote_file(LXmlNode *n) { FITEM *it; const char *name,*md5,*size; name=l_xml_get_prop(n,"name"); md5=l_xml_get_prop(n,"md5"); size=l_xml_get_prop(n,"size"); if(!name || !md5 || !size) return NULL; it=l_new0(FITEM); it->size=atoi(size); it->file=l_strdup(name); it->md5=l_strdup(md5); return it; }
/////////////////////////////////////////////////////////////////////////////// // // Open // /////////////////////////////////////////////////////////////////////////////// int TTransFile::Open() { typestr aLine; char* InChar; char* OutChar; typestr InListe; typestr OutListe; int Line; typestr aSpec; // Ouverture du fichier if (TFile::Open()) return 1; // Lecture de chaque ligne du fichier while (NextLine(&aLine, false, &aSpec, &Line) == 0) { // Si la ligne n'est pas vide, la traiter if (*aLine.str()) { // Decoupage sur le | InChar=OutChar=aLine.str(); while(*OutChar && *OutChar!='|') ++OutChar; if (*OutChar) ++OutChar; // Test de la valeur par defaut if (memcmp(InChar,"#DEFAULT",8)==0) { if (strstr(OutChar, "COPY")) { itsDefaultCopy = true; } else { GetValues(OutChar,&OutListe); itsDefaultValue = OutListe; itsDefaultValueValid = true; } } else { // Affectation des valeurs GetValues(InChar,&InListe); GetValues(OutChar,&OutListe); // Creation de l'arbre lexicographique if (*InListe.str()) insere((unsigned char*) InListe.str(), (unsigned char*)l_strdup(OutListe.str())); } } } // On referme le fichier TFile::Close(); return 0; }
LKeyFile *l_key_file_open(const char *file,int create,...) { LKeyFile *key_file=NULL; char *data; size_t length; va_list ap; va_start(ap,create); data=l_file_vget_contents(file,&length,ap); va_end(ap); if(data!=NULL) { key_file=l_key_file_load(data,length); l_free(data); key_file->file=l_strdup(file); } else if(create!=0) { key_file=l_new0(LKeyFile); key_file->utf8=1; key_file->file=l_strdup(file); } return key_file; }
char *l_key_file_get_string(LKeyFile *key_file,const char *group,const char *key) { char temp[256]; int i,pos,c; const char *data=l_key_file_get_data(key_file,group,key); if(!data) return 0; for(i=pos=0;pos<250 && (c=data[i])!=0;i++) { if(c=='\\') { c=data[++i]; switch(c){ case '\\': temp[pos++]='\\'; break; case 's': temp[pos++]=' '; break; case 'n': temp[pos++]='\n'; break; case 'r': temp[pos++]='\r'; break; case 't': temp[pos++]='\t'; break; case '\'': temp[pos++]='\''; break; case '\"': temp[pos++]='\"'; break; default: temp[pos++]='\\'; temp[pos++]=c; break; } } else { temp[pos++]=c; } } temp[pos]=0; return l_strdup(temp); }
static char *md5_file(const char *path,uint32_t *size) { MD5_CTX ctx; void *data; size_t length; char temp[64]; int i; if(path[0]=='/') path++; data=l_file_get_contents(path,&length,y_im_get_path("DATA"),NULL); if(!data) return NULL; MD5Init(&ctx); MD5Update(&ctx,data,length); MD5Final(&ctx); l_free(data); for(i=0;i<16;i++) { sprintf((char*)temp+i*2,"%02x",ctx.digest[i]); } *size=(uint32_t)length; return l_strdup((char*)temp); }
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; }
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; }
void get_ini_string(const char *section_name, const char *key_name, const char *default_value, typestr &return_buffer, const char *ini_name, const char *a_config_overrides) { // First check the configuration overrides char* overrides = l_strdup(a_config_overrides ? a_config_overrides : ""); char* entry = overrides; while (entry && *entry) { char* p = strstr(entry, "\t\t"); char* next = p ? p + 2 : NULL; if (p) *p = '\0'; char* section = entry; p = strchr(entry, '\t'); if (p) { *p = '\0'; char* key = p + 1; p = strchr(key, '\t'); if (p) { *p = '\0'; char* value = p + 1; if (strcasecmp(section, section_name) == 0 && strcasecmp(key, key_name) == 0) { return_buffer = value; free(overrides); trim_string(return_buffer.str()); trim_string_quotes(return_buffer.str()); return; } } } entry = next; } free(overrides); FILE *ini_file; bool found = false; return_buffer = ""; if ((ini_file = fopen(ini_name, "r"))) { char line[2048]; char line_key[2048]; bool section_ok = !*section_name; while (!found && fgets(line, sizeof(line), ini_file)) { trim_string(line); switch (*line) { case '\0': case ';': case '#': continue; case '[': // section line[strlen(line) - 1] = '\0'; section_ok = !*section_name || strcasecmp(line + 1, section_name) == 0; continue; default: // assume it's a value if (!section_ok) continue; char *eq_pos = strchr(line, '='); if (!eq_pos) continue; *eq_pos = '\0'; strcpy(line_key, line); trim_string(line_key); if (strcasecmp(line_key, key_name) == 0) { // found a match return_buffer = eq_pos + 1; found = true; break; } break; } } fclose(ini_file); } if (!found) return_buffer = default_value; trim_string(return_buffer.str()); trim_string_quotes(return_buffer.str()); }