forked from dazjo/nxtool
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfilepath.c
90 lines (69 loc) · 1.54 KB
/
filepath.c
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
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "types.h"
#include "filepath.h"
void filepath_init(filepath* fpath)
{
fpath->valid = 0;
}
void filepath_copy(filepath* fpath, filepath* copy)
{
if (copy != 0 && copy->valid)
memcpy(fpath, copy, sizeof(filepath));
else
memset(fpath, 0, sizeof(filepath));
}
void filepath_append_utf16(filepath* fpath, const u8* name)
{
u32 size;
if (fpath->valid == 0)
return;
size = strlen(fpath->pathname);
if (size > 0 && size < (MAX_PATH-1))
{
if (fpath->pathname[size-1] != PATH_SEPERATOR)
fpath->pathname[size++] = PATH_SEPERATOR;
}
while(size < (MAX_PATH-1))
{
u8 lo = *name++;
u8 hi = *name++;
u16 code = (hi<<8) | lo;
if (code == 0)
break;
// convert non-ANSI to '#', because unicode support is too much work
if (code > 0x7F)
code = '#';
fpath->pathname[size++] = (char) code;
}
fpath->pathname[size] = 0;
if (size >= (MAX_PATH-1))
fpath->valid = 0;
}
void filepath_append(filepath* fpath, const char* format, ...)
{
char tmppath[MAX_PATH];
va_list args;
if (fpath->valid == 0)
return;
memset(tmppath, 0, MAX_PATH);
va_start(args, format);
vsprintf(tmppath, format, args);
va_end(args);
strcat(fpath->pathname, "/");
strcat(fpath->pathname, tmppath);
}
void filepath_set(filepath* fpath, const char* path)
{
fpath->valid = 1;
memset(fpath->pathname, 0, MAX_PATH);
strncpy(fpath->pathname, path, MAX_PATH);
}
const char* filepath_get(filepath* fpath)
{
if (fpath->valid == 0)
return 0;
else
return fpath->pathname;
}