00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef __TEXTWRITER__H__
00010 #define __TEXTWRITER__H__
00011
00012 #include "3dmath.h"
00013
00014 class TextWriter
00015 {
00016 private:
00017 wxBufferedOutputStream * outputStream;
00018
00019 public:
00020 TextWriter(wxBufferedOutputStream * _outputStream) : outputStream(_outputStream) {};
00021
00022 ~TextWriter()
00023 {
00024 };
00025
00026 void SetOutputStream(wxBufferedOutputStream * _outputStream)
00027 {
00028 outputStream = _outputStream;
00029 }
00030
00032 TextWriter* Write(const char *str)
00033 {
00034 outputStream->Write(str, strlen(str));
00035 return this;
00036 }
00037
00039 TextWriter* Write(const char *str, size_t size)
00040 {
00041 outputStream->Write(str, size);
00042 return this;
00043 }
00044
00046 TextWriter* Write(const float &val)
00047 {
00048 if (val == 0)
00049 return Write("0", 1);
00050 if (_isnan(val))
00051 return Write("1.#IND000", 9);
00052 char tmp[30];
00053 char *index=tmp+29;
00054 *index=0;
00055 int lg=log10l(fabs(val));
00056 int lgr=lg;
00057 float rpart=val;
00058 if ((lg<-2) || (lg>10))
00059 {
00060 if (lg<0)
00061 lg--;
00062 rpart/=pow(10.0f, lg);
00063 lgr=1;
00064 int lgtmp=abs(lg);
00065 do {
00066 index--;
00067 *index = (char)(lgtmp%10) + '0';
00068 } while(lgtmp /= 10);
00069 if (lg<0)
00070 {
00071 index--;
00072 *index='-';
00073 }
00074 index--;
00075 *index='e';
00076 }
00077 int ipart = rpart;
00078 rpart=fabs(rpart-ipart);
00079 int count=7-lgr;
00080 if (ipart==0)
00081 count++;
00082 long ripart = rpart*pow(10.0f, count)+0.5f;
00083 if (ripart!=0)
00084 {
00085 while (ripart%10==0)
00086 {
00087 ripart/=10;
00088 count--;
00089 }
00090 while (count--)
00091 {
00092 index--;
00093 *index = (char)(ripart%10) + '0';
00094 ripart/=10;
00095 }
00096 index--;
00097 *index='.';
00098 }
00099 ipart=abs(ipart);
00100 do {
00101 index--;
00102 *index = (char)(ipart%10) + '0';
00103 } while(ipart /= 10);
00104 if (val<0)
00105 {
00106 index--;
00107 *index='-';
00108 }
00109 return Write(index, tmp+29-index);
00110 }
00111
00113 TextWriter* Write(const int &val)
00114 {
00115 if(val == 0)
00116 return Write("0", 1);
00117 char tmp[30];
00118 char *index=tmp+29;
00119 *index=0;
00120 int numLeft = abs(val);
00121 do {
00122 index--;
00123 *index = (char)(numLeft%10) + '0';
00124 } while(numLeft /= 10);
00125 if(val < 0)
00126 {
00127 index--;
00128 *index = '-';
00129 }
00130 return Write(index, tmp+29-index);
00131 }
00132
00134 TextWriter* Write(const unsigned int &val)
00135 {
00136 return Write((int)val);
00137 }
00138
00140 TextWriter* Write(const long &val)
00141 {
00142 if(val == 0)
00143 return Write("0", 1);
00144 char tmp[30];
00145 char *index=tmp+29;
00146 *index=0;
00147 long numLeft = abs(val);
00148 do {
00149 index--;
00150 *index = (char)(numLeft%10) + '0';
00151
00152 } while(numLeft /= 10);
00153 if(val < 0)
00154 {
00155 index--;
00156 *index = '-';
00157 }
00158 return Write(index, tmp+29-index);
00159 }
00160
00162 TextWriter* Write(const wxString &str)
00163 {
00164 return Write(str.mb_str(wxConvISO8859_1), str.size());
00165 }
00166
00168 TextWriter* Write(const float *values, int num, const char *delimiter)
00169 {
00170 for (int i=0; i<num; i++)
00171 {
00172 if (i)
00173 Write(delimiter);
00174 Write(values[i]);
00175 }
00176 return this;
00177 }
00178 };
00179
00180
00181 #endif