-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
157 lines (132 loc) · 3.36 KB
/
utils.c
File metadata and controls
157 lines (132 loc) · 3.36 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include "utils.h"
ListPrintProperties defaultPrintProperties = {"[ ",", "," ]"};
MapPrintProperties defaultMapPrintProperties = {"( "," : "," )", &defaultPrintProperties};
TreePrintProperties defaultTreePrintProperties = {"[", "]", " |", "__________"};
/* -----------------------------------------------------------------------------
* hashpjw
* Peter J. Weinberger's hash function
* Source: Aho, Sethi, and Ullman, "Compilers", Addison-Wesley, 1986 (page 436).
*/
int hashpjw( void* value ) {
char *s = (char*)value;
char *p;
unsigned h = 0, g;
for ( p = s; *p != EOS; p++ ) {
h = (h << 4) + (*p);
if ( g = h & 0xf0000000 ) {
h = h ^ ( g >> 24 );
h = h ^ g;
}
}
return h % TABLE_SIZE;
}
void resetOptions (ForEachOptions * options) {
options->remove = false;
options->insert = 0;
}
char * intToString(int integer) {
if(integer == 0)
return copyString("0");
int totalSize = 0;
int tempValue = integer;
bool neg = tempValue < 0;
if(neg) {
tempValue *= -1;
totalSize++;
}
while(tempValue != 0) {
tempValue /= 10;
totalSize++;
}
char * temp = malloc(sizeof(char) * (totalSize + 1));
sprintf(temp, "%i", integer);
return temp;
}
int getStringSize(char * stringValue) {
if(!stringValue)
return -1;
int size = 0;
char c = *stringValue;
while (c != '\0') {
size++;
c = *(stringValue + size);
}
return size;
}
int stringToInt(char * value) {
int firstHalf = 0;
int index = 0;
char c = value[0];
while (c != 'E' && c != '\0') {
firstHalf = firstHalf * 10 + c - '0';
index ++;
c = value [index];
}
if ( c == 'E' ) {
bool negate = false;
index++;
c = value[index];
if (c == '+') {
index++;
c = value[index];
} else if(c == '-') {
negate = true;
index++;
c = value[index];
}
int secondHalf = 0;
while ( c != '\0' ) {
secondHalf = secondHalf * 10 + c - '0';
index ++;
c = value [index];
}
if (negate)
secondHalf *= -1;
secondHalf = pow(10, secondHalf);
return firstHalf * secondHalf;
} else {
return firstHalf;
}
}
void stringInsert(char * totalString, char * insertString, int startIndex) {
int i = 0;
char c = *insertString;
while(c != '\0') {
*(totalString + i + startIndex) = c;
i++;
c = *(insertString+i);
}
}
char * copyString(char * stringValue) {
int stringSize = getStringSize(stringValue);
char * temp = malloc(sizeof(char)*(stringSize+1));
stringInsert(temp, stringValue, 0);
temp[stringSize] = '\0';
return temp;
}
char * stringTakeLast(char * totalString, int startInclusive) {
int stringSize = getStringSize(totalString);
if(startInclusive < 0 || startInclusive > stringSize)
return 0;
int newSize = stringSize - startInclusive;
char * lastPart = malloc(sizeof(char) * (newSize + 1));
for(int i = 0; i < newSize; i++)
lastPart[i] = totalString[startInclusive+i];
lastPart[newSize] = '\0';
return lastPart;
}
char * stringTakeSubstring(char * totalString, int startInclusive, int endExclusize) {
int stringSize = getStringSize(totalString);
if(startInclusive < 0 || endExclusize > stringSize || startInclusive > endExclusize)
return 0;
int newSize = endExclusize - startInclusive;
char * lastPart = malloc(sizeof(char) * (newSize + 1));
for(int i = 0; i < newSize; i++)
lastPart[i] = totalString[startInclusive+i];
lastPart[newSize] = '\0';
return lastPart;
}