Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include "FindFiles.h"
- /**********************/
- /** String functions **/
- /**********************/
- int lenght(FONTCHARACTER* str) {
- int i;
- for(i=0 ; str[i] ; i++);
- return i;
- }
- FONTCHARACTER* str2FONTCHARACTER(char* source, FONTCHARACTER* dest) {
- int i;
- for(i=0 ; source[i] ; i++)
- dest[i] = source[i];
- dest[i] = 0;
- return dest;
- }
- char* FONTCHARACTER2str(FONTCHARACTER* source, char* dest) {
- int i;
- for(i=0 ; source[i] ; i++)
- dest[i] = source[i];
- dest[i] = 0;
- return dest;
- }
- char* filename(char* completePath) {
- int i;
- char* p;
- p = completePath;
- for(i=0 ; completePath[i] ; i++) {
- if(completePath[i] == '\\')
- p = &completePath[i+1];
- }
- return p;
- }
- /*********************/
- /** Array functions **/
- /*********************/
- int fileListLenght(File* list) {
- int i;
- for(i=0 ; list[i].path ; i++);
- return i;
- }
- File* joinFilelists(File* list1, File* list2, int deleteSources) {
- int i, j;
- File* list = malloc((fileListLenght(list1) + fileListLenght(list2) + 1) * sizeof(File));
- if(list) {
- for(i=0 ; list1[i].path ; i++)
- list[i] = list1[i];
- for(j=0 ; list2[j].path ; j++)
- list[i+j] = list2[j];
- }
- if(deleteSources) {
- freeFilelist(list1);
- freeFilelist(list2);
- }
- return list;
- }
- /**********************/
- /** Search functions **/
- /**********************/
- int countFiles(char* mask) {
- int count = 0, handle;
- FONTCHARACTER* _mask, foundfile[30];
- FILE_INFO fileinfo;
- _mask = malloc((strlen(mask) + 1) * sizeof(FONTCHARACTER));
- if(_mask) {
- str2FONTCHARACTER(mask, _mask);
- if(Bfile_FindFirst(_mask, &handle, foundfile, &fileinfo) == 0) {
- do count++;
- while(Bfile_FindNext(handle, foundfile, &fileinfo) == 0);
- Bfile_FindClose(handle);
- }
- free(_mask);
- }
- return count;
- }
- File* findFiles(char* mask) {
- int handle, i=0, pathlength;
- File* list = 0;
- FONTCHARACTER* _mask, foundfile[30];
- FILE_INFO fileinfo;
- list = malloc((countFiles(mask) + 1) * sizeof(char*));
- if(list) {
- _mask = malloc((strlen(mask) + 1) * sizeof(FONTCHARACTER));
- if(_mask) {
- str2FONTCHARACTER(mask, _mask);
- filename(mask)[0] = 0;
- pathlength = strlen(mask);
- if(Bfile_FindFirst(_mask, &handle, foundfile, &fileinfo) == 0) {
- do {
- list[i].path = malloc(pathlength + lenght(foundfile) + 1);
- if(list[i].path) {
- strcpy(list[i].path, mask);
- list[i].name = filename(list[i].path);
- FONTCHARACTER2str(foundfile, list[i].name);
- list[i].size = fileinfo.fsize;
- } else {
- list[i].name = 0;
- list[i].size = 0;
- }
- i++;
- } while(Bfile_FindNext(handle, foundfile, &fileinfo) == 0);
- Bfile_FindClose(handle);
- }
- free(_mask);
- }
- list[i].path = 0;
- list[i].name = 0;
- list[i].size = 0;
- }
- return list;
- }
- void freeFilelist(File* list) {
- int i;
- if(list) {
- for(i = 0 ; list[i].path ; i++)
- free(list[i].path);
- free(list);
- }
- }
- /********************/
- /** File functions **/
- /********************/
- int openFile(char* path, char* mode) {
- int _mode = 0;
- FONTCHARACTER filename[30];
- if(strcmp(mode, "r")==0) _mode = _OPENMODE_READ;
- if(strcmp(mode, "w")==0) _mode = _OPENMODE_WRITE;
- if(strcmp(mode, "r+")==0) _mode = _OPENMODE_READWRITE;
- return Bfile_OpenFile(str2FONTCHARACTER(path, filename), _mode);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement