From 38d60726082b04e79edae1c8c797c6dcb8314504 Mon Sep 17 00:00:00 2001 From: Pixel Date: Sun, 25 Aug 2002 14:39:48 +0000 Subject: Workiiiiiiiiiiiiiiiiiing!!!! --- generic/Exceptions.cpp | 49 +++++++++++++++++++++---------------------------- generic/Input.cpp | 19 ++++++++++++------- generic/Makefile | 2 +- generic/Output.cpp | 6 +++++- generic/String.cpp | 18 ++++++++++++++++-- 5 files changed, 55 insertions(+), 39 deletions(-) (limited to 'generic') diff --git a/generic/Exceptions.cpp b/generic/Exceptions.cpp index f24c0fb..b7c00a6 100644 --- a/generic/Exceptions.cpp +++ b/generic/Exceptions.cpp @@ -3,6 +3,10 @@ #include #include #include +#include +#ifdef DEBUG +#include +#endif #include "String.h" #include "Exceptions.h" #include "generic.h" @@ -82,36 +86,26 @@ void * xmalloc(size_t s) throw (GeneralException) { return 0; } - if (!(r = (char *) ::malloc(s + 2 * sizeof(size_t)))) { - throw MemoryException(s + 2 * sizeof(size_t)); + if (!(r = (char *) ::malloc(s))) { + throw MemoryException(s); } +#ifdef DEBUG + fprintf(stderr, "Allocating %i bytes of memory, got it at %p\n", s, r); +#endif - memset(r, 0, s + 2 * sizeof(size_t)); + memset(r, 0, s); - *((size_t *)r) = s; - *(((size_t *)r) + 1) = 0xdeedbeef; - - return (void *)(r + 2 * sizeof(size_t)); + return (void *)(r); } void * xrealloc(void * ptr, size_t s) { - char * r; - size_t os; - - if (!ptr) { - return xmalloc(s); - } - - os = *(((size_t *) ptr) - 1); - - r = (char *) xmalloc(s); - - if (s) { - memcpy(r, ptr, MIN(s, os)); - } - - xfree(ptr); +#ifdef DEBUG + void * r = realloc(ptr, s); + fprintf(stderr, "Reallocating pointer at %p for %i bytes, now at %p\n", ptr, s, r); return r; +#else + return realloc(ptr, s); +#endif } #ifdef OVER_FREE @@ -123,12 +117,11 @@ void xfree(void *& p) { } void xfree(char *& p) { +#ifdef DEBUG + fprintf(stderr, "Freeing pointer at %p\n", p); +#endif if (p) { - if (*(((size_t *)p) - 1) != 0xdeedbeef) { - fprintf(stderr, "Error: trying to xfree() a non xmalloc()ed bloc.\n"); - exit(-1); - } - ::free(p - 2 * sizeof(size_t)); + ::free(p); p = 0; } } diff --git a/generic/Input.cpp b/generic/Input.cpp index b7ee99c..d415279 100644 --- a/generic/Input.cpp +++ b/generic/Input.cpp @@ -16,18 +16,23 @@ Input::Input(const String & no) throw (GeneralException) : Handle(no.strlen() ? open(no.to_charp(), O_RDONLY) : dup(0)), n(no) { + +#ifdef DEBUG + fprintf(stderr, "Opening file %s, Input at %p\n", no.to_charp(), this); +#endif + if (GetHandle() < 0) { throw IOGeneral(String(_("Error opening file ")) + no + _(" for reading: ") + strerror(errno)); } - size = lseek(GetHandle(), 0, SEEK_END); - lseek(GetHandle(), 0, SEEK_SET); - struct stat s; - fstat(GetHandle(), &s); - date_modif = s.st_mtime; + + if (S_ISREG(s.st_mode)) { + size = seek(0, SEEK_END); + seek(0, SEEK_SET); + } } Input::Input(const Input & i) : Handle(i), n(i.n), size(i.size), date_modif(i.date_modif) { @@ -63,11 +68,11 @@ time_t Input::GetModif() { off_t Input::seek(off_t offset, int whence) throw (GeneralException) { if ((itell = lseek(GetHandle(), offset, whence)) < 0) { - throw IOGeneral(String(_("Error seeking file ")) + n + _(": ") + strerror(errno)); + throw IOGeneral(String(_("Error seeking file ")) + GetName() + _(": ") + strerror(errno)); } #ifdef PARANOID_SEEK if (itell != lseek(GetHandle(), 0, SEEK_CUR)) { - throw IOGeneral(String(_("Error seeking file ")) + n + _(": the position does not match")); + throw IOGeneral(String(_("Error seeking file ")) + GetName() + _(": the position does not match")); } #endif return itell; diff --git a/generic/Makefile b/generic/Makefile index 4ca5e32..7107501 100755 --- a/generic/Makefile +++ b/generic/Makefile @@ -1,6 +1,6 @@ #!/usr/bin/make -f -CPPFLAGS=-Wall -g -O3 -mcpu=i686 -pedantic -pedantic-errors -I../includes -DPARANOID_SEEK -DHAVE_ZLIB +CPPFLAGS=-Wall -g -O3 -mcpu=i686 -pedantic -pedantic-errors -I../includes -DPARANOID_SEEK -DHAVE_ZLIB `pkg-config --cflags glib-2.0` CXX=g++ CC=gcc diff --git a/generic/Output.cpp b/generic/Output.cpp index e136524..1d7981e 100644 --- a/generic/Output.cpp +++ b/generic/Output.cpp @@ -14,7 +14,11 @@ #endif Output::Output(String no, int create, int trunc) throw (GeneralException) : - Handle(no.strlen() ? open(no.to_charp(), O_WRONLY | (O_CREAT * (create ? 1 : 0)) | (trunc ? O_TRUNC : O_APPEND), 00666) : dup(1)), + Handle(no.strlen() ? open(no.to_charp(), O_WRONLY | (O_CREAT * (create ? 1 : 0)) | (trunc ? O_TRUNC : O_APPEND) +#ifdef __linux__ +, 00666 +#endif +) : dup(1)), n(no) { if (GetHandle() < 0) { throw IOGeneral(String(_("Error opening file ")) + no + _(" for writing: ") + strerror(errno)); diff --git a/generic/String.cpp b/generic/String.cpp index c389c13..22d77d6 100644 --- a/generic/String.cpp +++ b/generic/String.cpp @@ -14,7 +14,11 @@ extern "C" { char String::t[BUFSIZ + 1]; -String::String(const String & s) : str(Base::strdup(s.str)), siz(s.siz) { } +String::String(const String & s) : str(Base::strdup(s.str)), siz(s.siz) { +#ifdef DEBUG + fprintf(stderr, "Duplicating string `%s', from %p to %p\n", str, s.str, str); +#endif +} String::String(char c) : siz(1) { char * t = (char *) malloc(2); @@ -23,8 +27,17 @@ String::String(char c) : siz(1) { str = t; } +String::String(const char * s) : str(Base::strdup(s)), siz(::strlen(str)) { +#ifdef DEBUG + fprintf(stderr, "Creating a string with `%s' at %p\n", str, str); +#endif +} + +#if 0 String::String(const char * s, ...) { va_list ap; + + fprintf(stderr, "Creating a String with s = '%s'\n", s); /* This causes a warning: cannot pass objects of type `const String' through `...' but it is not really a problem. */ @@ -33,7 +46,8 @@ String::String(const char * s, ...) { str = Base::strdup(t); va_end(ap); siz = ::strlen(str); -} +} +#endif String::String(int hs, const char * s) : str(s ? Base::strdup(s) : Base::strdup("")), siz(hs) { } -- cgit v1.2.3