summaryrefslogtreecommitdiffstats
path: root/kspread/valueparser.cc
blob: 2d2604a198c5d02ded28efa4059d580b3010b903 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/* This file is part of the KDE project
   Copyright 2004 Tomas Mecir <mecirt@gmail.com>
   Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "valueparser.h"

#include "kspread_cell.h"
#include "kspread_format.h"
#include "kspread_locale.h"
#include "kspread_value.h"

using namespace KSpread;

ValueParser::ValueParser( KLocale* locale ) : parserLocale( locale )
{
}

KLocale* ValueParser::locale()
{
  return parserLocale;
}

void ValueParser::parse (const QString& str, Cell *cell)
{
  FormatType format = cell->formatType();
  
  // If the text is empty, we don't have a value
  // If the user stated explicitly that he wanted text
  // (using the format or using a quote),
  // then we don't parse as a value, but as string.
  if ( str.isEmpty() || format == Text_format || str.at(0)=='\'' )
  {
    cell->setValue (str);
    return;
  }

  QString strStripped = str.stripWhiteSpace();
  
  // Try parsing as various datatypes, to find the type of the cell
  
  // First as number
  if (tryParseNumber (strStripped, cell))
    return;
  
  // Then as bool
  if (tryParseBool (strStripped, cell))
    return;


  // Test for money number
  bool ok;
  double money = parserLocale->readMoney (strStripped, &ok);
  if (ok)
  {
    cell->format()->setPrecision(2);
    Value val (money);
    val.setFormat (Value::fmt_Money);
    cell->setValue (val);
    return;
  }

  if (tryParseDate (strStripped, cell))
    return;

  if (tryParseTime (strStripped, cell))
    return;

  // Nothing particular found, then this is simply a string
  cell->setValue (Value (str));
}

Value ValueParser::parse (const QString &str)
{
  Value val;
  
  // If the text is empty, we don't have a value
  // If the user stated explicitly that he wanted text
  // (using the format or using a quote),
  // then we don't parse as a value, but as string.
  if ( str.isEmpty() || str.at(0)=='\'' )
  {
    val.setValue (str);
    return val;
  }

  bool ok;
  
  QString strStripped = str.stripWhiteSpace();
  // Try parsing as various datatypes, to find the type of the string
  
  // First as number
  val = tryParseNumber (strStripped, &ok);
  if (ok)
    return val;

 // Then as bool
 // Note - I swapped the order of these two to try parsing as a number
 // first because that will probably be the most common case
  val = tryParseBool (strStripped, &ok);
  if (ok)
    return val;


  // Test for money number
  double money = parserLocale->readMoney (strStripped, &ok);
  if (ok)
  {
    val.setValue (money);
    val.setFormat (Value::fmt_Money);
    return val;
  }

  val = tryParseDate (strStripped, &ok);
  if (ok)
    return val;

  val = tryParseTime (strStripped, &ok);
  if (ok)
    return val;

  // Nothing particular found, then this is simply a string
  val.setValue (str);
  return val;
}

bool ValueParser::tryParseBool (const QString& str, Cell *cell)
{
  bool ok;
  Value val = tryParseBool (str, &ok);
  if (ok)
    cell->setValue (val);
  return ok;
}

bool ValueParser::tryParseNumber (const QString& str, Cell *cell)
{
  bool ok;
  Value val = tryParseNumber (str, &ok);
  if (ok)
    cell->setValue (val);
  return ok;
}

bool ValueParser::tryParseDate (const QString& str, Cell *cell)
{
  bool ok;
  Value value = tryParseDate (str, &ok);
  if (ok)
    cell->setValue (value);
  return ok;
}

bool ValueParser::tryParseTime (const QString& str, Cell *cell)
{
  bool ok;
  Value value = tryParseTime (str, &ok);
  if (ok)
    cell->setValue (value);
  return ok;
}


Value ValueParser::tryParseBool (const QString& str, bool *ok)
{
  Value val;
  if (ok) *ok = false;

  const QString& lowerStr = str.lower();
  
  if ((lowerStr == "true") ||
      (lowerStr == parserLocale->translate("true").lower()))
  {
    val.setValue (true);
    if (ok) *ok = true;
  }
  else if ((lowerStr == "false") ||
      (lowerStr == parserLocale->translate("false").lower()))
  {
    val.setValue (false);
    if (ok) *ok = true;
    fmtType = Number_format;    //TODO: really?
  }
  return val;
}

double ValueParser::readNumber(const QString &_str, bool * ok, bool * isInt)
{
  QString str = _str.stripWhiteSpace();
  bool neg = str.find(parserLocale->negativeSign()) == 0;
  if (neg)
	str.remove( 0, parserLocale->negativeSign().length() );
 
  /* will hold the scientific notation portion of the number.
	 Example, with 2.34E+23, exponentialPart == "E+23"
  */
  QString exponentialPart;
  int EPos;
 
  EPos = str.find('E', 0, false);
 
  if (EPos != -1)
  {
    exponentialPart = str.mid(EPos);
    str = str.left(EPos);
  }
 
  int pos = str.find(parserLocale->decimalSymbol());
  QString major;
  QString minor;
  if ( pos == -1 )
  {
    major = str;
    if (isInt) *isInt = true;
  }
  else
  {
    major = str.left(pos);
    minor = str.mid(pos + parserLocale->decimalSymbol().length());
    if (isInt) *isInt = false;
  }
 
  // Remove thousand separators
  int thlen = parserLocale->thousandsSeparator().length();
  int lastpos = 0;
  while ( ( pos = major.find( parserLocale->thousandsSeparator() ) ) > 0 )
  {
    // e.g. 12,,345,,678,,922 Acceptable positions (from the end) are 5, 10, 15... i.e. (3+thlen)*N
    int fromEnd = major.length() - pos;
    if ( fromEnd % (3+thlen) != 0 // Needs to be a multiple, otherwise it's an error
         || pos - lastpos > 3 // More than 3 digits between two separators -> error
         || pos == 0          // Can't start with a separator
         || (lastpos>0 && pos-lastpos!=3))   // Must have exactly 3 digits between two separators
    {
      if (ok) *ok = false;
      return 0.0;
    }
 
    lastpos = pos;
    major.remove( pos, thlen );
  }
  if (lastpos>0 && major.length()-lastpos!=3)   // Must have exactly 3 digits after the last separator
  {
    if (ok) *ok = false;
    return 0.0;
  }
 
  QString tot;
  if (neg) tot = '-';
 
  tot += major + '.' + minor + exponentialPart;
 
  return tot.toDouble(ok);
}

