blobwarsAttrition/src/system/io.c

226 lines
4.0 KiB
C
Raw Normal View History

2018-01-21 10:31:38 +01:00
/*
2019-06-02 17:13:34 +02:00
Copyright (C) 2018-2019 Parallel Realities
2018-01-21 10:31:38 +01:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "io.h"
static int stringComparator(const void *a, const void *b);
int fileExists(const char *filename)
{
struct stat buffer;
return (stat(filename, &buffer) == 0);
}
2018-03-19 09:08:29 +01:00
long getFileModTime(const char *filename)
{
struct stat buffer;
2019-06-02 17:13:34 +02:00
2018-03-19 09:08:29 +01:00
stat(filename, &buffer);
2019-06-02 17:13:34 +02:00
2018-03-19 09:08:29 +01:00
return buffer.st_mtime;
}
2018-01-21 10:31:38 +01:00
const char *getFileLocation(const char *filename)
{
static char path[MAX_FILENAME_LENGTH];
memset(path, '\0', MAX_FILENAME_LENGTH);
if (fileExists(filename))
{
return filename;
}
sprintf(path, DATA_DIR"/%s", filename);
2019-06-02 17:13:34 +02:00
2018-01-21 10:31:38 +01:00
if (!fileExists(path))
{
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_CRITICAL, "No such file '%s'", path);
exit(1);
}
return path;
}
char *readFile(const char *filename)
{
char *buffer = 0;
unsigned long length;
FILE *file;
file = fopen(getFileLocation(filename), "rb");
2018-01-21 10:31:38 +01:00
if (file)
{
fseek(file, 0, SEEK_END);
length = ftell(file);
fseek(file, 0, SEEK_SET);
buffer = malloc(length + 1);
memset(buffer, 0, length + 1);
fread(buffer, 1, length, file);
fclose(file);
2019-06-02 17:13:34 +02:00
2018-01-21 10:31:38 +01:00
buffer[length] = '\0';
}
return buffer;
}
2018-02-08 09:07:04 +01:00
char *readCompressedFile(const char *filename)
{
unsigned char *buffer, *cBuffer;
2018-02-08 22:30:02 +01:00
uint32_t l1, l2;
2018-02-08 09:07:04 +01:00
unsigned long length, cLength;
FILE *file;
2019-06-02 17:13:34 +02:00
file = fopen(getFileLocation(filename), "rb");
2018-02-08 09:07:04 +01:00
buffer = 0;
cBuffer = 0;
if (file)
{
2018-02-08 22:30:02 +01:00
fread(&l1, sizeof(uint32_t), 1, file);
fread(&l2, sizeof(uint32_t), 1, file);
2019-06-02 17:13:34 +02:00
2018-02-08 22:30:02 +01:00
l1 = SDL_SwapLE32(l1);
l2 = SDL_SwapLE32(l2);
2019-06-02 17:13:34 +02:00
2018-02-08 22:30:02 +01:00
length = l1;
cLength = l2;
2019-06-02 17:13:34 +02:00
2018-02-08 09:07:04 +01:00
buffer = malloc(length + 1);
memset(buffer, 0, length + 1);
2019-06-02 17:13:34 +02:00
2018-02-08 09:07:04 +01:00
cBuffer = malloc(cLength + 1);
memset(cBuffer, 0, cLength + 1);
2019-06-02 17:13:34 +02:00
2018-02-08 09:07:04 +01:00
fread(cBuffer, 1, cLength, file);
2019-06-02 17:13:34 +02:00
2018-02-08 09:07:04 +01:00
uncompress(buffer, &length, cBuffer, cLength);
fclose(file);
2019-06-02 17:13:34 +02:00
2018-02-08 09:07:04 +01:00
buffer[length] = '\0';
2019-06-02 17:13:34 +02:00
2018-02-08 09:07:04 +01:00
free(cBuffer);
2019-06-02 17:13:34 +02:00
2018-02-08 09:07:04 +01:00
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "Decompressed '%s' %ld -> %ld", filename, cLength, length);
}
return (char*) buffer;
}
2018-01-21 10:31:38 +01:00
int writeFile(const char *filename, const char *data)
{
FILE *file;
int result;
file = fopen(filename, "wb");
2018-01-21 10:31:38 +01:00
if (file)
{
result = fprintf(file, "%s\n", data);
2018-01-21 10:31:38 +01:00
fclose(file);
return result == strlen(data) + 1;
2018-01-21 10:31:38 +01:00
}
return 0;
}
char **getFileList(const char *dir, int *count)
{
DIR *d;
int i;
struct dirent *ent;
char **filenames;
i = 0;
filenames = NULL;
if ((d = opendir(getFileLocation(dir))) != NULL)
{
while ((ent = readdir(d)) != NULL)
{
if (ent->d_name[0] != '.')
{
i++;
}
}
if (i > 0)
{
filenames = malloc(sizeof(char*) * i);
memset(filenames, 0, sizeof(char*) * i);
rewinddir(d);
i = 0;
while ((ent = readdir(d)) != NULL)
{
if (ent->d_name[0] != '.')
{
filenames[i] = malloc(sizeof(char) * MAX_FILENAME_LENGTH);
STRNCPY(filenames[i], ent->d_name, MAX_FILENAME_LENGTH);
i++;
}
}
}
closedir(d);
}
*count = i;
if (filenames)
{
qsort(filenames, i, sizeof(char*), stringComparator);
}
return filenames;
}
2018-03-19 23:51:37 +01:00
int deleteFile(char *path)
{
if (fileExists(path))
{
return unlink(path) == 0;
}
return 1;
2018-03-19 23:51:37 +01:00
}
int renameFile(char *src, char *dest)
{
return deleteFile(dest) && rename(src, dest) == 0;
}
2018-01-21 10:31:38 +01:00
static int stringComparator(const void *a, const void *b)
{
char **s1 = (char **)a;
char **s2 = (char **)b;
return strcmp(*s1, *s2);
}