summaryrefslogtreecommitdiffstats
path: root/kpf/src/Utils.cpp
blob: 631059a70cdb5c750148e9e49058526dd800c05d (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
/*
  KPF - Public fileserver for KDE

  Copyright 2001 Rik Hemsley (rikkus) <rik@kde.org>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to
  deal in the Software without restriction, including without limitation the
  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  sell copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <ctime>
#include <clocale>

#include <tqfile.h>
#include <tqregexp.h>

#include <klocale.h>

#include "Defines.h"
#include "Utils.h"

namespace KPF
{
  static bool dateInitDone = false;

  static TQStringList monthList;

  static const char rfc1123Format[] = "%a, %d %b %Y %H:%M:%S GMT";

  static time_t qDateTimeToTimeT(const TQDateTime & t)
  {
    struct tm tempTm;

    tempTm.tm_year  = t.date().year();
    tempTm.tm_mon   = t.date().month();
    tempTm.tm_mday  = t.date().day();
    tempTm.tm_hour  = t.time().hour();
    tempTm.tm_min   = t.time().minute();
    tempTm.tm_sec   = t.time().second();
    tempTm.tm_isdst = -1;

    // Fix up differences between TQDateTime and tm.

    tempTm.tm_year -= 1900;

    --tempTm.tm_mon;

    return ::mktime(&tempTm);
  }

    TQDateTime
  toGMT(const TQDateTime & dt)
  {
    time_t dtAsTimeT = qDateTimeToTimeT(dt);

    struct tm * dtAsGmTm = ::gmtime(&dtAsTimeT);

    if (0 == dtAsGmTm)
      return TQDateTime();

    time_t dtAsGmTimeT = ::mktime(dtAsGmTm);

    TQDateTime ret;

    ret.setTime_t(dtAsGmTimeT);

    return ret;
  }

  void dateInit()
  {
    if (dateInitDone)
      return;

    dateInitDone = true;

    monthList
      << "Jan"
      << "Feb"
      << "Mar"
      << "Apr"
      << "May"
      << "Jun"
      << "Jul"
      << "Aug"
      << "Sep"
      << "Oct"
      << "Nov"
      << "Dec"
      ;
  }

  TQString dateString()
  {
    return dateString(TQDateTime::currentDateTime());
  }


  TQString dateString(const TQDateTime & t)
  {
    time_t asTimeT = qDateTimeToTimeT(t);

    struct tm * asTm = ::gmtime(&asTimeT);

    if (0 == asTm)
    {
      kpfDebug << "::gmtime() failed" << endl;
      return TQString();
    }

    asTm->tm_isdst = -1;

    const int len = 128;

    char buf[len];

    // Ensure we use locale "C" for strftime.

    TQCString oldLC_ALL  = ::strdup(::setlocale(LC_TIME, "C"));
    TQCString oldLC_TIME = ::strdup(::setlocale(LC_ALL, "C"));
    {
      ::strftime(static_cast<char *>(buf), len, rfc1123Format, asTm);
    }
    ::setlocale(LC_TIME,  oldLC_TIME.data());
    ::setlocale(LC_ALL,   oldLC_ALL.data());

    return TQString::fromUtf8(buf);
  }

    bool
  parseDate(const TQString & s, TQDateTime & dt)
  {
    dateInit();

//    kpfDebug << "Parsing date `" << s << "'" << endl;

    TQStringList l(TQStringList::split(' ', s));

    switch (l.count())
    {
      case 4:
//        kpfDebug << "Date type is RFC 850" << endl;
        return parseDateRFC850(l, dt);
        break;
      case 5:
//        kpfDebug << "Date type is asctime" << endl;
        return parseDateAscTime(l, dt);
        break;
      case 6:
//        kpfDebug << "Date type is RFC1123" << endl;
        return parseDateRFC1123(l, dt);
        break;
      default:
//        kpfDebug << "Date type is unknown" << endl;
        return false;
        break;
    }
    
    return false;
  }

    bool
  parseDateRFC1123(const TQStringList & l, TQDateTime & dt)
  {
    if ("GMT" != l[5])
      return false;

    uint day(l[1].toUInt());

    bool haveMonth = false;
    uint month = 0;

    TQStringList::ConstIterator it;

    for (it = monthList.begin(); it != monthList.end(); ++it)
    {
      if (*it == l[2])
      {
        haveMonth = true;
        break;
      }

      ++month;
    }

    if (!haveMonth)
      return false;

    uint year(l[3].toUInt());

    TQStringList timeTokenList(TQStringList::split(':', l[4]));

    if (3 != timeTokenList.count())
      return false;

    uint hours    (timeTokenList[0].toUInt());
    uint minutes  (timeTokenList[1].toUInt());
    uint seconds  (timeTokenList[2].toUInt());

    dt.setDate(TQDate(year, month + 1, day));
    dt.setTime(TQTime(hours, minutes, seconds));

    return dt.isValid();
  }

    bool
  parseDateRFC850(const TQStringList & l, TQDateTime & dt)
  {
    if ("GMT" != l[3])
      return false;

    TQStringList dateTokenList(TQStringList::split('-', l[1]));

    if (3 != dateTokenList.count())
      return false;

    uint day(dateTokenList[0].toUInt());

    bool haveMonth = false;
    uint month = 0;

    TQStringList::ConstIterator it;

    for (it = monthList.begin(); it != monthList.end(); ++it)
    {
      if (*it == dateTokenList[1])
      {
        haveMonth = true;
        break;
      }

      ++month;
    }

    if (!haveMonth)
      return false;

    uint year(dateTokenList[2].toUInt());

    if (year < 50)
      year += 2000;
    else if (year < 100)
      year += 1900;

    TQStringList timeTokenList(TQStringList::split(':', l[2]));

    if (3 != timeTokenList.count())
      return false;

    uint hours    (timeTokenList[0].toUInt());
    uint minutes  (timeTokenList[1].toUInt());
    uint seconds  (timeTokenList[2].toUInt());

    dt.setDate(TQDate(year, month + 1, day));
    dt.setTime(TQTime(hours, minutes, seconds));

    return dt.isValid();
  }

     bool
  parseDateAscTime(const TQStringList & l, TQDateTime & dt)
  {
    bool haveMonth = false;
    uint month = 0;

    TQStringList::ConstIterator it;

    for (it = monthList.begin(); it != monthList.end(); ++it)
    {
      if (*it == l[1])
      {
        haveMonth = true;
        break;
      }

      ++month;
    }

    if (!haveMonth)
      return false;

    uint day(l[2].toUInt());

    TQStringList timeTokenList(TQStringList::split(':', l[3]));

    if (3 != timeTokenList.count())
      return false;

    uint hours    (timeTokenList[0].toUInt());
    uint minutes  (timeTokenList[1].toUInt());
    uint seconds  (timeTokenList[2].toUInt());

    uint year(l[4].toUInt());

    dt.setDate(TQDate(year, month + 1, day));
    dt.setTime(TQTime(hours, minutes, seconds));

    return dt.isValid();
  }

     TQString
  translatedResponseName(uint code)
  {
    TQString s;

    switch (code)
    {
      case 200:
        s = i18n("OK");
        break;
      case 206:
        s = i18n("Partial content");
        break;
      case 304:
        s = i18n("Not modified");
        break;
      case 400:
        s = i18n("Bad request");
        break;
      case 403:
        s = i18n("Forbidden");
        break;
      case 404:
        s = i18n("Not found");
        break;
      case 412:
        s = i18n("Precondition failed");
        break;
      case 416:
        s = i18n("Bad range");
        break;
      case 500:
        s = i18n("Internal error");
        break;
      case 501:
        s = i18n("Not implemented");
        break;
      case 505:
        s = i18n("HTTP version not supported");
        break;
      default:
        s = i18n("Unknown");
        break;
    }

    return s;
  }

     TQString
  responseName(uint code)
  {
    TQString s;

    switch (code)
    {
      case 200:
        s = "OK";
        break;
      case 206:
        s = "Partial content";
        break;
      case 304:
        s = "Not modified";
        break;
      case 400:
        s = "Bad request";
        break;
      case 403:
        s = "Forbidden";
        break;
      case 404:
        s = "Not found";
        break;
      case 412:
        s = "Precondition failed";
        break;
      case 416:
        s = "Bad range";
        break;
      case 500:
        s = "Internal error";
        break;
      case 501:
        s = "Not implemented";
        break;
      case 505:
        s = "HTTP version not supported";
        break;
      default:
        s = "Unknown";
        break;
    }

    return s;
  }


} // End namespace KPF


// vim:ts=2:sw=2:tw=78:et