open-source-search-engine/Strings.h

60 lines
1.5 KiB
C
Raw Normal View History

2013-08-03 00:12:24 +04:00
// Matt Wells, copyright Aug 2002
// . a Strings class is a list of NULL-terminated strings all in one string
#ifndef _STRINGS_H_
#define _STRINGS_H_
#include "Mem.h"
class StringArray {
public:
Strings() { m_s = NULL; m_len = 0; };
~Strings () { if (m_s) mfree(m_s,m_len,"Strings"); m_s = NULL; };
2014-11-11 01:45:11 +03:00
void set ( char *s , int32_t len ) { m_s=s; m_len=len; m_end=s+len; };
char *getString ( int32_t i ) ;
2013-08-03 00:12:24 +04:00
// returns false and sets errno on error
bool addString ( char *s ) {
2014-11-11 01:45:11 +03:00
int32_t len = gbstrlen(s);
m_s = (int32_t *) mrealloc (m_s, m_len, m_len+len+1, "Strings");
2013-08-03 00:12:24 +04:00
if ( ! m_s ) return false;
strcpy ( m_s + m_len , s );
m_len += len + 1;
};
char *m_s;
char *m_end;
2014-11-11 01:45:11 +03:00
int32_t m_len;
2013-08-03 00:12:24 +04:00
};
class LongArray {
public:
LongArray() { m_x = NULL; m_n = 0; };
~LongArray() { if (m_x) mfree(m_x,m_n*4,"LongArray"); m_x = NULL; };
// returns false and sets errno on error
2014-11-11 01:45:11 +03:00
bool addLong ( int32_t x ) {
m_x = (int32_t *) mrealloc (m_x, m_n*4, m_n*4+4, "LongArray");
2013-08-03 00:12:24 +04:00
if ( ! m_x ) return false;
m_x [ m_n++ ] = x;
};
2014-11-11 01:45:11 +03:00
int32_t getLong ( int32_t i ) { return m_x[i]; };
int32_t *m_x;
int32_t m_n;
2013-08-03 00:12:24 +04:00
};
class CharArray {
public:
CharArray() { m_x = NULL; m_n = 0; };
~CharArray() { if (m_x) mfree(m_x,m_n,"CharArray"); m_x = NULL; };
// returns false and sets errno on error
2014-11-11 01:45:11 +03:00
bool addChar ( int32_t x ) {
m_x = (int32_t *) mrealloc (m_x, m_n, m_n+1, "CharArray");
2013-08-03 00:12:24 +04:00
if ( ! m_x ) return false;
m_x [ m_n++ ] = x;
};
2014-11-11 01:45:11 +03:00
char getChar ( int32_t i ) { return m_x[i]; };
2013-08-03 00:12:24 +04:00
char *m_x;
2014-11-11 01:45:11 +03:00
int32_t m_n;
2013-08-03 00:12:24 +04:00
};
#endif