Value ValueParser::tryParseNumber (const QString& str, bool *ok)
{
  Value value;

  bool percent = false;
  QString str2;
  if( str.at(str.length()-1)=='%')
  {
    str2 = str.left (str.length()-1).stripWhiteSpace();
    percent = true;
  }
  else
    str2 = str;

  
  // First try to understand the number using the parserLocale
  bool isInt;
  double val = readNumber (str2, ok, &isInt);
  // If not, try with the '.' as decimal separator
  if (!(*ok))
  {
    val = str2.toDouble(ok);
    if (str.contains('.'))
      isInt = false;
    else
      isInt = true;
  }

  if (*ok)
  {
    if (percent)
    {
      //kdDebug(36001) << "ValueParser::tryParseNumber '" << str <<
      //    "' successfully parsed as percentage: " << val << "%" << endl;
      value.setValue (val / 100.0);
      value.setFormat (Value::fmt_Percent);
      fmtType = Percentage_format;
    }
    else
    {
      //kdDebug(36001) << "ValueParser::tryParseNumber '" << str <<
      //    "' successfully parsed as number: " << val << endl;
	  if (isInt)
		value.setValue (static_cast<long> (val));
	  else
		value.setValue (val);
      
      if ( str2.contains('E') || str2.contains('e') )
        fmtType = Scientific_format;
      else
      {
        if (val > 1e+10)
          fmtType = Scientific_format;
        else
          fmtType = Number_format;
      }
    }
  }

  return value;
}

