-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathstrTools.cpp
More file actions
154 lines (114 loc) · 4.31 KB
/
Copy pathstrTools.cpp
File metadata and controls
154 lines (114 loc) · 4.31 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
153
154
#include <strTools.h>
//****************************************************************************************
// upCase(), lwrCase():
//
// Pass in a c string and make all the letters either uppercase orlowercase.
//****************************************************************************************
// Pass in a string and this makes all the letters uppercase.
void upCase(char* inStr) {
int i;
i=0;
while(inStr[i]) {
inStr[i] = toupper(inStr[i]);
i++;
}
}
// Pass in a string and this makes all the letters lowercase.
void lwrCase(char* inStr) {
int i;
i=0;
while(inStr[i]) {
inStr[i] = tolower(inStr[i]);
i++;
}
}
//*****************************************************************************************
// delChar(char* strPtr):
//
// Pass in a pointer into a c string. This will delete this char and then patch the hole
// by moving the rest of the string up to fill it.
//
//
// void delChar(char* aStr,int inIndex):
//
// Pass in a c string with an index into that string. This will delete this char at that
// index and then patch the hole by moving the rest of the string up to fill it.
//
//****************************************************************************************
// Pass in a pointer to a char on a string. This'll delete that char from the c string.
void delChar(char* strPtr) {
char* tracePtr;
if (strPtr) {
if (strlen(strPtr)) { // If we can find a '\0' after this point..
tracePtr = strPtr+1;
while(*tracePtr) {
*strPtr = *tracePtr;
strPtr = strPtr+1;
tracePtr = tracePtr+1;
}
*strPtr = *tracePtr;
}
}
}
// Pass an index to a string . This'll delete that char from the c string.
void delChar(char* aStr,int inIndex) {
int strIndex;
int numChars;
if (aStr) {
if (inIndex<=strlen(aStr)) {
delChar(&(aStr[inIndex]));
}
}
}
//****************************************************************************************
// heapStr:
//
// For reallocating strings.
// Good for passing string data from one function to another. Lets you have an easy way to
// "land" the data. Typically this is used for setting the size of a global buffer to fit
// the current data. No more max sized buffers clogging up the heap. each time you write a
// string to it it auto resizes to just fit that string.
//****************************************************************************************
// Load in this string. Allocate, or re-allocate your char* to save it.
bool heapStr(char** resultStr,const char* inStr) {
int numChars;
bool success;
success = false; // We need to prove our success!
if (inStr) { // If we got a non-NULL string..
numChars = strlen(inStr); // Count the chars in inStr.
if (resizeBuff(numChars+1,resultStr)) { // If we can allocated the memory..
strcpy(*resultStr,inStr); // Copy input into the resulting string.
success = true; // And that's a success!
} //
} else { // Else, they handed us a NULL string?
resizeBuff(0,resultStr); // I guess they want the result to be NULL as well.
success = true; // In that case, I guess we were successful.
} //
return success; // And we return our success. Good or bad.
}
// When you are done with the string you were using, call this to recycle the RAM.
void freeStr(char** resultStr) { resizeBuff(0,resultStr); }
//****************************************************************************************
// tempStr
// Very good for grabbing data that may change and holding a copy it so you can do your
// stuff without worry. Auto deallocated when it goes out of scope.
//
// DO NOT CREATE THESE INSIDE A SWITCH STATEMENT! Creating variables and classes in switch
// statements totally breaks them. You can USE them in switch statements as long as they
// are created outside of the switch statements.
//****************************************************************************************
tempStr::tempStr(const char* inStr) {
theStr = NULL;
if (inStr) {
setStr(inStr);
}
}
tempStr::~tempStr(void) { freeStr(&theStr); }
void tempStr::setStr(const char* inStr) { heapStr(&theStr,inStr); }
int tempStr::numChars(void) {
if (theStr) {
return strlen(theStr);
}
return 0;
}
const char* tempStr::getStr(void) { return (const char*)theStr; }