00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include <lestes/common.hh>
00035 #include <lestes/msg/message_stencil.hh>
00036
00037 package(lestes);
00038 package(msg);
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 message_stencil::message_stencil(ulint a_nparams, const lstring &a_format, flags_type a_flags):
00049 nparams(a_nparams),
00050 kind(kind_counter++),
00051 flags(a_flags),
00052 texts(texts_type::create()),
00053 params(params_type::create())
00054 {
00055
00056 parse(a_format);
00057 }
00058
00059
00060
00061
00062
00063 ulint message_stencil::nparams_get(void) const
00064 {
00065 return nparams;
00066 }
00067
00068
00069
00070
00071
00072
00073 ulint message_stencil::kind_get(void) const
00074 {
00075 return kind;
00076 }
00077
00078
00079
00080
00081
00082
00083
00084 bool message_stencil::equals(const ptr<message_stencil> &other) const
00085 {
00086 return other && other == this;
00087 }
00088
00089
00090
00091
00092
00093
00094 void message_stencil::parse(const lstring &a_format)
00095 {
00096 bool percent = false;
00097
00098 ulint start = 0;
00099 lstring out;
00100
00101 for (ulint i = 0, len = a_format.length(); i < len; i++) {
00102 char c = a_format[i];
00103 if (percent) {
00104
00105 out += a_format.substr(start,i - start - 1);
00106 if (c == '%') {
00107 out += '%';
00108 } else {
00109 ucn u = character::create_from_host(c);
00110
00111 lassert2(character::is_digit(u),"Invalid `%' sequence.");
00112 ulint x = character::extract_digit(u);
00113 lassert2(x < nparams,"Invalid parameter in `%' sequence.");
00114
00115 if (out.length()) {
00116
00117 texts->push_back(out);
00118 out = "";
00119
00120 params->push_back(nparams);
00121 }
00122 params->push_back(x);
00123 }
00124 start = i + 1;
00125 percent = false;
00126 } else {
00127 percent = (c == '%');
00128 }
00129 }
00130
00131 lassert2(!percent,"Unterminated `%' sequence.");
00132
00133 out += a_format.substr(start);
00134 if (out.length()) {
00135
00136 texts->push_back(out);
00137
00138 params->push_back(nparams);
00139 }
00140 }
00141
00142
00143
00144
00145
00146
00147
00148 ptr<message> message_stencil::generate(const ptr<args_type> &args) const
00149 {
00150 lassert(args);
00151 lassert(args->size() == nparams);
00152
00153 lstring result;
00154
00155 texts_type::iterator tit = texts->begin();
00156 texts_type::iterator tend = texts->end();
00157
00158 for (params_type::iterator it = params->begin(), end = params->end(); it != end; ++it) {
00159 ulint idx = *it;
00160 if (idx == nparams) {
00161 lassert(tit != tend);
00162 result += *tit;
00163 ++tit;
00164 } else {
00165 lassert(idx < nparams);
00166
00167 result += args->operator[](idx);
00168 }
00169 }
00170
00171
00172 return message::create(kind,result,flags);
00173 }
00174
00175
00176
00177
00178 void message_stencil::gc_mark(void)
00179 {
00180 texts.gc_mark();
00181 params.gc_mark();
00182 object::gc_mark();
00183 }
00184
00185
00186
00187
00188 ulint message_stencil::kind_counter = 0;
00189
00190 end_package(msg);
00191 end_package(lestes);
00192
00193