forked from DescentDevelopers/Descent3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpstring.cpp
159 lines (135 loc) · 3.88 KB
/
pstring.cpp
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
155
156
157
158
159
/*
* $Logfile: /DescentIII/Main/misc/pstring.cpp $
* $Revision: 4 $
* $Date: 4/15/99 1:51a $
* $Author: Jeff $
*
* Safe string manipulation and creation functions
*
* $Log: /DescentIII/Main/misc/pstring.cpp $
*
* 4 4/15/99 1:51a Jeff
* changes for linux compile
*
* 3 12/16/98 1:57p Samir
* Replaced CleanupString2 with CleanupStr
*
* 2 11/01/98 1:56a Jeff
* added pstring.cpp/.h
*
* $NoKeywords: $
*/
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "pstring.h"
#ifdef __LINUX__
#include "lnxfix.h"
#endif
// Pvsprintf
// Similar to vsprintf/_vsnprintf, however handles the case of a buffer overflow. Arguments are the
// same as _vsnprintf. In the case of count, pass in the size of the buffer, in the case where there is
// a buffer overflow, a \0 will be tacked on as the last byte in the buffer, ensuring a valid string.
int Pvsprintf(char *buffer, int count, const char *format, va_list argptr) {
int ret = _vsnprintf(buffer, count - 1, format, argptr);
if (ret == -1)
buffer[count - 1] = '\0';
return ret;
}
// Psprintf
// Similar to sprintf/_snprintf, however handles the case of a buffer overflow. Arguments are the
// same as _snprintf. In the case of count, pass in the size of the buffer, in the case where there is
// a buffer overflow, a \0 will be tacked on as the last byte in the buffer, ensuring a valid string.
int Psprintf(char *buffer, int count, const char *format, ...) {
va_list ap;
va_start(ap, format);
int ret = Pvsprintf(buffer, count, format, ap);
va_end(ap);
return ret;
}
// CleanupStr
// this function strips all leading and trailing spaces, keeping internal spaces. this goes
// for tabs too.
int CleanupStr(char *dest, const char *src, int destlen) {
int i, j, err, begin = 0, end = 0, len;
err = 0;
len = strlen(src);
for (i = 0; i < len; i++) {
char ch;
ch = src[i];
// mark beginning.
if ((ch > ' ' && ch > '\t') && err < 1) {
err = 1;
begin = i;
end = i;
} else if (ch == ' ' && err == 1) {
err = 2;
end = i;
} else if (ch > ' ' && err >= 1) {
end = i;
}
}
j = 0;
for (i = begin; i < (end + 1); i++) {
char ch;
ch = src[i];
if (j == destlen - 1)
break;
if (ch != '\"')
dest[j++] = ch;
}
dest[j] = 0;
return 0;
}
// tStringTok
// you may start a string tokenization object by calling the start function
// then call next, to get the following tokens.
// note that this class uses it's own copy of the string to ensure that strtok doesn't
// get corrupted.
tStringTok::~tStringTok() {
if (m_strbuf)
delete[] m_strbuf;
}
char *tStringTok::start(const char *str, const char *tokens) {
// if we pass a null string, then reset this object
if (str == NULL) {
if (m_strbuf)
delete[] m_strbuf;
m_strbuf = NULL;
m_curptr = NULL;
return NULL;
}
char *new_str = new char[strlen(str) + 1];
// copy string into new string buffer. AFTER THIS, delete the current string buffer, since the pointer
// passed in could point to the current m_strbuf ptr.
strcpy(new_str, str);
if (m_strbuf)
delete[] m_strbuf;
m_strbuf = new_str;
m_curptr = m_strbuf;
return this->next(tokens);
}
char *tStringTok::next(const char *tokens) {
// create string by terminating m_strbuf when a token is it.
char *cur_str, *end_str;
int slen2, j;
if (!m_curptr)
return m_curptr;
slen2 = strlen(tokens);
cur_str = NULL;
for (j = 0; j < slen2; j++) {
end_str = strchr(m_curptr, tokens[j]);
if (end_str) {
*end_str = 0;
cur_str = m_curptr;
m_curptr = end_str + 1;
return cur_str;
}
}
// at this point, we found no tokens, so m_curptr will point to the string we want to return.
// then we set m_curptr to NULL, telling any subsequent calls to next to return NULL.
cur_str = m_curptr;
m_curptr = NULL;
return cur_str;
}