| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ |
| 2 | /* librevenge |
| 3 | * Version: MPL 2.0 / LGPLv2.1+ |
| 4 | * |
| 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | * |
| 9 | * Major Contributor(s): |
| 10 | * Copyright (C) 2004 William Lachance ([email protected]) |
| 11 | * Copyright (C) 2005 Net Integration Technologies (http://www.net-itech.com) |
| 12 | * |
| 13 | * For minor contributions see the git repository. |
| 14 | * |
| 15 | * Alternatively, the contents of this file may be used under the terms |
| 16 | * of the GNU Lesser General Public License Version 2.1 or later |
| 17 | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
| 18 | * applicable instead of those above. |
| 19 | */ |
| 20 | |
| 21 | #include <librevenge/librevenge.h> |
| 22 | |
| 23 | #include <ctype.h> |
| 24 | #include <locale.h> |
| 25 | |
| 26 | #include <sstream> |
| 27 | #include <string> |
| 28 | |
| 29 | namespace librevenge |
| 30 | { |
| 31 | |
| 32 | namespace |
| 33 | { |
| 34 | |
| 35 | static RVNGString doubleToString(const double value) |
| 36 | { |
| 37 | RVNGString tempString; |
| 38 | if (value < 0.0001 && value > -0.0001) |
| 39 | tempString.sprintf("0.0000"); |
| 40 | else |
| 41 | tempString.sprintf("%.4f", value); |
| 42 | #ifndef __ANDROID__ |
| 43 | std::string decimalPoint(localeconv()->decimal_point); |
| 44 | #else |
| 45 | std::string decimalPoint("."); |
| 46 | #endif |
| 47 | if ((decimalPoint.size() == 0) || (decimalPoint == ".")) |
| 48 | return tempString; |
| 49 | std::string stringValue(tempString.cstr()); |
| 50 | if (!stringValue.empty()) |
| 51 | { |
| 52 | std::string::size_type pos; |
| 53 | while ((pos = stringValue.find(decimalPoint)) != std::string::npos) |
| 54 | stringValue.replace(pos,decimalPoint.size(),"."); |
| 55 | } |
| 56 | return RVNGString(stringValue.c_str()); |
| 57 | } |
| 58 | |
| 59 | } // anonymous namespace |
| 60 | |
| 61 | class RVNGStringProperty : public RVNGProperty |
| 62 | { |
| 63 | public: |
| 64 | RVNGStringProperty(const RVNGString &str); |
| 65 | RVNGStringProperty(const char *str); |
| 66 | ~RVNGStringProperty() {} |
| 67 | virtual int getInt() const |
| 68 | { |
| 69 | return 0; |
| 70 | } |
| 71 | virtual double getDouble() const |
| 72 | { |
| 73 | return 0; |
| 74 | } |
| 75 | virtual RVNGUnit getUnit() const |
| 76 | { |
| 77 | return RVNG_UNIT_ERROR; |
| 78 | } |
| 79 | virtual RVNGString getStr() const; |
| 80 | virtual RVNGProperty *clone() const; |
| 81 | |
| 82 | private: |
| 83 | RVNGString m_str; |
| 84 | }; |
| 85 | |
| 86 | class RVNGBinaryDataProperty : public RVNGProperty |
| 87 | { |
| 88 | public: |
| 89 | RVNGBinaryDataProperty(const RVNGBinaryData &data); |
| 90 | RVNGBinaryDataProperty(const unsigned char *buffer, const unsigned long bufferSize); |
| 91 | ~RVNGBinaryDataProperty() {} |
| 92 | virtual int getInt() const |
| 93 | { |
| 94 | return 0; |
| 95 | } |
| 96 | virtual double getDouble() const |
| 97 | { |
| 98 | return 0; |
| 99 | } |
| 100 | virtual RVNGUnit getUnit() const |
| 101 | { |
| 102 | return RVNG_UNIT_ERROR; |
| 103 | } |
| 104 | virtual RVNGString getStr() const; |
| 105 | virtual RVNGProperty *clone() const; |
| 106 | |
| 107 | private: |
| 108 | RVNGBinaryData m_data; |
| 109 | }; |
| 110 | |
| 111 | class RVNGIntProperty : public RVNGProperty |
| 112 | { |
| 113 | public: |
| 114 | RVNGIntProperty(const int val); |
| 115 | ~RVNGIntProperty() {} |
| 116 | virtual int getInt() const; |
| 117 | virtual double getDouble() const; |
| 118 | virtual RVNGUnit getUnit() const |
| 119 | { |
| 120 | return RVNG_GENERIC; |
| 121 | } |
| 122 | virtual RVNGString getStr() const; |
| 123 | virtual RVNGProperty *clone() const; |
| 124 | |
| 125 | private: |
| 126 | int m_val; |
| 127 | }; |
| 128 | |
| 129 | class RVNGBoolProperty : public RVNGIntProperty |
| 130 | { |
| 131 | public: |
| 132 | RVNGBoolProperty(const bool val); |
| 133 | ~RVNGBoolProperty() {} |
| 134 | virtual RVNGUnit getUnit() const |
| 135 | { |
| 136 | return RVNG_UNIT_ERROR; |
| 137 | } |
| 138 | virtual RVNGString getStr() const; |
| 139 | virtual RVNGProperty *clone() const; |
| 140 | }; |
| 141 | |
| 142 | class RVNGDoubleProperty : public RVNGProperty |
| 143 | { |
| 144 | public: |
| 145 | RVNGDoubleProperty(const double val); |
| 146 | ~RVNGDoubleProperty() {} |
| 147 | virtual int getInt() const; |
| 148 | virtual double getDouble() const; |
| 149 | virtual RVNGUnit getUnit() const |
| 150 | { |
| 151 | return RVNG_GENERIC; |
| 152 | } |
| 153 | virtual RVNGString getStr() const; |
| 154 | virtual RVNGProperty *clone() const; |
| 155 | |
| 156 | private: |
| 157 | double m_val; |
| 158 | }; |
| 159 | |
| 160 | class RVNGInchProperty : public RVNGDoubleProperty |
| 161 | { |
| 162 | public: |
| 163 | RVNGInchProperty(const double val); |
| 164 | ~RVNGInchProperty() {} |
| 165 | virtual RVNGUnit getUnit() const |
| 166 | { |
| 167 | return RVNG_INCH; |
| 168 | } |
| 169 | virtual RVNGString getStr() const; |
| 170 | virtual RVNGProperty *clone() const; |
| 171 | }; |
| 172 | |
| 173 | class RVNGPercentProperty : public RVNGDoubleProperty |
| 174 | { |
| 175 | public: |
| 176 | RVNGPercentProperty(const double val); |
| 177 | ~RVNGPercentProperty() {} |
| 178 | virtual RVNGUnit getUnit() const |
| 179 | { |
| 180 | return RVNG_PERCENT; |
| 181 | } |
| 182 | virtual RVNGString getStr() const; |
| 183 | virtual RVNGProperty *clone() const; |
| 184 | }; |
| 185 | |
| 186 | class RVNGPointProperty : public RVNGDoubleProperty |
| 187 | { |
| 188 | public: |
| 189 | RVNGPointProperty(const double val); |
| 190 | ~RVNGPointProperty() {} |
| 191 | virtual RVNGUnit getUnit() const |
| 192 | { |
| 193 | return RVNG_POINT; |
| 194 | } |
| 195 | virtual RVNGString getStr() const; |
| 196 | virtual RVNGProperty *clone() const; |
| 197 | }; |
| 198 | |
| 199 | class RVNGTwipProperty : public RVNGDoubleProperty |
| 200 | { |
| 201 | public: |
| 202 | RVNGTwipProperty(const double val); |
| 203 | ~RVNGTwipProperty() {} |
| 204 | virtual RVNGUnit getUnit() const |
| 205 | { |
| 206 | return RVNG_TWIP; |
| 207 | } |
| 208 | virtual RVNGString getStr() const; |
| 209 | virtual RVNGProperty *clone() const; |
| 210 | }; |
| 211 | |
| 212 | class RVNGGenericProperty : public RVNGDoubleProperty |
| 213 | { |
| 214 | public: |
| 215 | RVNGGenericProperty(const double val); |
| 216 | ~RVNGGenericProperty() {} |
| 217 | virtual RVNGString getStr() const; |
| 218 | virtual RVNGProperty *clone() const; |
| 219 | }; |
| 220 | |
| 221 | RVNGProperty::~RVNGProperty() |
| 222 | { |
| 223 | } |
| 224 | |
| 225 | RVNGStringProperty::RVNGStringProperty(const RVNGString &str) : |
| 226 | m_str(str) |
| 227 | { |
| 228 | } |
| 229 | RVNGStringProperty::RVNGStringProperty(const char *str) : |
| 230 | m_str(str) |
| 231 | { |
| 232 | } |
| 233 | |
| 234 | RVNGString RVNGStringProperty::getStr() const |
| 235 | { |
| 236 | return m_str; |
| 237 | } |
| 238 | |
| 239 | RVNGProperty *RVNGStringProperty::clone() const |
| 240 | { |
| 241 | return new RVNGStringProperty(m_str); |
| 242 | } |
| 243 | |
| 244 | RVNGBinaryDataProperty::RVNGBinaryDataProperty(const RVNGBinaryData &data) : |
| 245 | m_data(data) |
| 246 | { |
| 247 | } |
| 248 | RVNGBinaryDataProperty::RVNGBinaryDataProperty(const unsigned char *buffer, const unsigned long bufferSize) : |
| 249 | m_data(buffer, bufferSize) |
| 250 | { |
| 251 | } |
| 252 | |
| 253 | RVNGString RVNGBinaryDataProperty::getStr() const |
| 254 | { |
| 255 | return m_data.getBase64Data(); |
| 256 | } |
| 257 | |
| 258 | RVNGProperty *RVNGBinaryDataProperty::clone() const |
| 259 | { |
| 260 | return new RVNGBinaryDataProperty(m_data); |
| 261 | } |
| 262 | |
| 263 | RVNGIntProperty::RVNGIntProperty(const int val) : |
| 264 | m_val(val) |
| 265 | { |
| 266 | } |
| 267 | |
| 268 | int RVNGIntProperty::getInt() const |
| 269 | { |
| 270 | return m_val; |
| 271 | } |
| 272 | |
| 273 | double RVNGIntProperty::getDouble() const |
| 274 | { |
| 275 | return (double)m_val; |
| 276 | } |
| 277 | |
| 278 | RVNGString RVNGIntProperty::getStr() const |
| 279 | { |
| 280 | RVNGString str; |
| 281 | str.sprintf("%d", m_val); |
| 282 | return str; |
| 283 | } |
| 284 | |
| 285 | RVNGProperty *RVNGIntProperty::clone() const |
| 286 | { |
| 287 | return new RVNGIntProperty(m_val); |
| 288 | } |
| 289 | |
| 290 | RVNGBoolProperty::RVNGBoolProperty(const bool val) : |
| 291 | RVNGIntProperty(val) |
| 292 | { |
| 293 | } |
| 294 | |
| 295 | RVNGString RVNGBoolProperty::getStr() const |
| 296 | { |
| 297 | if (getInt()) |
| 298 | return "true"; |
| 299 | else |
| 300 | return "false"; |
| 301 | } |
| 302 | |
| 303 | RVNGProperty *RVNGBoolProperty::clone() const |
| 304 | { |
| 305 | return new RVNGBoolProperty(getInt() != 0); |
| 306 | } |
| 307 | |
| 308 | RVNGDoubleProperty::RVNGDoubleProperty(const double val) : |
| 309 | m_val(val) |
| 310 | { |
| 311 | } |
| 312 | |
| 313 | int RVNGDoubleProperty::getInt() const |
| 314 | { |
| 315 | return (int)m_val; |
| 316 | } |
| 317 | |
| 318 | double RVNGDoubleProperty::getDouble() const |
| 319 | { |
| 320 | return m_val; |
| 321 | } |
| 322 | |
| 323 | RVNGString RVNGDoubleProperty::getStr() const |
| 324 | { |
| 325 | RVNGString str = doubleToString(getDouble()); |
| 326 | return str; |
| 327 | } |
| 328 | |
| 329 | RVNGProperty *RVNGDoubleProperty::clone() const |
| 330 | { |
| 331 | return new RVNGDoubleProperty(getDouble()); |
| 332 | } |
| 333 | |
| 334 | RVNGInchProperty::RVNGInchProperty(const double val) : |
| 335 | RVNGDoubleProperty(val) |
| 336 | { |
| 337 | } |
| 338 | |
| 339 | RVNGString RVNGInchProperty::getStr() const |
| 340 | { |
| 341 | RVNGString str = doubleToString(getDouble()); |
| 342 | str.append("in"); |
| 343 | return str; |
| 344 | } |
| 345 | |
| 346 | RVNGProperty *RVNGInchProperty::clone() const |
| 347 | { |
| 348 | return new RVNGInchProperty(getDouble()); |
| 349 | } |
| 350 | |
| 351 | RVNGPercentProperty::RVNGPercentProperty(const double val) : |
| 352 | RVNGDoubleProperty(val) |
| 353 | { |
| 354 | } |
| 355 | |
| 356 | RVNGString RVNGPercentProperty::getStr() const |
| 357 | { |
| 358 | RVNGString str = doubleToString(getDouble()*100.0); |
| 359 | str.append("%"); |
| 360 | return str; |
| 361 | } |
| 362 | |
| 363 | RVNGProperty *RVNGPercentProperty::clone() const |
| 364 | { |
| 365 | return new RVNGPercentProperty(getDouble()); |
| 366 | } |
| 367 | |
| 368 | RVNGPointProperty::RVNGPointProperty(const double val) : |
| 369 | RVNGDoubleProperty(val) |
| 370 | { |
| 371 | } |
| 372 | |
| 373 | RVNGString RVNGPointProperty::getStr() const |
| 374 | { |
| 375 | RVNGString str = doubleToString(getDouble()); |
| 376 | str.append("pt"); |
| 377 | return str; |
| 378 | } |
| 379 | |
| 380 | RVNGProperty *RVNGPointProperty::clone() const |
| 381 | { |
| 382 | return new RVNGPointProperty(getDouble()); |
| 383 | } |
| 384 | |
| 385 | RVNGTwipProperty::RVNGTwipProperty(const double val) : |
| 386 | RVNGDoubleProperty(val) |
| 387 | { |
| 388 | } |
| 389 | |
| 390 | RVNGString RVNGTwipProperty::getStr() const |
| 391 | { |
| 392 | RVNGString str; |
| 393 | str.sprintf("%i*", getInt()); |
| 394 | return str; |
| 395 | } |
| 396 | |
| 397 | RVNGProperty *RVNGTwipProperty::clone() const |
| 398 | { |
| 399 | return new RVNGTwipProperty(getDouble()); |
| 400 | } |
| 401 | |
| 402 | RVNGProperty *RVNGPropertyFactory::newStringProp(const RVNGString &str) |
| 403 | { |
| 404 | return new RVNGStringProperty(str); |
| 405 | } |
| 406 | |
| 407 | RVNGProperty *RVNGPropertyFactory::newStringProp(const char *str) |
| 408 | { |
| 409 | return new RVNGStringProperty(str); |
| 410 | } |
| 411 | |
| 412 | RVNGProperty *RVNGPropertyFactory::newBinaryDataProp(const RVNGBinaryData &data) |
| 413 | { |
| 414 | return new RVNGBinaryDataProperty(data); |
| 415 | } |
| 416 | |
| 417 | RVNGProperty *RVNGPropertyFactory::newBinaryDataProp(const unsigned char *buffer, const unsigned long bufferSize) |
| 418 | { |
| 419 | return new RVNGBinaryDataProperty(buffer, bufferSize); |
| 420 | } |
| 421 | |
| 422 | RVNGProperty *RVNGPropertyFactory::newIntProp(const int val) |
| 423 | { |
| 424 | return new RVNGIntProperty(val); |
| 425 | } |
| 426 | |
| 427 | RVNGProperty *RVNGPropertyFactory::newBoolProp(const bool val) |
| 428 | { |
| 429 | return new RVNGBoolProperty(val); |
| 430 | } |
| 431 | |
| 432 | RVNGProperty *RVNGPropertyFactory::newDoubleProp(const double val) |
| 433 | { |
| 434 | return new RVNGDoubleProperty(val); |
| 435 | } |
| 436 | |
| 437 | RVNGProperty *RVNGPropertyFactory::newInchProp(const double val) |
| 438 | { |
| 439 | return new RVNGInchProperty(val); |
| 440 | } |
| 441 | |
| 442 | RVNGProperty *RVNGPropertyFactory::newPercentProp(const double val) |
| 443 | { |
| 444 | return new RVNGPercentProperty(val); |
| 445 | } |
| 446 | |
| 447 | RVNGProperty *RVNGPropertyFactory::newPointProp(const double val) |
| 448 | { |
| 449 | return new RVNGPointProperty(val); |
| 450 | } |
| 451 | |
| 452 | RVNGProperty *RVNGPropertyFactory::newTwipProp(const double val) |
| 453 | { |
| 454 | return new RVNGTwipProperty(val); |
| 455 | } |
| 456 | |
| 457 | } |
| 458 | |
| 459 | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |