summaryrefslogtreecommitdiffstats
path: root/src/translators/pilotdb/pilotdb.cpp
blob: d7779e4263bf1215fee6b5a45b34fa4894aee9f5 (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
/***************************************************************************
    copyright            : (C) 2003-2006 by Robby Stephenson
    email                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of version 2 of the GNU General Public License as  *
 *   published by the Free Software Foundation;                            *
 *                                                                         *
 ***************************************************************************/

#include "pilotdb.h"
#include "strop.h"
#include "libflatfile/Record.h"

#include <kdebug.h>

#include <qbuffer.h>

using namespace PalmLib;
using Tellico::Export::PilotDB;

namespace {
  static const int PI_HDR_SIZE          = 78;
  static const int PI_RESOURCE_ENT_SIZE = 10;
  static const int PI_RECORD_ENT_SIZE   = 8;
}

PilotDB::PilotDB() : Database(false), m_app_info(), m_sort_info(),
    m_next_record_list_id(0) {
  pi_int32_t now = StrOps::get_current_time();
  creation_time(now);
  modification_time(now);
  backup_time(now);
}

PilotDB::~PilotDB() {
  for(record_list_t::iterator i = m_records.begin(); i != m_records.end(); ++i) {
    delete (*i);
  }
}

QByteArray PilotDB::data() {
  QBuffer b;
  b.open(IO_WriteOnly);

  pi_char_t buf[PI_HDR_SIZE];
  pi_int16_t ent_hdr_size = isResourceDB() ? PI_RESOURCE_ENT_SIZE : PI_RECORD_ENT_SIZE;
  std::streampos offset = PI_HDR_SIZE + m_records.size() * ent_hdr_size + 2;

  for(int i=0; i<32; ++i) {
    buf[i] = 0;
  }
  memcpy(buf, name().c_str(), QMIN(31, name().length()));
  set_short(buf + 32, flags());
  set_short(buf + 34, version());
  set_long(buf + 36, creation_time());
  set_long(buf + 40, modification_time());
  set_long(buf + 44, backup_time());
  set_long(buf + 48, modnum());
  if(m_app_info.raw_size() > 0) {
    set_long(buf + 52, offset);
    offset += m_app_info.raw_size();
  } else {
    set_long(buf + 52, 0);
  }
  if(m_sort_info.raw_size() > 0) {
    set_long(buf + 56, offset);
    offset += m_sort_info.raw_size();
  } else {
    set_long(buf + 56, 0);
  }
  set_long(buf + 60, type());
  set_long(buf + 64, creator());
  set_long(buf + 68, unique_id_seed());
  set_long(buf + 72, m_next_record_list_id);
  set_short(buf + 76, m_records.size());

  // Write the PDB/PRC header to the string.
  b.writeBlock(reinterpret_cast<char *>(buf), sizeof(buf));

  for(record_list_t::iterator i = m_records.begin(); i != m_records.end(); ++i) {
    Block* entry = *i;

    if(isResourceDB()) {
      Resource * resource = reinterpret_cast<Resource *> (entry);
      set_long(buf, resource->type());
      set_short(buf + 4, resource->id());
      set_long(buf + 6, offset);
    } else {
      Record * record = reinterpret_cast<Record *> (entry);
      set_long(buf, offset);
      buf[4] = record->attrs();
      set_treble(buf + 5, record->unique_id());
    }
    b.writeBlock(reinterpret_cast<char *>(buf), ent_hdr_size);
    offset += entry->raw_size();
  }

  b.writeBlock("\0", 1);
  b.writeBlock("\0", 1);

  if(m_app_info.raw_size() > 0) {
    b.writeBlock((char *) m_app_info.raw_data(), m_app_info.raw_size());
  }

  if(m_sort_info.raw_size() > 0) {
    b.writeBlock((char *) m_sort_info.raw_data(), m_sort_info.raw_size());
  }

  for(record_list_t::iterator q = m_records.begin(); q != m_records.end(); ++q) {
    Block* entry = *q;
    b.writeBlock((char *) entry->raw_data(), entry->raw_size());
  }

  b.close();
  return b.buffer();
}

// Return the record identified by the given index. The caller owns
// the returned RawRecord object.
Record PilotDB::getRecord(unsigned index) const
{
    if (index >= m_records.size()) kdDebug() << "invalid index" << endl;
    return *(reinterpret_cast<Record *> (m_records[index]));
}

// Set the record identified by the given index to the given record.
void PilotDB::setRecord(unsigned index, const Record& rec)
{
//    if (index >= m_records.size()) kdDebug() << "invalid index");
    *(reinterpret_cast<Record *> (m_records[index])) = rec;
}

// Append the given record to the database.
void PilotDB::appendRecord(const Record& rec)
{
    Record* record = new Record(rec);

    // If this new record has a unique ID that duplicates any other
    // record, then reset the unique ID to an unused value.
    if (m_uid_map.find(record->unique_id()) != m_uid_map.end()) {
        uid_map_t::iterator iter = max_element(m_uid_map.begin(),
                                               m_uid_map.end());
        pi_uint32_t maxuid = (*iter).first;

        // The new unique ID becomes the max plus one.
        record->unique_id(maxuid + 1);
    }

    m_uid_map[record->unique_id()] = record;
    m_records.push_back(record);
}


void PilotDB::clearRecords()
{
  m_records.erase(m_records.begin(), m_records.end());
}

// Return the resource with the given type and ID. NULL is returned if
// the specified (type, ID) combination is not present in the
// database. The caller owns the returned RawRecord object.
Resource PilotDB::getResourceByType(pi_uint32_t type, pi_uint32_t id) const
{
    for (record_list_t::const_iterator i = m_records.begin();
         i != m_records.end(); ++i) {
        Resource* resource = reinterpret_cast<Resource *> (*i);
        if (resource->type() == type && resource->id() == id)
            return *resource;
    }

  kdWarning() << "PilotDB::getResourceByType() - not found!" << endl;
  return Resource();
}

// Return the resource present at the given index. NULL is returned if
// the index is invalid. The caller owns the returned RawRecord
// object.
Resource PilotDB::getResourceByIndex(unsigned index) const
{
    if (index >= m_records.size()) kdDebug() << "invalid index" << endl;
    return *(reinterpret_cast<Resource *> (m_records[index]));
}

// Set the resouce at given index to passed RawResource object.
void PilotDB::setResource(unsigned index, const Resource& resource)
{
    if (index >= m_records.size()) kdDebug() << "invalid index" << endl;
    *(reinterpret_cast<Resource *> (m_records[index])) = resource;
}

FlatFile::Field PilotDB::string2field(FlatFile::Field::FieldType type, const std::string& fldstr) {
  FlatFile::Field field;

  switch (type) {
    case FlatFile::Field::STRING:
      field.type = FlatFile::Field::STRING;
      field.v_string = fldstr;
      break;

    case FlatFile::Field::BOOLEAN:
      field.type = FlatFile::Field::BOOLEAN;
      field.v_boolean = StrOps::string2boolean(fldstr);
      break;

    case FlatFile::Field::INTEGER:
      field.type = FlatFile::Field::INTEGER;
      StrOps::convert_string(fldstr, field.v_integer);
      break;

    case FlatFile::Field::FLOAT:
      field.type = FlatFile::Field::FLOAT;
      StrOps::convert_string(fldstr, field.v_float);
      break;

    case FlatFile::Field::NOTE:
      field.type = FlatFile::Field::NOTE;
      field.v_string = fldstr.substr(0,NOTETITLE_LENGTH - 1);
      field.v_note = fldstr;
      break;

    case FlatFile::Field::LIST:
      field.type = FlatFile::Field::LIST;
      field.v_string = fldstr;
      break;

    case FlatFile::Field::LINK:
      field.type = FlatFile::Field::LINK;
      field.v_integer = 0;
      field.v_string = fldstr;
      break;

    case FlatFile::Field::LINKED:
      field.type = FlatFile::Field::LINKED;
      field.v_string = fldstr;
      break;

    case FlatFile::Field::CALCULATED:
      field.type = FlatFile::Field::CALCULATED;
      field.v_string = fldstr;
      break;

    case FlatFile::Field::DATE:
      field.type = FlatFile::Field::DATE;
      struct tm time;
      if (!fldstr.empty()) {
#ifdef strptime
        if(!strptime(fldstr.c_str(), "%Y/%m/%d", &time)) {
#else
        if(!StrOps::strptime(fldstr.c_str(), "%Y/%m/%d", &time)) {
#endif
          kdDebug() << "invalid date in field" << endl;
        }
        field.v_date.month = time.tm_mon + 1;
        field.v_date.day = time.tm_mday;
        field.v_date.year = time.tm_year + 1900;
        field.v_time.hour = time.tm_hour;
        field.v_time.minute = time.tm_min;
      } else {
        field.v_date.month = 0;
        field.v_date.day = 0;
        field.v_date.year = 0;
        field.v_time.hour = 24;
        field.v_time.minute = 0;
      }
      break;

    default:
      kdWarning() << "PilotDB::string2field() - unsupported field type" <<  endl;
      break;
  }

  return field;
}