From b7e7052f4ecc79bf19bd545daf8a9ccd1dd0e2f1 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Wed, 28 Apr 2010 14:43:59 +0000 Subject: [PATCH] Allow TextFile to return its own filename --- .../fastlib-armadillo/fastlib/file/textfile.cc | 1 + .../fastlib-armadillo/fastlib/file/textfile.h | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/fastlib/branches/fastlib-armadillo/fastlib/file/textfile.cc b/fastlib/branches/fastlib-armadillo/fastlib/file/textfile.cc index fe61194a1f..fee1cc1d88 100644 --- a/fastlib/branches/fastlib-armadillo/fastlib/file/textfile.cc +++ b/fastlib/branches/fastlib-armadillo/fastlib/file/textfile.cc @@ -108,6 +108,7 @@ void TextLineReader::Error(const char *format, ...) { success_t TextLineReader::Open(const char *fname) { f_ = fopen(fname, "r"); + fname_ = strncpy(new char[strlen(fname) + 1], fname, strlen(fname) + 1); line_num_ = 0; has_line_ = false; line_.Init(); diff --git a/fastlib/branches/fastlib-armadillo/fastlib/file/textfile.h b/fastlib/branches/fastlib-armadillo/fastlib/file/textfile.h index 881984e7cc..521c4b7d67 100644 --- a/fastlib/branches/fastlib-armadillo/fastlib/file/textfile.h +++ b/fastlib/branches/fastlib-armadillo/fastlib/file/textfile.h @@ -62,6 +62,7 @@ class TextLineReader { private: FILE *f_; + char *fname_; String line_; int line_num_; bool has_line_; @@ -70,15 +71,17 @@ class TextLineReader { /** Creates an unitialized object. */ TextLineReader() { f_ = NULL; + fname_ = NULL; } /** * Automatically closes the file. */ ~TextLineReader() { - if (f_) { + if(f_) ::fclose(f_); - } + if(fname_) + delete[] fname_; } /** @@ -96,6 +99,16 @@ class TextLineReader { void Close() { (void)fclose(f_); f_ = NULL; + if(fname_) + delete[] fname_; + } + + /** + * Return the name of the file we are working with. + * This will return NULL if no file has been opened yet. + */ + const char *filename() const { + return fname_; } /**