summaryrefslogtreecommitdiffstats
path: root/filters/kword/palmdoc/palmdb.cpp
blob: f411adabe834e1099d5b9e590c97b4609e0274a7 (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
/* This file is part of the KDE project
   Copyright (C) 2002 Ariya Hidayat <ariyahidayat@yahoo.de>

   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.
*/

/*
   The database layout for PalmDB files is described in 
   http://www.palmos.com/dev/support/docs/protein_books/FileFormats/Intro.html
*/

#include "palmdb.h"

#include <tqcstring.h>
#include <tqdatastream.h>
#include <tqdatetime.h>
#include <tqfile.h>
#include <tqmemarray.h>
#include <tqptrlist.h>
#include <tqstring.h>

PalmDB::PalmDB()
{
  // some default values
  setName( "Unnamed" );
  setAttributes( 0 );
  setVersion( 0 );
  setCreationDate( TQDateTime::currentDateTime() );
  setModificationDate( TQDateTime::currentDateTime() );
  setLastBackupDate( TQDateTime::currentDateTime() );
  setType( TQString() );
  setCreator( TQString() );

  // crash if autodelete ?
  records.setAutoDelete( true );
}

PalmDB::~PalmDB()
{
  records.clear();
}

bool PalmDB::load( const char* filename )
{
  // open input file
  TQFile in (filename);
  if (!in.open (IO_ReadOnly))
    return false;

  TQDataStream stream;
  stream.setDevice (&in);

  unsigned filesize = stream.device()->size();
  if( filesize < 72 ) return false;

  // always big-endian
  stream.setByteOrder (TQDataStream::BigEndian);

  // now start to read PDB header (72 bytes)

  // read and encode database name
  // The name field is 32 bytes long, and is NUL terminated.
  // Use the length parameter of fromLatin1() anyway.
  TQ_UINT8 name[32];
  for(int k = 0; k < 32; k++)
    stream >> name[k];
  m_name = TQString::fromLatin1( (char*) name, 31 );

  // read database attribute
  TQ_UINT16 attr;
  stream >> attr;
  m_attributes = attr;

  // read database version (app-specific)
  TQ_UINT16 ver;
  stream >> ver;
  m_version = ver;

  // NOTE: PDB specifies date as number of seconds since 1 Jan 1904
  // TQDateTime::setTime_t expects number of seconds since 1 Jan 1970
  // so, we make adjustment with a constant offset of 2082844800
  const int adjust = 2082844800;

  // read creation date
  TQ_UINT32 creation;
  stream >> creation;
  m_creationDate.setTime_t( creation - adjust );

  // read modification date
  TQ_UINT32 modification;
  stream >> modification;
  m_modificationDate.setTime_t( modification - adjust );

  // read last backup date
  TQ_UINT32 lastbackup;
  stream >> lastbackup;
  m_lastBackupDate.setTime_t( lastbackup - adjust );

  // read modification number
  TQ_UINT32 modnum;
  stream >> modnum;

  // read app info id and sort info id
  TQ_UINT32 appid, sortid;
  stream >> appid;
  stream >> sortid;

  // read and encode database type
  TQ_UINT8 dbt[4];
  stream >> dbt[0] >> dbt[1] >> dbt[2] >> dbt[3];
  m_type = TQString::fromLatin1( (char*) dbt, 4 );

  // read and encode database creator
  TQ_UINT8 dbc[4];
  stream >> dbc[0] >> dbc[1] >> dbc[2] >> dbc[3];
  m_creator = TQString::fromLatin1( (char*) dbc, 4 );

  // read unique id seed
  TQ_UINT32 idseed;
  stream >> idseed;
  m_uniqueIDSeed = idseed;

  // now start to read PDB record list (variable-length)

  // next record list
  // FIXME what to do with this ?
  TQ_UINT32 nextlist;
  stream >> nextlist;

  // number of records
  TQ_UINT16 numrec;
  stream >> numrec;

  // read entries in record list
  // find out location and size of each record
  TQMemArray<unsigned> recpos( numrec );
  TQMemArray<int> recsize( numrec );

  // FIXME any other better way to find record size ?
  for( int r = 0; r < numrec; r++ )
  {
    TQ_UINT32 pos;
    TQ_UINT8 flag, dummy;
    stream >> pos >> flag >> dummy >> dummy >> dummy;
    recpos[r] = pos; recsize[r] = filesize - pos;
    if( r> 0 ) recsize[r-1] = pos - recpos[r-1]; // fixup
  }

  // debugging
#ifdef PDB_DEBUG
  tqDebug( "name: \"%s\"", m_name.latin1() );
  tqDebug( "type: \"%s\"", m_type.latin1() );
  tqDebug( "creator: \"%s\"", m_creator.latin1() );
  tqDebug( "attributes: 0x%04X", m_attributes );
  tqDebug( "version: 0x%04X", m_version );
  tqDebug( "creation date: %s", m_creationDate.toString().latin1() );
  tqDebug( "modification date: %s", m_modificationDate.toString().latin1() );
  tqDebug( "last backup date: %s", m_lastBackupDate.toString().latin1() );
  tqDebug( "number of records: %d", numrec );
  for( int r = 0; r < numrec; r++ )
    tqDebug( "  rec %d at 0x%X size %d", r, recpos[r], recsize[r] );
#endif

  // load all records
  records.clear();
  for( int r = 0; r < numrec; r++ )
  {
    TQByteArray* data = new TQByteArray;

    if( recpos[r] < filesize )
      if( recsize[r] >= 0 )
        {
          data->resize( recsize[r] );
          stream.device()->at( recpos[r] );
          for( int q = 0; q < recsize[r]; q++ )
            { TQ_UINT8 c; stream >> c; data->at(q) = c; }
        }
    records.append( data );
  }

  // close input file
  in.close();

  return true;
}

bool PalmDB::save( const char* filename )
{
  // open output file
  TQFile out( filename );
  if( !out.open( IO_WriteOnly ) )
    return false;

  TQDataStream stream;
  stream.setDevice( &out );

  // always big-endian
  stream.setByteOrder (TQDataStream::BigEndian);

  // now write PDB header (72 bytes)

  // write database name
  setName( name() );
  const char *dbname = m_name.latin1();
  for( unsigned k=0; k<31; k++ )
  {
    TQ_UINT8 c = (k<m_name.length()) ? dbname[k] : 0;
    stream << c;
  }
  {
    // NUL-terminate the database name
    TQ_UINT8 c = 0;
    stream << c;
  }



  // write database attribute
  TQ_UINT16 attr = m_attributes;
  stream << attr;

  // write database version (app-specific)
  TQ_UINT16 ver = m_version;
  stream << ver;

  // reference date is 1 Jan 1904
  // see also note in function load() above
  TQDateTime ref = TQDate( 1904, 1, 1 );

  // write creation date
  TQ_UINT32 creation = -m_creationDate.secsTo( ref );
  stream << creation;

  // write modification date
  TQ_UINT32 modification = -m_modificationDate.secsTo( ref );
  stream << modification;

  // write last backup date
  TQ_UINT32 lastbackup = -m_lastBackupDate.secsTo( ref );
  stream << lastbackup;

  // write modification number
  TQ_UINT32 modnum = 0;
  stream << modnum;

  // write app info id and sort info id
  TQ_UINT32 appid = 0, sortid = 0;
  stream << appid;
  stream << sortid;

  // write and encode database type
  TQ_UINT8 dbt[4];
  const char *dbtype = m_type.latin1();
  for( int p=0; p<4; p++ ) dbt[p]=dbtype[p];
  stream << dbt[0] << dbt[1] << dbt[2] << dbt[3];

  // write and encode database creator
  TQ_UINT8 dbc[4];
  const char *dbcreator = m_creator.latin1();
  for( int p=0; p<4; p++ ) dbc[p]=dbcreator[p];
  stream << dbc[0] << dbc[1] << dbc[2] << dbc[3];

  // write unique id seed
  TQ_UINT32 idseed = 0;
  stream << idseed;

  // now start to read PDB record list (variable-length)

  // next record list
  TQ_UINT32 nextlist = 0;
  stream << nextlist;

  // number of records
  TQ_UINT16 numrec = records.count();
  stream << numrec;

  // where is the first record ?
  // 78 is size of PDB header, 2 is filler before data
  TQ_UINT32 pos = 78 + 2;
  pos += records.count()*8;

  // write record list
  for( unsigned r = 0; r < records.count(); r++ )
  {
    TQ_UINT8 flag = 0, dummy = 0;
    stream << pos;
    stream << flag;
    stream  << dummy << dummy << dummy;
    pos += records.at(r)->count();
  }

  // write 2-byte dummy
  TQ_UINT16 filler = 0;
  stream << filler;

  // write all records
  for( unsigned r = 0; r < records.count(); r++ )
  {
    TQByteArray *data = records.at( r );
    if( !data ) continue;
    for( unsigned j=0; j<data->count(); j++ )
      {
        TQ_UINT8 c = data->at( j ); 
        stream << c;  
      }
  }

  // close output file
  out.close();

  return true;
}

void PalmDB::setType( const TQString& t )
{
  m_type = t;
  if( m_type.length() > 4 )
    m_type = m_type.left( 4 );
  while( m_type.length() < 4 )
    m_type.append( 32 );
}


void PalmDB::setCreator( const TQString& c )
{
  m_creator = c;
  if( m_creator.length() > 4 )
    m_type = m_creator.left( 4 );
  while( m_creator.length() < 4 )
    m_creator.append( 32 );
}