initial creation of trunk directory
git-svn-id: svn://anubis/hos/trunk@1 5b3e749e-e535-0410-8002-a9bb6afbdfca
This commit is contained in:
1 parent
0aaf5db5ad
commit
43d1ca0e18
113 files changed
-12671
No files matched your search
@@ -1,109 +0,0 @@
|
||||
// conv.h
|
||||
// Author: Josh Holtrop
|
||||
// Date: 08/02/04
|
||||
// Modified: 12/30/04
|
||||
|
||||
#include "conv.h"
|
||||
#include "hos_defines.h"
|
||||
|
||||
// BCD to string
|
||||
// returns number of characters generated before null terminator
|
||||
int bcdtoa(u32_t bcd, char *buf)
|
||||
{
|
||||
*buf++ = ((bcd & 0xF0) >> 4) + '0';
|
||||
*buf++ = (bcd & 0xF) + '0';
|
||||
*buf = 0;
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
||||
// convert integer to hexadecimal string
|
||||
int itox(u32_t num, char *buf)
|
||||
{
|
||||
int s, i = 0;
|
||||
for (s = 28; s >= 0; s -= 4)
|
||||
{
|
||||
u32_t val = (num >> s) & 0xF;
|
||||
if (i || val || (!s))
|
||||
{
|
||||
val = (val > 9) ? (val + 'A' - 10) : (val + '0');
|
||||
buf[i++] = val;
|
||||
}
|
||||
}
|
||||
buf[i] = 0;
|
||||
return i - 1;
|
||||
}
|
||||
|
||||
|
||||
// convert integer to an octal string
|
||||
int itoo(u32_t num, char *buf)
|
||||
{
|
||||
int s, i = 0;
|
||||
for (s = 30; s >= 0; s -= 3)
|
||||
{
|
||||
u32_t val = (num >> s) & 0x7;
|
||||
if (i || val || (!s))
|
||||
buf[i++] = val + '0';
|
||||
}
|
||||
buf[i] = 0;
|
||||
return i - 1;
|
||||
}
|
||||
|
||||
|
||||
// convert signed integer to decimal string
|
||||
int itoa(int num, char *buf)
|
||||
{
|
||||
// 4,294,967,296
|
||||
int hitNum = 0, i = 0, mod, val;
|
||||
if (num < 0)
|
||||
{
|
||||
buf[i++] = '-';
|
||||
num = -num;
|
||||
}
|
||||
for (mod = 1000000000; mod >= 1; mod /= 10)
|
||||
{
|
||||
val = num / mod;
|
||||
num %= mod;
|
||||
if (val || (mod == 1))
|
||||
hitNum = 1;
|
||||
if (hitNum)
|
||||
{
|
||||
buf[i++] = val + '0';
|
||||
}
|
||||
}
|
||||
buf[i] = 0;
|
||||
return i - 1;
|
||||
}
|
||||
|
||||
|
||||
// convert an unsigned integer to decimal string
|
||||
int utoa(u32_t num, char *buf)
|
||||
{
|
||||
// 4,294,967,296
|
||||
int hitNum = 0, i = 0, mod, val;
|
||||
for (mod = 1000000000; mod >= 1; mod /= 10)
|
||||
{
|
||||
val = num / mod;
|
||||
num %= mod;
|
||||
if (val || (mod == 1))
|
||||
hitNum = 1;
|
||||
if (hitNum)
|
||||
{
|
||||
buf[i++] = val + '0';
|
||||
}
|
||||
}
|
||||
buf[i] = 0;
|
||||
return i - 1;
|
||||
}
|
||||
|
||||
|
||||
char asciiSwitchCase(char chr)
|
||||
{
|
||||
if (chr >= 'A' && chr <= 'Z')
|
||||
return chr + ('a' - 'A');
|
||||
if (chr >= 'a' && chr <= 'z')
|
||||
return chr - ('a' - 'A');
|
||||
return chr;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
// conv.h
|
||||
// Author: Josh Holtrop
|
||||
// Date: 08/02/04
|
||||
// Modified: 12/30/04
|
||||
|
||||
#ifndef __HOS_CONV__
|
||||
#define __HOS_CONV__ __HOS_CONV__
|
||||
|
||||
#include "hos_defines.h"
|
||||
|
||||
int bcdtoa(u32_t bcd, char *buf);
|
||||
int itox(u32_t num, char *buf);
|
||||
int itoo(u32_t num, char *buf);
|
||||
int itoa(int num, char *buf);
|
||||
int utoa(u32_t num, char *buf);
|
||||
|
||||
char asciiSwitchCase(char chr);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,362 +0,0 @@
|
||||
; lang.asm
|
||||
; Josh Holtrop
|
||||
; Created: 10/23/03
|
||||
; Modified: 12/30/04
|
||||
|
||||
[bits 32]
|
||||
|
||||
%macro jzfar 1
|
||||
jnz %%skip
|
||||
jmp %1
|
||||
%%skip:
|
||||
|
||||
%endmacro
|
||||
|
||||
;implements a lock
|
||||
[global _lock]
|
||||
_lock:
|
||||
push ebp
|
||||
mov ebp, esp
|
||||
push eax
|
||||
push ebx
|
||||
push esi
|
||||
|
||||
mov esi, [ebp + 8]
|
||||
_lock_loop0:
|
||||
xor eax, eax
|
||||
mov ebx, 1
|
||||
cmpxchg [esi], ebx
|
||||
jnz _lock_loop0 ; failed to acquire lock
|
||||
|
||||
pop esi
|
||||
pop ebx
|
||||
pop eax
|
||||
pop ebp
|
||||
ret
|
||||
|
||||
;releases a lock
|
||||
[global _unlock]
|
||||
_unlock:
|
||||
push ebp
|
||||
mov ebp, esp
|
||||
push esi
|
||||
|
||||
mov esi, [ebp + 8]
|
||||
mov [esi], dword 0
|
||||
|
||||
pop esi
|
||||
pop ebp
|
||||
ret
|
||||
|
||||
;returns the value in the CR0 register
|
||||
;extern dword read_cr0();
|
||||
[global _read_cr0]
|
||||
_read_cr0:
|
||||
mov eax, cr0;
|
||||
ret
|
||||
|
||||
;stores the parameter to the CR0 register
|
||||
;extern dword write_cr0(dword cr0);
|
||||
[global _write_cr0]
|
||||
_write_cr0:
|
||||
push ebp
|
||||
mov ebp, esp
|
||||
mov eax, [ebp+8]
|
||||
mov cr0, eax
|
||||
pop ebp
|
||||
ret
|
||||
|
||||
;returns the value in the CR2 register
|
||||
;extern dword read_cr2();
|
||||
[global _read_cr2]
|
||||
_read_cr2:
|
||||
mov eax, cr2;
|
||||
ret
|
||||
|
||||
|
||||
|
||||
;returns the value in the CR3 register
|
||||
;extern dword read_cr3();
|
||||
[global _read_cr3]
|
||||
_read_cr3:
|
||||
mov eax, cr3;
|
||||
ret
|
||||
|
||||
|
||||
;stores the parameter to the CR3 register
|
||||
;extern dword write_cr3(dword cr3);
|
||||
[global _write_cr3]
|
||||
_write_cr3:
|
||||
push ebp
|
||||
mov ebp, esp
|
||||
mov eax, [ebp+8]
|
||||
mov cr3, eax
|
||||
pop ebp
|
||||
ret
|
||||
|
||||
|
||||
; read ss register
|
||||
[global _read_ss]
|
||||
_read_ss:
|
||||
mov ax, ss
|
||||
ret
|
||||
|
||||
|
||||
;copies a string from the source to the destination parameter
|
||||
;extern void strcpy(char *dest, char *src);
|
||||
[global _strcpy]
|
||||
_strcpy:
|
||||
push ebp
|
||||
mov ebp, esp
|
||||
push esi
|
||||
push edi
|
||||
mov edi, [ebp+8]
|
||||
mov esi, [ebp+12]
|
||||
strcpyloop:
|
||||
lodsb
|
||||
stosb
|
||||
or al, al
|
||||
jnz strcpyloop
|
||||
pop edi
|
||||
pop esi
|
||||
pop ebp
|
||||
ret
|
||||
|
||||
;copies memory of n bytes from src to destination
|
||||
;void memcpy(void *dest, void *src, dword n);
|
||||
[global _memcpy]
|
||||
_memcpy:
|
||||
push ebp
|
||||
mov ebp, esp
|
||||
push esi
|
||||
push edi
|
||||
push ecx
|
||||
mov edi, [ebp+8]
|
||||
mov esi, [ebp+12]
|
||||
mov ecx, [ebp+16]
|
||||
|
||||
cld
|
||||
rep movsb
|
||||
|
||||
pop ecx
|
||||
pop edi
|
||||
pop esi
|
||||
pop ebp
|
||||
ret
|
||||
|
||||
|
||||
;copies memory of n words (n*2 bytes) from src to destination
|
||||
;void memcpyw(void *dest, void *src, dword n);
|
||||
[global _memcpyw]
|
||||
_memcpyw:
|
||||
push ebp
|
||||
mov ebp, esp
|
||||
push esi
|
||||
push edi
|
||||
push ecx
|
||||
mov edi, [ebp+8]
|
||||
mov esi, [ebp+12]
|
||||
mov ecx, [ebp+16]
|
||||
|
||||
cld
|
||||
rep movsw
|
||||
|
||||
pop ecx
|
||||
pop edi
|
||||
pop esi
|
||||
pop ebp
|
||||
ret
|
||||
|
||||
|
||||
;copies memory of n dwords (n*4 bytes) from src to destination
|
||||
;void memcpyd(void *dest, void *src, dword n);
|
||||
[global _memcpyd]
|
||||
_memcpyd:
|
||||
push ebp
|
||||
mov ebp, esp
|
||||
push esi
|
||||
push edi
|
||||
push ecx
|
||||
mov edi, [ebp+8]
|
||||
mov esi, [ebp+12]
|
||||
mov ecx, [ebp+16]
|
||||
|
||||
cld
|
||||
rep movsd
|
||||
|
||||
pop ecx
|
||||
pop edi
|
||||
pop esi
|
||||
pop ebp
|
||||
ret
|
||||
|
||||
|
||||
;sets num bytes at buffer to the value of c
|
||||
;void *memset(void *buffer, int c, int num);
|
||||
[global _memset]
|
||||
_memset:
|
||||
push ebp
|
||||
mov ebp, esp
|
||||
push edi
|
||||
push ecx
|
||||
mov edi, [ebp+8]
|
||||
push edi ;save for return address
|
||||
mov eax, [ebp+12]
|
||||
mov ecx, [ebp+16]
|
||||
|
||||
rep stosb
|
||||
|
||||
pop eax
|
||||
pop ecx
|
||||
pop edi
|
||||
pop ebp
|
||||
ret
|
||||
|
||||
|
||||
;sets num words at buffer to the value of c
|
||||
;void *memsetw(void *buffer, int c, int num);
|
||||
[global _memsetw]
|
||||
_memsetw:
|
||||
push ebp
|
||||
mov ebp, esp
|
||||
push edi
|
||||
push ecx
|
||||
mov edi, [ebp+8]
|
||||
push edi ;save for return address
|
||||
mov eax, [ebp+12]
|
||||
mov ecx, [ebp+16]
|
||||
|
||||
rep stosw
|
||||
|
||||
pop eax
|
||||
pop ecx
|
||||
pop edi
|
||||
pop ebp
|
||||
ret
|
||||
|
||||
|
||||
;sets num dwords at buffer to the value of c
|
||||
;void *memsetd(void *buffer, int c, int num);
|
||||
[global _memsetd]
|
||||
_memsetd:
|
||||
push ebp
|
||||
mov ebp, esp
|
||||
push edi
|
||||
push ecx
|
||||
mov edi, [ebp+8]
|
||||
push edi ;save for return address
|
||||
mov eax, [ebp+12]
|
||||
mov ecx, [ebp+16]
|
||||
|
||||
rep stosd
|
||||
|
||||
pop eax
|
||||
pop ecx
|
||||
pop edi
|
||||
pop ebp
|
||||
ret
|
||||
|
||||
|
||||
;returns the number of characters in a string
|
||||
;extern dword strlen(char *str);
|
||||
[global _strlen]
|
||||
_strlen:
|
||||
push ebp
|
||||
mov ebp, esp
|
||||
push esi
|
||||
push ebx
|
||||
mov esi, [ebp+8]
|
||||
xor ebx, ebx
|
||||
strlenloop:
|
||||
lodsb
|
||||
or al, al
|
||||
jz strlendone
|
||||
inc ebx
|
||||
jmp strlenloop
|
||||
strlendone:
|
||||
mov eax, ebx
|
||||
pop ebx
|
||||
pop esi
|
||||
pop ebp
|
||||
ret
|
||||
|
||||
|
||||
;this function invalidates the page directory/table entry that
|
||||
; would be used to access the memory address given in the parameter
|
||||
;extern void invlpg_(dword addr);
|
||||
[global _invlpg_]
|
||||
_invlpg_:
|
||||
mov eax, [esp+4]
|
||||
invlpg [eax]
|
||||
ret
|
||||
|
||||
|
||||
;
|
||||
;void writeCursorPosition(word pos)
|
||||
;
|
||||
[global _writeCursorPosition]
|
||||
_writeCursorPosition:
|
||||
push ebp
|
||||
mov ebp, esp
|
||||
|
||||
push eax
|
||||
push ebx
|
||||
push edx
|
||||
|
||||
mov eax, [ebp+8] ;cursor position in ax
|
||||
|
||||
mov bl, al
|
||||
mov dx, 0x03D4
|
||||
mov al, 0x0E
|
||||
out dx, al
|
||||
|
||||
inc dx
|
||||
mov al, ah
|
||||
out dx, al
|
||||
|
||||
dec dx
|
||||
mov al, 0x0F
|
||||
out dx, al
|
||||
|
||||
inc dx
|
||||
mov al, bl
|
||||
out dx, al
|
||||
|
||||
pop edx
|
||||
pop ebx
|
||||
pop eax
|
||||
pop ebp
|
||||
|
||||
ret
|
||||
|
||||
|
||||
;
|
||||
;word getCursorPosition()
|
||||
;
|
||||
[global _getCursorPosition]
|
||||
_getCursorPosition:
|
||||
push ebx
|
||||
push edx
|
||||
|
||||
xor eax, eax
|
||||
mov dx, 0x03D4
|
||||
mov al, 0x0E
|
||||
out dx, al
|
||||
|
||||
inc dx
|
||||
in al, dx
|
||||
mov bl, al
|
||||
|
||||
dec dx
|
||||
mov al, 0x0F
|
||||
out dx, al
|
||||
|
||||
inc dx
|
||||
in al, dx
|
||||
mov ah, bl
|
||||
|
||||
pop edx
|
||||
pop ebx
|
||||
|
||||
ret
|
||||
|
||||
@@ -1,114 +0,0 @@
|
||||
// lang.c
|
||||
// Author: Josh Holtrop
|
||||
// Date: 12/30/04
|
||||
// Modified: 12/30/04
|
||||
|
||||
#include "lang.h"
|
||||
|
||||
|
||||
/* strcmp compares two strings
|
||||
* Returns:
|
||||
* 0 if the strings are equal
|
||||
* <0 if the second string is less than the first
|
||||
* >0 if the second string is greater than the first
|
||||
*/
|
||||
int strcmp(char *str1, char *str2)
|
||||
{
|
||||
while (*str1 || *str2)
|
||||
{
|
||||
if (*str1 != *str2)
|
||||
return *str2 - *str1;
|
||||
str1++;
|
||||
str2++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* strncmp compares up to n characters of two strings
|
||||
* Returns:
|
||||
* 0 if the strings are equal
|
||||
* <0 if the second string is less than the first
|
||||
* >0 if the second string is greater than the first
|
||||
*/
|
||||
int strncmp(char *str1, char *str2, int n)
|
||||
{
|
||||
while (n > 0 && (*str1 || *str2))
|
||||
{
|
||||
if (*str1 != *str2)
|
||||
return *str2 - *str1;
|
||||
str1++;
|
||||
str2++;
|
||||
n--;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* str_change changes a certain character in a string
|
||||
* to a different character
|
||||
* Returns: how many characters were changed
|
||||
*/
|
||||
int str_change(char *str, char ch1, char ch2)
|
||||
{
|
||||
int count = 0;
|
||||
while (*str)
|
||||
{
|
||||
if (*str == ch1)
|
||||
{
|
||||
*str = ch2;
|
||||
count++;
|
||||
}
|
||||
str++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// counts the occurrences of lookfor in str
|
||||
int str_count(char *str, char lookfor)
|
||||
{
|
||||
int count = 0;
|
||||
while (*str)
|
||||
{
|
||||
if (*str == lookfor)
|
||||
count++;
|
||||
str++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// split the string into substrings by the splitchar, return number of substrings
|
||||
int str_split(char *str, char splitchar)
|
||||
{
|
||||
if (!(*str))
|
||||
return 0;
|
||||
int subs = 1;
|
||||
while (*str)
|
||||
{
|
||||
if (*str == splitchar)
|
||||
{
|
||||
*str = 0;
|
||||
subs++;
|
||||
}
|
||||
str++;
|
||||
}
|
||||
return subs;
|
||||
}
|
||||
|
||||
// advance the string pointer to the next substring, return a pointer to the next substring
|
||||
char *str_advance(char *str)
|
||||
{
|
||||
char *next = str;
|
||||
while (*next)
|
||||
next++; // advance pointer to end of this substring (null character)
|
||||
return next + 1;
|
||||
}
|
||||
|
||||
|
||||
// concatentate src onto the end of dest
|
||||
void strcat(char *dest, char *src)
|
||||
{
|
||||
while (*dest)
|
||||
dest++;
|
||||
strcpy(dest, src);
|
||||
}
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
// lang.h
|
||||
// Author: Josh Holtrop
|
||||
// Created: 02/26/04
|
||||
// Modified: 12/30/04
|
||||
|
||||
#ifndef __HOS_LANG__
|
||||
#define __HOS_LANG__ __HOS_LANG__
|
||||
|
||||
#include "hos_defines.h"
|
||||
|
||||
/* lang.asm */
|
||||
void lock(lock_t *addr);
|
||||
void unlock(lock_t *addr);
|
||||
u32_t read_cr0();
|
||||
u32_t write_cr0(u32_t cr0);
|
||||
u32_t read_cr2();
|
||||
u32_t read_cr3();
|
||||
u32_t write_cr3(u32_t cr3);
|
||||
u32_t read_ss();
|
||||
void writeCursorPosition(u32_t pos);
|
||||
u32_t getCursorPosition();
|
||||
void strcpy(char *dest, const char *src);
|
||||
void memcpy(void *dest, const void *src, u32_t n);
|
||||
void memcpyw(void *dest, const void *src, u32_t n);
|
||||
void memcpyd(void *dest, const void *src, u32_t n);
|
||||
void *memset(void *buffer, int c, int num);
|
||||
void *memsetw(void *buffer, int c, int num);
|
||||
void *memsetd(void *buffer, int c, int num);
|
||||
u32_t strlen(const char *str);
|
||||
void invlpg_(u32_t addr);
|
||||
|
||||
/* lang.c */
|
||||
int strcmp(char *str1, char *str2);
|
||||
int strncmp(char *str1, char *str2, int n);
|
||||
int str_change(char *str, char ch1, char ch2);
|
||||
int str_count(char *str, char lookfor);
|
||||
int str_split(char *str, char splitchar);
|
||||
char *str_advance(char *str);
|
||||
void strcat(char *dest, char *src);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
// 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); }
|
||||
@@ -1,258 +0,0 @@
|
||||
|
||||
// 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); }
|
||||
|
||||
|
||||
|
||||
@@ -1,110 +0,0 @@
|
||||
|
||||
// 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; }
|
||||
|
||||
static inline string operator+(const char *cstring, const string & str)
|
||||
{ return string(cstring, str); }
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,181 +0,0 @@
|
||||
|
||||
// vector.h
|
||||
// implements c++ vector object for HOS
|
||||
// Author: Josh Holtrop
|
||||
// Date: 05/30/05
|
||||
// Modified: 05/30/05
|
||||
|
||||
|
||||
#ifndef __HOS_VECTOR__
|
||||
#define __HOS_VECTOR__ __HOS_VECTOR__
|
||||
|
||||
#include "hos_defines.h"
|
||||
|
||||
template<typename type>
|
||||
class vector
|
||||
{
|
||||
protected:
|
||||
/* Pointers to vector elements */
|
||||
type **myData;
|
||||
|
||||
/* How many elements are present */
|
||||
unsigned int mySize;
|
||||
|
||||
/* How many elements there are pointers for */
|
||||
unsigned int myAllocated;
|
||||
|
||||
/* Causes the vector to double in its allocated size */
|
||||
void grow();
|
||||
|
||||
public:
|
||||
/* Constructors/Destructor */
|
||||
vector<type>();
|
||||
vector<type>(unsigned int size);
|
||||
vector<type>(const vector<type> & v1);
|
||||
~vector<type>();
|
||||
|
||||
/* Assignment operator */
|
||||
vector<type> & operator=(const vector<type> & v1);
|
||||
|
||||
/* Returns the size of the vector */
|
||||
unsigned int size() const;
|
||||
|
||||
/* Add an element to the end of the vector */
|
||||
vector<type> & add(type elem);
|
||||
|
||||
/* Remove an element from the vector */
|
||||
vector<type> & remove(unsigned int index);
|
||||
|
||||
/* Insert an element into a position in the vector */
|
||||
vector<type> & insert(type elem, unsigned int position);
|
||||
|
||||
/* Direct access operators */
|
||||
const type & operator[](unsigned int index) const;
|
||||
type & operator[](unsigned int index);
|
||||
|
||||
/* Returns pointer to data */
|
||||
type **data();
|
||||
};
|
||||
|
||||
template<typename type>
|
||||
vector<type>::vector<type>()
|
||||
{
|
||||
myData = NULL;
|
||||
mySize = 0;
|
||||
myAllocated = 0;
|
||||
}
|
||||
|
||||
template<typename type>
|
||||
vector<type>::vector<type>(unsigned int size)
|
||||
{
|
||||
myData = new (type *)[size];
|
||||
mySize = 0;
|
||||
myAllocated = size;
|
||||
}
|
||||
|
||||
template<typename type>
|
||||
vector<type>::vector<type>(const vector<type> & v1)
|
||||
{
|
||||
mySize = v1.mySize;
|
||||
myAllocated = v1.myAllocated;
|
||||
myData = new (type *)[myAllocated];
|
||||
for (u32_t i = 0; i < mySize; i++)
|
||||
myData[i] = new type(*v1.myData[i]);
|
||||
}
|
||||
|
||||
template<typename type>
|
||||
vector<type>::~vector<type>()
|
||||
{
|
||||
for (unsigned int i = 0; i < mySize; i++)
|
||||
delete myData[i];
|
||||
delete[] myData;
|
||||
}
|
||||
|
||||
template<typename type>
|
||||
vector<type> & vector<type>::operator=(const vector<type> & v1)
|
||||
{
|
||||
mySize = v1.mySize;
|
||||
myAllocated = v1.myAllocated;
|
||||
myData = new (type *)[myAllocated];
|
||||
for (u32_t i = 0; i < mySize; i++)
|
||||
myData[i] = new type(*v1.myData[i]);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename type>
|
||||
u32_t vector<type>::size() const
|
||||
{
|
||||
return mySize;
|
||||
}
|
||||
|
||||
template<typename type>
|
||||
const type & vector<type>::operator[](unsigned int index) const
|
||||
{
|
||||
return *myData[index];
|
||||
}
|
||||
|
||||
template<typename type>
|
||||
type & vector<type>::operator[](unsigned int index)
|
||||
{
|
||||
return *myData[index];
|
||||
}
|
||||
|
||||
template<typename type>
|
||||
vector<type> & vector<type>::add(type elem)
|
||||
{
|
||||
while (mySize >= myAllocated)
|
||||
grow();
|
||||
myData[mySize++] = new type(elem);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename type>
|
||||
void vector<type>::grow()
|
||||
{
|
||||
myAllocated <<= 1;
|
||||
if (myAllocated == 0)
|
||||
myAllocated = 1;
|
||||
type **data_new = new (type *)[myAllocated];
|
||||
for (unsigned int i = 0; i < mySize; i++)
|
||||
data_new[i] = myData[i];
|
||||
if (myData)
|
||||
delete[] myData;
|
||||
myData = data_new;
|
||||
}
|
||||
|
||||
template<typename type>
|
||||
vector<type> & vector<type>::remove(unsigned int index)
|
||||
{
|
||||
if (index < mySize)
|
||||
{
|
||||
delete myData[index];
|
||||
for (unsigned int i = index; i < (mySize - 1); i++)
|
||||
myData[i] = myData[i+1];
|
||||
mySize--;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename type>
|
||||
vector<type> & vector<type>::insert(type elem, unsigned int position)
|
||||
{
|
||||
if (position <= mySize)
|
||||
{
|
||||
if (mySize >= myAllocated)
|
||||
grow();
|
||||
for (unsigned int i = mySize; i > position; i--)
|
||||
myData[i] = myData[i-1];
|
||||
myData[position] = new type(elem);
|
||||
mySize++;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template<typename type>
|
||||
type **vector<type>::data()
|
||||
{
|
||||
return myData;
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user