Debian Codegraph @

src/lib/MWAWCell.cxx

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2002 William Lachance ([email protected])
18 * Copyright (C) 2002,2004 Marc Maurer ([email protected])
19 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
20 * Copyright (C) 2006, 2007 Andrew Ziem
21 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
22 *
23 *
24 * All Rights Reserved.
25 *
26 * For minor contributions see the git repository.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
30 * in which case the provisions of the LGPLv2+ are applicable
31 * instead of those above.
32 */
33
34 /** \file MWAWCell.cxx
35 * Implements MWAWCell (cell content and format)
36 */
37 #include <time.h>
38
39 #include <cmath>
40 #include <iomanip>
41 #include <iostream>
42 #include <sstream>
43
44 #include <librevenge/librevenge.h>
45
46 #include "MWAWFontConverter.hxx"
47 #include "MWAWListener.hxx"
48
49 #include "MWAWCell.hxx"
50
51 ////////////////////////////////////////////////////////////
52 // MWAWCell::Format
53 ////////////////////////////////////////////////////////////
54 MWAWCell::Format::~Format()
55 {
56 }
57
58 std::string MWAWCell::Format::getValueType() const
59 {
60 switch (m_format) {
61 case F_NUMBER:
62 if (m_numberFormat==F_NUMBER_CURRENCY) return "currency";
63 if (m_numberFormat==F_NUMBER_PERCENT) return "percent";
64 if (m_numberFormat==F_NUMBER_SCIENTIFIC) return "scientific";
65 return "float";
66 case F_BOOLEAN:
67 return "boolean";
68 case F_DATE:
69 return "date";
70 case F_TIME:
71 return "time";
72 case F_TEXT:
73 case F_UNKNOWN:
74 #if !defined(__clang__)
75 default:
76 #endif
77 break;
78 }
79 return "float";
80 }
81
82 bool MWAWCell::Format::getNumberingProperties(librevenge::RVNGPropertyList &propList) const
83 {
84 librevenge::RVNGPropertyListVector pVect;
85 switch (m_format) {
86 case F_BOOLEAN:
87 propList.insert("librevenge:value-type", "boolean");
88 break;
89 case F_NUMBER:
90 if (m_digits>-1000)
91 propList.insert("number:decimal-places", m_digits);
92 if (m_thousandHasSeparator)
93 propList.insert("number:grouping", true);
94 switch (m_numberFormat) {
95 case F_NUMBER_GENERIC:
96 propList.insert("librevenge:value-type", "number");
97 propList.remove("number:decimal-places");
98 break;
99 case F_NUMBER_SCIENTIFIC:
100 propList.insert("librevenge:value-type", "scientific");
101 break;
102 case F_NUMBER_PERCENT:
103 propList.insert("librevenge:value-type", "percentage");
104 break;
105 case F_NUMBER_DECIMAL:
106 propList.insert("librevenge:value-type", "number");
107 if (m_integerDigits>=0) {
108 propList.insert("number:min-integer-digits", m_integerDigits+1);
109 propList.insert("number:decimal-places", 0);
110 }
111 break;
112 case F_NUMBER_FRACTION:
113 propList.insert("librevenge:value-type", "fraction");
114 propList.insert("number:min-integer-digits", 0);
115 propList.insert("number:min-numerator-digits", m_numeratorDigits>0 ? m_numeratorDigits : 1);
116 propList.insert("number:min-denominator-digits", m_denominatorDigits>0 ? m_denominatorDigits : 1);
117 propList.remove("number:decimal-places");
118 break;
119 case F_NUMBER_CURRENCY: {
120 propList.clear();
121 propList.insert("librevenge:value-type", "currency");
122 librevenge::RVNGPropertyList list;
123 list.insert("librevenge:value-type", "currency-symbol");
124 list.insert("number:language","en");
125 list.insert("number:country","US");
126 list.insert("librevenge:currency",m_currencySymbol.c_str());
127 pVect.append(list);
128
129 list.clear();
130 list.insert("librevenge:value-type", "number");
131 if (m_digits>-1000)
132 list.insert("number:decimal-places", m_digits);
133 pVect.append(list);
134 break;
135 }
136 case F_NUMBER_UNKNOWN:
137 #if !defined(__clang__)
138 default:
139 #endif
140 return false;
141 }
142 break;
143 case F_DATE:
144 propList.insert("librevenge:value-type", "date");
145 propList.insert("number:automatic-order", "true");
146 if (!libmwaw::convertDTFormat(m_DTFormat.empty() ? "%m/%d/%Y" : m_DTFormat, pVect))
147 return false;
148 break;
149 case F_TIME:
150 propList.insert("librevenge:value-type", "time");
151 propList.insert("number:automatic-order", "true");
152 if (!libmwaw::convertDTFormat(m_DTFormat.empty() ? "%H:%M:%S" : m_DTFormat, pVect))
153 return false;
154 break;
155 case F_TEXT:
156 case F_UNKNOWN:
157 #if !defined(__clang__)
158 default:
159 #endif
160 return false;
161 }
162 if (pVect.count())
163 propList.insert("librevenge:format", pVect);
164 return true;
165 }
166
167 std::ostream &operator<<(std::ostream &o, MWAWCell::Format const &format)
168 {
169 switch (format.m_format) {
170 case MWAWCell::F_BOOLEAN:
171 o << "boolean";
172 break;
173 case MWAWCell::F_TEXT:
174 o << "text";
175 break;
176 case MWAWCell::F_NUMBER:
177 o << "number";
178 switch (format.m_numberFormat) {
179 case MWAWCell::F_NUMBER_GENERIC:
180 break;
181 case MWAWCell::F_NUMBER_DECIMAL:
182 o << "[decimal]";
183 break;
184 case MWAWCell::F_NUMBER_SCIENTIFIC:
185 o << "[exp]";
186 break;
187 case MWAWCell::F_NUMBER_PERCENT:
188 o << "[percent]";
189 break;
190 case MWAWCell::F_NUMBER_CURRENCY:
191 o << "[money=" << format.m_currencySymbol << "]";
192 break;
193 case MWAWCell::F_NUMBER_FRACTION:
194 o << "[fraction]";
195 break;
196 case MWAWCell::F_NUMBER_UNKNOWN:
197 #if !defined(__clang__)
198 default:
199 #endif
200 MWAW_DEBUG_MSG(("MWAWCell::operator<<(Format): find unexpected type\n"));
201 o << "###format,";
202 break;
203 }
204 if (format.m_thousandHasSeparator)
205 o << "[thousandSep]";
206 if (format.m_parenthesesForNegative)
207 o << "[parenthesis<0]";
208 break;
209 case MWAWCell::F_DATE:
210 o << "date[" << format.m_DTFormat << "]";
211 break;
212 case MWAWCell::F_TIME:
213 o << "time[" << format.m_DTFormat << "]";
214 break;
215 case MWAWCell::F_UNKNOWN:
216 #if !defined(__clang__)
217 default:
218 #endif
219 break; // default
220 }
221 o << ",";
222
223 if (format.m_digits > -1000) o << "digits=" << format.m_digits << ",";
224 if (format.m_integerDigits != -1) o << "digits[min]=" << format.m_integerDigits << ",";
225 if (format.m_numeratorDigits != -1) o << "digits[num]=" << format.m_numeratorDigits << ",";
226 if (format.m_denominatorDigits != -1) o << "digits[den]=" << format.m_denominatorDigits << ",";
227 return o;
228 }
229
230 int MWAWCell::Format::compare(MWAWCell::Format const &cell) const
231 {
232 if (m_format<cell.m_format) return 1;
233 if (m_format>cell.m_format) return -1;
234 if (m_numberFormat<cell.m_numberFormat) return 1;
235 if (m_numberFormat>cell.m_numberFormat) return -1;
236 if (m_digits<cell.m_digits) return 1;
237 if (m_digits>cell.m_digits) return -1;
238 if (m_integerDigits<cell.m_integerDigits) return 1;
239 if (m_integerDigits>cell.m_integerDigits) return -1;
240 if (m_numeratorDigits<cell.m_numeratorDigits) return 1;
241 if (m_numeratorDigits>cell.m_numeratorDigits) return -1;
242 if (m_denominatorDigits<cell.m_denominatorDigits) return 1;
243 if (m_denominatorDigits>cell.m_denominatorDigits) return -1;
244 if (m_thousandHasSeparator!=cell.m_thousandHasSeparator) return m_thousandHasSeparator ? -1:1;
245 if (m_parenthesesForNegative!=cell.m_parenthesesForNegative) return m_parenthesesForNegative ? -1:1;
246 if (m_DTFormat<cell.m_DTFormat) return 1;
247 if (m_DTFormat>cell.m_DTFormat) return -1;
248 if (m_currencySymbol<cell.m_currencySymbol) return 1;
249 if (m_currencySymbol>cell.m_currencySymbol) return -1;
250 return 0;
251 }
252 ////////////////////////////////////////////////////////////
253 // MWAWCell
254 ////////////////////////////////////////////////////////////
255 void MWAWCell::addTo(librevenge::RVNGPropertyList &propList, std::shared_ptr<MWAWFontConverter> fontConverter) const
256 {
257 propList.insert("librevenge:column", position()[0]);
258 propList.insert("librevenge:row", position()[1]);
259
260 propList.insert("table:number-columns-spanned", numSpannedCells()[0]);
261 propList.insert("table:number-rows-spanned", numSpannedCells()[1]);
262
263 if (m_fontSet)
264 m_font.addTo(propList, fontConverter);
265 for (size_t c = 0; c < m_bordersList.size(); c++) {
266 switch (c) {
267 case libmwaw::Left:
268 m_bordersList[c].addTo(propList, "left");
269 break;
270 case libmwaw::Right:
271 m_bordersList[c].addTo(propList, "right");
272 break;
273 case libmwaw::Top:
274 m_bordersList[c].addTo(propList, "top");
275 break;
276 case libmwaw::Bottom:
277 m_bordersList[c].addTo(propList, "bottom");
278 break;
279 default:
280 MWAW_DEBUG_MSG(("MWAWCell::addTo: can not send %d border\n",int(c)));
281 break;
282 }
283 }
284 if (!backgroundColor().isWhite())
285 propList.insert("fo:background-color", backgroundColor().str().c_str());
286 if (isProtected())
287 propList.insert("style:cell-protect","protected");
288 // alignment
289 switch (hAlignment()) {
290 case HALIGN_LEFT:
291 propList.insert("fo:text-align", "first");
292 propList.insert("style:text-align-source", "fix");
293 break;
294 case HALIGN_CENTER:
295 propList.insert("fo:text-align", "center");
296 propList.insert("style:text-align-source", "fix");
297 break;
298 case HALIGN_RIGHT:
299 propList.insert("fo:text-align", "end");
300 propList.insert("style:text-align-source", "fix");
301 break;
302 case HALIGN_DEFAULT:
303 break; // default
304 case HALIGN_FULL:
305 #if !defined(__clang__)
306 default:
307 #endif
308 MWAW_DEBUG_MSG(("MWAWCell::addTo: called with unknown halign=%d\n", hAlignment()));
309 }
310 // no padding
311 propList.insert("fo:padding", 0, librevenge::RVNG_POINT);
312 // alignment
313 switch (vAlignment()) {
314 case VALIGN_TOP:
315 propList.insert("style:vertical-align", "top");
316 break;
317 case VALIGN_CENTER:
318 propList.insert("style:vertical-align", "middle");
319 break;
320 case VALIGN_BOTTOM:
321 propList.insert("style:vertical-align", "bottom");
322 break;
323 case VALIGN_DEFAULT:
324 break; // default
325 #if !defined(__clang__)
326 default:
327 #endif
328 MWAW_DEBUG_MSG(("MWAWCell::addTo: called with unknown valign=%d\n", vAlignment()));
329 }
330 int rot=int(m_rotation); // int seems better than double
331 if (rot)
332 propList.insert("style:rotation-angle", rot);
333 }
334
335 std::string MWAWCell::getColumnName(int col)
336 {
337 std::stringstream f;
338 f << "[.";
339 if (col > 26) f << char('A'+col/26);
340 f << char('A'+(col%26));
341 f << "]";
342 return f.str();
343 }
344
345 std::string MWAWCell::getBasicCellName(MWAWVec2i const &pos)
346 {
347 std::stringstream f;
348 int col = pos[0];
349 if (col > 26*26) {
350 f << char('A'+col/(26*26));
351 col *= 26*26;
352 }
353 if (col > 26) {
354 f << char('A'+col/26);
355 col %= 26;
356 }
357 f << char('A'+col);
358 f << pos[1]+1;
359 return f.str();
360 }
361
362 std::string MWAWCell::getCellName(MWAWVec2i const &pos, MWAWVec2b const &absolute)
363 {
364 std::stringstream f;
365 f << "[.";
366 if (absolute[1]) f << "$";
367 int col = pos[0];
368 if (col > 26*26) {
369 f << char('A'+col/(26*26));
370 col *= 26*26;
371 }
372 if (col > 26) {
373 f << char('A'+col/26);
374 col %= 26;
375 }
376 f << char('A'+col);
377 if (absolute[0]) f << "$";
378 f << pos[1]+1 << ']';
379 return f.str();
380 }
381
382 void MWAWCell::setBorders(int wh, MWAWBorder const &border)
383 {
384 int const allBits = libmwaw::LeftBit|libmwaw::RightBit|libmwaw::TopBit|libmwaw::BottomBit|libmwaw::HMiddleBit|libmwaw::VMiddleBit;
385 if (wh & (~allBits)) {
386 MWAW_DEBUG_MSG(("MWAWCell::setBorders: unknown borders\n"));
387 return;
388 }
389 size_t numData = 4;
390 if (wh & (libmwaw::HMiddleBit|libmwaw::VMiddleBit))
391 numData=6;
392 if (m_bordersList.size() < numData) {
393 MWAWBorder emptyBorder;
394 emptyBorder.m_style = MWAWBorder::None;
395 m_bordersList.resize(numData, emptyBorder);
396 }
397 if (wh & libmwaw::LeftBit) m_bordersList[libmwaw::Left] = border;
398 if (wh & libmwaw::RightBit) m_bordersList[libmwaw::Right] = border;
399 if (wh & libmwaw::TopBit) m_bordersList[libmwaw::Top] = border;
400 if (wh & libmwaw::BottomBit) m_bordersList[libmwaw::Bottom] = border;
401 if (wh & libmwaw::HMiddleBit) m_bordersList[libmwaw::HMiddle] = border;
402 if (wh & libmwaw::VMiddleBit) m_bordersList[libmwaw::VMiddle] = border;
403 }
404
405 std::ostream &operator<<(std::ostream &o, MWAWCell const &cell)
406 {
407 o << MWAWCell::getCellName(cell.m_position, MWAWVec2b(false,false)) << ":";
408 if (cell.numSpannedCells()[0] != 1 || cell.numSpannedCells()[1] != 1)
409 o << "span=[" << cell.numSpannedCells()[0] << "," << cell.numSpannedCells()[1] << "],";
410
411 if (cell.m_protected) o << "protected,";
412 if (cell.m_bdBox.size()[0]>0 || cell.m_bdBox.size()[1]>0)
413 o << "bdBox=" << cell.m_bdBox << ",";
414 if (cell.m_bdSize[0]>0 || cell.m_bdSize[1]>0)
415 o << "bdSize=" << cell.m_bdSize << ",";
416 o << cell.m_format;
417 if (cell.m_fontSet) o << "hasFont,";
418 switch (cell.m_hAlign) {
419 case MWAWCell::HALIGN_LEFT:
420 o << "left,";
421 break;
422 case MWAWCell::HALIGN_CENTER:
423 o << "centered,";
424 break;
425 case MWAWCell::HALIGN_RIGHT:
426 o << "right,";
427 break;
428 case MWAWCell::HALIGN_FULL:
429 o << "full,";
430 break;
431 case MWAWCell::HALIGN_DEFAULT:
432 #if !defined(__clang__)
433 default:
434 #endif
435 break; // default
436 }
437 switch (cell.m_vAlign) {
438 case MWAWCell::VALIGN_TOP:
439 o << "top,";
440 break;
441 case MWAWCell::VALIGN_CENTER:
442 o << "centered[y],";
443 break;
444 case MWAWCell::VALIGN_BOTTOM:
445 o << "bottom,";
446 break;
447 case MWAWCell::VALIGN_DEFAULT:
448 #if !defined(__clang__)
449 default:
450 #endif
451 break; // default
452 }
453
454 if (!cell.m_backgroundColor.isWhite())
455 o << "backColor=" << cell.m_backgroundColor << ",";
456 for (size_t i = 0; i < cell.m_bordersList.size(); i++) {
457 if (cell.m_bordersList[i].m_style == MWAWBorder::None)
458 continue;
459 o << "bord";
460 if (i < 6) {
461 static char const *wh[] = { "L", "R", "T", "B", "MiddleH", "MiddleV" };
462 o << wh[i];
463 }
464 else o << "[#wh=" << i << "]";
465 o << "=" << cell.m_bordersList[i] << ",";
466 }
467 switch (cell.m_extraLine) {
468 case MWAWCell::E_None:
469 break;
470 case MWAWCell::E_Line1:
471 o << "line[TL->RB],";
472 break;
473 case MWAWCell::E_Line2:
474 o << "line[BL->RT],";
475 break;
476 case MWAWCell::E_Cross:
477 o << "line[cross],";
478 break;
479 #if !defined(__clang__)
480 default:
481 break;
482 #endif
483 }
484 if (cell.m_rotation<0 || cell.m_rotation>0)
485 o << "rot=" << cell.m_rotation << ",";
486 if (cell.m_extraLine!=MWAWCell::E_None)
487 o << cell.m_extraLineType << ",";
488 return o;
489 }
490
491 // send data to listener
492 bool MWAWCell::send(MWAWListenerPtr listener, MWAWTable &table)
493 {
494 if (!listener) return true;
495 listener->openTableCell(*this);
496 bool ok=sendContent(listener, table);
497 listener->closeTableCell();
498 return ok;
499 }
500
501 bool MWAWCell::sendContent(MWAWListenerPtr, MWAWTable &)
502 {
503 MWAW_DEBUG_MSG(("MWAWCell::sendContent: must not be called!!!\n"));
504 return false;
505 }
506
507 ////////////////////////////////////////////////////////////
508 // MWAWCellContent
509 ////////////////////////////////////////////////////////////
510 bool MWAWCellContent::double2Date(double val, int &Y, int &M, int &D)
511 {
512 /* first convert the date in long*/
513 auto numDaysSinceOrigin=long(val+0.4);
514 // checkme: do we need to check before for isNan(val) ?
515 if (numDaysSinceOrigin<-10000*365 || numDaysSinceOrigin>10000*365) {
516 /* normally, we can expect documents to contain date between 1904
517 and 2004. So even if such a date can make sense, storing it as
518 a number of days is clearly abnormal */
519 MWAW_DEBUG_MSG(("MWAWCellContent::double2Date: using a double to represent the date %ld seems odd\n", numDaysSinceOrigin));
520 Y=1904;
521 M=D=1;
522 return false;
523 }
524 // find the century
525 int century=19;
526 while (numDaysSinceOrigin>=36500+24) {
527 long numDaysInCentury=36500+24+((century%4)?0:1);
528 if (numDaysSinceOrigin<numDaysInCentury) break;
529 numDaysSinceOrigin-=numDaysInCentury;
530 ++century;
531 }
532 while (numDaysSinceOrigin<0) {
533 --century;
534 numDaysSinceOrigin+=36500+24+((century%4)?0:1);
535 }
536 // now compute the year
537 Y=int(numDaysSinceOrigin/365);
538 long numDaysToEndY1=Y*365+(Y>0 ? (Y-1)/4+((century%4)?0:1) : 0);
539 if (numDaysToEndY1>numDaysSinceOrigin) {
540 --Y;
541 numDaysToEndY1=Y*365+(Y>0 ? (Y-1)/4+((century%4)?0:1) : 0);
542 }
543 // finish to compute the date
544 auto numDaysFrom1Jan=int(numDaysSinceOrigin-numDaysToEndY1);
545 Y+=century*100;
546 bool isLeap=(Y%4)==0 && ((Y%400)==0 || (Y%100)!=0);
547
548 for (M=0; M<12; ++M) {
549 static const int days[2][12] = {
550 { 0,31,59,90,120,151,181,212,243,273,304,334},
551 { 0,31,60,91,121,152,182,213,244,274,305,335}
552 };
553 if (M<11 && days[isLeap ? 1 : 0][M+1]<=numDaysFrom1Jan) continue;
554 D=(numDaysFrom1Jan-days[isLeap ? 1 : 0][M++])+1;
555 break;
556 }
557 return true;
558 }
559
560 bool MWAWCellContent::date2Double(int Y, int M, int D, double &val)
561 {
562 --M;
563 --D;
564 if (M>11) {
565 Y += M/12;
566 M %= 12;
567 }
568 else if (M<0) {
569 int yDiff = (-M + 11)/12;
570 Y -= yDiff;
571 M+=12*yDiff;
572 }
573 // sanity check
574 if (M<0||M>11) {
575 MWAW_DEBUG_MSG(("MWAWCellContent::date2Double: something is bad\n"));
576 return false;
577 }
578 bool isLeap=(Y%4)==0 && ((Y%400)==0 || (Y%100)!=0);
579 int32_t const daysFrom0=365*Y+(Y/400)-(Y/100)+(Y/4);
580 int32_t const daysFrom1900=365*1900+(1900/400)-(1900/100)+(1900/4);
581 static const int32_t days[2][12] = {
582 { 0,31,59,90,120,151,181,212,243,273,304,334},
583 { 0,31,60,91,121,152,182,213,244,274,305,335}
584 };
585 int32_t daysFrom1Jan=days[isLeap ? 1 : 0][M] + D;
586 val=double(daysFrom0-daysFrom1900+daysFrom1Jan);
587 return true;
588 }
589
590 bool MWAWCellContent::double2Time(double val, int &H, int &M, int &S)
591 {
592 if (val < 0.0 || val > 1.0) return false;
593 double time = 24.*3600.*val+0.5;
594 H=int(time/3600.);
595 time -= H*3600.;
596 M=int(time/60.);
597 time -= M*60.;
598 S=int(time);
599 return true;
600 }
601
602 bool MWAWCellContent::double2String(double val, MWAWCell::Format const &format, std::string &str)
603 {
604 std::stringstream s;
605 switch (format.m_format) {
606 case MWAWCell::F_BOOLEAN:
607 if (val<0 || val >0) s << "true";
608 else s << "false";
609 break;
610 case MWAWCell::F_NUMBER:
611 if (format.m_digits>-1000 && format.m_numberFormat!=MWAWCell::F_NUMBER_GENERIC)
612 s << std::setprecision(format.m_digits);
613 switch (format.m_numberFormat) {
614 case MWAWCell::F_NUMBER_CURRENCY:
615 s << std::fixed << val << "$";
616 break;
617 case MWAWCell::F_NUMBER_DECIMAL:
618 s << val;
619 break;
620 case MWAWCell::F_NUMBER_SCIENTIFIC:
621 s << std::scientific << val;
622 break;
623 case MWAWCell::F_NUMBER_PERCENT:
624 s << std::fixed << 100*val << "%";
625 break;
626 case MWAWCell::F_NUMBER_FRACTION:
627 case MWAWCell::F_NUMBER_GENERIC:
628 case MWAWCell::F_NUMBER_UNKNOWN:
629 #if !defined(__clang__)
630 default:
631 #endif
632 s << val;
633 break;
634 }
635 break;
636 case MWAWCell::F_DATE: {
637 int Y, M, D;
638 if (!double2Date(val, Y, M, D)) return false;
639 struct tm time;
640 time.tm_sec=time.tm_min=time.tm_hour=0;
641 time.tm_mday=D;
642 time.tm_mon=M;
643 time.tm_year=Y;
644 time.tm_wday=time.tm_yday=time.tm_isdst=-1;
645 #if HAVE_STRUCT_TM_TM_ZONE
646 time.tm_zone=nullptr;
647 #endif
648 char buf[256];
649 #pragma GCC diagnostic ignored "-Wformat-y2k"
650 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
651 if (mktime(&time)==-1 ||
652 !strftime(buf, 256, format.m_DTFormat.empty() ? "%m/%d/%y" : format.m_DTFormat.c_str(), &time))
653 return false;
654 s << buf;
655 break;
656 }
657 case MWAWCell::F_TIME: {
658 if (val<0 || val>=1)
659 val=std::fmod(val,1.);
660 int H, M, S;
661 if (!double2Time(val, H, M, S)) return false;
662 struct tm time;
663 time.tm_sec=S;
664 time.tm_min=M;
665 time.tm_hour=H;
666 time.tm_mday=time.tm_mon=1;
667 time.tm_year=100;
668 time.tm_wday=time.tm_yday=time.tm_isdst=-1;
669 #if HAVE_STRUCT_TM_TM_ZONE
670 time.tm_zone=nullptr;
671 #endif
672 char buf[256];
673 if (mktime(&time)==-1 ||
674 !strftime(buf, 256, format.m_DTFormat.empty() ? "%H:%M:%S" : format.m_DTFormat.c_str(), &time))
675 return false;
676 s << buf;
677 break;
678 }
679 case MWAWCell::F_TEXT:
680 case MWAWCell::F_UNKNOWN:
681 #if !defined(__clang__)
682 default:
683 #endif
684 MWAW_DEBUG_MSG(("MWAWCellContent::double2String: called with bad format\n"));
685 return false;
686 }
687 str=s.str();
688 return true;
689 }
690
691 std::ostream &operator<<(std::ostream &o, MWAWCellContent const &content)
692 {
693 switch (content.m_contentType) {
694 case MWAWCellContent::C_NONE:
695 break;
696 case MWAWCellContent::C_TEXT:
697 o << ",text=\"" << content.m_textEntry << "\"";
698 break;
699 case MWAWCellContent::C_NUMBER: {
700 o << ",val=";
701 bool textAndVal = false;
702 if (content.hasText()) {
703 o << "entry=" << content.m_textEntry;
704 textAndVal = content.isValueSet();
705 }
706 if (textAndVal) o << "[";
707 if (content.isValueSet()) o << content.m_value;
708 if (textAndVal) o << "]";
709 }
710 break;
711 case MWAWCellContent::C_FORMULA:
712 o << ",formula=";
713 for (auto const &form : content.m_formula)
714 o << form;
715 if (content.isValueSet()) o << "[" << content.m_value << "]";
716 break;
717 case MWAWCellContent::C_UNKNOWN:
718 break;
719 #if !defined(__clang__)
720 default:
721 o << "###unknown type,";
722 break;
723 #endif
724 }
725 return o;
726 }
727
728 // ---------- WKSContentListener::FormulaInstruction ------------------
729 librevenge::RVNGPropertyList MWAWCellContent::FormulaInstruction::getPropertyList(MWAWFontConverter &fontConverter, int fontId) const
730 {
731 librevenge::RVNGPropertyList pList;
732 switch (m_type) {
733 case F_Operator:
734 pList.insert("librevenge:type","librevenge-operator");
735 pList.insert("librevenge:operator",m_content.c_str());
736 break;
737 case F_Function:
738 pList.insert("librevenge:type","librevenge-function");
739 pList.insert("librevenge:function",m_content.c_str());
740 break;
741 case F_Text: {
742 // we must use the font converter here to get the final string
743 pList.insert("librevenge:type","librevenge-text");
744 librevenge::RVNGString finalStr("");
745 for (auto c : m_content) {
746 int unicode=fontConverter.unicode(fontId,static_cast<unsigned char>(c));
747 if (unicode==-1) {
748 if (c < 0x20 && c!=9) {
749 MWAW_DEBUG_MSG(("MWAWCellContent::FormulaInstruction: Find odd char %x\n", static_cast<unsigned int>(c)));
750 }
751 else
752 finalStr.append(char(c));
753 }
754 else if (unicode != 0xfffd)
755 libmwaw::appendUnicode(static_cast<uint32_t>(unicode), finalStr);
756 }
757 pList.insert("librevenge:text",finalStr);
758 break;
759 }
760 case F_Unicode:
761 pList.insert("librevenge:type","librevenge-text");
762 if (m_content.c_str())
763 pList.insert("librevenge:text",librevenge::RVNGString(m_content.c_str()));
764 else
765 pList.insert("librevenge:text","");
766 break;
767 case F_Double:
768 pList.insert("librevenge:type","librevenge-number");
769 pList.insert("librevenge:number",m_doubleValue, librevenge::RVNG_GENERIC);
770 break;
771 case F_Long:
772 pList.insert("librevenge:type","librevenge-number");
773 pList.insert("librevenge:number",m_longValue, librevenge::RVNG_GENERIC);
774 break;
775 case F_Cell:
776 pList.insert("librevenge:type","librevenge-cell");
777 if (m_position[0][0]>=0) {
778 pList.insert("librevenge:column",m_position[0][0]);
779 pList.insert("librevenge:column-absolute",!m_positionRelative[0][0]);
780 }
781 if (m_position[0][1]>=0) {
782 pList.insert("librevenge:row",m_position[0][1]);
783 pList.insert("librevenge:row-absolute",!m_positionRelative[0][1]);
784 }
785 if (!m_sheet[0].empty())
786 pList.insert("librevenge:sheet-name",m_sheet[0]);
787 if (!m_fileName.empty())
788 pList.insert("librevenge:file-name",m_fileName.cstr());
789 break;
790 case F_CellList:
791 pList.insert("librevenge:type","librevenge-cells");
792 if (m_position[0][0]>=0) {
793 pList.insert("librevenge:start-column",m_position[0][0]);
794 pList.insert("librevenge:start-column-absolute",!m_positionRelative[0][0]);
795 }
796 if (m_position[0][1]>=0) {
797 pList.insert("librevenge:start-row",m_position[0][1]);
798 pList.insert("librevenge:start-row-absolute",!m_positionRelative[0][1]);
799 }
800 if (m_position[1][0]>=0) {
801 pList.insert("librevenge:end-column",m_position[1][0]);
802 pList.insert("librevenge:end-column-absolute",!m_positionRelative[1][0]);
803 }
804 if (m_position[1][1]>=0) {
805 pList.insert("librevenge:end-row",m_position[1][1]);
806 pList.insert("librevenge:end-row-absolute",!m_positionRelative[1][1]);
807 }
808 if (!m_sheet[0].empty())
809 pList.insert("librevenge:sheet-name",m_sheet[0]);
810 if (!m_sheet[1].empty())
811 pList.insert("librevenge:end-sheet-name",m_sheet[1]);
812 if (!m_fileName.empty())
813 pList.insert("librevenge:file-name",m_fileName.cstr());
814 break;
815 #if !defined(__clang__)
816 default:
817 MWAW_DEBUG_MSG(("MWAWCellContent::FormulaInstruction::getPropertyList: unexpected type\n"));
818 #endif
819 }
820 return pList;
821 }
822
823 std::ostream &operator<<(std::ostream &o, MWAWCellContent::FormulaInstruction const &inst)
824 {
825 if (inst.m_type==MWAWCellContent::FormulaInstruction::F_Double)
826 o << inst.m_doubleValue;
827 else if (inst.m_type==MWAWCellContent::FormulaInstruction::F_Long)
828 o << inst.m_longValue;
829 else if (inst.m_type==MWAWCellContent::FormulaInstruction::F_Cell) {
830 if (!inst.m_sheet[0].empty()) o << "\"" << inst.m_sheet[0].cstr() << "\"";
831 if (!inst.m_fileName.empty()) o << "[" << inst.m_fileName.cstr() << "]";
832 if (!inst.m_positionRelative[0][0]) o << "$";
833 if (inst.m_position[0][0]<0) o << "C" << inst.m_position[0][0];
834 else {
835 if (inst.m_position[0][0]>=26) o << char(inst.m_position[0][0]/26-1 + 'A');
836 o << char(inst.m_position[0][0]%26+'A');
837 }
838 if (!inst.m_positionRelative[0][1]) o << "$";
839 if (inst.m_position[0][1]<0) o << "R" << inst.m_position[0][1];
840 else o << inst.m_position[0][1];
841 }
842 else if (inst.m_type==MWAWCellContent::FormulaInstruction::F_CellList) {
843 if (!inst.m_fileName.empty()) o << "[" << inst.m_fileName.cstr() << "]";
844 for (int l=0; l<2; ++l) {
845 if (!inst.m_sheet[l].empty() && (l==0 || inst.m_sheet[0]!=inst.m_sheet[1]))
846 o << "\"" << inst.m_sheet[l].cstr() << "\"";
847 if (!inst.m_positionRelative[l][0]) o << "$";
848 if (inst.m_position[l][0]<0) o << "C" << inst.m_position[l][0];
849 else {
850 if (inst.m_position[l][0]>=26) o << char(inst.m_position[l][0]/26-1 + 'A');
851 o << char(inst.m_position[l][0]%26+'A');
852 }
853 if (!inst.m_positionRelative[l][1]) o << "$";
854 if (inst.m_position[l][1]<0) o << "R" << inst.m_position[l][1];
855 else o << inst.m_position[l][1];
856 if (l==0) o << ":";
857 }
858 }
859 else if (inst.m_type==MWAWCellContent::FormulaInstruction::F_Text ||
860 inst.m_type==MWAWCellContent::FormulaInstruction::F_Unicode)
861 o << "\"" << inst.m_content << "\"";
862 else
863 o << inst.m_content;
864 return o;
865 }
866
867 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: