Remove LAMEString.cpp/h

This commit is contained in:
Henrik Rydgard 2012-07-23 20:45:04 +02:00
parent 0f8d52fecb
commit 7e2e7856d8
8 changed files with 27 additions and 566 deletions

View File

@ -1,5 +1,4 @@
set(SRCS
LAMEString.cpp
colorutil.cpp
timeutil.cpp
threadutil.cpp

View File

@ -1,415 +0,0 @@
// NOTE: See warning in header.
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include "LAMEString.h"
#define assert(a)
const int MAX_LENGTH = 1024; // largest size string for input
#define _tcslen strlen
#define _tcscpy strcpy
#define _tcscmp strcmp
#define _tcsncmp strncmp
String::String()
{
Capacity = 1;
CString = new TCHAR[Capacity];
CString[0] = '\0'; // make c-style string zero length
}
String::String(const TCHAR * s)
{
Capacity = (int)_tcslen(s) + 1;
CString = new TCHAR[Capacity];
_tcscpy(CString,s);
}
String::String(const String & str)
{
Capacity = str.length() + 1;
CString = new TCHAR[Capacity];
_tcscpy(CString,str.CString);
}
String::~String()
{
delete [] CString; // free memory
}
const String & String::operator = (const String & rhs)
{
if (this != &rhs) { // check aliasing
if (Capacity < rhs.length() + 1) { // more memory needed?
delete[] CString; // delete old string
Capacity = rhs.length() + 1; // add 1 for '\0'
CString = new TCHAR[Capacity];
}
_tcscpy(CString,rhs.CString);
}
return *this;
}
const String & String::operator = (const TCHAR * s)
{
int len = 0; // length of newly constructed string
assert(s != 0); // make sure s non-NULL
len = (int)_tcslen(s); // # of characters in string
// free old string if necessary
if (Capacity < len + 1)
{
delete[] CString; // delete old string
Capacity = len + 1; // add 1 for '\0'
CString = new TCHAR[Capacity];
}
_tcscpy(CString,s);
return *this;
}
const String & String::operator = (TCHAR ch)
{
if (Capacity < 2)
{
delete [] CString;
Capacity = 2;
CString = new TCHAR[Capacity];
}
CString[0] = ch; // make string one character long
CString[1] = '\0';
return *this;
}
int String::length() const {
int myLength = 0;
while (CString[myLength] != '\0')
myLength++;
return myLength;
}
TCHAR * String::getPointer() const
{
return CString;
}
TCHAR & String::operator [] (int k)
{
if (k < 0 || (int)_tcslen(CString) <= k)
{
//cerr << "index out of range: " << k << " string: " << CString << endl;
assert(0 <= k && k < _tcslen(CString));
}
return CString[k];
}
const String & String::operator += (const String & str)
{
String copystring(str); // copy to avoid aliasing problems
int newLength = length() + str.length(); // self + added string
int lastLocation = length(); // index of '\0'
// check to see if local buffer not big enough
if (newLength >= Capacity)
{
Capacity = newLength + 1;
TCHAR * newBuffer = new TCHAR[Capacity];
_tcscpy(newBuffer,CString); // copy into new buffer
delete [] CString; // delete old string
CString = newBuffer;
}
// now catenate str (copystring) to end of CString
_tcscpy(CString+lastLocation,copystring.getPointer() );
return *this;
}
const String & String::operator += (const TCHAR * s)
{
int newLength = length() + (int)_tcslen(s); // self + added string
int lastLocation = length(); // index of '\0'
// check to see if local buffer not big enough
if (newLength >= Capacity)
{
Capacity = newLength + 1;
TCHAR * newBuffer = new TCHAR[Capacity];
_tcscpy(newBuffer,CString); // copy into new buffer
delete [] CString; // delete old string
CString = newBuffer;
}
// now catenate s to end of CString
_tcscpy(CString+lastLocation,s);
return *this;
}
const String & String::operator += ( TCHAR ch )
{
String temp; // make string equivalent of ch
temp = ch;
*this += temp;
return *this;
}
String operator + (const String & lhs, const String & rhs)
{
String result(lhs); // copies lhs to result
result += rhs; // catenate rhs
return result; // returns a copy of result
}
String operator + ( TCHAR ch, const String & str )
{
String result; // make string equivalent of ch
result = ch;
result += str;
return result;
}
String operator + ( const String & str, TCHAR ch )
{
String result(str);
result += ch;
return result;
}
String String::subString(int pos, int len) const
{
String result(*this); // make sure enough space allocated
if(pos < 0) // start at front when pos < 0
{
pos = 0;
}
if(pos >= (int)_tcslen(CString))
{
result = ""; // empty string
return result;
}
int lastIndex = pos + len - 1; // last char's index (to copy)
if(lastIndex >= (int)_tcslen(CString)) { // off end of string?
lastIndex = (int)_tcslen(CString)-1;
}
int j,k;
for(j=0,k=pos; k <= lastIndex; j++,k++) {
result.CString[j] = CString[k];
}
result.CString[j] = '\0'; // properly terminate C-string
return result;
}
int String::find(const String & str, int startpos) const
{
int len = str.length();
int lastIndex = length() - len;
int k;
for (k=startpos; k <= lastIndex; k++) {
if (_tcsncmp(CString + k,str.getPointer(),len) == 0) return k;
}
return -1;
}
int String::find(TCHAR ch, int startpos) const
{
int k;
for(k=startpos; k < (int)_tcslen(CString); k++) {
if (CString[k] == ch) {
return k;
}
}
return -1;
}
int String::findLast(TCHAR ch, int count)
{
for (int k=length()-1; k; k--)
{
if (CString[k] == ch)
{
if (count==0)
return k;
count--;
}
}
return -1;
}
int String::parseIntoWords(String *words, int maxWords)
{
int numWords=0;
String currentWord;
for (int i=0; i<length()+1; i++)
{
TCHAR c = (i<length()) ? CString[i] : ' ';
if (c==' ')
{
if (currentWord.length()>0)
{
words[numWords] = currentWord;
currentWord="";
numWords++;
if (numWords == maxWords)
return numWords;
}
}
else
{
currentWord += c;
}
}
return numWords;
}
void String::toLower()
{
for (int i=0; i<length(); i++)
CString[i] = (TCHAR)tolower(CString[i]);
}
void String::toUpper()
{
for (int i=0; i<length(); i++)
CString[i] = (TCHAR)toupper(CString[i]);
}
bool operator == ( const String & lhs, const String & rhs )
{
return _tcscmp(lhs.getPointer(), rhs.getPointer()) == 0;
}
bool operator != ( const String & lhs, const String & rhs )
{
return ! (lhs == rhs);
}
bool operator < ( const String & lhs, const String & rhs )
{
return _tcscmp(lhs.getPointer(), rhs.getPointer()) < 0;
}
bool operator <= ( const String & lhs, const String & rhs )
{
return lhs < rhs || lhs == rhs;
}
bool operator > ( const String & lhs, const String & rhs )
{
return rhs < lhs;
}
bool operator >= ( const String & lhs, const String & rhs )
{
return rhs <= lhs;
}
/*
int String::convertToInt() const
{
return(_ttoi(CString));
}*/
void String::reverseString()
{
int n = 0, i = 0;
TCHAR *ny = new TCHAR[length()+1];
for (i=length()-1; i >= 0; i--, n++) {
ny[i] = CString[n];
}
ny[length()]='\0';
_tcscpy(CString, ny);
delete [] ny;
}
String String::getPath()
{
int p=find(String("\\"),0);
int lastp=-1;
while (p!=-1) {
lastp=p;
p=find(String("\\"),p+1);
}
if (lastp!=-1) {
return subString(0,lastp);
} else {
return String("");
}
}
String String::getFName()
{
int p=find(String("\\"),0);
int lastp=-1;
while (p!=-1)
{
lastp=p;
p=find(String("\\"),p+1);
}
if (lastp!=-1)
{
return subString(lastp+1,100);
}
else
{
return String("");
}
}
void String::toUnicode(wchar_t *dest)
{
for (int i=0; i<length(); i++)
dest[i]=(wchar_t)CString[i];
}
int wideLength(wchar_t *s)
{
int len=0;
while (*s++)
len++;
return len;
}
void String::fromUnicode(wchar_t *src)
{
struct Local {
static int clamp(int i) {
return i>255?' ':i;
}
};
int newLength = wideLength(src);
delete [] CString;
Capacity = newLength + 1;
CString = new TCHAR[Capacity];
int i;
for (i=0; i<newLength; i++)
{
CString[i] = Local::clamp(src[i]);
}
CString[i]=0;
}

View File

@ -1,104 +0,0 @@
// DEPRECATED
// This is only here for legacy reasons.
// In new code, please use std::string.
#pragma once
#include <stdlib.h>
#include <string.h>
#include "base/basictypes.h"
#ifdef UNICODE
typedef wchar_t TCHAR;
#else
typedef char TCHAR;
#endif
class String
{
public:
// constructors/destructor
String(); // construct empty string ""
explicit String(const char * s); // construct from string literal
String(const String & str); // copy constructor
~String(); // destructor
// accessors
int find(const String & str, int startpos=0) const; // index of first occurrence of str
int find(TCHAR ch, int startpos=0) const; // index of first occurrence of ch
int findLast(TCHAR ch,int count=0);
String subString(int pos, int len) const; // substring of len chars
int length() const; // number of chars
// starting at pos
TCHAR * getPointer( ) const; // explicit conversion to char *
const TCHAR *c_str() const {
return getPointer();
}
// assignment
const String & operator = ( const String & str ); // assign str
const String & operator = ( const TCHAR * s ); // assign s
const String & operator = ( TCHAR ch ); // assign ch
//int operator == (String &other);
//int operator != (String &other) {return !(*this == other);}
// indexing
TCHAR & operator [] ( int k ); // range-checked indexing
// modifiers
const String & operator += ( const String & str ); // append str
const String & operator += ( const TCHAR * s); // append s
const String & operator += ( TCHAR ch ); // append char
int parseIntoWords(String *words, int maxWords);
int convertToInt() const;
/*
static String fromInt(int i) {
TCHAR temp[15];
_itot_s(i,temp,15,10);
return String(temp);
}*/
void reverseString();
void toUnicode(wchar_t *dest);
void fromUnicode(wchar_t *src);
String getPath();
String getFName();
void toUpper();
void toLower();
private:
int Capacity; // capacity of string
TCHAR * CString; // storage for characters
};
// The following free (non-member) functions operate on strings
//
// I/O functions
/*
ostream & operator << ( ostream & os, const String & str );
istream & operator >> ( istream & is, String & str );
istream & getline( istream & is, String & str );
*/
// comparison operators:
bool operator == ( const String & lhs, const String & rhs );
bool operator != ( const String & lhs, const String & rhs );
bool operator < ( const String & lhs, const String & rhs );
bool operator <= ( const String & lhs, const String & rhs );
bool operator > ( const String & lhs, const String & rhs );
bool operator >= ( const String & lhs, const String & rhs );
// concatenation operator +
String operator + ( const String & lhs, const String & rhs );
String operator + ( TCHAR ch, const String & str );
String operator + ( const String & str, TCHAR ch );

View File

@ -17,7 +17,7 @@ ChunkFile::ChunkFile(const char *filename, bool _read) {
if (fastMode) {
size_t size;
data = (uint8 *)VFSReadFile(filename, &size);
data = (uint8_t *)VFSReadFile(filename, &size);
if (!data) {
ELOG("Chunkfile fail: %s", filename);
didFail = true;
@ -190,6 +190,7 @@ void ChunkFile::writeData(const void *what, int count) {
}
}
/*
void ChunkFile::writeWString(String str) {
wchar_t *text;
int len=str.length();
@ -206,6 +207,7 @@ void ChunkFile::writeWString(String str) {
delete [] text;
#endif
}
*/
void ChunkFile::writeWString(const std::string &str) {
unsigned short *text;
@ -226,24 +228,6 @@ void ChunkFile::writeWString(const std::string &str) {
#endif
}
String ChunkFile::readWString() {
int len=readInt();
wchar_t *text = new wchar_t[len+1];
readData((char *)text,len*sizeof(wchar_t));
text[len]=0;
#ifdef UNICODE
String s(text);
delete [] text;
return s;
#else
String temp;
temp.fromUnicode(text);
delete [] text;
return temp;
#endif
}
static void toUnicode(const std::string &str, uint16 *t) {
for (int i=0; i<(int)str.size(); i++) {
*t++ = str[i];
@ -251,22 +235,25 @@ static void toUnicode(const std::string &str, uint16 *t) {
*t++ = '\0';
}
static std::string fromUnicode(const uint16 *src, int len) {
struct Local {
static int clamp(int i) {
return i>255?' ':i;
}
};
static std::string fromUnicode(const uint16_t *src, int len) {
std::string str;
str.resize(len);
for (int i=0; i<len; i++) {
str[i] = Local::clamp(src[i]);
str[i] = i > 255 ? ' ' : i;
}
return str;
}
std::string ChunkFile::readWString() {
int len=readInt();
uint16_t *text = new uint16_t[len+1];
readData((char *)text, len*sizeof(uint16_t));
text[len] = 0;
std::string temp = fromUnicode(text, len);
delete [] text;
return temp;
}
void ChunkFile::writeString(const std::string &str) {
uint16_t *text;
int len = str.size();

View File

@ -11,7 +11,6 @@
#include <string>
#include "base/basictypes.h"
#include "base/LAMEString.h"
#include "file/easy_file.h"
inline uint32 flipID(uint32 id) {
@ -29,18 +28,19 @@ public:
int readInt();
void readInt(int &i) {i = readInt();}
void readData(void *data, int count);
String readWString();
// String readWString();
std::string readWString();
void writeString(const std::string &str);
std::string readString();
void writeInt(int i);
void writeWString(String str);
//void writeWString(String str);
void writeWString(const std::string &str);
void writeData(const void *data, int count);
int getCurrentChunkSize();
bool failed() const {return didFail;}
bool failed() const { return didFail; }
std::string filename() const { return fn; }
private:
@ -56,8 +56,8 @@ private:
ChunkInfo stack[8];
int numLevels;
uint8 *data;
int pos,eof;
uint8_t *data;
int pos, eof;
bool fastMode;
bool read;
bool didFail;
@ -65,4 +65,3 @@ private:
void seekTo(int _pos);
int getPos() const {return pos;}
};

View File

@ -1,7 +1,6 @@
#include <stdio.h>
#include "base/basictypes.h"
#include "base/LAMEString.h"
#include "file/easy_file.h"
LAMEFile::LAMEFile() : file_(NULL) {

View File

@ -88,6 +88,7 @@
</Lib>
</ItemDefinitionGroup>
<ItemGroup>
<None Include="base\CMakeLists.txt" />
<None Include="file\CMakeLists.txt" />
<None Include="README.md" />
</ItemGroup>
@ -102,7 +103,6 @@
<ClInclude Include="base\colorutil.h" />
<ClInclude Include="base\display.h" />
<ClInclude Include="base\error_context.h" />
<ClInclude Include="base\LAMEString.h" />
<ClInclude Include="base\logging.h" />
<ClInclude Include="base\mutex.h" />
<ClInclude Include="base\NativeApp.h" />
@ -171,7 +171,6 @@
<ClCompile Include="base\colorutil.cpp" />
<ClCompile Include="base\display.cpp" />
<ClCompile Include="base\error_context.cpp" />
<ClCompile Include="base\LAMEString.cpp" />
<ClCompile Include="base\stringutil.cpp" />
<ClCompile Include="base\timeutil.cpp" />
<ClCompile Include="ext\etcpack\etcdec.cpp" />

View File

@ -5,6 +5,9 @@
<Filter>file</Filter>
</None>
<None Include="README.md" />
<None Include="base\CMakeLists.txt">
<Filter>base</Filter>
</None>
</ItemGroup>
<ItemGroup>
<ClInclude Include="gfx\gl_debug_log.h">
@ -70,9 +73,6 @@
<ClInclude Include="base\basictypes.h">
<Filter>base</Filter>
</ClInclude>
<ClInclude Include="base\LAMEString.h">
<Filter>base</Filter>
</ClInclude>
<ClInclude Include="base\scoped_ptr.h">
<Filter>base</Filter>
</ClInclude>
@ -237,9 +237,6 @@
<ClCompile Include="file\zip_read.cpp">
<Filter>file</Filter>
</ClCompile>
<ClCompile Include="base\LAMEString.cpp">
<Filter>base</Filter>
</ClCompile>
<ClCompile Include="gfx\gl_lost_manager.cpp">
<Filter>gfx</Filter>
</ClCompile>