D:/work/code_by/IOStream/InputStream.cpp

查看本檔案說明文件.
00001 #include <string.h>
00002 #include "InputStream.h"
00003 
00004 TInputStream::TInputStream()
00005 {
00006 }
00007 
00008 TInputStream::~TInputStream()
00009 {
00010   Close();
00011 }
00012 
00013 void TInputStream::Close(void)
00014 {
00015 }
00016 
00017 int TInputStream::Reads(void *dst, int size)
00018 {
00019   int i, b;
00020   unsigned char *p;
00021 
00022   p = (unsigned char *)dst;
00023   for (i = 0; i < size; i++)
00024   {
00025     b = Read();
00026     if (b == -1)
00027       break;
00028     *(p++) = b;
00029   }
00030   return i;
00031 }
00032 
00033 void TInputStream::Reset(void)
00034 {
00035 }
00036 
00037 TFileInputStream::TFileInputStream()
00038 {
00039   cacheSize = 1024;
00040   cache = new unsigned char[cacheSize];
00041   fin = NULL;
00042   Reset();
00043 }
00044 
00045 TFileInputStream::TFileInputStream(int cs)
00046 {
00047   cacheSize = cs <= 0? 1024 : cs;
00048   cache = new unsigned char[cacheSize];
00049   fin = NULL;
00050   Reset();
00051 }
00052 
00053 TFileInputStream::~TFileInputStream()
00054 {
00055   Close();
00056   delete[] cache;
00057 }
00058 
00059 bool TFileInputStream::Open(char *fname)
00060 {
00061   Close();
00062 
00063   fin = fopen(fname, "rb");
00064   Reset();
00065   return fin != NULL;
00066 }
00067 
00068 int TFileInputStream::Available(void)
00069 {
00070   return available;
00071 }
00072 
00073 void TFileInputStream::Close(void)
00074 {
00075   if (fin != NULL)
00076     fclose(fin);  //直接關閉
00077   fin = NULL;
00078   available = position = 0;
00079 }
00080 
00081 int TFileInputStream::Position(void)
00082 {
00083   return position;
00084 }
00085 
00086 int TFileInputStream::Read(void)
00087 {
00088   if (available == 0)
00089     return -1;
00090 
00091   ++position;
00092   --available;
00093   if (cacheIndex >= cacheDataSize)
00094   {
00095     cacheDataSize = fread(cache, sizeof(char), cacheSize, fin);
00096     cacheIndex = 0;
00097   }
00098   return cache[cacheIndex++];
00099 }
00100 
00101 void TFileInputStream::Reset(void)
00102 {
00103   if (fin == NULL)
00104     available = 0;
00105   else
00106   {
00107     fseek(fin, 0, SEEK_END);
00108     available = ftell(fin);
00109     fseek(fin, 0, SEEK_SET);
00110   }
00111   position = 0;
00112   cacheIndex = cacheDataSize = 0;
00113 }
00114 
00115 int TFileInputStream::Skip(int size)
00116 {
00117   if (fin == NULL || size < 0)
00118     return 0;
00119 
00120   if (size > available)
00121     size = available;
00122   if (size > cacheDataSize - cacheIndex)
00123   {
00124     fseek(fin, size - cacheDataSize + cacheIndex, SEEK_CUR);
00125     cacheDataSize = 0;
00126   }
00127   else
00128     cacheIndex += size;
00129 
00130   position += size;
00131   available -= size;
00132   return size;
00133 }
00134 
00135 TFilterInputStream::TFilterInputStream(TInputStream *Stream)
00136 {
00137   In = Stream;
00138 }
00139 
00140 TFilterInputStream::~TFilterInputStream()
00141 {
00142   Close();
00143 }
00144 
00145 int TFilterInputStream::Available(void)
00146 {
00147   return In->Available();
00148 }
00149 
00150 void TFilterInputStream::Close(void)
00151 {
00152   In->Close();
00153 }
00154 
00155 int TFilterInputStream::Position(void)
00156 {
00157   return In->Position();
00158 }
00159 
00160 int TFilterInputStream::Read(void)
00161 {
00162   return In->Read();
00163 }
00164 
00165 void TFilterInputStream::Reset(void)
00166 {
00167   In->Reset();
00168 }
00169 
00170 int TFilterInputStream::Skip(int size)
00171 {
00172   return In->Skip(size);
00173 }
00174 
00175 TDataInputStream::TDataInputStream(TInputStream *Stream) : TFilterInputStream(Stream)
00176 {
00177 }
00178 
00179 TDataInputStream::~TDataInputStream()
00180 {
00181 }
00182 
00183 char TDataInputStream::ReadChar(void)
00184 {
00185   return Read();
00186 }
00187 
00188 float TDataInputStream::ReadFloat(void)
00189 {
00190   float f;
00191 
00192   Reads(&f, sizeof(float));
00193   return f;
00194 }
00195 
00196 double TDataInputStream::ReadDouble(void)
00197 {
00198   double d;
00199 
00200   Reads(&d, sizeof(double));
00201   return d;
00202 }
00203 
00204 int TDataInputStream::ReadInt(void)
00205 {
00206   int n;
00207 
00208   Reads(&n, sizeof(int));
00209   return n;
00210 }
00211 
00212 int TDataInputStream::ReadLine(char *str, int size)
00213 {
00214   int i, n;
00215 
00216   i = 0;
00217   while (size-- > 0)
00218   {
00219     n = Read();
00220     if (n == -1 || n == 0x0A)
00221       break;
00222     if (n != 0x0D)
00223       str[i++] = n;
00224   }
00225   str[i] = '\0';
00226 
00227   return i;
00228 }
00229 
00230 TCipherInputStream::TCipherInputStream(TInputStream *Stream) : TFilterInputStream(Stream)
00231 {
00232   key = NULL;
00233   SetKey(NULL);
00234 }
00235 
00236 TCipherInputStream::~TCipherInputStream()
00237 {
00238   delete[] key;
00239 }
00240 
00247 void TCipherInputStream::SetKey(char *k)
00248 {
00249   delete[] key;
00250 
00251   if (k == NULL)
00252   {
00253     key = NULL;
00254     keySize = 0;
00255     return;
00256   }
00257   keySize = strlen(k);
00258   key = new char[keySize + 1];
00259   strcpy(key, k);
00260   index = 0;
00261 }
00262 
00267 int TCipherInputStream::Read(void)
00268 {
00269   int n;
00270 
00271   n = TFilterInputStream::Read();
00272   if (keySize != 0)
00273   {
00274     n ^= key[index];
00275     index = (index + 1) % keySize;
00276   }
00277   return n;
00278 }
00279 
00280 void TCipherInputStream::Reset(void)
00281 {
00282   TFilterInputStream::Reset();
00283   index = 0;
00284 }
00285 
00286 int TCipherInputStream::Skip(int size)
00287 {
00288   if (size < 0)
00289     return 0;
00290 
00291   size = TFilterInputStream::Skip(size);
00292   if (keySize != 0)
00293     index = (index + size) % keySize;
00294 
00295   return size;
00296 }
00297 

產生日期:Tue Jul 11 11:52:19 2006, 專案:cbmr, 產生器:  doxygen 1.4.7