00001
00011 #ifndef __NMECpp__
00012 #define __NMECpp__
00013
00014 #include "NME.h"
00015 #include "NMEErrorCpp.h"
00016 #include <string.h>
00017
00028 class NME
00029 {
00030 public:
00031
00034 NME()
00035 {
00036 input = NULL;
00037 buf = NULL;
00038 output = NULL;
00039 format = NMEOutputFormatText;
00040 fontSize = 0;
00041 }
00042
00048 NME(char const *input, int inputLength = -1)
00049 {
00050 this->input = input;
00051 if (inputLength >= 0)
00052 this->inputLength = inputLength;
00053 else
00054 this->inputLength = strlen(input);
00055 buf = output = NULL;
00056 format = NMEOutputFormatText;
00057 fontSize = 0;
00058 }
00059
00063 NME(NME const &nme)
00064 {
00065 input = nme.input;
00066 inputLength = nme.inputLength;
00067 buf = output = NULL;
00068 format = nme.format;
00069 fontSize = nme.fontSize;
00070 }
00071
00073 ~NME()
00074 {
00075 if (buf)
00076 delete [] buf;
00077 }
00078
00083 NME NME::operator = (NME const &nme)
00084 {
00085 if (this != &nme)
00086 {
00087 input = nme.input;
00088 inputLength = nme.inputLength;
00089 if (buf)
00090 delete [] buf;
00091 buf = output = NULL;
00092 format = nme.format;
00093 fontSize = nme.fontSize;
00094 }
00095 return *this;
00096 }
00097
00103 void setInput(char const *input, int inputLength = -1)
00104 {
00105 this->input = input;
00106 if (inputLength >= 0)
00107 this->inputLength = inputLength;
00108 else
00109 this->inputLength = strlen(input);
00110 output = NULL;
00111 }
00112
00116 void setFormat(NMEOutputFormat const &format)
00117 {
00118 this->format = format;
00119 output = NULL;
00120 }
00121
00125 void setFontSize(NMEInt fontSize = 0)
00126 {
00127 this->fontSize = fontSize;
00128 output = NULL;
00129 }
00130
00136 NMEErr getOutput(NMEConstText *output, NMEInt *outputLength = NULL)
00137 {
00138 if (this->output)
00139 {
00140 if (output)
00141 *output = this->output;
00142 if (outputLength)
00143 *outputLength = this->outputLength;
00144 return kNMEErrOk;
00145 }
00146
00147 if (!input || inputLength == 0)
00148 {
00149 if (output)
00150 *output = "";
00151 if (outputLength)
00152 *outputLength = 0;
00153 }
00154
00155 if (!buf)
00156 {
00157 bufSize = 1024 + 2 * inputLength;
00158 buf = new NMEChar[bufSize];
00159 }
00160
00161 for (;;)
00162 {
00163 NMEErr err = NMEProcess(input, inputLength,
00164 buf, bufSize,
00165 kNMEProcessOptDefault, "\n", &format, fontSize,
00166 &this->output, &this->outputLength, NULL);
00167 if (err == kNMEErrOk)
00168 {
00169 if (output)
00170 *output = this->output;
00171 if (outputLength)
00172 *outputLength = this->outputLength;
00173 return kNMEErrOk;
00174 }
00175 else if (err == kNMEErrNotEnoughMemory)
00176 {
00177 if (bufSize >= 65536 + 10 * inputLength)
00178 #if defined(UseNMECppException)
00179 throw NMEError(kNMEErrNotEnoughMemory);
00180 #else
00181 return kNMEErrNotEnoughMemory;
00182 #endif
00183 delete [] buf;
00184 bufSize *= 2;
00185 buf = new NMEChar[bufSize];
00186 }
00187 else
00188 #if defined(UseNMECppException)
00189 throw NMEError(err);
00190 #else
00191 return err;
00192 #endif
00193 }
00194 }
00195
00196 protected:
00197
00198 NMEConstText input;
00199 NMEInt inputLength;
00200
00201 NMEText buf;
00202 NMEInt bufSize;
00203
00204 NMEText output;
00205 NMEInt outputLength;
00206
00207 NMEOutputFormat format;
00208 NMEInt fontSize;
00209 };
00210
00211 #endif