Go to the documentation of this file.00001
00012
00013
00014 #ifndef __NMECpp__
00015 #define __NMECpp__
00016
00017 #include "NME.h"
00018 #include "NMEErrorCpp.h"
00019 #include <string.h>
00020
00031 class NME
00032 {
00033 public:
00034
00037 NME()
00038 {
00039 input = NULL;
00040 buf = NULL;
00041 output = NULL;
00042 format = NMEOutputFormatText;
00043 fontSize = 0;
00044 }
00045
00051 NME(char const *input, int inputLength = -1)
00052 {
00053 this->input = input;
00054 if (inputLength >= 0)
00055 this->inputLength = inputLength;
00056 else
00057 this->inputLength = strlen(input);
00058 buf = output = NULL;
00059 format = NMEOutputFormatText;
00060 fontSize = 0;
00061 }
00062
00066 NME(NME const &nme)
00067 {
00068 input = nme.input;
00069 inputLength = nme.inputLength;
00070 buf = output = NULL;
00071 format = nme.format;
00072 fontSize = nme.fontSize;
00073 }
00074
00076 ~NME()
00077 {
00078 if (buf)
00079 delete [] buf;
00080 }
00081
00086 NME operator = (NME const &nme)
00087 {
00088 if (this != &nme)
00089 {
00090 input = nme.input;
00091 inputLength = nme.inputLength;
00092 if (buf)
00093 delete [] buf;
00094 buf = output = NULL;
00095 format = nme.format;
00096 fontSize = nme.fontSize;
00097 }
00098 return *this;
00099 }
00100
00106 void setInput(char const *input, int inputLength = -1)
00107 {
00108 this->input = input;
00109 if (inputLength >= 0)
00110 this->inputLength = inputLength;
00111 else
00112 this->inputLength = strlen(input);
00113 output = NULL;
00114 }
00115
00119 void setFormat(NMEOutputFormat const &format)
00120 {
00121 this->format = format;
00122 output = NULL;
00123 }
00124
00128 void setFontSize(NMEInt fontSize = 0)
00129 {
00130 this->fontSize = fontSize;
00131 output = NULL;
00132 }
00133
00139 NMEErr getOutput(NMEConstText *output, NMEInt *outputLength = NULL)
00140 {
00141 if (this->output)
00142 {
00143 if (output)
00144 *output = this->output;
00145 if (outputLength)
00146 *outputLength = this->outputLength;
00147 return kNMEErrOk;
00148 }
00149
00150 if (!input || inputLength == 0)
00151 {
00152 if (output)
00153 *output = "";
00154 if (outputLength)
00155 *outputLength = 0;
00156 }
00157
00158 if (!buf)
00159 {
00160 bufSize = 1024 + 2 * inputLength;
00161 buf = new NMEChar[bufSize];
00162 }
00163
00164 for (;;)
00165 {
00166 NMEErr err = NMEProcess(input, inputLength,
00167 buf, bufSize,
00168 kNMEProcessOptDefault, "\n", &format, fontSize,
00169 &this->output, &this->outputLength, NULL);
00170 if (err == kNMEErrOk)
00171 {
00172 if (output)
00173 *output = this->output;
00174 if (outputLength)
00175 *outputLength = this->outputLength;
00176 return kNMEErrOk;
00177 }
00178 else if (err == kNMEErrNotEnoughMemory)
00179 {
00180 if (bufSize >= 65536 + 10 * inputLength)
00181 #if defined(UseNMECppException)
00182 throw NMEError(kNMEErrNotEnoughMemory);
00183 #else
00184 return kNMEErrNotEnoughMemory;
00185 #endif
00186 delete [] buf;
00187 bufSize *= 2;
00188 buf = new NMEChar[bufSize];
00189 }
00190 else
00191 #if defined(UseNMECppException)
00192 throw NMEError(err);
00193 #else
00194 return err;
00195 #endif
00196 }
00197 }
00198
00199 protected:
00200
00201 NMEConstText input;
00202 NMEInt inputLength;
00203
00204 NMEText buf;
00205 NMEInt bufSize;
00206
00207 NMEText output;
00208 NMEInt outputLength;
00209
00210 NMEOutputFormat format;
00211 NMEInt fontSize;
00212 };
00213
00214 #endif