-rw-r--r-- | noncore/unsupported/qpdf/goo/GHash.cc | 240 | ||||
-rw-r--r-- | noncore/unsupported/qpdf/goo/GHash.h | 67 | ||||
-rw-r--r-- | noncore/unsupported/qpdf/goo/GList.cc | 91 | ||||
-rw-r--r-- | noncore/unsupported/qpdf/goo/GList.h | 89 | ||||
-rw-r--r-- | noncore/unsupported/qpdf/goo/GString.cc | 231 | ||||
-rw-r--r-- | noncore/unsupported/qpdf/goo/GString.h | 98 | ||||
-rw-r--r-- | noncore/unsupported/qpdf/goo/gfile.h | 135 | ||||
-rw-r--r-- | noncore/unsupported/qpdf/goo/gmem.h | 53 | ||||
-rw-r--r-- | noncore/unsupported/qpdf/goo/gtypes.h | 29 |
9 files changed, 1033 insertions, 0 deletions
diff --git a/noncore/unsupported/qpdf/goo/GHash.cc b/noncore/unsupported/qpdf/goo/GHash.cc new file mode 100644 index 0000000..9ef6bb1 --- a/dev/null +++ b/noncore/unsupported/qpdf/goo/GHash.cc @@ -0,0 +1,240 @@ +//======================================================================== +// +// GHash.cc +// +// Copyright 2001 Derek B. Noonburg +// +//======================================================================== + +#ifdef __GNUC__ +#pragma implementation +#endif + +#include <aconf.h> +#include "gmem.h" +#include "GString.h" +#include "GHash.h" + +//------------------------------------------------------------------------ + +struct GHashBucket { + GString *key; + void *val; + GHashBucket *next; +}; + +struct GHashIter { + int h; + GHashBucket *p; +}; + +//------------------------------------------------------------------------ + +GHash::GHash(GBool deleteKeysA) { + int h; + + deleteKeys = deleteKeysA; + size = 7; + tab = (GHashBucket **)gmalloc(size * sizeof(GHashBucket *)); + for (h = 0; h < size; ++h) { + tab[h] = NULL; + } + len = 0; +} + +GHash::~GHash() { + GHashBucket *p; + int h; + + for (h = 0; h < size; ++h) { + while (tab[h]) { + p = tab[h]; + tab[h] = p->next; + if (deleteKeys) { + delete p->key; + } + delete p; + } + } + gfree(tab); +} + +void GHash::add(GString *key, void *val) { + GHashBucket **oldTab; + GHashBucket *p; + int oldSize, i, h; + + // expand the table if necessary + if (len >= size) { + oldSize = size; + oldTab = tab; + size = 2*size + 1; + tab = (GHashBucket **)gmalloc(size * sizeof(GHashBucket *)); + for (h = 0; h < size; ++h) { + tab[h] = NULL; + } + for (i = 0; i < oldSize; ++i) { + while (oldTab[i]) { + p = oldTab[i]; + oldTab[i] = oldTab[i]->next; + h = hash(p->key); + p->next = tab[h]; + tab[h] = p; + } + } + gfree(oldTab); + } + + // add the new symbol + p = new GHashBucket; + p->key = key; + p->val = val; + h = hash(key); + p->next = tab[h]; + tab[h] = p; + ++len; +} + +void *GHash::lookup(GString *key) { + GHashBucket *p; + int h; + + if (!(p = find(key, &h))) { + return NULL; + } + return p->val; +} + +void *GHash::lookup(char *key) { + GHashBucket *p; + int h; + + if (!(p = find(key, &h))) { + return NULL; + } + return p->val; +} + +void *GHash::remove(GString *key) { + GHashBucket *p; + GHashBucket **q; + void *val; + int h; + + if (!(p = find(key, &h))) { + return NULL; + } + q = &tab[h]; + while (*q != p) { + q = &((*q)->next); + } + *q = p->next; + if (deleteKeys) { + delete p->key; + } + val = p->val; + delete p; + --len; + return val; +} + +void *GHash::remove(char *key) { + GHashBucket *p; + GHashBucket **q; + void *val; + int h; + + if (!(p = find(key, &h))) { + return NULL; + } + q = &tab[h]; + while (*q != p) { + q = &((*q)->next); + } + *q = p->next; + if (deleteKeys) { + delete p->key; + } + val = p->val; + delete p; + --len; + return val; +} + +void GHash::startIter(GHashIter **iter) { + *iter = new GHashIter; + (*iter)->h = -1; + (*iter)->p = NULL; +} + +GBool GHash::getNext(GHashIter **iter, GString **key, void **val) { + if (!*iter) { + return gFalse; + } + if ((*iter)->p) { + (*iter)->p = (*iter)->p->next; + } + while (!(*iter)->p) { + if (++(*iter)->h == size) { + delete *iter; + *iter = NULL; + return gFalse; + } + (*iter)->p = tab[(*iter)->h]; + } + *key = (*iter)->p->key; + *val = (*iter)->p->val; + return gTrue; +} + +void GHash::killIter(GHashIter **iter) { + delete *iter; + *iter = NULL; +} + +GHashBucket *GHash::find(GString *key, int *h) { + GHashBucket *p; + + *h = hash(key); + for (p = tab[*h]; p; p = p->next) { + if (!p->key->cmp(key)) { + return p; + } + } + return NULL; +} + +GHashBucket *GHash::find(char *key, int *h) { + GHashBucket *p; + + *h = hash(key); + for (p = tab[*h]; p; p = p->next) { + if (!p->key->cmp(key)) { + return p; + } + } + return NULL; +} + +int GHash::hash(GString *key) { + char *p; + unsigned int h; + int i; + + h = 0; + for (p = key->getCString(), i = 0; i < key->getLength(); ++p, ++i) { + h = 17 * h + (int)(*p & 0xff); + } + return (int)(h % size); +} + +int GHash::hash(char *key) { + char *p; + unsigned int h; + + h = 0; + for (p = key; *p; ++p) { + h = 17 * h + (int)(*p & 0xff); + } + return (int)(h % size); +} diff --git a/noncore/unsupported/qpdf/goo/GHash.h b/noncore/unsupported/qpdf/goo/GHash.h new file mode 100644 index 0000000..be1e573 --- a/dev/null +++ b/noncore/unsupported/qpdf/goo/GHash.h @@ -0,0 +1,67 @@ +//======================================================================== +// +// GHash.h +// +// Copyright 2001 Derek B. Noonburg +// +//======================================================================== + +#ifndef GHASH_H +#define GHASH_H + +#ifdef __GNUC__ +#pragma interface +#endif + +#include "gtypes.h" + +class GString; +struct GHashBucket; +struct GHashIter; + +//------------------------------------------------------------------------ + +class GHash { +public: + + GHash(GBool deleteKeysA = gFalse); + ~GHash(); + void add(GString *key, void *val); + void *lookup(GString *key); + void *lookup(char *key); + void *remove(GString *key); + void *remove(char *key); + int getLength() { return len; } + void startIter(GHashIter **iter); + GBool getNext(GHashIter **iter, GString **key, void **val); + void killIter(GHashIter **iter); + +private: + + GHashBucket *find(GString *key, int *h); + GHashBucket *find(char *key, int *h); + int hash(GString *key); + int hash(char *key); + + GBool deleteKeys; // set if key strings should be deleted + int size; // number of buckets + int len; // number of entries + GHashBucket **tab; +}; + +#define deleteGHash(hash, T) \ + do { \ + GHash *_hash = (hash); \ + { \ + GHashIter *_iter; \ + GString *_key; \ + void *_p; \ + _hash->startIter(&_iter); \ + while (_hash->getNext(&_iter, &_key, &_p)) { \ + delete (T*)_p; \ + } \ + delete _hash; \ + } \ + } while(0) + +#endif diff --git a/noncore/unsupported/qpdf/goo/GList.cc b/noncore/unsupported/qpdf/goo/GList.cc new file mode 100644 index 0000000..cfe0a25 --- a/dev/null +++ b/noncore/unsupported/qpdf/goo/GList.cc @@ -0,0 +1,91 @@ +//======================================================================== +// +// GList.cc +// +// Copyright 2001 Derek B. Noonburg +// +//======================================================================== + +#ifdef __GNUC__ +#pragma implementation +#endif + +#include <aconf.h> +#include <string.h> +#include "gmem.h" +#include "GList.h" + +//------------------------------------------------------------------------ +// GList +//------------------------------------------------------------------------ + +GList::GList() { + size = 8; + data = (void **)gmalloc(size * sizeof(void*)); + length = 0; + inc = 0; +} + +GList::GList(int sizeA) { + size = sizeA; + data = (void **)gmalloc(size * sizeof(void*)); + length = 0; + inc = 0; +} + +GList::~GList() { + gfree(data); +} + +void GList::append(void *p) { + if (length >= size) { + expand(); + } + data[length++] = p; +} + +void GList::append(GList *list) { + int i; + + while (length + list->length > size) { + expand(); + } + for (i = 0; i < list->length; ++i) { + data[length++] = list->data[i]; + } +} + +void GList::insert(int i, void *p) { + if (length >= size) { + expand(); + } + if (i < length) { + memmove(data+i+1, data+i, (length - i) * sizeof(void *)); + } + data[i] = p; + ++length; +} + +void *GList::del(int i) { + void *p; + + p = data[i]; + if (i < length - 1) { + memmove(data+i, data+i+1, (length - i - 1) * sizeof(void *)); + } + --length; + if (size - length >= ((inc > 0) ? inc : size/2)) { + shrink(); + } + return p; +} + +void GList::expand() { + size += (inc > 0) ? inc : size; + data = (void **)grealloc(data, size * sizeof(void*)); +} + +void GList::shrink() { + size -= (inc > 0) ? inc : size/2; + data = (void **)grealloc(data, size * sizeof(void*)); +} diff --git a/noncore/unsupported/qpdf/goo/GList.h b/noncore/unsupported/qpdf/goo/GList.h new file mode 100644 index 0000000..cc23731 --- a/dev/null +++ b/noncore/unsupported/qpdf/goo/GList.h @@ -0,0 +1,89 @@ +//======================================================================== +// +// GList.h +// +// Copyright 2001 Derek B. Noonburg +// +//======================================================================== + +#ifndef GLIST_H +#define GLIST_H + +#ifdef __GNUC__ +#pragma interface +#endif + +#include "gtypes.h" + +//------------------------------------------------------------------------ +// GList +//------------------------------------------------------------------------ + +class GList { +public: + + // Create an empty list. + GList(); + + // Create an empty list with space for <size1> elements. + GList(int sizeA); + + // Destructor - does not free pointed-to objects. + ~GList(); + + //----- general + + // Get the number of elements. + int getLength() { return length; } + + //----- ordered list support + + // Return the <i>th element. + // Assumes 0 <= i < length. + void *get(int i) { return data[i]; } + + // Append an element to the end of the list. + void append(void *p); + + // Append another list to the end of this one. + void append(GList *list); + + // Insert an element at index <i>. + // Assumes 0 <= i <= length. + void insert(int i, void *p); + + // Deletes and returns the element at index <i>. + // Assumes 0 <= i < length. + void *del(int i); + + //----- control + + // Set allocation increment to <inc>. If inc > 0, that many + // elements will be allocated every time the list is expanded. + // If inc <= 0, the list will be doubled in size. + void setAllocIncr(int incA) { inc = incA; } + +private: + + void expand(); + void shrink(); + + void **data; // the list elements + int size; // size of data array + int length; // number of elements on list + int inc; // allocation increment +}; + +#define deleteGList(list, T) \ + do { \ + GList *_list = (list); \ + { \ + int _i; \ + for (_i = 0; _i < _list->getLength(); ++_i) { \ + delete (T*)_list->get(_i); \ + } \ + delete _list; \ + } \ + } while (0) + +#endif diff --git a/noncore/unsupported/qpdf/goo/GString.cc b/noncore/unsupported/qpdf/goo/GString.cc new file mode 100644 index 0000000..414c490 --- a/dev/null +++ b/noncore/unsupported/qpdf/goo/GString.cc @@ -0,0 +1,231 @@ +//======================================================================== +// +// GString.cc +// +// Simple variable-length string type. +// +// Copyright 1996 Derek B. Noonburg +// +//======================================================================== + +#ifdef __GNUC__ +#pragma implementation +#endif + +#include <aconf.h> +#include <stdlib.h> +#include <stddef.h> +#include <string.h> +#include <ctype.h> +#include "gtypes.h" +#include "GString.h" + +static inline int size(int len) { + int delta; + + delta = len < 256 ? 7 : 255; + return ((len + 1) + delta) & ~delta; +} + +inline void GString::resize(int length1) { + char *s1; + + if (!s) { + s = new char[size(length1)]; + } else if (size(length1) != size(length)) { + s1 = new char[size(length1)]; + memcpy(s1, s, length + 1); + delete[] s; + s = s1; + } +} + +GString::GString() { + s = NULL; + resize(length = 0); + s[0] = '\0'; +} + +GString::GString(const char *sA) { + int n = strlen(sA); + + s = NULL; + resize(length = n); + memcpy(s, sA, n + 1); +} + +GString::GString(const char *sA, int lengthA) { + s = NULL; + resize(length = lengthA); + memcpy(s, sA, length * sizeof(char)); + s[length] = '\0'; +} + +GString::GString(GString *str, int idx, int lengthA) { + s = NULL; + resize(length = lengthA); + memcpy(s, str->getCString() + idx, length); + s[length] = '\0'; +} + +GString::GString(GString *str) { + s = NULL; + resize(length = str->getLength()); + memcpy(s, str->getCString(), length + 1); +} + +GString::GString(GString *str1, GString *str2) { + int n1 = str1->getLength(); + int n2 = str2->getLength(); + + s = NULL; + resize(length = n1 + n2); + memcpy(s, str1->getCString(), n1); + memcpy(s + n1, str2->getCString(), n2 + 1); +} + +GString *GString::fromInt(int x) { + char buf[24]; // enough space for 64-bit ints plus a little extra + GBool neg; + Guint y; + int i; + + i = 24; + if (x == 0) { + buf[--i] = '0'; + } else { + if ((neg = x < 0)) { + y = (Guint)-x; + } else { + y = (Guint)x; + } + while (i > 0 && y > 0) { + buf[--i] = '0' + y % 10; + y /= 10; + } + if (neg && i > 0) { + buf[--i] = '-'; + } + } + return new GString(buf + i, 24 - i); +} + +GString::~GString() { + delete[] s; +} + +GString *GString::clear() { + s[length = 0] = '\0'; + resize(0); + return this; +} + +GString *GString::append(char c) { + resize(length + 1); + s[length++] = c; + s[length] = '\0'; + return this; +} + +GString *GString::append(GString *str) { + int n = str->getLength(); + + resize(length + n); + memcpy(s + length, str->getCString(), n + 1); + length += n; + return this; +} + +GString *GString::append(const char *str) { + int n = strlen(str); + + resize(length + n); + memcpy(s + length, str, n + 1); + length += n; + return this; +} + +GString *GString::append(const char *str, int lengthA) { + resize(length + lengthA); + memcpy(s + length, str, lengthA); + length += lengthA; + s[length] = '\0'; + return this; +} + +GString *GString::insert(int i, char c) { + int j; + + resize(length + 1); + for (j = length + 1; j > i; --j) + s[j] = s[j-1]; + s[i] = c; + ++length; + return this; +} + +GString *GString::insert(int i, GString *str) { + int n = str->getLength(); + int j; + + resize(length + n); + for (j = length; j >= i; --j) + s[j+n] = s[j]; + memcpy(s+i, str->getCString(), n); + length += n; + return this; +} + +GString *GString::insert(int i, const char *str) { + int n = strlen(str); + int j; + + resize(length + n); + for (j = length; j >= i; --j) + s[j+n] = s[j]; + memcpy(s+i, str, n); + length += n; + return this; +} + +GString *GString::insert(int i, const char *str, int lengthA) { + int j; + + resize(length + lengthA); + for (j = length; j >= i; --j) + s[j+lengthA] = s[j]; + memcpy(s+i, str, lengthA); + length += lengthA; + return this; +} + +GString *GString::del(int i, int n) { + int j; + + if (n > 0) { + for (j = i; j <= length - n; ++j) + s[j] = s[j + n]; + resize(length -= n); + } + return this; +} + +GString *GString::upperCase() { + int i; + + for (i = 0; i < length; ++i) { + if (islower(s[i])) + s[i] = toupper(s[i]); + } + return this; +} + +GString *GString::lowerCase() { + int i; + + for (i = 0; i < length; ++i) { + if (isupper(s[i])) + s[i] = tolower(s[i]); + } + return this; +} diff --git a/noncore/unsupported/qpdf/goo/GString.h b/noncore/unsupported/qpdf/goo/GString.h new file mode 100644 index 0000000..3ced1c4 --- a/dev/null +++ b/noncore/unsupported/qpdf/goo/GString.h @@ -0,0 +1,98 @@ +//======================================================================== +// +// GString.h +// +// Simple variable-length string type. +// +// Copyright 1996 Derek B. Noonburg +// +//======================================================================== + +#ifndef GSTRING_H +#define GSTRING_H + +#ifdef __GNUC__ +#pragma interface +#endif + +#include <string.h> + +class GString { +public: + + // Create an empty string. + GString(); + + // Create a string from a C string. + GString(const char *sA); + + // Create a string from <lengthA> chars at <sA>. This string + // can contain null characters. + GString(const char *sA, int lengthA); + + // Create a string from <lengthA> chars at <idx> in <str>. + GString(GString *str, int idx, int lengthA); + + // Copy a string. + GString(GString *str); + GString *copy() { return new GString(this); } + + // Concatenate two strings. + GString(GString *str1, GString *str2); + + // Convert an integer to a string. + static GString *fromInt(int x); + + // Destructor. + ~GString(); + + // Get length. + int getLength() { return length; } + + // Get C string. + char *getCString() { return s; } + + // Get <i>th character. + char getChar(int i) { return s[i]; } + + // Change <i>th character. + void setChar(int i, char c) { s[i] = c; } + + // Clear string to zero length. + GString *clear(); + + // Append a character or string. + GString *append(char c); + GString *append(GString *str); + GString *append(const char *str); + GString *append(const char *str, int lengthA); + + // Insert a character or string. + GString *insert(int i, char c); + GString *insert(int i, GString *str); + GString *insert(int i, const char *str); + GString *insert(int i, const char *str, int lengthA); + + // Delete a character or range of characters. + GString *del(int i, int n = 1); + + // Convert string to all-upper/all-lower case. + GString *upperCase(); + GString *lowerCase(); + + // Compare two strings: -1:< 0:= +1:> + // These functions assume the strings do not contain null characters. + int cmp(GString *str) { return strcmp(s, str->getCString()); } + int cmpN(GString *str, int n) { return strncmp(s, str->getCString(), n); } + int cmp(const char *sA) { return strcmp(s, sA); } + int cmpN(const char *sA, int n) { return strncmp(s, sA, n); } + +private: + + int length; + char *s; + + void resize(int length1); +}; + +#endif diff --git a/noncore/unsupported/qpdf/goo/gfile.h b/noncore/unsupported/qpdf/goo/gfile.h new file mode 100644 index 0000000..16cbca0 --- a/dev/null +++ b/noncore/unsupported/qpdf/goo/gfile.h @@ -0,0 +1,135 @@ +//======================================================================== +// +// gfile.h +// +// Miscellaneous file and directory name manipulation. +// +// Copyright 1996 Derek B. Noonburg +// +//======================================================================== + +#ifndef GFILE_H +#define GFILE_H + +#include <stdio.h> +#include <stdlib.h> +#include <stddef.h> +#if defined(WIN32) +# include <sys/stat.h> +# ifdef FPTEX +# include <win32lib.h> +# else +# include <windows.h> +# endif +#elif defined(ACORN) +#elif defined(MACOS) +# include <ctime.h> +#else +# include <unistd.h> +# include <sys/types.h> +# ifdef VMS +# include "vms_dirent.h" +# elif HAVE_DIRENT_H +# include <dirent.h> +# define NAMLEN(d) strlen((d)->d_name) +# else +# define dirent direct +# define NAMLEN(d) (d)->d_namlen +# if HAVE_SYS_NDIR_H +# include <sys/ndir.h> +# endif +# if HAVE_SYS_DIR_H +# include <sys/dir.h> +# endif +# if HAVE_NDIR_H +# include <ndir.h> +# endif +# endif +#endif +#include "gtypes.h" + +class GString; + +//------------------------------------------------------------------------ + +// Get home directory path. +extern GString *getHomeDir(); + +// Get current directory. +extern GString *getCurrentDir(); + +// Append a file name to a path string. <path> may be an empty +// string, denoting the current directory). Returns <path>. +extern GString *appendToPath(GString *path, char *fileName); + +// Grab the path from the front of the file name. If there is no +// directory component in <fileName>, returns an empty string. +extern GString *grabPath(char *fileName); + +// Is this an absolute path or file name? +extern GBool isAbsolutePath(char *path); + +// Make this path absolute by prepending current directory (if path is +// relative) or prepending user's directory (if path starts with '~'). +extern GString *makePathAbsolute(GString *path); + +// Get the modification time for <fileName>. Returns 0 if there is an +// error. +extern time_t getModTime(char *fileName); + +// Create a temporary file and open it for writing. If <ext> is not +// NULL, it will be used as the file name extension. Returns both the +// name and the file pointer. For security reasons, all writing +// should be done to the returned file pointer; the file may be +// reopened later for reading, but not for writing. The <mode> string +// should be "w" or "wb". Returns true on success. +extern GBool openTempFile(GString **name, FILE **f, char *mode, char *ext); + +// Just like fgets, but handles Unix, Mac, and/or DOS end-of-line +// conventions. +extern char *getLine(char *buf, int size, FILE *f); + +//------------------------------------------------------------------------ +// GDir and GDirEntry +//------------------------------------------------------------------------ + +class GDirEntry { +public: + + GDirEntry(char *dirPath, char *nameA, GBool doStat); + ~GDirEntry(); + GString *getName() { return name; } + GBool isDir() { return dir; } + +private: + + GString *name; // dir/file name + GBool dir; // is it a directory? +}; + +class GDir { +public: + + GDir(char *name, GBool doStatA = gTrue); + ~GDir(); + GDirEntry *getNextEntry(); + void rewind(); + +private: + + GString *path; // directory path + GBool doStat; // call stat() for each entry? +#if defined(WIN32) + WIN32_FIND_DATA ffd; + HANDLE hnd; +#elif defined(ACORN) +#elif defined(MACOS) +#else + DIR *dir; // the DIR structure from opendir() +#ifdef VMS + GBool needParent; // need to return an entry for [-] +#endif +#endif +}; + +#endif diff --git a/noncore/unsupported/qpdf/goo/gmem.h b/noncore/unsupported/qpdf/goo/gmem.h new file mode 100644 index 0000000..7ab5ddb --- a/dev/null +++ b/noncore/unsupported/qpdf/goo/gmem.h @@ -0,0 +1,53 @@ +/* + * gmem.h + * + * Memory routines with out-of-memory checking. + * + * Copyright 1996 Derek B. Noonburg + */ + +#ifndef GMEM_H +#define GMEM_H + +#include <stdio.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Same as malloc, but prints error message and exits if malloc() + * returns NULL. + */ +extern void *gmalloc(int size); + +/* + * Same as realloc, but prints error message and exits if realloc() + * returns NULL. If <p> is NULL, calls malloc instead of realloc(). + */ +extern void *grealloc(void *p, int size); + +/* + * Same as free, but checks for and ignores NULL pointers. + */ +extern void gfree(void *p); + +#ifdef DEBUG_MEM +/* + * Report on unfreed memory. + */ +extern void gMemReport(FILE *f); +#else +#define gMemReport(f) +#endif + +/* + * Allocate memory and copy a string into it. + */ +extern char *copyString(char *s); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/noncore/unsupported/qpdf/goo/gtypes.h b/noncore/unsupported/qpdf/goo/gtypes.h new file mode 100644 index 0000000..6593267 --- a/dev/null +++ b/noncore/unsupported/qpdf/goo/gtypes.h @@ -0,0 +1,29 @@ +/* + * gtypes.h + * + * Some useful simple types. + * + * Copyright 1996 Derek B. Noonburg + */ + +#ifndef GTYPES_H +#define GTYPES_H + +/* + * These have stupid names to avoid conflicts with some (but not all) + * C++ compilers which define them. + */ +typedef int GBool; +#define gTrue 1 +#define gFalse 0 + +/* + * These have stupid names to avoid conflicts with <sys/types.h>, + * which on various systems defines some random subset of these. + */ +typedef unsigned char Guchar; +typedef unsigned short Gushort; +typedef unsigned int Guint; +typedef unsigned long Gulong; + +#endif |