forked from aquynh/unidos
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdospath.c
112 lines (95 loc) · 2.31 KB
/
dospath.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <stdlib.h>
#include <string.h>
#include "dospath.h"
#include "global.h"
void path_to_string(struct dospath path, char* buf)
{
if (path.drive != -1)
{
buf[0] = path.drive + 'A';
buf[1] = ':';
buf[2] = 0;
}
if (path.depth == 0)
{
strcat(buf, "\\");
}
else
{
for (int i = 0; i < path.depth; i++)
{
strcat(buf, "\\");
strcat(buf, path.path[i]);
}
}
}
void path_parse(char* str, struct dospath* path)
{
if (str[1] == ':')
{
path->drive = str[0] - 'A';
str += 2;
}
else
{
path->drive = -1;
}
if (str[0] == '\\')
{
str++;
}
char* buf = malloc(strlen(str) + 1);
strcpy(buf, str);
char* p = strtok(buf, "\\");
path->depth = 0;
path->path = NULL;
while (p)
{
if (strlen(p) != 0)
{
path->path = realloc(path->path, sizeof(char*) * ++path->depth);
path->path[path->depth - 1] = p;
}
p = strtok(NULL, "\\");
}
}
void path_combine(struct dospath first, struct dospath second, struct dospath* output)
{
if (second.drive != -1)
{
path_copy(second, output);
return;
}
output->drive = first.drive;
output->depth = first.depth + second.depth;
output->path = (char**) malloc(output->depth * sizeof(char*));
for (int i = 0; i < first.depth; i++)
{
size_t len = strlen(first.path[i]) + 1;
output->path[i] = (char*) malloc(len);
memcpy(output->path[i], first.path[i], len);
}
for (int i = 0; i < second.depth; i++)
{
int off = i + first.depth;
size_t len = strlen(second.path[i]) + 1;
output->path[off] = (char*) malloc(len);
memcpy(output->path[off], second.path[i], len);
}
}
void path_absolute(struct dospath path, struct dospath* output)
{
path_combine(cur_path[cur_drive], path, output);
}
void path_copy(struct dospath src, struct dospath* dest)
{
dest->drive = src.drive;
dest->depth = src.depth;
dest->path = (char**) malloc(dest->depth * sizeof(char*));
for (int i = 0; i < dest->depth; i++)
{
size_t len = strlen(src.path[i]) + 1;
dest->path[i] = (char*) malloc(len);
memcpy(dest->path[i], src.path[i], len);
}
}