|
|
| version 1.39, 2004/07/28 14:38:20 | version 1.47, 2007/04/23 16:34:03 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: @b math parser class. | Parser: @b math parser class. |
| Copyright(c) 2001-2004 ArtLebedev Group(http://www.artlebedev.com) | Copyright(c) 2001-2005 ArtLebedev Group(http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru>(http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru>(http://paf.design.ru) |
| portions from gen_uuid.c, | portions from gen_uuid.c, |
| Line 164 static inline int _random(uint top) { | Line 164 static inline int _random(uint top) { |
| } | } |
| static void _random(Request& r, MethodParams& params) { | static void _random(Request& r, MethodParams& params) { |
| Value& range=params.as_junction(0, "range must be expression"); | double top=params.as_double(0, "range must be expression", r); |
| double top=r.process_to_value(range).as_double(); | |
| if(top<=0 || top>MAX_UINT) | if(top<=0 || top>MAX_UINT) |
| throw Exception("parser.runtime", | throw Exception(PARSER_RUNTIME, |
| 0, | 0, |
| "top(%g) must be [1..%u]", top, MAX_UINT); | "top(%g) must be [1..%u]", top, MAX_UINT); |
| Line 181 static double degrees(double param) { re | Line 180 static double degrees(double param) { re |
| static double radians(double param) { return param /180 *PI; } | static double radians(double param) { return param /180 *PI; } |
| static void math1(Request& r, MethodParams& params, math1_func_ptr func) { | static void math1(Request& r, MethodParams& params, math1_func_ptr func) { |
| Value& param=params.as_junction(0, "parameter must be expression"); | double param=params.as_double(0, "parameter must be expression", r); |
| double result=func(param); | |
| double result=func(r.process_to_value(param).as_double()); | |
| r.write_no_lang(*new VDouble(result)); | r.write_no_lang(*new VDouble(result)); |
| } | } |
| Line 198 static void math1(Request& r, MethodPara | Line 196 static void math1(Request& r, MethodPara |
| MATH1(round); MATH1(floor); MATH1P(ceiling, ceil); | MATH1(round); MATH1(floor); MATH1P(ceiling, ceil); |
| MATH1(trunc); MATH1(frac); | MATH1(trunc); MATH1(frac); |
| MATH1P(abs, fabs); MATH1(sign); | MATH1P(abs, fabs); MATH1(sign); |
| MATH1(exp); MATH1(log); | MATH1(exp); |
| MATH1(log); MATH1P(ln, log); MATH1(log10); | |
| MATH1(sin); MATH1(asin); | MATH1(sin); MATH1(asin); |
| MATH1(cos); MATH1(acos); | MATH1(cos); MATH1(acos); |
| MATH1(tan); MATH1(atan); | MATH1(tan); MATH1(atan); |
| Line 208 MATH1(sqrt); | Line 207 MATH1(sqrt); |
| typedef double (*math2_func_ptr)(double, double); | typedef double (*math2_func_ptr)(double, double); |
| static void math2(Request& r, MethodParams& params, math2_func_ptr func) { | static void math2(Request& r, MethodParams& params, math2_func_ptr func) { |
| Value& a=params.as_junction(0, "parameter must be expression"); | double a=params.as_double(0, "parameter must be expression", r); |
| Value& b=params.as_junction(1, "parameter must be expression"); | double b=params.as_double(1, "parameter must be expression", r); |
| double result=func(a, b); | |
| double result=func( | |
| r.process_to_value(a).as_double(), | |
| r.process_to_value(b).as_double()); | |
| r.write_no_lang(*new VDouble(result)); | r.write_no_lang(*new VDouble(result)); |
| } | } |
| Line 273 static void _crypt(Request& r, MethodPar | Line 269 static void _crypt(Request& r, MethodPar |
| if(!static_sample_buf // nothing generated | if(!static_sample_buf // nothing generated |
| || !static_sample_buf[0] // generated nothing | || !static_sample_buf[0] // generated nothing |
| || strncmp(static_sample_buf, normal_salt, prefix_size)!=0) // salt prefix not preserved | || strncmp(static_sample_buf, normal_salt, prefix_size)!=0) // salt prefix not preserved |
| throw Exception("parser.runtime", | throw Exception(PARSER_RUNTIME, |
| 0, | 0, |
| "crypt on this platform does not support '%.*s' salt prefix", prefix_size, normal_salt); | "crypt on this platform does not support '%.*s' salt prefix", prefix_size, normal_salt); |
| r.write_pass_lang(String(pa_strdup(static_sample_buf))); | r.write_pass_lang(String(pa_strdup(static_sample_buf))); |
| #else | #else |
| throw Exception("parser.runtime", | throw Exception(PARSER_RUNTIME, |
| 0, | 0, |
| "salt must start with '" PA_MD5PW_ID "'"); | "salt must start with '" PA_MD5PW_ID "'"); |
| #endif | #endif |
| } | } |
| } | } |
| static const char* hex_string(unsigned char* bytes, size_t size, bool upcase) { | |
| char *bytes_hex=new(PointerFreeGC) char [size*2/*byte->hh*/+1/*for zero-teminator*/]; | |
| unsigned char *src=bytes; | |
| unsigned char *end=bytes+size; | |
| char *dest=bytes_hex; | |
| static const char *hex=upcase?"0123456789ABCDEF":"0123456789abcdef"; | |
| for(; src<end; src++) { | |
| *dest++=hex[*src/0x10]; | |
| *dest++=hex[*src%0x10]; | |
| } | |
| *dest=0; | |
| return bytes_hex; | |
| } | |
| static void _md5(Request& r, MethodParams& params) { | static void _md5(Request& r, MethodParams& params) { |
| const char *string=params.as_string(0, "parameter must be string").cstr(); | const char *string=params.as_string(0, "parameter must be string").cstr(); |
| PA_MD5_CTX context; | PA_MD5_CTX context; |
| unsigned char digest[16]; | unsigned char digest[16]; |
| pa_MD5Init(&context); | pa_MD5Init(&context); |
| Line 367 static void _uid64(Request& r, MethodPar | Line 345 static void _uid64(Request& r, MethodPar |
| r.write_pass_lang(*new String(hex_string(id, sizeof(id), true))); | r.write_pass_lang(*new String(hex_string(id, sizeof(id), true))); |
| } | } |
| static void _crc32(Request& r, MethodParams& params) { | |
| const char *string=params.as_string(0, "parameter must be string").cstr(); | |
| r.write_no_lang(*new VInt(pa_crc32(string, strlen(string)))); | |
| } | |
| // constructor | // constructor |
| MMath::MMath(): Methoded("math") { | MMath::MMath(): Methoded("math") { |
| Line 380 MMath::MMath(): Methoded("math") { | Line 363 MMath::MMath(): Methoded("math") { |
| ADD1(round); ADD1(floor); ADD1(ceiling); | ADD1(round); ADD1(floor); ADD1(ceiling); |
| ADD1(trunc); ADD1(frac); | ADD1(trunc); ADD1(frac); |
| ADD1(abs); ADD1(sign); | ADD1(abs); ADD1(sign); |
| ADD1(exp); ADD1(log); | ADD1(exp); |
| ADD1(log); ADD1(ln); ADD1(log10); | |
| ADD1(sin); ADD1(asin); | ADD1(sin); ADD1(asin); |
| ADD1(cos); ADD1(acos); | ADD1(cos); ADD1(acos); |
| ADD1(tan); ADD1(atan); | ADD1(tan); ADD1(atan); |
| Line 397 MMath::MMath(): Methoded("math") { | Line 381 MMath::MMath(): Methoded("math") { |
| // ^md5[string] | // ^md5[string] |
| ADD1(md5); | ADD1(md5); |
| ADD1(crc32); | |
| // ^uuid[] | // ^uuid[] |
| ADD0(uuid); | ADD0(uuid); |