int usersFileRead(char* path) { char *p; File *file; Fsys *fsys; int len, r; uvlong size; if((fsys = fsysGet("main")) == nil) return 0; fsysFsRlock(fsys); if(path == nil) path = "/active/adm/users"; r = 0; if((file = fileOpen(fsysGetFs(fsys), path)) != nil){ if(fileGetSize(file, &size)){ len = size; p = vtMemAlloc(size+1); if(fileRead(file, p, len, 0) == len){ p[len] = '\0'; r = uboxInit(p, len); } } fileDecRef(file); } fsysFsRUnlock(fsys); fsysPut(fsys); return r; }
MeteoConfig loadConfig() { DynamicJsonBuffer jsonBuffer; MeteoConfig cfg; if (fileExist(METEO_CONFIG_FILE)) { int size = fileGetSize(METEO_CONFIG_FILE); char* jsonString = new char[size + 1]; fileGetContent(METEO_CONFIG_FILE, jsonString, size + 1); JsonObject& root = jsonBuffer.parseObject(jsonString); JsonObject& network = root["network"]; cfg.NetworkSSID = String((const char*)network["ssid"]); cfg.NetworkPassword = String((const char*)network["password"]); JsonObject& correction = root["correction"]; cfg.AddT1 = correction["T1"]; cfg.AddT2 = correction["T2"]; cfg.AddTZ = correction["TZ"]; JsonObject& trigger = root["trigger"]; cfg.Trigger = (TriggerType)(int)trigger["type"]; cfg.RangeMin = trigger["min"]; cfg.RangeMax = trigger["max"]; delete[] jsonString; } else { cfg.NetworkSSID = WIFI_SSID; cfg.NetworkPassword = WIFI_PWD; } return cfg; }
virtual void transferData(TcpConnectionEvent sourceEvent) { if (completed) return; Vector<String> list = fileList(); debugf("send file list: %d", list.count()); for (int i = 0; i < list.count(); i++) writeString("01-01-15 01:00AM " + String(fileGetSize(list[i])) + " " + list[i] + "\r\n"); completed = true; finishTransfer(); }
PSTR fileLoad(PSTR szName,DWORD dwHead,PDWORD pdwSize,PFN_EncryptDecrypt EncryptDecrypt,PVOID pKey) { PSTR szTemp; DWORD dwSize; FILE *fp; __API_ENTER("fileLoad",PSTR,NULL); if (NULL == (fp=fopen(szName,"rb"))) __API_FINISH(); dwSize = fileGetSize(fp); retCode = MemCalloc(dwHead+dwSize+1,1); fread(&retCode[dwHead],dwSize,1,fp); fclose(fp); if (NULL != EncryptDecrypt){ if (NULL == (szTemp=(PSTR)EncryptDecrypt(pKey,(PBYTE)retCode,dwSize,&dwSize))) __API_FINISH(); MemFree(retCode); __API_PTR_TRANS(retCode,szTemp); } if (NULL != pdwSize) *pdwSize = dwSize; __API_END_POINT: __API_LEAVE("fileLoad"); }
/* Returns file size if file exists, returns 0 and creates file is file doesnt exist */ int fileInit(int shardID) { int fd, i; char fileName[FILE_NAME_SIZE]; char key[KV_SIZE], value[KV_SIZE]; // open file sprintf(fileName, "f%d", shardID); fd = open(fileName, O_RDWR, S_IRUSR | S_IWUSR); if (fd < 0) { // file doesn't exist - creating new one. fd = open(fileName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); if (fd < 0) return -1; lseek(fd, 0, SEEK_SET); write(fd, 0, sizeof(int)); close(fd); return 0; } else { fileSize[shardID] = fileGetSize(fd); lseek(fd, fileCalculateOffset(0), SEEK_SET); for (i = 0; i < fileSize[shardID]; i++) { read(fd, key, KV_SIZE); read(fd, value, KV_SIZE); if (key[0] == '\0') { do { lseek(fd, fileCalculateOffset(fileDecrementSize(fd, shardID)), SEEK_SET); read(fd, key, KV_SIZE); read(fd, value, KV_SIZE); } while(key[0] == '\0' && fileSize[shardID] > (i + 1)); lseek(fd, fileCalculateOffset(i), SEEK_SET); write(fd, key, KV_SIZE); write(fd, value, KV_SIZE); } ShardInsertFromFile(shards[shardID], key, value, i); } ftruncate(fd, (off_t) (sizeof(int) + fileSize[shardID] * 2 * KV_SIZE * sizeof(char))); close(fd); return 0; } }
//---------------------------------------------------------------------------- // //---------------------------------------------------------------------------- void CApplication::confLoad() { DynamicJsonBuffer jsonBuffer; if (!confExists()) confSave(); if (confExists()) { int size = fileGetSize(CAPP_CONF_FILE); char* strJson = new char[size + 1]; fileGetContent(CAPP_CONF_FILE, strJson, size + 1); JsonObject& root = jsonBuffer.parseObject(strJson); m_cpuBoost = root["cpuBoost"]; m_otaBaseUrl = (const char *)root["otaBaseUrl"]; m_gpiodEmul = root["emul"]; m_gpiodMode = root["mode"]; m_gpiodLock = root["lock"]; m_gpiodDisable = root["disable"]; m_gpiodInDebounce[0] = root["in0Debounce"]; m_gpiodInDebounce[1] = root["in1Debounce"]; m_gpiodInDebounce[2] = root["in2Debounce"]; m_gpiodInDebounce[3] = root["in3Debounce"]; m_gpiodOutDefRun[0] = root["out0DefRun"]; m_gpiodOutDefRun[1] = root["out1DefRun"]; m_gpiodOutDefRun[2] = root["out2DefRun"]; m_gpiodOutDefRun[3] = root["out3DefRun"]; m_gpiodUdmDefRun[0] = root["udm0DefRun"]; m_gpiodUdmDefRun[1] = root["udm1DefRun"]; delete[] strJson; } } // confLoad
static void soundPlay(struct Sound *s,char *filename,int fileofs,int filesize,int loop) { // read sound file header int err; fileHandle fh=0; long fsize; #define HEADSIZE 78 unsigned char buf[HEADSIZE]; memset(buf,0,HEADSIZE); #ifdef EXTENDFILE rfSuperStart=fileofs; rfSuperSize=filesize; #endif s->loop=loop; #define MCALL1(f) err=f;if(err) {reportErr(err); return;} #define MCALL2(f) err=f;if(err) {reportErr(err); err=fileClose(fh); if(err) reportErr(err); return;} MCALL1(fileOpen(filename,fileOpenRead,&fh)); MCALL2(fileGetSize(fh,&fsize)); MCALL2(fileRead(fh,buf,fsize<HEADSIZE?fsize:HEADSIZE)); MCALL1(fileClose(fh)); // analyze sound file header // waw sample? starts with "RIFF" with fmt!=55h if (buf[0]=='R' && buf[1]=='I' && buf[2]=='F' && buf[3]=='F' /*&& buf[8]=='W' && buf[9]=='A' && buf[10]=='V' && buf[11]=='E'*/ && buf[20]!=0x55) { extern int lastWAVfreq; s->type=Sample; s->datahandle=(int)MIDASloadWaveSample(filename,loop?MIDAS_LOOP_YES:MIDAS_LOOP_NO); if(s->datahandle) s->playhandle=(int)MIDASplaySample((MIDASsample)s->datahandle,MIDAS_CHANNEL_AUTO,0/*priority*/,lastWAVfreq,64,MIDAS_PAN_MIDDLE); } #ifdef EXTENDFILE // iff sample with 0x16 characters long name? (FT2 makes them) // it isn't ment as IFF support, just quick 99% working hack else if (buf[0]=='F' && buf[1]=='O' && buf[2]=='R' && buf[3]=='M' && buf[8]=='8' && buf[9]=='S' && buf[10]=='V' && buf[11]=='X' && buf[12]=='N' && buf[13]=='A' && buf[14]=='M' && buf[15]=='E' && buf[16]==0 && buf[17]==0 && buf[18]==0 && buf[19]==0x16) { int rate=(buf[62]<<8)+buf[63]; rfSuperStart+=78; rfSuperSize=MIN(fsize-78,(buf[74]<<24)+(buf[75]<<16)+(buf[76]<<8)+buf[77]); rfSuperXor=128; s->type=Sample; s->datahandle=(int)MIDASloadRawSample(filename,MIDAS_SAMPLE_8BIT_MONO,loop?MIDAS_LOOP_YES:MIDAS_LOOP_NO); if(s->datahandle) s->playhandle=(int)MIDASplaySample((MIDASsample)s->datahandle,MIDAS_CHANNEL_AUTO,0/*priority*/,rate,64,MIDAS_PAN_MIDDLE); } #endif // mp3 stream? starts with FF Fx or FF Ex or "ID3" or "RIFF" with fmt=55h else if ( (buf[0]==0xff && (buf[1] & 0xe0)==0xe0) || (buf[0]=='I' && buf[1]=='D' && buf[2]=='3') || (buf[0]=='R' && buf[1]=='I' && buf[2]=='F' && buf[3]=='F' && buf[20]==0x55) ) { int rate,stereo; s->type=Stream; s->datahandle=(int)MP3open(filename,fileofs,filesize,&rate,&stereo,errAdd); if(s->datahandle) s->playhandle=(int)MIDASplayStreamPolling(stereo?MIDAS_SAMPLE_16BIT_STEREO:MIDAS_SAMPLE_16BIT_MONO,rate,1000*MP3PRECALCSEC); s->bufferbytes=rate*(stereo?4:2)*MP3PRECALCSEC; } // maybe module? else { s->type=Module; s->datahandle=(int)MIDASloadModule(filename); if(s->datahandle) s->playhandle=(int)MIDASplayModule((MIDASmodule)s->datahandle,loop); } #ifdef EXTENDFILE rfSuperStart=0; rfSuperSize=0; rfSuperXor=0; #endif MIDASreportErr(); }