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

查看本檔案說明文件.
00001 #include <string.h>
00002 #include "OutputStream.h"
00003 
00004 TOutputStream::TOutputStream()
00005 {
00006 }
00007 
00008 TOutputStream::~TOutputStream()
00009 {
00010   Close();
00011 }
00012 
00013 void TOutputStream::Close(void)
00014 {
00015   Flush();
00016 }
00017 
00018 void TOutputStream::Flush(void)
00019 {
00020 }
00021 
00022 void TOutputStream::Writes(void *src, int size)
00023 {
00024   unsigned char *p;
00025   int i;
00026 
00027   p = (unsigned char *)src;
00028   for (i = 0; i < size; i++)
00029     Write(p[i]);
00030 }
00031 
00032 TFileOutputStream::TFileOutputStream()
00033 {
00034   cacheSize = 1024;
00035   cache = new unsigned char[cacheSize];
00036   cacheDataSize = 0;
00037   fout = NULL;
00038 }
00039 
00040 TFileOutputStream::TFileOutputStream(int cs)
00041 {
00042   cacheSize = cs <= 0? 1024 : cs;
00043   cache = new unsigned char[cacheSize];
00044   cacheDataSize = 0;
00045   fout = NULL;
00046 }
00047 
00048 TFileOutputStream::~TFileOutputStream()
00049 {
00050   Close();
00051   delete[] cache;
00052 }
00053 
00054 bool TFileOutputStream::Open(char *fname)
00055 {
00056   Close();
00057 
00058   fout = fopen(fname, "wb");
00059   return fout != NULL;
00060 }
00061 
00062 void TFileOutputStream::Close(void)
00063 {
00064   if (fout != NULL)
00065   {
00066     FlushCache();
00067     fclose(fout);
00068     fout = NULL;
00069   }
00070 }
00071 
00072 void TFileOutputStream::Flush(void)
00073 {
00074   if (fout != NULL)
00075   {
00076     FlushCache();
00077     fflush(fout);
00078   }
00079 }
00080 
00081 int TFileOutputStream::Size(void)
00082 {
00083   return fout == NULL? 0 : ftell(fout) + cacheDataSize;
00084 }
00085 
00086 void TFileOutputStream::Write(int n)
00087 {
00088   if (fout != NULL)
00089   {
00090     cache[cacheDataSize++] = n;
00091     if (cacheDataSize >= cacheSize)
00092       FlushCache();
00093   }
00094 }
00095 
00096 void TFileOutputStream::FlushCache(void)
00097 {
00098   fwrite(cache, sizeof(char), cacheDataSize, fout);
00099   cacheDataSize = 0;
00100 }
00101 
00102 TFilterOutputStream::TFilterOutputStream(TOutputStream *Stream)
00103 {
00104   Out = Stream;
00105 }
00106 
00107 TFilterOutputStream::~TFilterOutputStream()
00108 {
00109   Close();
00110 }
00111 
00112 void TFilterOutputStream::Close(void)
00113 {
00114   Out->Close();
00115 }
00116 
00117 void TFilterOutputStream::Flush(void)
00118 {
00119   Out->Flush();
00120 }
00121 
00122 int TFilterOutputStream::Size(void)
00123 {
00124   return Out->Size();
00125 }
00126 
00127 void TFilterOutputStream::Write(int n)
00128 {
00129   Out->Write(n);
00130 }
00131 
00132 TDataOutputStream::TDataOutputStream(TOutputStream *Stream) : TFilterOutputStream(Stream)
00133 {
00134 }
00135 
00136 TDataOutputStream::~TDataOutputStream()
00137 {
00138 }
00139 
00140 void TDataOutputStream::WriteChar(char c)
00141 {
00142   Writes(&c, sizeof(char));
00143 }
00144 
00145 void TDataOutputStream::WriteDouble(double d)
00146 {
00147   Writes(&d, sizeof(double));
00148 }
00149 
00150 void TDataOutputStream::WriteFloat(float f)
00151 {
00152   Writes(&f, sizeof(float));
00153 }
00154 
00155 void TDataOutputStream::WriteInt(int n)
00156 {
00157   Writes(&n, sizeof(int));
00158 }
00159 
00160 void TDataOutputStream::WriteString(char *str)
00161 {
00162   Writes(str, strlen(str));
00163 }
00164 
00165 TCipherOutputStream::TCipherOutputStream(TOutputStream *Stream) : TFilterOutputStream(Stream)
00166 {
00167   key = NULL;
00168   SetKey(NULL);
00169 }
00170 
00171 TCipherOutputStream::~TCipherOutputStream()
00172 {
00173   delete[] key;
00174 }
00181 void TCipherOutputStream::SetKey(char *k)
00182 {
00183   delete[] key;
00184 
00185   if (k == NULL)
00186   {
00187     key = NULL;
00188     keySize = 0;
00189     return;
00190   }
00191   keySize = strlen(k);
00192   key = new char[keySize + 1];
00193   strcpy(key, k);
00194   index = 0;
00195 }
00200 void TCipherOutputStream::Write(int n)
00201 {
00202   if (keySize != 0)
00203   {
00204     n ^= key[index];
00205     index = (index + 1) % keySize;
00206   }
00207   TFilterOutputStream::Write(n);
00208 }
00209 

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