/*
"kernel_string_pool.c"
created by: grayspace aka J. Leveille
for: UbixOS Project
date: May 11, 2002
purpose: to provide a mechanism for maintaining a pool of strings
for use by the kernel without unnecessary waste of memory
NOTEs:
- for now only ASCII is supported
- done quickly, pretty hacky
TODO:
- expand to support unicode
- use huffman encoding instead
$Id$
*/
#include "../../grayspace-misc/gsdefines.h"
#include "../../sys/include/misc/kernel_string_pool.h"
// returns pointer to character *after* substring read
static char * ReadSubString( char * p_dst, const BYTEg * p_substr )
{
char * p_retval;
char * p_curdst;
const char * p_cursubstr;
p_retval = p_dst + (*p_substr) + 1;
p_cursubstr = (const char *) (p_substr + 1);
do
{
*p_curdst = *p_cursubstr;
p_cursubstr++;
p_curdst++;
}
while( p_curdst < p_retval );
return p_retval;
}
// returns pointer to character *after* space appended after substring read
static char * ReadSubStringAppendSpace( char * p_dst, const BYTEg * p_substr )
{
char * p_retval;
p_retval = ReadSubString( p_dst, p_substr );
*p_retval = ' ';
p_retval++;
return p_retval;
}
// gets the substring indicated by 'id' from the pool 'p_ksp' into 'p_dst'
// - returns pointer to 'p_dst'
char * KSTR_POOL_GetString( KSTR_POOL * p_ksp, char * p_dst, DWORDg id )
{
DWORDg numsubstrs;
DWORDg offset;
char * p_curdst;
const BYTEg * p_substr;
const BYTEg * p_krnstr;
numsubstrs = (id & 0xFF);
offset = (id >> 8);
p_krnstr = p_ksp->p_krnstrs + offset;
while( numsubstrs )
{
// find substring
offset = (DWORDg) *p_krnstr;
p_krnstr++;
if( offset == 255 )
{
offset += (((DWORDg) *p_krnstr)<<8);
p_krnstr++;
offset += (DWORDg) *p_krnstr;
p_krnstr++;
}
p_substr = p_ksp->p_substrs + p_ksp->p_substr_off[offset];
// append to destination and also append a space
p_curdst = ReadSubStringAppendSpace( p_curdst, p_substr );
// one less sub string
numsubstrs--;
}
// find final substring
offset = (DWORDg) *p_krnstr;
p_krnstr++;
if( offset == 255 )
{
offset += (((DWORDg) *p_krnstr)<<8);
p_krnstr++;
offset += (DWORDg) *p_krnstr;
p_krnstr++;
}
p_substr = p_ksp->p_substrs + p_ksp->p_substr_off[offset];
// append to destination
p_curdst = ReadSubString( p_curdst, p_substr );
return p_dst;
}