-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.c
More file actions
136 lines (103 loc) · 2.81 KB
/
Copy pathutil.c
File metadata and controls
136 lines (103 loc) · 2.81 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "include/vector.h"
#include "include/util.h"
#define TRUE 1
#define FALSE 0
static execution_type e_type = EXECUTE;
VECTOR *tokenize(char *s, char c) {
VECTOR *ret = vector_create(1, 2, NULL, free);
int in_token = FALSE;
/* initialize our looping variables */
unsigned int c_i = 0;
int s_len = strlen(s);
/* initialize our token boundary variables */
unsigned int tok_start = c_i;
unsigned int tok_end = c_i;
for(; c_i < s_len; ++c_i) {
if(s[c_i] == c) {
if(in_token) {
/* we were just in a token, and we found our delimeter character,
grab the token and throw it in the vector */
unsigned int tok_len = tok_end - tok_start + 1;
char *token = (char *)calloc(tok_len + 1, sizeof(char));
strncpy(token, &s[tok_start], tok_len);
vector_push_back(ret, token);
in_token = FALSE;
} else {
/* we weren't in our token, there must me
multiple delimiters grouped together */
}
} else {
if(in_token) {
/* we are already in a token, nothing special to do here other than
increment the tok_end */
++tok_end;
} else {
/* we just finished looking at a delimeter, or the loop just started,
either way the current character denotes the start of a new token */
tok_start = c_i;
tok_end = tok_start;
in_token = TRUE;
}
}
}
/* the string might end on a valid token, we want to grab it */
if(in_token) {
unsigned int tok_len = tok_end - tok_start + 1;
char *token = (char *)calloc(tok_len + 1, sizeof(char));
strncpy(token, &s[tok_start], tok_len);
vector_push_back(ret, token);
}
return ret;
}
char *join(VECTOR *tokens, char *join_str) {
char *ret;
unsigned int len = 0;
int t_idx = 0;
int t_len;
if(tokens == NULL || join_str == NULL)
return NULL;
t_len = vector_size(tokens);
for(; t_idx < t_len; ++t_idx) {
len += strlen((char *)vector_get(tokens, t_idx)) + 1; // + 1 for join_char
}
ret = (char *)malloc(sizeof(char) * len + 1);
t_idx = 0;
for(; t_idx < t_len; ++t_idx) {
strcat(ret, (char *)vector_get(tokens, t_idx));
strcat(ret, join_str);
}
return ret;
}
/* Dan Bernstein's djb2 hash algorithm */
unsigned long djb2(unsigned char *str) {
unsigned long hash = 5381;
int c;
while ((c = *str++))
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}
void set_print_or_execute(execution_type t) {
e_type = t;
}
execution_type get_print_or_execute() {
return e_type;
}
// todo - implement this
/* void process_file_path(char *path) {
int p_idx = 0;
int p_len;
int s_count = 0;
if(path == NULL)
return;
p_len = strlen(path);
for(; p_idx < p_len; ++p_idx) {
if(path[p_idx] == '\\')
s_count++;
}
if(s_count > 0) {
path = (char *)realloc(path, strlen(path) + s_count + 1);
}
} */