Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FileSystem.c - github.com/SoftEtherVPN/libhamcore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 396856232257bcbcf5d4f9c7146f09e7ab36cfe8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "FileSystem.h"

#include <string.h>

#include <sys/stat.h>

FILE *Ham_FileOpen(const char *path, const bool write)
{
	if (!path)
	{
		return NULL;
	}

	return fopen(path, write ? "wb" : "rb");
}

bool Ham_FileClose(FILE *file)
{
	if (!file)
	{
		return false;
	}

	return fclose(file) == 0;
}

bool Ham_FileRead(FILE *file, void *dst, const size_t size)
{
	if (!file || !dst || size == 0)
	{
		return false;
	}

	return fread(dst, 1, size, file) == size;
}

bool Ham_FileWrite(FILE *file, const void *src, const size_t size)
{
	if (!file || !src || size == 0)
	{
		return false;
	}

	return fwrite(src, 1, size, file) == size;
}

bool Ham_FileSeek(FILE *file, const size_t offset)
{
	if (!file)
	{
		return false;
	}

	return fseek(file, (long)offset, SEEK_SET) == 0;
}

size_t Ham_FileSize(const char *path)
{
	if (!path)
	{
		return 0;
	}

	struct stat st;
	if (stat(path, &st) == -1)
	{
		return 0;
	}

	return st.st_size;
}

const char *Ham_PathRelativeToBase(const char *full, const char *base)
{
	if (!full || !base)
	{
		return NULL;
	}

	if (strstr(full, base) != &full[0])
	{
		return NULL;
	}

	full += strlen(base);
	if (full[0] == '/')
	{
		++full;
	}

	return full;
}