| 1 | /* |
| 2 | www.sourceforge.net/projects/tinyxpath |
| 3 | Copyright (c) 2002-2004 Yves Berquin ([email protected]) |
| 4 | |
| 5 | This software is provided 'as-is', without any express or implied |
| 6 | warranty. In no event will the authors be held liable for any |
| 7 | damages arising from the use of this software. |
| 8 | |
| 9 | Permission is granted to anyone to use this software for any |
| 10 | purpose, including commercial applications, and to alter it and |
| 11 | redistribute it freely, subject to the following restrictions: |
| 12 | |
| 13 | 1. The origin of this software must not be misrepresented; you must |
| 14 | not claim that you wrote the original software. If you use this |
| 15 | software in a product, an acknowledgment in the product documentation |
| 16 | would be appreciated but is not required. |
| 17 | |
| 18 | 2. Altered source versions must be plainly marked as such, and |
| 19 | must not be misrepresented as being the original software. |
| 20 | |
| 21 | 3. This notice may not be removed or altered from any source |
| 22 | distribution. |
| 23 | */ |
| 24 | |
| 25 | #include "xpath_expression.h" |
| 26 | #include "lex_util.h" |
| 27 | |
| 28 | namespace TinyXPath |
| 29 | { |
| 30 | |
| 31 | /// Get the expression_result as an int |
| 32 | int expression_result::i_get_int () |
| 33 | { |
| 34 | switch (e_type) |
| 35 | { |
| 36 | case e_int : |
| 37 | return i_content; |
| 38 | case e_bool : |
| 39 | return o_content ? 1 : 0; |
| 40 | case e_double : |
| 41 | return (int) (d_content); |
| 42 | default : |
| 43 | return atoi (S_get_string () . c_str ()); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /// Get the expression_result as a double |
| 48 | double expression_result::d_get_double () |
| 49 | { |
| 50 | switch (e_type) |
| 51 | { |
| 52 | case e_double : |
| 53 | return d_content; |
| 54 | case e_int : |
| 55 | return (double) i_content; |
| 56 | case e_bool : |
| 57 | return o_content ? 1.0 : 0.0; |
| 58 | default : |
| 59 | return atof (S_get_string () . c_str ()); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /// Get the expression_result as a string |
| 64 | TIXML_STRING expression_result::S_get_string () |
| 65 | { |
| 66 | TIXML_STRING S_res; |
| 67 | node_set * nsp_ptr; |
| 68 | |
| 69 | S_res = ""; |
| 70 | switch (e_type) |
| 71 | { |
| 72 | case e_string : |
| 73 | S_res = S_content; |
| 74 | break; |
| 75 | case e_int : |
| 76 | v_assign_int_to_string (S_res, i_get_int ()); |
| 77 | break; |
| 78 | case e_double : |
| 79 | v_assign_double_to_string (S_res, d_get_double ()); |
| 80 | break; |
| 81 | case e_node_set : |
| 82 | // See XPath 1.0 spec, 3.2 : |
| 83 | // An argument is converted to type string as if by calling the string function |
| 84 | // ... |
| 85 | // A node-set is converted to a string by returning the string-value of the node |
| 86 | // in the node-set that is first in document order. If the node-set is empty, an empty string is returned. |
| 87 | nsp_ptr = nsp_get_node_set (); |
| 88 | if (nsp_ptr -> u_get_nb_node_in_set ()) |
| 89 | { |
| 90 | nsp_ptr -> v_document_sort (); |
| 91 | if (nsp_ptr -> o_is_attrib (0)) |
| 92 | S_res = nsp_ptr -> XAp_get_attribute_in_set (0) -> Value (); |
| 93 | else |
| 94 | S_res = nsp_ptr -> XNp_get_node_in_set (0) -> Value (); |
| 95 | } |
| 96 | break; |
| 97 | case e_bool : |
| 98 | if (o_get_bool ()) |
| 99 | S_res = "true"; |
| 100 | else |
| 101 | S_res = "false"; |
| 102 | break; |
| 103 | |
| 104 | // jan 21th, 07 - neoneurone: silently ignore the other cases |
| 105 | default: |
| 106 | break; |
| 107 | } |
| 108 | return S_res; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | Get the expression_result as a bool\n |
| 113 | The boolean function converts its argument to a boolean as follows: |
| 114 | - a number is true if and only if it is neither positive or negative zero nor NaN |
| 115 | - a node-set is true if and only if it is non-empty |
| 116 | - a string is true if and only if its length is non-zero |
| 117 | - an object of a type other than the four basic types is converted to a boolean in a way that is dependent on that type |
| 118 | */ |
| 119 | bool expression_result::o_get_bool () |
| 120 | { |
| 121 | node_set * nsp_ptr; |
| 122 | switch (e_type) |
| 123 | { |
| 124 | case e_int : |
| 125 | return i_content != 0; |
| 126 | case e_double : |
| 127 | return (d_get_double () == 0.0); |
| 128 | case e_string : |
| 129 | return S_content . length () > 0; |
| 130 | break; |
| 131 | case e_node_set : |
| 132 | // See XPath 1.0 spec, 3.2 : |
| 133 | // An argument is converted to type string as if by calling the string function |
| 134 | // ... |
| 135 | // A node-set is converted to a string by returning the string-value of the node |
| 136 | // in the node-set that is first in document order. If the node-set is empty, an empty string is returned. |
| 137 | nsp_ptr = nsp_get_node_set (); |
| 138 | return nsp_ptr -> u_get_nb_node_in_set () != 0; |
| 139 | case e_bool : |
| 140 | return o_content; |
| 141 | |
| 142 | // jan 21th, 07 - neoneurone: silently ignore the other cases |
| 143 | default: |
| 144 | break; |
| 145 | } |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | #ifdef TINYXPATH_DEBUG |
| 150 | |
| 151 | /// Debug function to print an expression_result to stdout |
| 152 | void expression_result::v_dump () |
| 153 | { |
| 154 | switch (e_type) |
| 155 | { |
| 156 | case e_bool : |
| 157 | printf (" bool : %s\n", o_content ? "true" : "false"); |
| 158 | break; |
| 159 | case e_string : |
| 160 | printf (" string : %s\n", S_content . c_str ()); |
| 161 | break; |
| 162 | case e_int : |
| 163 | printf (" int : %d", i_content); |
| 164 | if (S_comment . length ()) |
| 165 | printf (" (%s)", S_comment . c_str ()); |
| 166 | printf ("\n"); |
| 167 | break; |
| 168 | case e_double : |
| 169 | printf (" double : %f\n", d_content); |
| 170 | break; |
| 171 | case e_node_set : |
| 172 | printf (" node set\n"); |
| 173 | ns_set . v_dump (); |
| 174 | break; |
| 175 | case e_invalid : |
| 176 | printf (" (invalid)\n"); |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | #endif |
| 181 | |
| 182 | } |