/// Loads data for testing the 'testFileName' file. void loadTestFileData(const char* testFileName) { fileName = getTestFileFullPath(SAMPLE_FILES_DIR, testFileName); const char* fileContent = loadTestFile(SAMPLE_FILES_DIR, testFileName, true); // must be binary mode! int fileSize = (int)fgetsize(fileName.c_str()); const WCHAR* wtestFileName = toWideChar(testFileName); FileData fileData; fileData.setName(wtestFileName); fileData.setSize(fileSize); fileData.setBody(fileContent, fileSize); unsigned long tstamp = getFileModTime(fileName); StringBuffer modTime = unixTimeToString(tstamp, true); // file's mod time is already in UTC WString wmodTime; wmodTime = modTime; fileData.setModified(wmodTime); fileDataContent = fileData.format(); fileDataSize = (int)strlen(fileDataContent); delete [] wtestFileName; delete [] fileContent; }
bool readFile(const char* path, char **message, size_t *len, bool binary) { bool ret = false; FILE *f; size_t msglen=0; char *msg=0; const char* mode = binary ? "rb" : "r"; f = fileOpen(path, mode); if ( !f ) { goto finally; } msglen = fgetsize(f); msg = new char[msglen+1]; *len=fread(msg, sizeof(char), msglen, f); if(ferror(f)){ delete [] msg; goto finally; } fclose(f); // Terminate the read msg msg[*len] = 0; // Set return parameters *message= msg ; //*len=msglen; ret = true; finally: return ret; }
void recurCompress(char * pathname){ WIN32_FIND_DATA fdFile; HANDLE hFind = NULL; FILE * output = NULL; FILE * input = NULL; char sPath[2048]; float outputsize; output = fopen(strcat(removeslash(pathname),".lar"),"wb"); //Specify a file mask. *.* = We want everything! sprintf(sPath, "%s\\*.*", pathname); if((hFind = FindFirstFile(sPath, &fdFile)) == INVALID_HANDLE_VALUE) { printf("Path not found: [%s]\n", pathname); return ; } do { //Find first file will always return "." // and ".." as the first two directories. if(strcmp(fdFile.cFileName, ".") != 0 && strcmp(fdFile.cFileName, "..") != 0) { //Build up our file path using the passed in sprintf(sPath, "%s\\%s", pathname, fdFile.cFileName); if(fdFile.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY) { printf("Directory: %s\n", sPath); recurCompress(sPath); } else{ /* Compress every file in the same output */ input = fopen(sPath, "rb"); if(!input){ printf(M_ERROR "Error while opening file\n"); return ; } compress(&lz, input,output); printf("~~ %s => %s\n", fdFile.cFileName, DrawByte((float)fdFile.nFileSizeLow)); fclose(input); } } } while(FindNextFile(hFind, &fdFile)); outputsize = fgetsize(output); printf("\n"); printf(M_OK "archive file size %s\n",DrawByte(outputsize)); FindClose(hFind); fclose(output); return; }
size_t winLog::getLogSize() { size_t ret = 0; logFile = _wfopen(wlogDir.c_str(), TEXT("r")); if (logFile) { ret = fgetsize(logFile); fclose(logFile); } return ret; }
size_t fgetsize(const char* fileName) { if (fileName) { FILE* f = fopen(fileName, "rb"); if (f) { size_t size = fgetsize(f); fclose(f); return size; } } return 0; }
/* * Allocate memory and read the entire file from the given file pointer. * The file size is stored into the size pointer if not NULL. */ static char * fgetcontents(FILE *fp, long *size) { long filesize = fgetsize(fp); char *contents = (char*) malloc(filesize + 255); if (!contents) return NULL; if (1 != fread(contents, filesize, 1, fp)) { free(contents); return NULL; } if (size) *size = filesize; contents[filesize] = '\0'; return contents; }
void recurCompress(char * pathname){ DIR * mydir; struct dirent *dptr = NULL; FILE * input; FILE * output; char path[256]; float outputsize; output = fopen(strcat(removeslash(pathname),".lar"),"wb"); mydir = opendir(pathname); if(!mydir){ printf(M_ERROR "can'found the folder `%s`\n",pathname); return; }else{ while((dptr = readdir(mydir)) != NULL){ printf("~~ %s => %d\n",dptr->d_name,dptr->d_reclen); sprintf(path,"%s%s",pathname,dptr->d_name,dptr); /* check if the file is valid */ if(strcmp(dptr->d_name, "..") == 0){ }else{ /* compress every file */ input = fopen(path ,"rb"); if(!input){ printf(M_ERROR "Error while opening file\n"); return ; } /* add header */ addHeader(input, path); compress(&lz,input,output); fclose(input); } } } outputsize = fgetsize(output); printf("\n"); printf(M_OK "archive file size %s\n",DrawByte(outputsize)); fclose(output); closedir(mydir); return ; }
bool readFile(const char* path, char **message, size_t *len, bool binary) { bool ret = false; FILE *f; size_t msglen=0; char *msg=0; // Convert to wide-char to be compatible with international chars. // If using char and UTF-8, fopen() is not working correctly and // will fail with special chars. (with UTF-8... why?) // So we use _wfopen() and a wide-char path. const WCHAR* mode = binary ? TEXT("rb") : TEXT("r"); WCHAR* wpath = toWideChar(path); f = _wfopen(wpath, mode); if ( !f ) { goto finally; } msglen = fgetsize(f); msg = new char[msglen+1]; *len=fread(msg, sizeof(char), msglen, f); if(ferror(f)) { delete [] msg; goto finally; } fclose(f); // Terminate the read msg msg[*len] = 0; // Set return parameters *message= msg ; //*len=msglen; ret = true; finally: delete [] wpath; return ret; }