word wrap - part 1 and 2

This commit is contained in:
Fournier Gabriel
2020-11-21 19:29:46 +01:00
parent 5b21c57e4c
commit 3e2b5178ed
20 changed files with 254 additions and 15 deletions

View File

@@ -31,6 +31,19 @@ bool stringEndsWith(const char* str, const char* pattern)
return false;
}
void stringNCopy(char* dest, int max, const char* src, int len)
{
while(len>0 && max >1 && *src)
{
*dest = *src;
dest++;
src++;
len--;
max--;
}
*dest=0;
}
#ifdef DEVICE
@@ -54,6 +67,34 @@ int filesWithExtension(const char* extension, External::Archive::File* files, in
}
#else
static void fillFileData(External::Archive::File& file)
{
file.data = nullptr;
file.dataLength = 0;
struct stat info;
if (stat(file.name, &info) != 0)
{
return;
}
unsigned char* content = new unsigned char[info.st_size];
if (content == NULL)
{
return ;
}
FILE *fp = fopen(file.name, "rb");
if (fp == NULL)
{
return ;
}
fread(content, info.st_size, 1, fp);
fclose(fp);
file.data = content;
file.dataLength = info.st_size;
}
int filesWithExtension(const char* extension, External::Archive::File* files, int filesSize)
{
dirent *file;
@@ -63,9 +104,10 @@ int filesWithExtension(const char* extension, External::Archive::File* files, in
{
while ((file = readdir(d)) != NULL)
{
if(stringEndsWith(dir->d_name, extension))
if(stringEndsWith(file->d_name, extension))
{
files[nb].name = strdup(file->d_name);//will probably leak
fillFileData(files[nb]);
nb++;
if(nb == filesSize)
break;
@@ -75,7 +117,6 @@ int filesWithExtension(const char* extension, External::Archive::File* files, in
}
return nb;
}
#endif
}