Value ValueParser::tryParseDate (const QString& str, bool *ok)
{
  bool valid = false;
  QDate tmpDate = parserLocale->readDate (str, &valid);
  if (!valid)
  {
    // Try without the year
    // The tricky part is that we need to remove any separator around the year
    // For instance %Y-%m-%d becomes %m-%d and %d/%m/%Y becomes %d/%m
    // If the year is in the middle, say %m-%Y/%d, we'll remove the sep.
    // before it (%m/%d).
    QString fmt = parserLocale->dateFormatShort();
    int yearPos = fmt.find ("%Y", 0, false);
    if ( yearPos > -1 )
    {
      if ( yearPos == 0 )
      {
        fmt.remove( 0, 2 );
        while ( fmt[0] != '%' )
          fmt.remove( 0, 1 );
      } else
      {
        fmt.remove( yearPos, 2 );
        for ( ; yearPos > 0 && fmt[yearPos-1] != '%'; --yearPos )
          fmt.remove( yearPos, 1 );
      }
      //kdDebug(36001) << "Cell::tryParseDate short format w/o date: " << fmt << endl;
      tmpDate = parserLocale->readDate( str, fmt, &valid );
    }
  }
  if (valid)
  {
    // Note: if shortdate format only specifies 2 digits year, then 3/4/1955
    // will be treated as in year 3055, while 3/4/55 as year 2055
    // (because 55 < 69, see KLocale) and thus there's no way to enter for
    // year 1995
  
    // The following fixes the problem, 3/4/1955 will always be 1955

    QString fmt = parserLocale->dateFormatShort();
    if( ( fmt.contains( "%y" ) == 1 ) && ( tmpDate.year() > 2999 ) )
      tmpDate = tmpDate.addYears( -1900 );

    // this is another HACK !
    // with two digit years, 0-69 is treated as year 2000-2069 (see KLocale)
    // however, in Excel only 0-29 is year 2000-2029, 30 or later is 1930
    // onwards

    // the following provides workaround for KLocale so we're compatible
    // with Excel
    // (e.g 3/4/45 is Mar 4, 1945 not Mar 4, 2045)
    if( ( tmpDate.year() >= 2030 ) && ( tmpDate.year() <= 2069 ) )
    {
      QString yearFourDigits = QString::number( tmpDate.year() );
      QString yearTwoDigits = QString::number( tmpDate.year() % 100 );

      // if year is 2045, check to see if "2045" isn't there --> actual
      // input is "45"
      if( ( str.contains( yearTwoDigits ) >= 1 ) &&
          ( str.contains( yearFourDigits ) == 0 ) )
        tmpDate = tmpDate.addYears( -100 );
    }
    
    //test if it's a short date or text date.
    if (parserLocale->formatDate (tmpDate, false) == str)
      fmtType = TextDate_format;
    else
      fmtType = ShortDate_format;
  }
  if (!valid)
  {
    //try to use the standard Qt date parsing, using ISO 8601 format
    tmpDate = QDate::fromString(str,Qt::ISODate);
    if (tmpDate.isValid())
    {
      valid = true;
    }
  }
  
  if (ok)
    *ok = valid;
    
  return Value (tmpDate);
}

Value ValueParser::tryParseTime (const QString& str, bool *ok)
{
  if (ok)
    *ok = false;

  bool valid    = false;
  bool duration = false;
  Value val;

  QDateTime tmpTime = readTime (str, true, &valid, duration);
  if (!tmpTime.isValid())
    tmpTime = readTime (str, false, &valid, duration);

  if (!valid)
  {
    QTime tm;
    if (parserLocale->use12Clock())
    {
      QString stringPm = parserLocale->translate("pm");
      QString stringAm = parserLocale->translate("am");
      int pos=0;
      if((pos=str.find(stringPm))!=-1)
      {
          QString tmp=str.mid(0,str.length()-stringPm.length());
          tmp=tmp.simplifyWhiteSpace();
          tm = parserLocale->readTime(tmp+" "+stringPm, &valid);
          if (!valid)
              tm = parserLocale->readTime(tmp+":00 "+stringPm, &valid);
      }
      else if((pos=str.find(stringAm))!=-1)
      {
          QString tmp = str.mid(0,str.length()-stringAm.length());
          tmp = tmp.simplifyWhiteSpace();
          tm = parserLocale->readTime (tmp + " " + stringAm, &valid);
          if (!valid)
              tm = parserLocale->readTime (tmp + ":00 " + stringAm, &valid);
      }
      if (valid)
          tmpTime.setTime(tm);
    }
  }
  if (valid)
  {
    fmtType = Time_format;
    if ( duration )
    {
      val.setValue (tmpTime);
      fmtType = Time_format7;
    }
    else
      val.setValue (tmpTime.time());
  }
  
  if (ok)
    *ok = valid;
    
  return val;
}

