Descent3/misc/pstring.cpp

178 lines
4.6 KiB
C++
Raw Normal View History

/*
* Descent 3
* Copyright (C) 2024 Parallax Software
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2024-04-16 03:43:29 +00:00
/*
2024-04-16 18:56:40 +00:00
* $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 $
*
2024-04-16 03:43:29 +00:00
* 4 4/15/99 1:51a Jeff
* changes for linux compile
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 3 12/16/98 1:57p Samir
* Replaced CleanupString2 with CleanupStr
2024-04-16 18:56:40 +00:00
*
2024-04-16 03:43:29 +00:00
* 2 11/01/98 1:56a Jeff
* added pstring.cpp/.h
2024-04-16 18:56:40 +00:00
*
* $NoKeywords: $
*/
2024-04-16 03:43:29 +00:00
#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.
2024-04-16 18:56:40 +00:00
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;
2024-04-16 03:43:29 +00:00
}
// 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.
2024-04-16 18:56:40 +00:00
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;
2024-04-16 03:43:29 +00:00
}
// CleanupStr
// this function strips all leading and trailing spaces, keeping internal spaces. this goes
// for tabs too.
2024-04-16 18:56:40 +00:00
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;
2024-04-16 03:43:29 +00:00
}
// 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.
2024-04-16 18:56:40 +00:00
tStringTok::~tStringTok() {
if (m_strbuf)
delete[] m_strbuf;
2024-04-16 03:43:29 +00:00
}
2024-04-16 18:56:40 +00:00
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;
}
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
char *new_str = new char[strlen(str) + 1];
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
// 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);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
if (m_strbuf)
delete[] m_strbuf;
m_strbuf = new_str;
m_curptr = m_strbuf;
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
return this->next(tokens);
}
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
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;
2024-04-16 03:43:29 +00:00
}