Import backup from 2005-05-12

This commit is contained in:
josh committed 2017-07-25 22:23:50 -04:00
1 parent 14792ef954
commit b0132c866e
35 files changed
+1637 -872

No files matched your search

+25
View File
@@ -0,0 +1,25 @@
// Author: Josh Holtrop
// Date: 05/10/05
// Adds C++ support for new, delete functions
extern "C"
{
#include "hos_defines.h"
#include "mm/vmm.h"
}
//overload the operator "new"
void * operator new (u32_t size)
{ return kmalloc(size); }
//overload the operator "new[]"
void * operator new[] (u32_t size)
{ return kmalloc(size); }
//overload the operator "delete"
void operator delete (void *p)
{ kfree(p); }
//overload the operator "delete[]"
void operator delete[] (void *p)
{ kfree(p); }
+258
View File
@@ -0,0 +1,258 @@
// string.cpp
// implements c++ string object for HOS
// Author: Josh Holtrop
// Date: 06/01/04
// Modified: 05/10/05
extern "C"
{
#include "string.h" //string class declaration
#include "lang/lang.h" //memcpy(void *dest, void *src, int n), strlen(char *str)
}
string::string()
{
myLength = 0;
myChars = new char; //myChars must be a valid pointer at all times
*myChars = 0;
}
string::~string()
{
delete[] myChars;
}
string::string(const string & orig)
{
myLength = orig.myLength;
myChars = new char[myLength + 1];
memcpy(myChars, orig.myChars, myLength + 1);
}
string::string(const char *cstring)
{
myLength = strlen(cstring);
myChars = new char[myLength + 1];
memcpy(myChars, cstring, myLength + 1);
}
string & string::operator=(const string & orig)
{
if (this != &orig)
{
delete[] myChars;
myLength = orig.myLength;
myChars = new char[myLength + 1];
memcpy(myChars, orig.myChars, myLength + 1);
}
return *this;
}
string & string::operator=(const char *cstring)
{
if (myChars != cstring)
{
delete[] myChars;
myLength = strlen(cstring);
myChars = new char[myLength + 1];
memcpy(myChars, cstring, myLength + 1);
}
return *this;
}
char * string::data() const { return myChars; }
int string::size() const { return myLength; }
string & string::operator+=(const string & str)
{
char *newStr = new char[myLength + str.myLength + 1];
memcpy(newStr, myChars, myLength);
memcpy(newStr + myLength, str.myChars, str.myLength + 1);
delete[] myChars;
myChars = newStr;
myLength += str.myLength;
return *this;
}
string & string::operator+=(const char *cstring)
{
char *newStr = new char[myLength + strlen(cstring) + 1];
memcpy(newStr, myChars, myLength);
memcpy(newStr + myLength, cstring, strlen(cstring) + 1);
delete[] myChars;
myChars = newStr;
myLength += strlen(cstring);
return *this;
}
string & string::operator+=(char chr)
{
myLength++;
char *newStr = new char[myLength + 1];
memcpy(newStr, myChars, myLength);
newStr[myLength - 1] = chr;
newStr[myLength] = 0;
delete[] myChars;
myChars = newStr;
return *this;
}
string string::operator+(const string & str)
{ return string(*this, str); }
string string::operator+(const char *cstring)
{ return string(*this, cstring); }
string string::operator+(char chr)
{ return string(*this, chr); }
string::string(const string & str1, const string & str2)
{
myLength = str1.myLength + str2.myLength;
myChars = new char[myLength + 1];
memcpy(myChars, str1.myChars, str1.myLength);
memcpy(myChars + str1.myLength, str2.myChars, str2.myLength + 1);
}
string::string(const string & str1, const char *cstring)
{
myLength = str1.myLength + strlen(cstring);
myChars = new char[myLength + 1];
memcpy(myChars, str1.myChars, str1.myLength);
memcpy(myChars + str1.myLength, cstring, strlen(cstring) + 1);
}
string::string(const char *cstring, const string & str)
{
myLength = str.myLength + strlen(cstring);
myChars = new char[myLength + 1];
memcpy(myChars, cstring, strlen(cstring));
memcpy(myChars + strlen(cstring), str.myChars, str.myLength + 1);
}
string::string(const string & str1, char chr)
{
myLength = str1.myLength + 1;
myChars = new char[myLength + 1];
memcpy(myChars, str1.myChars, myLength);
myChars[myLength - 1] = chr;
myChars[myLength] = 0;
}
string::string(char chr, const string & str1)
{
myLength = str1.myLength + 1;
myChars = new char[myLength + 1];
memcpy(myChars + 1, str1.myChars, myLength + 1);
*myChars = chr;
}
const char & string::operator[](unsigned int index) const
{
if (index < myLength)
return myChars[index];
return *myChars; //if index is invalid, return a pointer to the trailing 0
}
char & string::operator[](unsigned int index)
{
if (index < myLength)
return myChars[index];
return *myChars; //if index is invalid, return a pointer to the trailing 0
}
bool string::operator==(const string & second) const
{
if (myLength != second.myLength)
return false;
for (unsigned int i = 0; i < myLength; i++)
{
if (myChars[i] != second.myChars[i])
return false;
}
return true;
}
bool string::operator==(const char *cstring) const
{
if (myLength != strlen(cstring))
return false;
for (unsigned int i = 0; i < myLength; i++)
{
if (myChars[i] != cstring[i])
return false;
}
return true;
}
bool string::operator!=(const string & second) const
{ return !operator==(second); }
bool string::operator!=(const char *cstring) const
{ return !operator==(cstring); }
bool string::operator<(const string & second) const
{
char *c1 = myChars, *c2 = second.myChars;
while (*c1 == *c2 && *c1)
{
c1++;
c2++;
}
return *c1 < *c2;
}
bool string::operator>(const string & second) const
{
char *c1 = myChars, *c2 = second.myChars;
while (*c1 == *c2 && *c1)
{
c1++;
c2++;
}
return *c1 > *c2;
}
bool string::operator<(const char *cstring) const
{
char *c1 = myChars;
while (*c1 == *cstring && *c1)
{
c1++;
cstring++;
}
return *c1 < *cstring;
}
bool string::operator>(const char *cstring) const
{
char *c1 = myChars;
while (*c1 == *cstring && *c1)
{
c1++;
cstring++;
}
return *c1 > *cstring;
}
bool string::operator<=(const string & second) const
{ return !operator>(second); }
bool string::operator>=(const string & second) const
{ return !operator<(second); }
bool string::operator<=(const char *cstring) const
{ return operator<(cstring) || operator==(cstring); }
bool string::operator>=(const char *cstring) const
{ return operator>(cstring) || operator==(cstring); }
+107
View File
@@ -0,0 +1,107 @@
// string.h
// implements c++ string object for HOS
// Author: Josh Holtrop
// Date: 06/01/04
// Modified: 05/10/05
#ifndef __HOS_STRING__
#define __HOS_STRING__ __HOS_STRING__
class string
{
private:
/* myChars is a pointer to a character array
* It will always be a valid pointer, not null.
* The constructor is responsible for initializing
* it to point to a 1-char array with myChars[0] == 0 */
char * myChars;
/* myLength holds how many characters are in the
* string, not including the trailing null value
* (ASCII 0 character) */
unsigned int myLength;
public:
/* Basic constructors */
string();
~string();
/* Copy constructor */
string(const string & orig);
/* Construct strings from c-style strings:
* Allows declarations like string s = "data"; */
string(const char *cstring);
/* Assignment operators */
string & operator=(const string & orig);
string & operator=(const char *cstring);
/* Boolean comparison operators */
bool operator==(const string & second) const;
bool operator==(const char *cstring) const;
bool operator!=(const string & second) const;
bool operator!=(const char *cstring) const;
bool operator<(const string & second) const;
bool operator>(const string & second) const;
bool operator<(const char *cstring) const;
bool operator>(const char *cstring) const;
bool operator<=(const string & second) const;
bool operator>=(const string & second) const;
bool operator<=(const char *cstring) const;
bool operator>=(const char *cstring) const;
/* Construct strings made up of combinations of other
* strings, c-style strings, and characters */
string(const string & str1, const string & str2);
string(const string & str1, const char *cstring);
string(const char *cstring, const string & str);
string(const string & str1, char chr);
string(char chr, const string & str1);
/* Append operators */
string & operator+=(const string & str);
string & operator+=(const char *cstring);
string & operator+=(char chr);
string operator+(const string & str);
string operator+(const char *cstring);
string operator+(char chr);
/* Direct character-access operators */
const char & operator[](unsigned int index) const;
char & operator[](unsigned int index);
/* Returns handle to actual character-array for programs
* that need raw data instead of a string object */
char * data() const;
/* Returns the size of the string. This is myLength
* and is the number of characters in the string
* not counting the trailing null (ASCII 0) character */
int size() const;
};
static inline bool operator==(char *cstring, const string & str)
{ return str == cstring; }
static inline bool operator!=(char *cstring, const string & str)
{ return str != cstring; }
static inline bool operator<(const char *cstring, const string & str)
{ return str > cstring; }
static inline bool operator>(const char *cstring, const string & str)
{ return str < cstring; }
static inline bool operator<=(const char *cstring, const string & str)
{ return str >= cstring; }
static inline bool operator>=(const char *cstring, const string & str)
{ return str <= cstring; }
#endif