From 14d0fbe96c6abdb9da80e99953aec672f999948c Mon Sep 17 00:00:00 2001 From: Michele Calgaro Date: Sun, 13 Dec 2020 19:22:19 +0900 Subject: Renaming of files in preparation for code style tools. Signed-off-by: Michele Calgaro --- kpdf/xpdf/goo/CMakeLists.txt | 2 +- kpdf/xpdf/goo/GHash.cc | 380 ---------------------- kpdf/xpdf/goo/GHash.cpp | 380 ++++++++++++++++++++++ kpdf/xpdf/goo/GList.cc | 97 ------ kpdf/xpdf/goo/GList.cpp | 97 ++++++ kpdf/xpdf/goo/GString.cc | 718 ------------------------------------------ kpdf/xpdf/goo/GString.cpp | 718 ++++++++++++++++++++++++++++++++++++++++++ kpdf/xpdf/goo/Makefile.am | 2 +- kpdf/xpdf/goo/gfile.cc | 731 ------------------------------------------- kpdf/xpdf/goo/gfile.cpp | 731 +++++++++++++++++++++++++++++++++++++++++++ kpdf/xpdf/goo/gmem.cc | 315 ------------------- kpdf/xpdf/goo/gmem.cpp | 315 +++++++++++++++++++ kpdf/xpdf/goo/gmempp.cc | 32 -- kpdf/xpdf/goo/gmempp.cpp | 32 ++ 14 files changed, 2275 insertions(+), 2275 deletions(-) delete mode 100644 kpdf/xpdf/goo/GHash.cc create mode 100644 kpdf/xpdf/goo/GHash.cpp delete mode 100644 kpdf/xpdf/goo/GList.cc create mode 100644 kpdf/xpdf/goo/GList.cpp delete mode 100644 kpdf/xpdf/goo/GString.cc create mode 100644 kpdf/xpdf/goo/GString.cpp delete mode 100644 kpdf/xpdf/goo/gfile.cc create mode 100644 kpdf/xpdf/goo/gfile.cpp delete mode 100644 kpdf/xpdf/goo/gmem.cc create mode 100644 kpdf/xpdf/goo/gmem.cpp delete mode 100644 kpdf/xpdf/goo/gmempp.cc create mode 100644 kpdf/xpdf/goo/gmempp.cpp (limited to 'kpdf/xpdf/goo') diff --git a/kpdf/xpdf/goo/CMakeLists.txt b/kpdf/xpdf/goo/CMakeLists.txt index 5a5a0ac8..dd6dc2d1 100644 --- a/kpdf/xpdf/goo/CMakeLists.txt +++ b/kpdf/xpdf/goo/CMakeLists.txt @@ -22,5 +22,5 @@ include_directories( tde_add_library( goo STATIC_PIC SOURCES - GHash.cc GList.cc GString.cc gfile.cc gmem.cc gmempp.cc + GHash.cpp GList.cpp GString.cpp gfile.cpp gmem.cpp gmempp.cpp ) diff --git a/kpdf/xpdf/goo/GHash.cc b/kpdf/xpdf/goo/GHash.cc deleted file mode 100644 index b51a7643..00000000 --- a/kpdf/xpdf/goo/GHash.cc +++ /dev/null @@ -1,380 +0,0 @@ -//======================================================================== -// -// GHash.cc -// -// Copyright 2001-2003 Glyph & Cog, LLC -// -//======================================================================== - -#include - -#ifdef USE_GCC_PRAGMAS -#pragma implementation -#endif - -#include "gmem.h" -#include "GString.h" -#include "GHash.h" - -//------------------------------------------------------------------------ - -struct GHashBucket { - GString *key; - union { - void *p; - int i; - } val; - GHashBucket *next; -}; - -struct GHashIter { - int h; - GHashBucket *p; -}; - -//------------------------------------------------------------------------ - -GHash::GHash(GBool deleteKeysA) { - int h; - - deleteKeys = deleteKeysA; - size = 7; - tab = (GHashBucket **)gmallocn(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 *p; - int h; - - // expand the table if necessary - if (len >= size) { - expand(); - } - - // add the new symbol - p = new GHashBucket; - p->key = key; - p->val.p = val; - h = hash(key); - p->next = tab[h]; - tab[h] = p; - ++len; -} - -void GHash::add(GString *key, int val) { - GHashBucket *p; - int h; - - // expand the table if necessary - if (len >= size) { - expand(); - } - - // add the new symbol - p = new GHashBucket; - p->key = key; - p->val.i = val; - h = hash(key); - p->next = tab[h]; - tab[h] = p; - ++len; -} - -void GHash::replace(GString *key, void *val) { - GHashBucket *p; - int h; - - if ((p = find(key, &h))) { - p->val.p = val; - delete key; - } else { - add(key, val); - } -} - -void GHash::replace(GString *key, int val) { - GHashBucket *p; - int h; - - if ((p = find(key, &h))) { - p->val.i = val; - delete key; - } else { - add(key, val); - } -} - -void *GHash::lookup(GString *key) { - GHashBucket *p; - int h; - - if (!(p = find(key, &h))) { - return NULL; - } - return p->val.p; -} - -int GHash::lookupInt(GString *key) { - GHashBucket *p; - int h; - - if (!(p = find(key, &h))) { - return 0; - } - return p->val.i; -} - -void *GHash::lookup(char *key) { - GHashBucket *p; - int h; - - if (!(p = find(key, &h))) { - return NULL; - } - return p->val.p; -} - -int GHash::lookupInt(char *key) { - GHashBucket *p; - int h; - - if (!(p = find(key, &h))) { - return 0; - } - return p->val.i; -} - -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.p; - delete p; - --len; - return val; -} - -int GHash::removeInt(GString *key) { - GHashBucket *p; - GHashBucket **q; - int val; - int h; - - if (!(p = find(key, &h))) { - return 0; - } - q = &tab[h]; - while (*q != p) { - q = &((*q)->next); - } - *q = p->next; - if (deleteKeys) { - delete p->key; - } - val = p->val.i; - 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.p; - delete p; - --len; - return val; -} - -int GHash::removeInt(char *key) { - GHashBucket *p; - GHashBucket **q; - int val; - int h; - - if (!(p = find(key, &h))) { - return 0; - } - q = &tab[h]; - while (*q != p) { - q = &((*q)->next); - } - *q = p->next; - if (deleteKeys) { - delete p->key; - } - val = p->val.i; - 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.p; - return gTrue; -} - -GBool GHash::getNext(GHashIter **iter, GString **key, int *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.i; - return gTrue; -} - -void GHash::killIter(GHashIter **iter) { - delete *iter; - *iter = NULL; -} - -void GHash::expand() { - GHashBucket **oldTab; - GHashBucket *p; - int oldSize, h, i; - - oldSize = size; - oldTab = tab; - size = 2*size + 1; - tab = (GHashBucket **)gmallocn(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); -} - -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/kpdf/xpdf/goo/GHash.cpp b/kpdf/xpdf/goo/GHash.cpp new file mode 100644 index 00000000..f642a261 --- /dev/null +++ b/kpdf/xpdf/goo/GHash.cpp @@ -0,0 +1,380 @@ +//======================================================================== +// +// GHash.cpp +// +// Copyright 2001-2003 Glyph & Cog, LLC +// +//======================================================================== + +#include + +#ifdef USE_GCC_PRAGMAS +#pragma implementation +#endif + +#include "gmem.h" +#include "GString.h" +#include "GHash.h" + +//------------------------------------------------------------------------ + +struct GHashBucket { + GString *key; + union { + void *p; + int i; + } val; + GHashBucket *next; +}; + +struct GHashIter { + int h; + GHashBucket *p; +}; + +//------------------------------------------------------------------------ + +GHash::GHash(GBool deleteKeysA) { + int h; + + deleteKeys = deleteKeysA; + size = 7; + tab = (GHashBucket **)gmallocn(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 *p; + int h; + + // expand the table if necessary + if (len >= size) { + expand(); + } + + // add the new symbol + p = new GHashBucket; + p->key = key; + p->val.p = val; + h = hash(key); + p->next = tab[h]; + tab[h] = p; + ++len; +} + +void GHash::add(GString *key, int val) { + GHashBucket *p; + int h; + + // expand the table if necessary + if (len >= size) { + expand(); + } + + // add the new symbol + p = new GHashBucket; + p->key = key; + p->val.i = val; + h = hash(key); + p->next = tab[h]; + tab[h] = p; + ++len; +} + +void GHash::replace(GString *key, void *val) { + GHashBucket *p; + int h; + + if ((p = find(key, &h))) { + p->val.p = val; + delete key; + } else { + add(key, val); + } +} + +void GHash::replace(GString *key, int val) { + GHashBucket *p; + int h; + + if ((p = find(key, &h))) { + p->val.i = val; + delete key; + } else { + add(key, val); + } +} + +void *GHash::lookup(GString *key) { + GHashBucket *p; + int h; + + if (!(p = find(key, &h))) { + return NULL; + } + return p->val.p; +} + +int GHash::lookupInt(GString *key) { + GHashBucket *p; + int h; + + if (!(p = find(key, &h))) { + return 0; + } + return p->val.i; +} + +void *GHash::lookup(char *key) { + GHashBucket *p; + int h; + + if (!(p = find(key, &h))) { + return NULL; + } + return p->val.p; +} + +int GHash::lookupInt(char *key) { + GHashBucket *p; + int h; + + if (!(p = find(key, &h))) { + return 0; + } + return p->val.i; +} + +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.p; + delete p; + --len; + return val; +} + +int GHash::removeInt(GString *key) { + GHashBucket *p; + GHashBucket **q; + int val; + int h; + + if (!(p = find(key, &h))) { + return 0; + } + q = &tab[h]; + while (*q != p) { + q = &((*q)->next); + } + *q = p->next; + if (deleteKeys) { + delete p->key; + } + val = p->val.i; + 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.p; + delete p; + --len; + return val; +} + +int GHash::removeInt(char *key) { + GHashBucket *p; + GHashBucket **q; + int val; + int h; + + if (!(p = find(key, &h))) { + return 0; + } + q = &tab[h]; + while (*q != p) { + q = &((*q)->next); + } + *q = p->next; + if (deleteKeys) { + delete p->key; + } + val = p->val.i; + 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.p; + return gTrue; +} + +GBool GHash::getNext(GHashIter **iter, GString **key, int *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.i; + return gTrue; +} + +void GHash::killIter(GHashIter **iter) { + delete *iter; + *iter = NULL; +} + +void GHash::expand() { + GHashBucket **oldTab; + GHashBucket *p; + int oldSize, h, i; + + oldSize = size; + oldTab = tab; + size = 2*size + 1; + tab = (GHashBucket **)gmallocn(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); +} + +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/kpdf/xpdf/goo/GList.cc b/kpdf/xpdf/goo/GList.cc deleted file mode 100644 index fb5fd628..00000000 --- a/kpdf/xpdf/goo/GList.cc +++ /dev/null @@ -1,97 +0,0 @@ -//======================================================================== -// -// GList.cc -// -// Copyright 2001-2003 Glyph & Cog, LLC -// -//======================================================================== - -#include - -#ifdef USE_GCC_PRAGMAS -#pragma implementation -#endif - -#include -#include -#include "gmem.h" -#include "GList.h" - -//------------------------------------------------------------------------ -// GList -//------------------------------------------------------------------------ - -GList::GList() { - size = 8; - data = (void **)gmallocn(size, sizeof(void*)); - length = 0; - inc = 0; -} - -GList::GList(int sizeA) { - size = sizeA; - data = (void **)gmallocn(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::sort(int (*cmp)(const void *obj1, const void *obj2)) { - qsort(data, length, sizeof(void *), cmp); -} - -void GList::expand() { - size += (inc > 0) ? inc : size; - data = (void **)greallocn(data, size, sizeof(void*)); -} - -void GList::shrink() { - size -= (inc > 0) ? inc : size/2; - data = (void **)greallocn(data, size, sizeof(void*)); -} diff --git a/kpdf/xpdf/goo/GList.cpp b/kpdf/xpdf/goo/GList.cpp new file mode 100644 index 00000000..2a4e4695 --- /dev/null +++ b/kpdf/xpdf/goo/GList.cpp @@ -0,0 +1,97 @@ +//======================================================================== +// +// GList.cpp +// +// Copyright 2001-2003 Glyph & Cog, LLC +// +//======================================================================== + +#include + +#ifdef USE_GCC_PRAGMAS +#pragma implementation +#endif + +#include +#include +#include "gmem.h" +#include "GList.h" + +//------------------------------------------------------------------------ +// GList +//------------------------------------------------------------------------ + +GList::GList() { + size = 8; + data = (void **)gmallocn(size, sizeof(void*)); + length = 0; + inc = 0; +} + +GList::GList(int sizeA) { + size = sizeA; + data = (void **)gmallocn(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::sort(int (*cmp)(const void *obj1, const void *obj2)) { + qsort(data, length, sizeof(void *), cmp); +} + +void GList::expand() { + size += (inc > 0) ? inc : size; + data = (void **)greallocn(data, size, sizeof(void*)); +} + +void GList::shrink() { + size -= (inc > 0) ? inc : size/2; + data = (void **)greallocn(data, size, sizeof(void*)); +} diff --git a/kpdf/xpdf/goo/GString.cc b/kpdf/xpdf/goo/GString.cc deleted file mode 100644 index e21fd3ea..00000000 --- a/kpdf/xpdf/goo/GString.cc +++ /dev/null @@ -1,718 +0,0 @@ -//======================================================================== -// -// GString.cc -// -// Simple variable-length string type. -// -// Copyright 1996-2003 Glyph & Cog, LLC -// -//======================================================================== - -#include - -#ifdef USE_GCC_PRAGMAS -#pragma implementation -#endif - -#include -#include -#include -#include -#include -#include "gmem.h" -#include "GString.h" - -//------------------------------------------------------------------------ - -union GStringFormatArg { - int i; - Guint ui; - long l; - Gulong ul; - double f; - char c; - char *s; - GString *gs; -}; - -enum GStringFormatType { - fmtIntDecimal, - fmtIntHex, - fmtIntOctal, - fmtIntBinary, - fmtUIntDecimal, - fmtUIntHex, - fmtUIntOctal, - fmtUIntBinary, - fmtLongDecimal, - fmtLongHex, - fmtLongOctal, - fmtLongBinary, - fmtULongDecimal, - fmtULongHex, - fmtULongOctal, - fmtULongBinary, - fmtDouble, - fmtDoubleTrim, - fmtChar, - fmtString, - fmtGString, - fmtSpace -}; - -static char *formatStrings[] = { - "d", "x", "o", "b", "ud", "ux", "uo", "ub", - "ld", "lx", "lo", "lb", "uld", "ulx", "ulo", "ulb", - "f", "g", - "c", - "s", - "t", - "w", - NULL -}; - -//------------------------------------------------------------------------ - -static inline int size(int len) { - int delta; - for (delta = 8; delta < len && delta < 0x100000; delta <<= 1) ; - // this is ((len + 1) + (delta - 1)) & ~(delta - 1) - return (len + delta) & ~(delta - 1); -} - -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)]; - if (length1 < length) { - memcpy(s1, s, length1); - s1[length1] = '\0'; - } else { - 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 - char *p; - int len; - - formatInt(x, buf, sizeof(buf), gFalse, 0, 10, &p, &len); - return new GString(p, len); -} - -GString *GString::format(char *fmt, ...) { - va_list argList; - GString *s; - - s = new GString(); - va_start(argList, fmt); - s->appendfv(fmt, argList); - va_end(argList); - return s; -} - -GString *GString::formatv(char *fmt, va_list argList) { - GString *s; - - s = new GString(); - s->appendfv(fmt, argList); - return s; -} - -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::appendf(char *fmt, ...) { - va_list argList; - - va_start(argList, fmt); - appendfv(fmt, argList); - va_end(argList); - return this; -} - -GString *GString::appendfv(char *fmt, va_list argList) { - GStringFormatArg *args; - int argsLen, argsSize; - GStringFormatArg arg; - int idx, width, prec; - GBool reverseAlign, zeroFill; - GStringFormatType ft; - char buf[65]; - int len, i; - char *p0, *p1, *str; - - argsLen = 0; - argsSize = 8; - args = (GStringFormatArg *)gmallocn(argsSize, sizeof(GStringFormatArg)); - - p0 = fmt; - while (*p0) { - if (*p0 == '{') { - ++p0; - if (*p0 == '{') { - ++p0; - append('{'); - } else { - - // parse the format string - if (!(*p0 >= '0' && *p0 <= '9')) { - break; - } - idx = *p0 - '0'; - for (++p0; *p0 >= '0' && *p0 <= '9'; ++p0) { - idx = 10 * idx + (*p0 - '0'); - } - if (*p0 != ':') { - break; - } - ++p0; - if (*p0 == '-') { - reverseAlign = gTrue; - ++p0; - } else { - reverseAlign = gFalse; - } - width = 0; - zeroFill = *p0 == '0'; - for (; *p0 >= '0' && *p0 <= '9'; ++p0) { - width = 10 * width + (*p0 - '0'); - } - if (*p0 == '.') { - ++p0; - prec = 0; - for (; *p0 >= '0' && *p0 <= '9'; ++p0) { - prec = 10 * prec + (*p0 - '0'); - } - } else { - prec = 0; - } - for (ft = (GStringFormatType)0; - formatStrings[ft]; - ft = (GStringFormatType)(ft + 1)) { - if (!strncmp(p0, formatStrings[ft], strlen(formatStrings[ft]))) { - break; - } - } - if (!formatStrings[ft]) { - break; - } - p0 += strlen(formatStrings[ft]); - if (*p0 != '}') { - break; - } - ++p0; - - // fetch the argument - if (idx > argsLen) { - break; - } - if (idx == argsLen) { - if (argsLen == argsSize) { - argsSize *= 2; - args = (GStringFormatArg *)greallocn(args, argsSize, - sizeof(GStringFormatArg)); - } - switch (ft) { - case fmtIntDecimal: - case fmtIntHex: - case fmtIntOctal: - case fmtIntBinary: - case fmtSpace: - args[argsLen].i = va_arg(argList, int); - break; - case fmtUIntDecimal: - case fmtUIntHex: - case fmtUIntOctal: - case fmtUIntBinary: - args[argsLen].ui = va_arg(argList, Guint); - break; - case fmtLongDecimal: - case fmtLongHex: - case fmtLongOctal: - case fmtLongBinary: - args[argsLen].l = va_arg(argList, long); - break; - case fmtULongDecimal: - case fmtULongHex: - case fmtULongOctal: - case fmtULongBinary: - args[argsLen].ul = va_arg(argList, Gulong); - break; - case fmtDouble: - case fmtDoubleTrim: - args[argsLen].f = va_arg(argList, double); - break; - case fmtChar: - args[argsLen].c = (char)va_arg(argList, int); - break; - case fmtString: - args[argsLen].s = va_arg(argList, char *); - break; - case fmtGString: - args[argsLen].gs = va_arg(argList, GString *); - break; - } - ++argsLen; - } - - // format the argument - arg = args[idx]; - switch (ft) { - case fmtIntDecimal: - formatInt(arg.i, buf, sizeof(buf), zeroFill, width, 10, &str, &len); - break; - case fmtIntHex: - formatInt(arg.i, buf, sizeof(buf), zeroFill, width, 16, &str, &len); - break; - case fmtIntOctal: - formatInt(arg.i, buf, sizeof(buf), zeroFill, width, 8, &str, &len); - break; - case fmtIntBinary: - formatInt(arg.i, buf, sizeof(buf), zeroFill, width, 2, &str, &len); - break; - case fmtUIntDecimal: - formatUInt(arg.ui, buf, sizeof(buf), zeroFill, width, 10, - &str, &len); - break; - case fmtUIntHex: - formatUInt(arg.ui, buf, sizeof(buf), zeroFill, width, 16, - &str, &len); - break; - case fmtUIntOctal: - formatUInt(arg.ui, buf, sizeof(buf), zeroFill, width, 8, &str, &len); - break; - case fmtUIntBinary: - formatUInt(arg.ui, buf, sizeof(buf), zeroFill, width, 2, &str, &len); - break; - case fmtLongDecimal: - formatInt(arg.l, buf, sizeof(buf), zeroFill, width, 10, &str, &len); - break; - case fmtLongHex: - formatInt(arg.l, buf, sizeof(buf), zeroFill, width, 16, &str, &len); - break; - case fmtLongOctal: - formatInt(arg.l, buf, sizeof(buf), zeroFill, width, 8, &str, &len); - break; - case fmtLongBinary: - formatInt(arg.l, buf, sizeof(buf), zeroFill, width, 2, &str, &len); - break; - case fmtULongDecimal: - formatUInt(arg.ul, buf, sizeof(buf), zeroFill, width, 10, - &str, &len); - break; - case fmtULongHex: - formatUInt(arg.ul, buf, sizeof(buf), zeroFill, width, 16, - &str, &len); - break; - case fmtULongOctal: - formatUInt(arg.ul, buf, sizeof(buf), zeroFill, width, 8, &str, &len); - break; - case fmtULongBinary: - formatUInt(arg.ul, buf, sizeof(buf), zeroFill, width, 2, &str, &len); - break; - case fmtDouble: - formatDouble(arg.f, buf, sizeof(buf), prec, gFalse, &str, &len); - break; - case fmtDoubleTrim: - formatDouble(arg.f, buf, sizeof(buf), prec, gTrue, &str, &len); - break; - case fmtChar: - buf[0] = arg.c; - str = buf; - len = 1; - reverseAlign = !reverseAlign; - break; - case fmtString: - str = arg.s; - len = strlen(str); - reverseAlign = !reverseAlign; - break; - case fmtGString: - str = arg.gs->getCString(); - len = arg.gs->getLength(); - reverseAlign = !reverseAlign; - break; - case fmtSpace: - str = buf; - len = 0; - width = arg.i; - break; - } - - // append the formatted arg, handling width and alignment - if (!reverseAlign && len < width) { - for (i = len; i < width; ++i) { - append(' '); - } - } - append(str, len); - if (reverseAlign && len < width) { - for (i = len; i < width; ++i) { - append(' '); - } - } - } - - } else if (*p0 == '}') { - ++p0; - if (*p0 == '}') { - ++p0; - } - append('}'); - - } else { - for (p1 = p0 + 1; *p1 && *p1 != '{' && *p1 != '}'; ++p1) ; - append(p0, p1 - p0); - p0 = p1; - } - } - - gfree(args); - return this; -} - -void GString::formatInt(long x, char *buf, int bufSize, - GBool zeroFill, int width, int base, - char **p, int *len) { - static char vals[17] = "0123456789abcdef"; - GBool neg; - int start, i, j; - - i = bufSize; - if ((neg = x < 0)) { - x = -x; - } - start = neg ? 1 : 0; - if (x == 0) { - buf[--i] = '0'; - } else { - while (i > start && x) { - buf[--i] = vals[x % base]; - x /= base; - } - } - if (zeroFill) { - for (j = bufSize - i; i > start && j < width - start; ++j) { - buf[--i] = '0'; - } - } - if (neg) { - buf[--i] = '-'; - } - *p = buf + i; - *len = bufSize - i; -} - -void GString::formatUInt(Gulong x, char *buf, int bufSize, - GBool zeroFill, int width, int base, - char **p, int *len) { - static char vals[17] = "0123456789abcdef"; - int i, j; - - i = bufSize; - if (x == 0) { - buf[--i] = '0'; - } else { - while (i > 0 && x) { - buf[--i] = vals[x % base]; - x /= base; - } - } - if (zeroFill) { - for (j = bufSize - i; i > 0 && j < width; ++j) { - buf[--i] = '0'; - } - } - *p = buf + i; - *len = bufSize - i; -} - -void GString::formatDouble(double x, char *buf, int bufSize, int prec, - GBool trim, char **p, int *len) { - GBool neg, started; - double x2; - int d, i, j; - - if ((neg = x < 0)) { - x = -x; - } - x = floor(x * pow(10, prec) + 0.5); - i = bufSize; - started = !trim; - for (j = 0; j < prec && i > 1; ++j) { - x2 = floor(0.1 * (x + 0.5)); - d = (int)floor(x - 10 * x2 + 0.5); - if (started || d != 0) { - buf[--i] = '0' + d; - started = gTrue; - } - x = x2; - } - if (i > 1 && started) { - buf[--i] = '.'; - } - if (i > 1) { - do { - x2 = floor(0.1 * (x + 0.5)); - d = (int)floor(x - 10 * x2 + 0.5); - buf[--i] = '0' + d; - x = x2; - } while (i > 1 && x); - } - if (neg) { - buf[--i] = '-'; - } - *p = buf + i; - *len = bufSize - i; -} - -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) { - if (i + n > length) { - n = length - i; - } - 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; -} - -int GString::cmp(GString *str) { - int n1, n2, i, x; - char *p1, *p2; - - n1 = length; - n2 = str->length; - for (i = 0, p1 = s, p2 = str->s; i < n1 && i < n2; ++i, ++p1, ++p2) { - x = *p1 - *p2; - if (x != 0) { - return x; - } - } - return n1 - n2; -} - -int GString::cmpN(GString *str, int n) { - int n1, n2, i, x; - char *p1, *p2; - - n1 = length; - n2 = str->length; - for (i = 0, p1 = s, p2 = str->s; - i < n1 && i < n2 && i < n; - ++i, ++p1, ++p2) { - x = *p1 - *p2; - if (x != 0) { - return x; - } - } - if (i == n) { - return 0; - } - return n1 - n2; -} - -int GString::cmp(const char *sA) { - int n1, i, x; - const char *p1, *p2; - - n1 = length; - for (i = 0, p1 = s, p2 = sA; i < n1 && *p2; ++i, ++p1, ++p2) { - x = *p1 - *p2; - if (x != 0) { - return x; - } - } - if (i < n1) { - return 1; - } - if (*p2) { - return -1; - } - return 0; -} - -int GString::cmpN(const char *sA, int n) { - int n1, i, x; - const char *p1, *p2; - - n1 = length; - for (i = 0, p1 = s, p2 = sA; i < n1 && *p2 && i < n; ++i, ++p1, ++p2) { - x = *p1 - *p2; - if (x != 0) { - return x; - } - } - if (i == n) { - return 0; - } - if (i < n1) { - return 1; - } - if (*p2) { - return -1; - } - return 0; -} diff --git a/kpdf/xpdf/goo/GString.cpp b/kpdf/xpdf/goo/GString.cpp new file mode 100644 index 00000000..89f122e1 --- /dev/null +++ b/kpdf/xpdf/goo/GString.cpp @@ -0,0 +1,718 @@ +//======================================================================== +// +// GString.cpp +// +// Simple variable-length string type. +// +// Copyright 1996-2003 Glyph & Cog, LLC +// +//======================================================================== + +#include + +#ifdef USE_GCC_PRAGMAS +#pragma implementation +#endif + +#include +#include +#include +#include +#include +#include "gmem.h" +#include "GString.h" + +//------------------------------------------------------------------------ + +union GStringFormatArg { + int i; + Guint ui; + long l; + Gulong ul; + double f; + char c; + char *s; + GString *gs; +}; + +enum GStringFormatType { + fmtIntDecimal, + fmtIntHex, + fmtIntOctal, + fmtIntBinary, + fmtUIntDecimal, + fmtUIntHex, + fmtUIntOctal, + fmtUIntBinary, + fmtLongDecimal, + fmtLongHex, + fmtLongOctal, + fmtLongBinary, + fmtULongDecimal, + fmtULongHex, + fmtULongOctal, + fmtULongBinary, + fmtDouble, + fmtDoubleTrim, + fmtChar, + fmtString, + fmtGString, + fmtSpace +}; + +static char *formatStrings[] = { + "d", "x", "o", "b", "ud", "ux", "uo", "ub", + "ld", "lx", "lo", "lb", "uld", "ulx", "ulo", "ulb", + "f", "g", + "c", + "s", + "t", + "w", + NULL +}; + +//------------------------------------------------------------------------ + +static inline int size(int len) { + int delta; + for (delta = 8; delta < len && delta < 0x100000; delta <<= 1) ; + // this is ((len + 1) + (delta - 1)) & ~(delta - 1) + return (len + delta) & ~(delta - 1); +} + +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)]; + if (length1 < length) { + memcpy(s1, s, length1); + s1[length1] = '\0'; + } else { + 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 + char *p; + int len; + + formatInt(x, buf, sizeof(buf), gFalse, 0, 10, &p, &len); + return new GString(p, len); +} + +GString *GString::format(char *fmt, ...) { + va_list argList; + GString *s; + + s = new GString(); + va_start(argList, fmt); + s->appendfv(fmt, argList); + va_end(argList); + return s; +} + +GString *GString::formatv(char *fmt, va_list argList) { + GString *s; + + s = new GString(); + s->appendfv(fmt, argList); + return s; +} + +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::appendf(char *fmt, ...) { + va_list argList; + + va_start(argList, fmt); + appendfv(fmt, argList); + va_end(argList); + return this; +} + +GString *GString::appendfv(char *fmt, va_list argList) { + GStringFormatArg *args; + int argsLen, argsSize; + GStringFormatArg arg; + int idx, width, prec; + GBool reverseAlign, zeroFill; + GStringFormatType ft; + char buf[65]; + int len, i; + char *p0, *p1, *str; + + argsLen = 0; + argsSize = 8; + args = (GStringFormatArg *)gmallocn(argsSize, sizeof(GStringFormatArg)); + + p0 = fmt; + while (*p0) { + if (*p0 == '{') { + ++p0; + if (*p0 == '{') { + ++p0; + append('{'); + } else { + + // parse the format string + if (!(*p0 >= '0' && *p0 <= '9')) { + break; + } + idx = *p0 - '0'; + for (++p0; *p0 >= '0' && *p0 <= '9'; ++p0) { + idx = 10 * idx + (*p0 - '0'); + } + if (*p0 != ':') { + break; + } + ++p0; + if (*p0 == '-') { + reverseAlign = gTrue; + ++p0; + } else { + reverseAlign = gFalse; + } + width = 0; + zeroFill = *p0 == '0'; + for (; *p0 >= '0' && *p0 <= '9'; ++p0) { + width = 10 * width + (*p0 - '0'); + } + if (*p0 == '.') { + ++p0; + prec = 0; + for (; *p0 >= '0' && *p0 <= '9'; ++p0) { + prec = 10 * prec + (*p0 - '0'); + } + } else { + prec = 0; + } + for (ft = (GStringFormatType)0; + formatStrings[ft]; + ft = (GStringFormatType)(ft + 1)) { + if (!strncmp(p0, formatStrings[ft], strlen(formatStrings[ft]))) { + break; + } + } + if (!formatStrings[ft]) { + break; + } + p0 += strlen(formatStrings[ft]); + if (*p0 != '}') { + break; + } + ++p0; + + // fetch the argument + if (idx > argsLen) { + break; + } + if (idx == argsLen) { + if (argsLen == argsSize) { + argsSize *= 2; + args = (GStringFormatArg *)greallocn(args, argsSize, + sizeof(GStringFormatArg)); + } + switch (ft) { + case fmtIntDecimal: + case fmtIntHex: + case fmtIntOctal: + case fmtIntBinary: + case fmtSpace: + args[argsLen].i = va_arg(argList, int); + break; + case fmtUIntDecimal: + case fmtUIntHex: + case fmtUIntOctal: + case fmtUIntBinary: + args[argsLen].ui = va_arg(argList, Guint); + break; + case fmtLongDecimal: + case fmtLongHex: + case fmtLongOctal: + case fmtLongBinary: + args[argsLen].l = va_arg(argList, long); + break; + case fmtULongDecimal: + case fmtULongHex: + case fmtULongOctal: + case fmtULongBinary: + args[argsLen].ul = va_arg(argList, Gulong); + break; + case fmtDouble: + case fmtDoubleTrim: + args[argsLen].f = va_arg(argList, double); + break; + case fmtChar: + args[argsLen].c = (char)va_arg(argList, int); + break; + case fmtString: + args[argsLen].s = va_arg(argList, char *); + break; + case fmtGString: + args[argsLen].gs = va_arg(argList, GString *); + break; + } + ++argsLen; + } + + // format the argument + arg = args[idx]; + switch (ft) { + case fmtIntDecimal: + formatInt(arg.i, buf, sizeof(buf), zeroFill, width, 10, &str, &len); + break; + case fmtIntHex: + formatInt(arg.i, buf, sizeof(buf), zeroFill, width, 16, &str, &len); + break; + case fmtIntOctal: + formatInt(arg.i, buf, sizeof(buf), zeroFill, width, 8, &str, &len); + break; + case fmtIntBinary: + formatInt(arg.i, buf, sizeof(buf), zeroFill, width, 2, &str, &len); + break; + case fmtUIntDecimal: + formatUInt(arg.ui, buf, sizeof(buf), zeroFill, width, 10, + &str, &len); + break; + case fmtUIntHex: + formatUInt(arg.ui, buf, sizeof(buf), zeroFill, width, 16, + &str, &len); + break; + case fmtUIntOctal: + formatUInt(arg.ui, buf, sizeof(buf), zeroFill, width, 8, &str, &len); + break; + case fmtUIntBinary: + formatUInt(arg.ui, buf, sizeof(buf), zeroFill, width, 2, &str, &len); + break; + case fmtLongDecimal: + formatInt(arg.l, buf, sizeof(buf), zeroFill, width, 10, &str, &len); + break; + case fmtLongHex: + formatInt(arg.l, buf, sizeof(buf), zeroFill, width, 16, &str, &len); + break; + case fmtLongOctal: + formatInt(arg.l, buf, sizeof(buf), zeroFill, width, 8, &str, &len); + break; + case fmtLongBinary: + formatInt(arg.l, buf, sizeof(buf), zeroFill, width, 2, &str, &len); + break; + case fmtULongDecimal: + formatUInt(arg.ul, buf, sizeof(buf), zeroFill, width, 10, + &str, &len); + break; + case fmtULongHex: + formatUInt(arg.ul, buf, sizeof(buf), zeroFill, width, 16, + &str, &len); + break; + case fmtULongOctal: + formatUInt(arg.ul, buf, sizeof(buf), zeroFill, width, 8, &str, &len); + break; + case fmtULongBinary: + formatUInt(arg.ul, buf, sizeof(buf), zeroFill, width, 2, &str, &len); + break; + case fmtDouble: + formatDouble(arg.f, buf, sizeof(buf), prec, gFalse, &str, &len); + break; + case fmtDoubleTrim: + formatDouble(arg.f, buf, sizeof(buf), prec, gTrue, &str, &len); + break; + case fmtChar: + buf[0] = arg.c; + str = buf; + len = 1; + reverseAlign = !reverseAlign; + break; + case fmtString: + str = arg.s; + len = strlen(str); + reverseAlign = !reverseAlign; + break; + case fmtGString: + str = arg.gs->getCString(); + len = arg.gs->getLength(); + reverseAlign = !reverseAlign; + break; + case fmtSpace: + str = buf; + len = 0; + width = arg.i; + break; + } + + // append the formatted arg, handling width and alignment + if (!reverseAlign && len < width) { + for (i = len; i < width; ++i) { + append(' '); + } + } + append(str, len); + if (reverseAlign && len < width) { + for (i = len; i < width; ++i) { + append(' '); + } + } + } + + } else if (*p0 == '}') { + ++p0; + if (*p0 == '}') { + ++p0; + } + append('}'); + + } else { + for (p1 = p0 + 1; *p1 && *p1 != '{' && *p1 != '}'; ++p1) ; + append(p0, p1 - p0); + p0 = p1; + } + } + + gfree(args); + return this; +} + +void GString::formatInt(long x, char *buf, int bufSize, + GBool zeroFill, int width, int base, + char **p, int *len) { + static char vals[17] = "0123456789abcdef"; + GBool neg; + int start, i, j; + + i = bufSize; + if ((neg = x < 0)) { + x = -x; + } + start = neg ? 1 : 0; + if (x == 0) { + buf[--i] = '0'; + } else { + while (i > start && x) { + buf[--i] = vals[x % base]; + x /= base; + } + } + if (zeroFill) { + for (j = bufSize - i; i > start && j < width - start; ++j) { + buf[--i] = '0'; + } + } + if (neg) { + buf[--i] = '-'; + } + *p = buf + i; + *len = bufSize - i; +} + +void GString::formatUInt(Gulong x, char *buf, int bufSize, + GBool zeroFill, int width, int base, + char **p, int *len) { + static char vals[17] = "0123456789abcdef"; + int i, j; + + i = bufSize; + if (x == 0) { + buf[--i] = '0'; + } else { + while (i > 0 && x) { + buf[--i] = vals[x % base]; + x /= base; + } + } + if (zeroFill) { + for (j = bufSize - i; i > 0 && j < width; ++j) { + buf[--i] = '0'; + } + } + *p = buf + i; + *len = bufSize - i; +} + +void GString::formatDouble(double x, char *buf, int bufSize, int prec, + GBool trim, char **p, int *len) { + GBool neg, started; + double x2; + int d, i, j; + + if ((neg = x < 0)) { + x = -x; + } + x = floor(x * pow(10, prec) + 0.5); + i = bufSize; + started = !trim; + for (j = 0; j < prec && i > 1; ++j) { + x2 = floor(0.1 * (x + 0.5)); + d = (int)floor(x - 10 * x2 + 0.5); + if (started || d != 0) { + buf[--i] = '0' + d; + started = gTrue; + } + x = x2; + } + if (i > 1 && started) { + buf[--i] = '.'; + } + if (i > 1) { + do { + x2 = floor(0.1 * (x + 0.5)); + d = (int)floor(x - 10 * x2 + 0.5); + buf[--i] = '0' + d; + x = x2; + } while (i > 1 && x); + } + if (neg) { + buf[--i] = '-'; + } + *p = buf + i; + *len = bufSize - i; +} + +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) { + if (i + n > length) { + n = length - i; + } + 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; +} + +int GString::cmp(GString *str) { + int n1, n2, i, x; + char *p1, *p2; + + n1 = length; + n2 = str->length; + for (i = 0, p1 = s, p2 = str->s; i < n1 && i < n2; ++i, ++p1, ++p2) { + x = *p1 - *p2; + if (x != 0) { + return x; + } + } + return n1 - n2; +} + +int GString::cmpN(GString *str, int n) { + int n1, n2, i, x; + char *p1, *p2; + + n1 = length; + n2 = str->length; + for (i = 0, p1 = s, p2 = str->s; + i < n1 && i < n2 && i < n; + ++i, ++p1, ++p2) { + x = *p1 - *p2; + if (x != 0) { + return x; + } + } + if (i == n) { + return 0; + } + return n1 - n2; +} + +int GString::cmp(const char *sA) { + int n1, i, x; + const char *p1, *p2; + + n1 = length; + for (i = 0, p1 = s, p2 = sA; i < n1 && *p2; ++i, ++p1, ++p2) { + x = *p1 - *p2; + if (x != 0) { + return x; + } + } + if (i < n1) { + return 1; + } + if (*p2) { + return -1; + } + return 0; +} + +int GString::cmpN(const char *sA, int n) { + int n1, i, x; + const char *p1, *p2; + + n1 = length; + for (i = 0, p1 = s, p2 = sA; i < n1 && *p2 && i < n; ++i, ++p1, ++p2) { + x = *p1 - *p2; + if (x != 0) { + return x; + } + } + if (i == n) { + return 0; + } + if (i < n1) { + return 1; + } + if (*p2) { + return -1; + } + return 0; +} diff --git a/kpdf/xpdf/goo/Makefile.am b/kpdf/xpdf/goo/Makefile.am index 04933cee..00ffad0d 100644 --- a/kpdf/xpdf/goo/Makefile.am +++ b/kpdf/xpdf/goo/Makefile.am @@ -1,5 +1,5 @@ INCLUDES = -I$(srcdir)/.. -libgoo_la_SOURCES = GHash.cc GList.cc GString.cc gfile.cc gmem.cc gmempp.cc +libgoo_la_SOURCES = GHash.cpp GList.cpp GString.cpp gfile.cpp gmem.cpp gmempp.cpp noinst_LTLIBRARIES = libgoo.la diff --git a/kpdf/xpdf/goo/gfile.cc b/kpdf/xpdf/goo/gfile.cc deleted file mode 100644 index 54a7be3d..00000000 --- a/kpdf/xpdf/goo/gfile.cc +++ /dev/null @@ -1,731 +0,0 @@ -//======================================================================== -// -// gfile.cc -// -// Miscellaneous file and directory name manipulation. -// -// Copyright 1996-2003 Glyph & Cog, LLC -// -//======================================================================== - -#include - -#ifdef WIN32 -# include -#else -# if defined(MACOS) -# include -# elif !defined(ACORN) -# include -# include -# include -# endif -# include -# include -# if !defined(VMS) && !defined(ACORN) && !defined(MACOS) -# include -# endif -# if defined(VMS) && (__DECCXX_VER < 50200000) -# include -# endif -#endif // WIN32 -#include "GString.h" -#include "gfile.h" - -// Some systems don't define this, so just make it something reasonably -// large. -#ifndef PATH_MAX -#define PATH_MAX 1024 -#endif - -//------------------------------------------------------------------------ - -GString *getHomeDir() { -#ifdef VMS - //---------- VMS ---------- - return new GString("SYS$LOGIN:"); - -#elif defined(__EMX__) || defined(WIN32) - //---------- OS/2+EMX and Win32 ---------- - char *s; - GString *ret; - - if ((s = getenv("HOME"))) - ret = new GString(s); - else - ret = new GString("."); - return ret; - -#elif defined(ACORN) - //---------- RISCOS ---------- - return new GString("@"); - -#elif defined(MACOS) - //---------- MacOS ---------- - return new GString(":"); - -#else - //---------- Unix ---------- - char *s; - struct passwd *pw; - GString *ret; - - if ((s = getenv("HOME"))) { - ret = new GString(s); - } else { - if ((s = getenv("USER"))) - pw = getpwnam(s); - else - pw = getpwuid(getuid()); - if (pw) - ret = new GString(pw->pw_dir); - else - ret = new GString("."); - } - return ret; -#endif -} - -GString *getCurrentDir() { - char buf[PATH_MAX+1]; - -#if defined(__EMX__) - if (_getcwd2(buf, sizeof(buf))) -#elif defined(WIN32) - if (GetCurrentDirectory(sizeof(buf), buf)) -#elif defined(ACORN) - if (strcpy(buf, "@")) -#elif defined(MACOS) - if (strcpy(buf, ":")) -#else - if (getcwd(buf, sizeof(buf))) -#endif - return new GString(buf); - return new GString(); -} - -GString *appendToPath(GString *path, char *fileName) { -#if defined(VMS) - //---------- VMS ---------- - //~ this should handle everything necessary for file - //~ requesters, but it's certainly not complete - char *p0, *p1, *p2; - char *q1; - - p0 = path->getCString(); - p1 = p0 + path->getLength() - 1; - if (!strcmp(fileName, "-")) { - if (*p1 == ']') { - for (p2 = p1; p2 > p0 && *p2 != '.' && *p2 != '['; --p2) ; - if (*p2 == '[') - ++p2; - path->del(p2 - p0, p1 - p2); - } else if (*p1 == ':') { - path->append("[-]"); - } else { - path->clear(); - path->append("[-]"); - } - } else if ((q1 = strrchr(fileName, '.')) && !strncmp(q1, ".DIR;", 5)) { - if (*p1 == ']') { - path->insert(p1 - p0, '.'); - path->insert(p1 - p0 + 1, fileName, q1 - fileName); - } else if (*p1 == ':') { - path->append('['); - path->append(']'); - path->append(fileName, q1 - fileName); - } else { - path->clear(); - path->append(fileName, q1 - fileName); - } - } else { - if (*p1 != ']' && *p1 != ':') - path->clear(); - path->append(fileName); - } - return path; - -#elif defined(WIN32) - //---------- Win32 ---------- - GString *tmp; - char buf[256]; - char *fp; - - tmp = new GString(path); - tmp->append('/'); - tmp->append(fileName); - GetFullPathName(tmp->getCString(), sizeof(buf), buf, &fp); - delete tmp; - path->clear(); - path->append(buf); - return path; - -#elif defined(ACORN) - //---------- RISCOS ---------- - char *p; - int i; - - path->append("."); - i = path->getLength(); - path->append(fileName); - for (p = path->getCString() + i; *p; ++p) { - if (*p == '/') { - *p = '.'; - } else if (*p == '.') { - *p = '/'; - } - } - return path; - -#elif defined(MACOS) - //---------- MacOS ---------- - char *p; - int i; - - path->append(":"); - i = path->getLength(); - path->append(fileName); - for (p = path->getCString() + i; *p; ++p) { - if (*p == '/') { - *p = ':'; - } else if (*p == '.') { - *p = ':'; - } - } - return path; - -#elif defined(__EMX__) - //---------- OS/2+EMX ---------- - int i; - - // appending "." does nothing - if (!strcmp(fileName, ".")) - return path; - - // appending ".." goes up one directory - if (!strcmp(fileName, "..")) { - for (i = path->getLength() - 2; i >= 0; --i) { - if (path->getChar(i) == '/' || path->getChar(i) == '\\' || - path->getChar(i) == ':') - break; - } - if (i <= 0) { - if (path->getChar(0) == '/' || path->getChar(0) == '\\') { - path->del(1, path->getLength() - 1); - } else if (path->getLength() >= 2 && path->getChar(1) == ':') { - path->del(2, path->getLength() - 2); - } else { - path->clear(); - path->append(".."); - } - } else { - if (path->getChar(i-1) == ':') - ++i; - path->del(i, path->getLength() - i); - } - return path; - } - - // otherwise, append "/" and new path component - if (path->getLength() > 0 && - path->getChar(path->getLength() - 1) != '/' && - path->getChar(path->getLength() - 1) != '\\') - path->append('/'); - path->append(fileName); - return path; - -#else - //---------- Unix ---------- - int i; - - // appending "." does nothing - if (!strcmp(fileName, ".")) - return path; - - // appending ".." goes up one directory - if (!strcmp(fileName, "..")) { - for (i = path->getLength() - 2; i >= 0; --i) { - if (path->getChar(i) == '/') - break; - } - if (i <= 0) { - if (path->getChar(0) == '/') { - path->del(1, path->getLength() - 1); - } else { - path->clear(); - path->append(".."); - } - } else { - path->del(i, path->getLength() - i); - } - return path; - } - - // otherwise, append "/" and new path component - if (path->getLength() > 0 && - path->getChar(path->getLength() - 1) != '/') - path->append('/'); - path->append(fileName); - return path; -#endif -} - -GString *grabPath(char *fileName) { -#ifdef VMS - //---------- VMS ---------- - char *p; - - if ((p = strrchr(fileName, ']'))) - return new GString(fileName, p + 1 - fileName); - if ((p = strrchr(fileName, ':'))) - return new GString(fileName, p + 1 - fileName); - return new GString(); - -#elif defined(__EMX__) || defined(WIN32) - //---------- OS/2+EMX and Win32 ---------- - char *p; - - if ((p = strrchr(fileName, '/'))) - return new GString(fileName, p - fileName); - if ((p = strrchr(fileName, '\\'))) - return new GString(fileName, p - fileName); - if ((p = strrchr(fileName, ':'))) - return new GString(fileName, p + 1 - fileName); - return new GString(); - -#elif defined(ACORN) - //---------- RISCOS ---------- - char *p; - - if ((p = strrchr(fileName, '.'))) - return new GString(fileName, p - fileName); - return new GString(); - -#elif defined(MACOS) - //---------- MacOS ---------- - char *p; - - if ((p = strrchr(fileName, ':'))) - return new GString(fileName, p - fileName); - return new GString(); - -#else - //---------- Unix ---------- - char *p; - - if ((p = strrchr(fileName, '/'))) - return new GString(fileName, p - fileName); - return new GString(); -#endif -} - -GBool isAbsolutePath(char *path) { -#ifdef VMS - //---------- VMS ---------- - return strchr(path, ':') || - (path[0] == '[' && path[1] != '.' && path[1] != '-'); - -#elif defined(__EMX__) || defined(WIN32) - //---------- OS/2+EMX and Win32 ---------- - return path[0] == '/' || path[0] == '\\' || path[1] == ':'; - -#elif defined(ACORN) - //---------- RISCOS ---------- - return path[0] == '$'; - -#elif defined(MACOS) - //---------- MacOS ---------- - return path[0] != ':'; - -#else - //---------- Unix ---------- - return path[0] == '/'; -#endif -} - -GString *makePathAbsolute(GString *path) { -#ifdef VMS - //---------- VMS ---------- - char buf[PATH_MAX+1]; - - if (!isAbsolutePath(path->getCString())) { - if (getcwd(buf, sizeof(buf))) { - path->insert(0, buf); - } - } - return path; - -#elif defined(WIN32) - //---------- Win32 ---------- - char buf[_MAX_PATH]; - char *fp; - - buf[0] = '\0'; - if (!GetFullPathName(path->getCString(), _MAX_PATH, buf, &fp)) { - path->clear(); - return path; - } - path->clear(); - path->append(buf); - return path; - -#elif defined(ACORN) - //---------- RISCOS ---------- - path->insert(0, '@'); - return path; - -#elif defined(MACOS) - //---------- MacOS ---------- - path->del(0, 1); - return path; - -#else - //---------- Unix and OS/2+EMX ---------- - struct passwd *pw; - char buf[PATH_MAX+1]; - GString *s; - char *p1, *p2; - int n; - - if (path->getChar(0) == '~') { - if (path->getChar(1) == '/' || -#ifdef __EMX__ - path->getChar(1) == '\\' || -#endif - path->getLength() == 1) { - path->del(0, 1); - s = getHomeDir(); - path->insert(0, s); - delete s; - } else { - p1 = path->getCString() + 1; -#ifdef __EMX__ - for (p2 = p1; *p2 && *p2 != '/' && *p2 != '\\'; ++p2) ; -#else - for (p2 = p1; *p2 && *p2 != '/'; ++p2) ; -#endif - if ((n = p2 - p1) > PATH_MAX) - n = PATH_MAX; - strncpy(buf, p1, n); - buf[n] = '\0'; - if ((pw = getpwnam(buf))) { - path->del(0, p2 - p1 + 1); - path->insert(0, pw->pw_dir); - } - } - } else if (!isAbsolutePath(path->getCString())) { - if (getcwd(buf, sizeof(buf))) { -#ifndef __EMX__ - path->insert(0, '/'); -#endif - path->insert(0, buf); - } - } - return path; -#endif -} - -time_t getModTime(char *fileName) { -#ifdef WIN32 - //~ should implement this, but it's (currently) only used in xpdf - return 0; -#else - struct stat statBuf; - - if (stat(fileName, &statBuf)) { - return 0; - } - return statBuf.st_mtime; -#endif -} - -GBool openTempFile(GString **name, FILE **f, char *mode, char *ext) { -#if defined(WIN32) - //---------- Win32 ---------- - char *tempDir; - GString *s, *s2; - char buf[32]; - FILE *f2; - int t, i; - - // this has the standard race condition problem, but I haven't found - // a better way to generate temp file names with extensions on - // Windows - if ((tempDir = getenv("TEMP"))) { - s = new GString(tempDir); - s->append('\\'); - } else { - s = new GString(); - } - s->append("x"); - t = (int)time(NULL); - for (i = 0; i < 1000; ++i) { - sprintf(buf, "%d", t + i); - s2 = s->copy()->append(buf); - if (ext) { - s2->append(ext); - } - if (!(f2 = fopen(s2->getCString(), "r"))) { - if (!(f2 = fopen(s2->getCString(), mode))) { - delete s2; - delete s; - return gFalse; - } - *name = s2; - *f = f2; - delete s; - return gTrue; - } - fclose(f2); - delete s2; - } - delete s; - return gFalse; -#elif defined(VMS) || defined(__EMX__) || defined(ACORN) || defined(MACOS) - //---------- non-Unix ---------- - char *s; - - // There is a security hole here: an attacker can create a symlink - // with this file name after the tmpnam call and before the fopen - // call. I will happily accept fixes to this function for non-Unix - // OSs. - if (!(s = tmpnam(NULL))) { - return gFalse; - } - *name = new GString(s); - if (ext) { - (*name)->append(ext); - } - if (!(*f = fopen((*name)->getCString(), mode))) { - delete (*name); - return gFalse; - } - return gTrue; -#else - //---------- Unix ---------- - char *s; - int fd; - - if (ext) { -#if HAVE_MKSTEMPS - if ((s = getenv("TMPDIR"))) { - *name = new GString(s); - } else { - *name = new GString("/tmp"); - } - (*name)->append("/XXXXXX")->append(ext); - fd = mkstemps((*name)->getCString(), strlen(ext)); -#else - if (!(s = tmpnam(NULL))) { - return gFalse; - } - *name = new GString(s); - (*name)->append(ext); - fd = open((*name)->getCString(), O_WRONLY | O_CREAT | O_EXCL, 0600); -#endif - } else { -#if HAVE_MKSTEMP - if ((s = getenv("TMPDIR"))) { - *name = new GString(s); - } else { - *name = new GString("/tmp"); - } - (*name)->append("/XXXXXX"); - fd = mkstemp((*name)->getCString()); -#else // HAVE_MKSTEMP - if (!(s = tmpnam(NULL))) { - return gFalse; - } - *name = new GString(s); - fd = open((*name)->getCString(), O_WRONLY | O_CREAT | O_EXCL, 0600); -#endif // HAVE_MKSTEMP - } - if (fd < 0 || !(*f = fdopen(fd, mode))) { - delete *name; - return gFalse; - } - return gTrue; -#endif -} - -GBool executeCommand(char *cmd) { -#ifdef VMS - return system(cmd) ? gTrue : gFalse; -#else - return system(cmd) ? gFalse : gTrue; -#endif -} - -char *getLine(char *buf, int size, FILE *f) { - int c, i; - - i = 0; - while (i < size - 1) { - if ((c = fgetc(f)) == EOF) { - break; - } - buf[i++] = (char)c; - if (c == '\x0a') { - break; - } - if (c == '\x0d') { - c = fgetc(f); - if (c == '\x0a' && i < size - 1) { - buf[i++] = (char)c; - } else if (c != EOF) { - ungetc(c, f); - } - break; - } - } - buf[i] = '\0'; - if (i == 0) { - return NULL; - } - return buf; -} - -//------------------------------------------------------------------------ -// GDir and GDirEntry -//------------------------------------------------------------------------ - -GDirEntry::GDirEntry(char *dirPath, char *nameA, GBool doStat) { -#ifdef VMS - char *p; -#elif defined(WIN32) - int fa; - GString *s; -#elif defined(ACORN) -#else - struct stat st; - GString *s; -#endif - - name = new GString(nameA); - dir = gFalse; - if (doStat) { -#ifdef VMS - if (!strcmp(nameA, "-") || - ((p = strrchr(nameA, '.')) && !strncmp(p, ".DIR;", 5))) - dir = gTrue; -#elif defined(ACORN) -#else - s = new GString(dirPath); - appendToPath(s, nameA); -#ifdef WIN32 - fa = GetFileAttributes(s->getCString()); - dir = (fa != 0xFFFFFFFF && (fa & FILE_ATTRIBUTE_DIRECTORY)); -#else - if (stat(s->getCString(), &st) == 0) - dir = S_ISDIR(st.st_mode); -#endif - delete s; -#endif - } -} - -GDirEntry::~GDirEntry() { - delete name; -} - -GDir::GDir(char *name, GBool doStatA) { - path = new GString(name); - doStat = doStatA; -#if defined(WIN32) - GString *tmp; - - tmp = path->copy(); - tmp->append("/*.*"); - hnd = FindFirstFile(tmp->getCString(), &ffd); - delete tmp; -#elif defined(ACORN) -#elif defined(MACOS) -#else - dir = opendir(name); -#ifdef VMS - needParent = strchr(name, '[') != NULL; -#endif -#endif -} - -GDir::~GDir() { - delete path; -#if defined(WIN32) - if (hnd) { - FindClose(hnd); - hnd = NULL; - } -#elif defined(ACORN) -#elif defined(MACOS) -#else - if (dir) - closedir(dir); -#endif -} - -GDirEntry *GDir::getNextEntry() { - GDirEntry *e; - -#if defined(WIN32) - if (hnd) { - e = new GDirEntry(path->getCString(), ffd.cFileName, doStat); - if (hnd && !FindNextFile(hnd, &ffd)) { - FindClose(hnd); - hnd = NULL; - } - } else { - e = NULL; - } -#elif defined(ACORN) -#elif defined(MACOS) -#elif defined(VMS) - struct dirent *ent; - e = NULL; - if (dir) { - if (needParent) { - e = new GDirEntry(path->getCString(), "-", doStat); - needParent = gFalse; - return e; - } - ent = readdir(dir); - if (ent) { - e = new GDirEntry(path->getCString(), ent->d_name, doStat); - } - } -#else - struct dirent *ent; - e = NULL; - if (dir) { - ent = (struct dirent *)readdir(dir); - if (ent && !strcmp(ent->d_name, ".")) { - ent = (struct dirent *)readdir(dir); - } - if (ent) { - e = new GDirEntry(path->getCString(), ent->d_name, doStat); - } - } -#endif - - return e; -} - -void GDir::rewind() { -#ifdef WIN32 - GString *tmp; - - if (hnd) - FindClose(hnd); - tmp = path->copy(); - tmp->append("/*.*"); - hnd = FindFirstFile(tmp->getCString(), &ffd); - delete tmp; -#elif defined(ACORN) -#elif defined(MACOS) -#else - if (dir) - rewinddir(dir); -#ifdef VMS - needParent = strchr(path->getCString(), '[') != NULL; -#endif -#endif -} diff --git a/kpdf/xpdf/goo/gfile.cpp b/kpdf/xpdf/goo/gfile.cpp new file mode 100644 index 00000000..bb75b855 --- /dev/null +++ b/kpdf/xpdf/goo/gfile.cpp @@ -0,0 +1,731 @@ +//======================================================================== +// +// gfile.cpp +// +// Miscellaneous file and directory name manipulation. +// +// Copyright 1996-2003 Glyph & Cog, LLC +// +//======================================================================== + +#include + +#ifdef WIN32 +# include +#else +# if defined(MACOS) +# include +# elif !defined(ACORN) +# include +# include +# include +# endif +# include +# include +# if !defined(VMS) && !defined(ACORN) && !defined(MACOS) +# include +# endif +# if defined(VMS) && (__DECCXX_VER < 50200000) +# include +# endif +#endif // WIN32 +#include "GString.h" +#include "gfile.h" + +// Some systems don't define this, so just make it something reasonably +// large. +#ifndef PATH_MAX +#define PATH_MAX 1024 +#endif + +//------------------------------------------------------------------------ + +GString *getHomeDir() { +#ifdef VMS + //---------- VMS ---------- + return new GString("SYS$LOGIN:"); + +#elif defined(__EMX__) || defined(WIN32) + //---------- OS/2+EMX and Win32 ---------- + char *s; + GString *ret; + + if ((s = getenv("HOME"))) + ret = new GString(s); + else + ret = new GString("."); + return ret; + +#elif defined(ACORN) + //---------- RISCOS ---------- + return new GString("@"); + +#elif defined(MACOS) + //---------- MacOS ---------- + return new GString(":"); + +#else + //---------- Unix ---------- + char *s; + struct passwd *pw; + GString *ret; + + if ((s = getenv("HOME"))) { + ret = new GString(s); + } else { + if ((s = getenv("USER"))) + pw = getpwnam(s); + else + pw = getpwuid(getuid()); + if (pw) + ret = new GString(pw->pw_dir); + else + ret = new GString("."); + } + return ret; +#endif +} + +GString *getCurrentDir() { + char buf[PATH_MAX+1]; + +#if defined(__EMX__) + if (_getcwd2(buf, sizeof(buf))) +#elif defined(WIN32) + if (GetCurrentDirectory(sizeof(buf), buf)) +#elif defined(ACORN) + if (strcpy(buf, "@")) +#elif defined(MACOS) + if (strcpy(buf, ":")) +#else + if (getcwd(buf, sizeof(buf))) +#endif + return new GString(buf); + return new GString(); +} + +GString *appendToPath(GString *path, char *fileName) { +#if defined(VMS) + //---------- VMS ---------- + //~ this should handle everything necessary for file + //~ requesters, but it's certainly not complete + char *p0, *p1, *p2; + char *q1; + + p0 = path->getCString(); + p1 = p0 + path->getLength() - 1; + if (!strcmp(fileName, "-")) { + if (*p1 == ']') { + for (p2 = p1; p2 > p0 && *p2 != '.' && *p2 != '['; --p2) ; + if (*p2 == '[') + ++p2; + path->del(p2 - p0, p1 - p2); + } else if (*p1 == ':') { + path->append("[-]"); + } else { + path->clear(); + path->append("[-]"); + } + } else if ((q1 = strrchr(fileName, '.')) && !strncmp(q1, ".DIR;", 5)) { + if (*p1 == ']') { + path->insert(p1 - p0, '.'); + path->insert(p1 - p0 + 1, fileName, q1 - fileName); + } else if (*p1 == ':') { + path->append('['); + path->append(']'); + path->append(fileName, q1 - fileName); + } else { + path->clear(); + path->append(fileName, q1 - fileName); + } + } else { + if (*p1 != ']' && *p1 != ':') + path->clear(); + path->append(fileName); + } + return path; + +#elif defined(WIN32) + //---------- Win32 ---------- + GString *tmp; + char buf[256]; + char *fp; + + tmp = new GString(path); + tmp->append('/'); + tmp->append(fileName); + GetFullPathName(tmp->getCString(), sizeof(buf), buf, &fp); + delete tmp; + path->clear(); + path->append(buf); + return path; + +#elif defined(ACORN) + //---------- RISCOS ---------- + char *p; + int i; + + path->append("."); + i = path->getLength(); + path->append(fileName); + for (p = path->getCString() + i; *p; ++p) { + if (*p == '/') { + *p = '.'; + } else if (*p == '.') { + *p = '/'; + } + } + return path; + +#elif defined(MACOS) + //---------- MacOS ---------- + char *p; + int i; + + path->append(":"); + i = path->getLength(); + path->append(fileName); + for (p = path->getCString() + i; *p; ++p) { + if (*p == '/') { + *p = ':'; + } else if (*p == '.') { + *p = ':'; + } + } + return path; + +#elif defined(__EMX__) + //---------- OS/2+EMX ---------- + int i; + + // appending "." does nothing + if (!strcmp(fileName, ".")) + return path; + + // appending ".." goes up one directory + if (!strcmp(fileName, "..")) { + for (i = path->getLength() - 2; i >= 0; --i) { + if (path->getChar(i) == '/' || path->getChar(i) == '\\' || + path->getChar(i) == ':') + break; + } + if (i <= 0) { + if (path->getChar(0) == '/' || path->getChar(0) == '\\') { + path->del(1, path->getLength() - 1); + } else if (path->getLength() >= 2 && path->getChar(1) == ':') { + path->del(2, path->getLength() - 2); + } else { + path->clear(); + path->append(".."); + } + } else { + if (path->getChar(i-1) == ':') + ++i; + path->del(i, path->getLength() - i); + } + return path; + } + + // otherwise, append "/" and new path component + if (path->getLength() > 0 && + path->getChar(path->getLength() - 1) != '/' && + path->getChar(path->getLength() - 1) != '\\') + path->append('/'); + path->append(fileName); + return path; + +#else + //---------- Unix ---------- + int i; + + // appending "." does nothing + if (!strcmp(fileName, ".")) + return path; + + // appending ".." goes up one directory + if (!strcmp(fileName, "..")) { + for (i = path->getLength() - 2; i >= 0; --i) { + if (path->getChar(i) == '/') + break; + } + if (i <= 0) { + if (path->getChar(0) == '/') { + path->del(1, path->getLength() - 1); + } else { + path->clear(); + path->append(".."); + } + } else { + path->del(i, path->getLength() - i); + } + return path; + } + + // otherwise, append "/" and new path component + if (path->getLength() > 0 && + path->getChar(path->getLength() - 1) != '/') + path->append('/'); + path->append(fileName); + return path; +#endif +} + +GString *grabPath(char *fileName) { +#ifdef VMS + //---------- VMS ---------- + char *p; + + if ((p = strrchr(fileName, ']'))) + return new GString(fileName, p + 1 - fileName); + if ((p = strrchr(fileName, ':'))) + return new GString(fileName, p + 1 - fileName); + return new GString(); + +#elif defined(__EMX__) || defined(WIN32) + //---------- OS/2+EMX and Win32 ---------- + char *p; + + if ((p = strrchr(fileName, '/'))) + return new GString(fileName, p - fileName); + if ((p = strrchr(fileName, '\\'))) + return new GString(fileName, p - fileName); + if ((p = strrchr(fileName, ':'))) + return new GString(fileName, p + 1 - fileName); + return new GString(); + +#elif defined(ACORN) + //---------- RISCOS ---------- + char *p; + + if ((p = strrchr(fileName, '.'))) + return new GString(fileName, p - fileName); + return new GString(); + +#elif defined(MACOS) + //---------- MacOS ---------- + char *p; + + if ((p = strrchr(fileName, ':'))) + return new GString(fileName, p - fileName); + return new GString(); + +#else + //---------- Unix ---------- + char *p; + + if ((p = strrchr(fileName, '/'))) + return new GString(fileName, p - fileName); + return new GString(); +#endif +} + +GBool isAbsolutePath(char *path) { +#ifdef VMS + //---------- VMS ---------- + return strchr(path, ':') || + (path[0] == '[' && path[1] != '.' && path[1] != '-'); + +#elif defined(__EMX__) || defined(WIN32) + //---------- OS/2+EMX and Win32 ---------- + return path[0] == '/' || path[0] == '\\' || path[1] == ':'; + +#elif defined(ACORN) + //---------- RISCOS ---------- + return path[0] == '$'; + +#elif defined(MACOS) + //---------- MacOS ---------- + return path[0] != ':'; + +#else + //---------- Unix ---------- + return path[0] == '/'; +#endif +} + +GString *makePathAbsolute(GString *path) { +#ifdef VMS + //---------- VMS ---------- + char buf[PATH_MAX+1]; + + if (!isAbsolutePath(path->getCString())) { + if (getcwd(buf, sizeof(buf))) { + path->insert(0, buf); + } + } + return path; + +#elif defined(WIN32) + //---------- Win32 ---------- + char buf[_MAX_PATH]; + char *fp; + + buf[0] = '\0'; + if (!GetFullPathName(path->getCString(), _MAX_PATH, buf, &fp)) { + path->clear(); + return path; + } + path->clear(); + path->append(buf); + return path; + +#elif defined(ACORN) + //---------- RISCOS ---------- + path->insert(0, '@'); + return path; + +#elif defined(MACOS) + //---------- MacOS ---------- + path->del(0, 1); + return path; + +#else + //---------- Unix and OS/2+EMX ---------- + struct passwd *pw; + char buf[PATH_MAX+1]; + GString *s; + char *p1, *p2; + int n; + + if (path->getChar(0) == '~') { + if (path->getChar(1) == '/' || +#ifdef __EMX__ + path->getChar(1) == '\\' || +#endif + path->getLength() == 1) { + path->del(0, 1); + s = getHomeDir(); + path->insert(0, s); + delete s; + } else { + p1 = path->getCString() + 1; +#ifdef __EMX__ + for (p2 = p1; *p2 && *p2 != '/' && *p2 != '\\'; ++p2) ; +#else + for (p2 = p1; *p2 && *p2 != '/'; ++p2) ; +#endif + if ((n = p2 - p1) > PATH_MAX) + n = PATH_MAX; + strncpy(buf, p1, n); + buf[n] = '\0'; + if ((pw = getpwnam(buf))) { + path->del(0, p2 - p1 + 1); + path->insert(0, pw->pw_dir); + } + } + } else if (!isAbsolutePath(path->getCString())) { + if (getcwd(buf, sizeof(buf))) { +#ifndef __EMX__ + path->insert(0, '/'); +#endif + path->insert(0, buf); + } + } + return path; +#endif +} + +time_t getModTime(char *fileName) { +#ifdef WIN32 + //~ should implement this, but it's (currently) only used in xpdf + return 0; +#else + struct stat statBuf; + + if (stat(fileName, &statBuf)) { + return 0; + } + return statBuf.st_mtime; +#endif +} + +GBool openTempFile(GString **name, FILE **f, char *mode, char *ext) { +#if defined(WIN32) + //---------- Win32 ---------- + char *tempDir; + GString *s, *s2; + char buf[32]; + FILE *f2; + int t, i; + + // this has the standard race condition problem, but I haven't found + // a better way to generate temp file names with extensions on + // Windows + if ((tempDir = getenv("TEMP"))) { + s = new GString(tempDir); + s->append('\\'); + } else { + s = new GString(); + } + s->append("x"); + t = (int)time(NULL); + for (i = 0; i < 1000; ++i) { + sprintf(buf, "%d", t + i); + s2 = s->copy()->append(buf); + if (ext) { + s2->append(ext); + } + if (!(f2 = fopen(s2->getCString(), "r"))) { + if (!(f2 = fopen(s2->getCString(), mode))) { + delete s2; + delete s; + return gFalse; + } + *name = s2; + *f = f2; + delete s; + return gTrue; + } + fclose(f2); + delete s2; + } + delete s; + return gFalse; +#elif defined(VMS) || defined(__EMX__) || defined(ACORN) || defined(MACOS) + //---------- non-Unix ---------- + char *s; + + // There is a security hole here: an attacker can create a symlink + // with this file name after the tmpnam call and before the fopen + // call. I will happily accept fixes to this function for non-Unix + // OSs. + if (!(s = tmpnam(NULL))) { + return gFalse; + } + *name = new GString(s); + if (ext) { + (*name)->append(ext); + } + if (!(*f = fopen((*name)->getCString(), mode))) { + delete (*name); + return gFalse; + } + return gTrue; +#else + //---------- Unix ---------- + char *s; + int fd; + + if (ext) { +#if HAVE_MKSTEMPS + if ((s = getenv("TMPDIR"))) { + *name = new GString(s); + } else { + *name = new GString("/tmp"); + } + (*name)->append("/XXXXXX")->append(ext); + fd = mkstemps((*name)->getCString(), strlen(ext)); +#else + if (!(s = tmpnam(NULL))) { + return gFalse; + } + *name = new GString(s); + (*name)->append(ext); + fd = open((*name)->getCString(), O_WRONLY | O_CREAT | O_EXCL, 0600); +#endif + } else { +#if HAVE_MKSTEMP + if ((s = getenv("TMPDIR"))) { + *name = new GString(s); + } else { + *name = new GString("/tmp"); + } + (*name)->append("/XXXXXX"); + fd = mkstemp((*name)->getCString()); +#else // HAVE_MKSTEMP + if (!(s = tmpnam(NULL))) { + return gFalse; + } + *name = new GString(s); + fd = open((*name)->getCString(), O_WRONLY | O_CREAT | O_EXCL, 0600); +#endif // HAVE_MKSTEMP + } + if (fd < 0 || !(*f = fdopen(fd, mode))) { + delete *name; + return gFalse; + } + return gTrue; +#endif +} + +GBool executeCommand(char *cmd) { +#ifdef VMS + return system(cmd) ? gTrue : gFalse; +#else + return system(cmd) ? gFalse : gTrue; +#endif +} + +char *getLine(char *buf, int size, FILE *f) { + int c, i; + + i = 0; + while (i < size - 1) { + if ((c = fgetc(f)) == EOF) { + break; + } + buf[i++] = (char)c; + if (c == '\x0a') { + break; + } + if (c == '\x0d') { + c = fgetc(f); + if (c == '\x0a' && i < size - 1) { + buf[i++] = (char)c; + } else if (c != EOF) { + ungetc(c, f); + } + break; + } + } + buf[i] = '\0'; + if (i == 0) { + return NULL; + } + return buf; +} + +//------------------------------------------------------------------------ +// GDir and GDirEntry +//------------------------------------------------------------------------ + +GDirEntry::GDirEntry(char *dirPath, char *nameA, GBool doStat) { +#ifdef VMS + char *p; +#elif defined(WIN32) + int fa; + GString *s; +#elif defined(ACORN) +#else + struct stat st; + GString *s; +#endif + + name = new GString(nameA); + dir = gFalse; + if (doStat) { +#ifdef VMS + if (!strcmp(nameA, "-") || + ((p = strrchr(nameA, '.')) && !strncmp(p, ".DIR;", 5))) + dir = gTrue; +#elif defined(ACORN) +#else + s = new GString(dirPath); + appendToPath(s, nameA); +#ifdef WIN32 + fa = GetFileAttributes(s->getCString()); + dir = (fa != 0xFFFFFFFF && (fa & FILE_ATTRIBUTE_DIRECTORY)); +#else + if (stat(s->getCString(), &st) == 0) + dir = S_ISDIR(st.st_mode); +#endif + delete s; +#endif + } +} + +GDirEntry::~GDirEntry() { + delete name; +} + +GDir::GDir(char *name, GBool doStatA) { + path = new GString(name); + doStat = doStatA; +#if defined(WIN32) + GString *tmp; + + tmp = path->copy(); + tmp->append("/*.*"); + hnd = FindFirstFile(tmp->getCString(), &ffd); + delete tmp; +#elif defined(ACORN) +#elif defined(MACOS) +#else + dir = opendir(name); +#ifdef VMS + needParent = strchr(name, '[') != NULL; +#endif +#endif +} + +GDir::~GDir() { + delete path; +#if defined(WIN32) + if (hnd) { + FindClose(hnd); + hnd = NULL; + } +#elif defined(ACORN) +#elif defined(MACOS) +#else + if (dir) + closedir(dir); +#endif +} + +GDirEntry *GDir::getNextEntry() { + GDirEntry *e; + +#if defined(WIN32) + if (hnd) { + e = new GDirEntry(path->getCString(), ffd.cFileName, doStat); + if (hnd && !FindNextFile(hnd, &ffd)) { + FindClose(hnd); + hnd = NULL; + } + } else { + e = NULL; + } +#elif defined(ACORN) +#elif defined(MACOS) +#elif defined(VMS) + struct dirent *ent; + e = NULL; + if (dir) { + if (needParent) { + e = new GDirEntry(path->getCString(), "-", doStat); + needParent = gFalse; + return e; + } + ent = readdir(dir); + if (ent) { + e = new GDirEntry(path->getCString(), ent->d_name, doStat); + } + } +#else + struct dirent *ent; + e = NULL; + if (dir) { + ent = (struct dirent *)readdir(dir); + if (ent && !strcmp(ent->d_name, ".")) { + ent = (struct dirent *)readdir(dir); + } + if (ent) { + e = new GDirEntry(path->getCString(), ent->d_name, doStat); + } + } +#endif + + return e; +} + +void GDir::rewind() { +#ifdef WIN32 + GString *tmp; + + if (hnd) + FindClose(hnd); + tmp = path->copy(); + tmp->append("/*.*"); + hnd = FindFirstFile(tmp->getCString(), &ffd); + delete tmp; +#elif defined(ACORN) +#elif defined(MACOS) +#else + if (dir) + rewinddir(dir); +#ifdef VMS + needParent = strchr(path->getCString(), '[') != NULL; +#endif +#endif +} diff --git a/kpdf/xpdf/goo/gmem.cc b/kpdf/xpdf/goo/gmem.cc deleted file mode 100644 index d8962aba..00000000 --- a/kpdf/xpdf/goo/gmem.cc +++ /dev/null @@ -1,315 +0,0 @@ -/* - * gmem.c - * - * Memory routines with out-of-memory checking. - * - * Copyright 1996-2003 Glyph & Cog, LLC - */ - -#include -#include -#include -#include -#include -#include -#include "gmem.h" - -#ifdef DEBUG_MEM - -typedef struct _GMemHdr { - unsigned int magic; - int size; - int index; - struct _GMemHdr *next, *prev; -} GMemHdr; - -#define gMemHdrSize ((sizeof(GMemHdr) + 7) & ~7) -#define gMemTrlSize (sizeof(long)) - -#define gMemMagic 0xabcd9999 - -#if gmemTrlSize==8 -#define gMemDeadVal 0xdeadbeefdeadbeefUL -#else -#define gMemDeadVal 0xdeadbeefUL -#endif - -/* round data size so trailer will be aligned */ -#define gMemDataSize(size) \ - ((((size) + gMemTrlSize - 1) / gMemTrlSize) * gMemTrlSize) - -static GMemHdr *gMemHead = NULL; -static GMemHdr *gMemTail = NULL; - -static int gMemIndex = 0; -static int gMemAlloc = 0; -static int gMemInUse = 0; - -#endif /* DEBUG_MEM */ - -void *gmalloc(int size) GMEM_EXCEP { -#ifdef DEBUG_MEM - int size1; - char *mem; - GMemHdr *hdr; - void *data; - unsigned long *trl, *p; - - if (size < 0) { -#if USE_EXCEPTIONS - throw GMemException(); -#else - fprintf(stderr, "Invalid memory allocation size\n"); - exit(1); -#endif - } - if (size == 0) { - return NULL; - } - size1 = gMemDataSize(size); - if (!(mem = (char *)malloc(size1 + gMemHdrSize + gMemTrlSize))) { -#if USE_EXCEPTIONS - throw GMemException(); -#else - fprintf(stderr, "Out of memory\n"); - exit(1); -#endif - } - hdr = (GMemHdr *)mem; - data = (void *)(mem + gMemHdrSize); - trl = (unsigned long *)(mem + gMemHdrSize + size1); - hdr->magic = gMemMagic; - hdr->size = size; - hdr->index = gMemIndex++; - if (gMemTail) { - gMemTail->next = hdr; - hdr->prev = gMemTail; - gMemTail = hdr; - } else { - hdr->prev = NULL; - gMemHead = gMemTail = hdr; - } - hdr->next = NULL; - ++gMemAlloc; - gMemInUse += size; - for (p = (unsigned long *)data; p <= trl; ++p) { - *p = gMemDeadVal; - } - return data; -#else - void *p; - - if (size < 0) { -#if USE_EXCEPTIONS - throw GMemException(); -#else - fprintf(stderr, "Invalid memory allocation size\n"); - exit(1); -#endif - } - if (size == 0) { - return NULL; - } - if (!(p = malloc(size))) { -#if USE_EXCEPTIONS - throw GMemException(); -#else - fprintf(stderr, "Out of memory\n"); - exit(1); -#endif - } - return p; -#endif -} - -void *grealloc(void *p, int size) GMEM_EXCEP { -#ifdef DEBUG_MEM - GMemHdr *hdr; - void *q; - int oldSize; - - if (size < 0) { -#if USE_EXCEPTIONS - throw GMemException(); -#else - fprintf(stderr, "Invalid memory allocation size\n"); - exit(1); -#endif - } - if (size == 0) { - if (p) { - gfree(p); - } - return NULL; - } - if (p) { - hdr = (GMemHdr *)((char *)p - gMemHdrSize); - oldSize = hdr->size; - q = gmalloc(size); - memcpy(q, p, size < oldSize ? size : oldSize); - gfree(p); - } else { - q = gmalloc(size); - } - return q; -#else - void *q; - - if (size < 0) { -#if USE_EXCEPTIONS - throw GMemException(); -#else - fprintf(stderr, "Invalid memory allocation size\n"); - exit(1); -#endif - } - if (size == 0) { - if (p) { - free(p); - } - return NULL; - } - if (p) { - q = realloc(p, size); - } else { - q = malloc(size); - } - if (!q) { -#if USE_EXCEPTIONS - throw GMemException(); -#else - fprintf(stderr, "Out of memory\n"); - exit(1); -#endif - } - return q; -#endif -} - -void *gmallocn(int nObjs, int objSize) GMEM_EXCEP { - int n; - - if (nObjs == 0) { - return NULL; - } - n = nObjs * objSize; - if (objSize <= 0 || nObjs < 0 || nObjs >= INT_MAX / objSize) { -#if USE_EXCEPTIONS - throw GMemException(); -#else - fprintf(stderr, "Bogus memory allocation size\n"); - exit(1); -#endif - } - return gmalloc(n); -} - -void *gmallocn_checkoverflow(int nObjs, int objSize) GMEM_EXCEP { - int n; - - if (nObjs == 0) { - return NULL; - } - n = nObjs * objSize; - if (objSize <= 0 || nObjs < 0 || nObjs >= INT_MAX / objSize) { -#if USE_EXCEPTIONS - throw GMemException(); -#else - fprintf(stderr, "Bogus memory allocation size\n"); - return NULL; -#endif - } - return gmalloc(n); -} - - -void *greallocn(void *p, int nObjs, int objSize) GMEM_EXCEP { - int n; - - if (nObjs == 0) { - if (p) { - gfree(p); - } - return NULL; - } - n = nObjs * objSize; - if (objSize <= 0 || nObjs < 0 || nObjs >= INT_MAX / objSize) { -#if USE_EXCEPTIONS - throw GMemException(); -#else - fprintf(stderr, "Bogus memory allocation size\n"); - exit(1); -#endif - } - return grealloc(p, n); -} - -void gfree(void *p) { -#ifdef DEBUG_MEM - int size; - GMemHdr *hdr; - unsigned long *trl, *clr; - - if (p) { - hdr = (GMemHdr *)((char *)p - gMemHdrSize); - if (hdr->magic == gMemMagic && - ((hdr->prev == NULL) == (hdr == gMemHead)) && - ((hdr->next == NULL) == (hdr == gMemTail))) { - if (hdr->prev) { - hdr->prev->next = hdr->next; - } else { - gMemHead = hdr->next; - } - if (hdr->next) { - hdr->next->prev = hdr->prev; - } else { - gMemTail = hdr->prev; - } - --gMemAlloc; - gMemInUse -= hdr->size; - size = gMemDataSize(hdr->size); - trl = (unsigned long *)((char *)hdr + gMemHdrSize + size); - if (*trl != gMemDeadVal) { - fprintf(stderr, "Overwrite past end of block %d at address %p\n", - hdr->index, p); - } - for (clr = (unsigned long *)hdr; clr <= trl; ++clr) { - *clr = gMemDeadVal; - } - free(hdr); - } else { - fprintf(stderr, "Attempted to free bad address %p\n", p); - } - } -#else - if (p) { - free(p); - } -#endif -} - -#ifdef DEBUG_MEM -void gMemReport(FILE *f) { - GMemHdr *p; - - fprintf(f, "%d memory allocations in all\n", gMemIndex); - if (gMemAlloc > 0) { - fprintf(f, "%d memory blocks left allocated:\n", gMemAlloc); - fprintf(f, " index size\n"); - fprintf(f, "-------- --------\n"); - for (p = gMemHead; p; p = p->next) { - fprintf(f, "%8d %8d\n", p->index, p->size); - } - } else { - fprintf(f, "No memory blocks left allocated\n"); - } -} -#endif - -char *copyString(char *s) { - char *s1; - - s1 = (char *)gmalloc(strlen(s) + 1); - strcpy(s1, s); - return s1; -} diff --git a/kpdf/xpdf/goo/gmem.cpp b/kpdf/xpdf/goo/gmem.cpp new file mode 100644 index 00000000..d8962aba --- /dev/null +++ b/kpdf/xpdf/goo/gmem.cpp @@ -0,0 +1,315 @@ +/* + * gmem.c + * + * Memory routines with out-of-memory checking. + * + * Copyright 1996-2003 Glyph & Cog, LLC + */ + +#include +#include +#include +#include +#include +#include +#include "gmem.h" + +#ifdef DEBUG_MEM + +typedef struct _GMemHdr { + unsigned int magic; + int size; + int index; + struct _GMemHdr *next, *prev; +} GMemHdr; + +#define gMemHdrSize ((sizeof(GMemHdr) + 7) & ~7) +#define gMemTrlSize (sizeof(long)) + +#define gMemMagic 0xabcd9999 + +#if gmemTrlSize==8 +#define gMemDeadVal 0xdeadbeefdeadbeefUL +#else +#define gMemDeadVal 0xdeadbeefUL +#endif + +/* round data size so trailer will be aligned */ +#define gMemDataSize(size) \ + ((((size) + gMemTrlSize - 1) / gMemTrlSize) * gMemTrlSize) + +static GMemHdr *gMemHead = NULL; +static GMemHdr *gMemTail = NULL; + +static int gMemIndex = 0; +static int gMemAlloc = 0; +static int gMemInUse = 0; + +#endif /* DEBUG_MEM */ + +void *gmalloc(int size) GMEM_EXCEP { +#ifdef DEBUG_MEM + int size1; + char *mem; + GMemHdr *hdr; + void *data; + unsigned long *trl, *p; + + if (size < 0) { +#if USE_EXCEPTIONS + throw GMemException(); +#else + fprintf(stderr, "Invalid memory allocation size\n"); + exit(1); +#endif + } + if (size == 0) { + return NULL; + } + size1 = gMemDataSize(size); + if (!(mem = (char *)malloc(size1 + gMemHdrSize + gMemTrlSize))) { +#if USE_EXCEPTIONS + throw GMemException(); +#else + fprintf(stderr, "Out of memory\n"); + exit(1); +#endif + } + hdr = (GMemHdr *)mem; + data = (void *)(mem + gMemHdrSize); + trl = (unsigned long *)(mem + gMemHdrSize + size1); + hdr->magic = gMemMagic; + hdr->size = size; + hdr->index = gMemIndex++; + if (gMemTail) { + gMemTail->next = hdr; + hdr->prev = gMemTail; + gMemTail = hdr; + } else { + hdr->prev = NULL; + gMemHead = gMemTail = hdr; + } + hdr->next = NULL; + ++gMemAlloc; + gMemInUse += size; + for (p = (unsigned long *)data; p <= trl; ++p) { + *p = gMemDeadVal; + } + return data; +#else + void *p; + + if (size < 0) { +#if USE_EXCEPTIONS + throw GMemException(); +#else + fprintf(stderr, "Invalid memory allocation size\n"); + exit(1); +#endif + } + if (size == 0) { + return NULL; + } + if (!(p = malloc(size))) { +#if USE_EXCEPTIONS + throw GMemException(); +#else + fprintf(stderr, "Out of memory\n"); + exit(1); +#endif + } + return p; +#endif +} + +void *grealloc(void *p, int size) GMEM_EXCEP { +#ifdef DEBUG_MEM + GMemHdr *hdr; + void *q; + int oldSize; + + if (size < 0) { +#if USE_EXCEPTIONS + throw GMemException(); +#else + fprintf(stderr, "Invalid memory allocation size\n"); + exit(1); +#endif + } + if (size == 0) { + if (p) { + gfree(p); + } + return NULL; + } + if (p) { + hdr = (GMemHdr *)((char *)p - gMemHdrSize); + oldSize = hdr->size; + q = gmalloc(size); + memcpy(q, p, size < oldSize ? size : oldSize); + gfree(p); + } else { + q = gmalloc(size); + } + return q; +#else + void *q; + + if (size < 0) { +#if USE_EXCEPTIONS + throw GMemException(); +#else + fprintf(stderr, "Invalid memory allocation size\n"); + exit(1); +#endif + } + if (size == 0) { + if (p) { + free(p); + } + return NULL; + } + if (p) { + q = realloc(p, size); + } else { + q = malloc(size); + } + if (!q) { +#if USE_EXCEPTIONS + throw GMemException(); +#else + fprintf(stderr, "Out of memory\n"); + exit(1); +#endif + } + return q; +#endif +} + +void *gmallocn(int nObjs, int objSize) GMEM_EXCEP { + int n; + + if (nObjs == 0) { + return NULL; + } + n = nObjs * objSize; + if (objSize <= 0 || nObjs < 0 || nObjs >= INT_MAX / objSize) { +#if USE_EXCEPTIONS + throw GMemException(); +#else + fprintf(stderr, "Bogus memory allocation size\n"); + exit(1); +#endif + } + return gmalloc(n); +} + +void *gmallocn_checkoverflow(int nObjs, int objSize) GMEM_EXCEP { + int n; + + if (nObjs == 0) { + return NULL; + } + n = nObjs * objSize; + if (objSize <= 0 || nObjs < 0 || nObjs >= INT_MAX / objSize) { +#if USE_EXCEPTIONS + throw GMemException(); +#else + fprintf(stderr, "Bogus memory allocation size\n"); + return NULL; +#endif + } + return gmalloc(n); +} + + +void *greallocn(void *p, int nObjs, int objSize) GMEM_EXCEP { + int n; + + if (nObjs == 0) { + if (p) { + gfree(p); + } + return NULL; + } + n = nObjs * objSize; + if (objSize <= 0 || nObjs < 0 || nObjs >= INT_MAX / objSize) { +#if USE_EXCEPTIONS + throw GMemException(); +#else + fprintf(stderr, "Bogus memory allocation size\n"); + exit(1); +#endif + } + return grealloc(p, n); +} + +void gfree(void *p) { +#ifdef DEBUG_MEM + int size; + GMemHdr *hdr; + unsigned long *trl, *clr; + + if (p) { + hdr = (GMemHdr *)((char *)p - gMemHdrSize); + if (hdr->magic == gMemMagic && + ((hdr->prev == NULL) == (hdr == gMemHead)) && + ((hdr->next == NULL) == (hdr == gMemTail))) { + if (hdr->prev) { + hdr->prev->next = hdr->next; + } else { + gMemHead = hdr->next; + } + if (hdr->next) { + hdr->next->prev = hdr->prev; + } else { + gMemTail = hdr->prev; + } + --gMemAlloc; + gMemInUse -= hdr->size; + size = gMemDataSize(hdr->size); + trl = (unsigned long *)((char *)hdr + gMemHdrSize + size); + if (*trl != gMemDeadVal) { + fprintf(stderr, "Overwrite past end of block %d at address %p\n", + hdr->index, p); + } + for (clr = (unsigned long *)hdr; clr <= trl; ++clr) { + *clr = gMemDeadVal; + } + free(hdr); + } else { + fprintf(stderr, "Attempted to free bad address %p\n", p); + } + } +#else + if (p) { + free(p); + } +#endif +} + +#ifdef DEBUG_MEM +void gMemReport(FILE *f) { + GMemHdr *p; + + fprintf(f, "%d memory allocations in all\n", gMemIndex); + if (gMemAlloc > 0) { + fprintf(f, "%d memory blocks left allocated:\n", gMemAlloc); + fprintf(f, " index size\n"); + fprintf(f, "-------- --------\n"); + for (p = gMemHead; p; p = p->next) { + fprintf(f, "%8d %8d\n", p->index, p->size); + } + } else { + fprintf(f, "No memory blocks left allocated\n"); + } +} +#endif + +char *copyString(char *s) { + char *s1; + + s1 = (char *)gmalloc(strlen(s) + 1); + strcpy(s1, s); + return s1; +} diff --git a/kpdf/xpdf/goo/gmempp.cc b/kpdf/xpdf/goo/gmempp.cc deleted file mode 100644 index b1ee970d..00000000 --- a/kpdf/xpdf/goo/gmempp.cc +++ /dev/null @@ -1,32 +0,0 @@ -//======================================================================== -// -// gmempp.cc -// -// Use gmalloc/gfree for C++ new/delete operators. -// -// Copyright 1996-2003 Glyph & Cog, LLC -// -//======================================================================== - -#include -#include "gmem.h" - -#ifdef DEBUG_MEM - -void *operator new(size_t size) { - return gmalloc((int)size); -} - -void *operator new[](size_t size) { - return gmalloc((int)size); -} - -void operator delete(void *p) { - gfree(p); -} - -void operator delete[](void *p) { - gfree(p); -} - -#endif diff --git a/kpdf/xpdf/goo/gmempp.cpp b/kpdf/xpdf/goo/gmempp.cpp new file mode 100644 index 00000000..4804889b --- /dev/null +++ b/kpdf/xpdf/goo/gmempp.cpp @@ -0,0 +1,32 @@ +//======================================================================== +// +// gmempp.cpp +// +// Use gmalloc/gfree for C++ new/delete operators. +// +// Copyright 1996-2003 Glyph & Cog, LLC +// +//======================================================================== + +#include +#include "gmem.h" + +#ifdef DEBUG_MEM + +void *operator new(size_t size) { + return gmalloc((int)size); +} + +void *operator new[](size_t size) { + return gmalloc((int)size); +} + +void operator delete(void *p) { + gfree(p); +} + +void operator delete[](void *p) { + gfree(p); +} + +#endif -- cgit v1.2.3