QDateTime ValueParser::readTime (const QString & intstr, bool withSeconds,
    bool *ok, bool & duration)
{
  duration = false;
  QString str = intstr.simplifyWhiteSpace().lower();
  QString format = parserLocale->timeFormat().simplifyWhiteSpace();
  if ( !withSeconds )
  {
    int n = format.find("%S");
    format = format.left( n - 1 );
  }

  int days = -1;
  int hour = -1, minute = -1;
  int second = withSeconds ? -1 : 0; // don't require seconds
  bool g_12h = false;
  bool pm = false;
  uint strpos = 0;
  uint formatpos = 0;

  QDate refDate( 1899, 12, 31 );

  uint l  = format.length();
  uint sl = str.length();

  while (l > formatpos || sl > strpos)
  {
    if ( !(l > formatpos && sl > strpos) )
      goto error;

    QChar c( format.at( formatpos++ ) );

    if (c != '%')
    {
      if (c.isSpace())
        ++strpos;
      else if (c != str.at(strpos++))
        goto error;
      continue;
    }

    // remove space at the begining
    if (sl > strpos && str.at( strpos).isSpace() )
      ++strpos;

    c = format.at( formatpos++ );
    switch (c)
    {
     case 'p':
      {
        QString s;
        s = parserLocale->translate("pm").lower();
        int len = s.length();
        if (str.mid(strpos, len) == s)
        {
          pm = true;
          strpos += len;
        }
        else
        {
          s = parserLocale->translate("am").lower();
          len = s.length();
          if (str.mid(strpos, len) == s)
          {
            pm = false;
            strpos += len;
          }
          else
            goto error;
        }
      }
      break;

     case 'k':
     case 'H':
      g_12h = false;
      hour = readInt(str, strpos);
      if (hour < 0)
        goto error;
      if (hour > 23)
      {
        days = (int)(hour / 24);
        hour %= 24;
      }

      break;

     case 'l':
     case 'I':
      g_12h = true;
      hour = readInt(str, strpos);
      if (hour < 1 || hour > 12)
        goto error;

      break;

     case 'M':
      minute = readInt(str, strpos);
      if (minute < 0 || minute > 59)
        goto error;

      break;

     case 'S':
      second = readInt(str, strpos);
      if (second < 0 || second > 59)
        goto error;

      break;
    }
  }

  if (g_12h)
  {
    hour %= 12;
    if (pm) hour += 12;
  }

  if (days > 0)
  {
    refDate.addDays( days );
    duration = true;
  }

  if (ok)
    *ok = true;
  return QDateTime( refDate, QTime( hour, minute, second ) );

 error:
  if (ok)
    *ok = false;
  // return invalid date if it didn't work
  return QDateTime( refDate, QTime( -1, -1, -1 ) ); 
}

/**
 * helper function to read integers, used in readTime
 * @param str
 * @param pos the position to start at. It will be updated when we parse it.
 * @return the integer read in the string, or -1 if no string
 */
int ValueParser::readInt (const QString &str, uint &pos)
{
  if (!str.at(pos).isDigit())
    return -1;
  int result = 0;
  for ( ; str.length() > pos && str.at(pos).isDigit(); pos++ )
  {
    result *= 10;
    result += str.at(pos).digitValue();
  }

  return result;
}