Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages   Examples  

CTBzfile.cxx

Go to the documentation of this file.
00001 
00006 /*----------------------------------------------------------------------------*/
00007 /* C Tool Box: Designed and implemented by:                                   */
00008 /*    Walter F.J. Mueller   Gesellschaft fuer Schwerionenforschung (GSI)      */
00009 /*                          Planckstrasse 1, D-64291 Darmstadt, Germany       */
00010 /*                  Email:  W.F.J.Mueller@gsi.de                              */
00011 /*                  WWW:    http://www-kp3.gsi.de/www/kp3/people/mueller.html */
00012 /*------------------------------------------+---------------------------------*/
00013 
00014 #include <stdio.h>
00015 #include <string.h>
00016 #include <zlib.h>
00017 
00018 #include "CTB.hxx"
00019 #include "CTB_Trace.hxx"
00020 #include "CTBosFill.hxx"
00021 #include "CTBzfile.hxx"
00022 
00030 //------------------------------------------+-----------------------------------
00032 
00033 CTBzfile::CTBzfile()
00034   : mi_type(closed),
00035     m_file(0),
00036     mb_bad(false),
00037     mb_eof(false)
00038 {
00039   CTB_Trace("CTBzfile()");
00040 }
00041 
00042 //------------------------------------------+-----------------------------------
00044 
00045 CTBzfile::~CTBzfile()
00046 {
00047   CTB_Trace("~CTBzfile()");
00048 
00049   Close();
00050 }
00051 
00052 //------------------------------------------+-----------------------------------
00054 
00055 bool CTBzfile::Open(const char *c_name, const char *c_mode)
00056 {
00057   CTB_Trace("CTBzfile::Open(const char*, const char*)");
00058   CTBint i_lenname = c_name ? strlen(c_name) : 0;
00059 
00060   Close();
00061   mb_bad = false;
00062   mb_eof = false;
00063 
00064   if (i_lenname >= 4 && strcmp(c_name+i_lenname-3,".gz") == 0) {
00065     mi_type = gzfile;
00066     m_file  = gzopen(c_name,c_mode);
00067   } else {
00068     mi_type = cfile;
00069     m_file = fopen(c_name,c_mode);
00070   }
00071   
00072   if (m_file == 0) mi_type = closed;
00073 
00074   return m_file;
00075 }
00076 
00077 //------------------------------------------+-----------------------------------
00079 
00080 void CTBzfile::Close()
00081 {
00082   CTB_Trace("CTBzfile::Close()");
00083 
00084   switch(mi_type) {
00085 
00086   case cfile:
00087     fclose((FILE*) m_file);
00088     mi_type = closed;
00089     break;
00090 
00091   case gzfile:
00092     gzclose(m_file);
00093     mi_type = closed;
00094     break;
00095   }
00096   
00097   return;
00098 }
00099 
00100 //------------------------------------------+-----------------------------------
00102 
00103 CTBint CTBzfile::Read(void* p_buf, CTBint i_size)
00104 {
00105   CTB_Trace("CTBzfile::Read(void*, CTBint)");
00106   CTBint i_nbyte = 0;
00107 
00108   switch(mi_type) {
00109 
00110   case cfile:
00111     i_nbyte = fread(p_buf, 1, i_size, (FILE*) m_file);
00112     break;
00113 
00114   case gzfile:
00115     i_nbyte = gzread(m_file, p_buf, i_size);
00116     if (i_size > 0) {
00117       if (i_nbyte == 0) mb_eof = true;
00118       if (i_nbyte  < 0) mb_bad = true;
00119     }
00120     
00121     break;
00122   }
00123   
00124   return i_nbyte;
00125 }
00126 
00127 //------------------------------------------+-----------------------------------
00129 
00130 CTBint CTBzfile::Write(const void* p_buf, CTBint i_size)
00131 {
00132   CTB_Trace("CTBzfile::Write(void*, CTBint)");
00133 
00134   CTBint i_nbyte = 0;
00135 
00136   switch(mi_type) {
00137 
00138   case cfile:
00139     i_nbyte = fwrite(p_buf, 1, i_size, (FILE*) m_file);
00140     break;
00141 
00142   case gzfile:
00143     i_nbyte = gzwrite(m_file, (void*) p_buf, i_size);
00144     if (i_size > 0) {
00145       if (i_nbyte < 0) mb_bad = true;
00146     }
00147     break;
00148   }
00149   
00150   
00151   return i_nbyte;
00152 }
00153 
00154 //------------------------------------------+-----------------------------------
00156 
00157 CTBint CTBzfile::ReadObject(void* p_buf, CTBint i_size)
00158 {
00159   CTB_Trace("CTBzfile::ReadObject(void*, CTBint)");
00160   CTBint i_done = 0;
00161   CTBint i_nbyte;
00162 
00163   switch(mi_type) {
00164 
00165   case cfile:
00166     i_done = fread(p_buf, i_size, 1, (FILE*) m_file);
00167     break;
00168 
00169   case gzfile:
00170     i_nbyte = gzread(m_file, p_buf, i_size);
00171     if (i_size > 0) {
00172       if (i_nbyte == 0)           mb_eof = true;
00173       else if (i_nbyte != i_size) mb_bad = true;
00174       else i_done = 1;
00175     }
00176     
00177     break;
00178   }
00179   
00180   return i_done;
00181 }
00182 
00183 //------------------------------------------+-----------------------------------
00185 
00186 CTBint CTBzfile::WriteObject(const void* p_buf, CTBint i_size)
00187 {
00188   CTB_Trace("CTBzfile::WriteObject(void*, CTBint)");
00189 
00190   CTBint i_done = 0;
00191   CTBint i_nbyte;
00192 
00193   switch(mi_type) {
00194 
00195   case cfile:
00196     i_done = fwrite(p_buf, i_size, 1, (FILE*) m_file);
00197     break;
00198 
00199   case gzfile:
00200     i_nbyte = gzwrite(m_file, (void*) p_buf, i_size);
00201     if (i_size > 0) {
00202       if (i_nbyte != i_size) mb_bad = true;
00203       else i_done = 1;
00204     }
00205     break;
00206   }
00207   
00208   
00209   return i_done;
00210 }
00211 
00212 //------------------------------------------+-----------------------------------
00214 
00215 bool CTBzfile::Good()
00216 {
00217   CTB_Trace("CTBzfile::Good()");
00218   
00219   return !Fail();
00220 }
00221 
00222 //------------------------------------------+-----------------------------------
00224 
00225 bool CTBzfile::Bad()
00226 {
00227   CTB_Trace("CTBzfile::Bad()");
00228   
00229   switch(mi_type) {
00230   case cfile:  return ferror((FILE*) m_file);
00231   case gzfile: return mb_bad;
00232   }
00233   
00234   return false;
00235 }
00236 
00237 //------------------------------------------+-----------------------------------
00239 
00240 bool CTBzfile::Fail()
00241 {
00242   CTB_Trace("CTBzfile::Fail()");
00243   
00244   return !m_file || Bad() || Eof();
00245 }
00246 
00247 //------------------------------------------+-----------------------------------
00249 
00250 bool CTBzfile::Eof()
00251 {
00252   CTB_Trace("CTBzfile::Eof()");
00253   
00254   switch(mi_type) {
00255   case cfile:  return feof((FILE*) m_file);
00256   case gzfile: return mb_eof;
00257   }
00258   
00259   return true;
00260 }
00261 
00262 //------------------------------------------+-----------------------------------
00264 
00265 void CTBzfile::Dump(int i_indent, ostream& os, const char* p_text) const
00266 {
00267   CTBosFill bl(i_indent);
00268 
00269   os << bl << "-- CTBzfile ";
00270   if (p_text) os << p_text;
00271   os << " @ " << this << endl;
00272 
00273   os << bl << "    mi_type:    " << mi_type << "\n"; 
00274   os << bl << "    m_file:     " << m_file << "\n"; 
00275   os << bl << "    mb_bad:     " << mb_bad << "\n"; 
00276   os << bl << "    mb_eof:     " << mb_eof << "\n"; 
00277 
00278   return;
00279 }

Generated at Fri Oct 24 18:11:31 2003 for CTBbase by doxygen1.2.9-20010812 written by Dimitri van Heesch, © 1997-2001