summaryrefslogtreecommitdiff
path: root/lib/gcstar/GCExport/GCExportPDB.pm
blob: af1e4db6e469c63e05ad422329fb233c48d4e8ed (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
package GCExport::GCExportPDB;

###################################################
#
#  Copyright 2009-2010 Andrew Ross
#
#  This file is part of GCstar.
#
#  GCstar is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  GCstar 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 General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with GCstar; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
#
###################################################

use strict;

use GCExport::GCExportBase;

{
    package GCExport::GCExporterPDB;
    
    use base qw(GCExport::GCExportBaseClass);
    use Encode;
    
    my @record_lengths;

    my $EPOCH_1904 = 2082844800;  # Difference between Palm's
                                  # epoch (Jan. 1, 1904) and
                                  # Unix's epoch (Jan. 1, 1970),
                                  # in seconds.


    sub new
    {
        my $proto = shift;
        my $class = ref($proto) || $proto;
        my $self  = $class->SUPER::new();
        
        bless ($self, $class);
        return $self;
    }

    sub getOptions
    {
        my $self = shift;
        
        return [
            {
                name => 'dbname',
                type => 'short text',
                label => 'DatabaseName',
                default => 'gcstar'
            },
        ];
        
    }
      
    sub wantsFieldsSelection
    {
        return 1;
    }
    
    sub wantsImagesSelection
    {
        return 0;
    }

    sub wantsSort
    {
        return 1;
    }

    sub needsUTF8
    {
        my $self = shift;
        return 0;
    }

    sub getSuffix
    {
        my $self = shift;
        
        return ".pdb";
    }
    
    sub preProcess
    {
        my $self =  shift;
        return 1;
    }

    sub transformValue
    {
        my ($self, $value, $field) = @_;
        
        if ($field)
        {
            $value = $self->SUPER::transformValue($value, $field);
        }
        $value =~ s/,+$//;
        $value =~ s/\n|\r//g;
        $value =~ s/<br\/>/ /g;

        return $value;
    }

    sub getHeader
    {
        my ($self, $number) = @_;
        my $result = '';
        
        # clear the record lengths array
        @record_lengths = ();

        # Add database title
        my $name = $self->{options}->{'dbname'};
        if (length($name) > 31) 
        {
            $name = substr($name, 0, 31);
        }
        while (length($name) < 32) 
        {
            $name .= "\x00"; # pack out with null's
        }
        $result .= $name;

        # Add attribute flags (=0)
        $result .= pack('n', 0);
        
        # Add file version (=0)
        $result .= pack('n', 0);

        # Add dates for create time, modify time, backup time
        # These dates are the number of seconds since 1st Jan 1904
        my $now = time() + $EPOCH_1904;

        $result .= pack('N', $now);
        $result .= pack('N', $now);
        $result .= pack('N', $now);

        # Add the Modification Number (=0)
        $result .= pack('N', 0);

        # Add the offset to the Application Info
        # offset calculated as:
        # Title:                       0x20
        # flags + version + 3 x dates  0x10
        # mod_number + app_offset      0x08
        # sortID + type                0x08
        # creator + seed               0x08
        # recordListID + cnt + 2byte   0x08
        # 8 bytes per record           8 * $number
        $result .= pack('N', 0x50 + (8 * $number));

        # Add null for the sortInfoID since we don't create a sortInfo
        $result .= pack('N', 0);

        # Add the type
        $result .= "DB00";

        # Add the creator
        $result .= "DBOS";

        # add the uniqueIDseed = 0
        $result .= pack('N', 0);

        # Add the nextRecordListID = 0 when on disk
        $result .= pack('N', 0);

        # add the record count
        $result .= pack('n',$number);
        
        # The record offset table goes here, but is added in postProcess()

        # "Traditional" 2-byte gap to data
        $result .= pack('n', 0);

        # Start the AppInfoID section
        $result .= pack('N', 2);


        # CHUNK_FIELD_NAMES (0)
        $result .= pack('n',0);
        my $fieldstring = '';
        foreach (@{$self->{options}->{fields}})
        {
            my $column = $self->{model}->{fieldsInfo}->{$_}->{displayed};
            $fieldstring .= $self->transformValue($column)."\x00";
        }
        $result .= pack('n', length($fieldstring));
        $result .= $fieldstring;

        # CHUNK_FIELD_TYPES (1)
        $result .= pack('n',1);
        $fieldstring = '';
        foreach (@{$self->{options}->{fields}})
        {
            $fieldstring .= pack('n',0);
        }
        $result .= pack('n', length($fieldstring));
        $result .= $fieldstring;

        # CHUNK_LISTVIEW_OPTIONS (65)
        $result .= pack('n',65);
        $result .= pack('n',4);
        $result .= pack('n',0);
        $result .= pack('n',0);

        # CHUNK_LFIND_OPTIONS (128)
        $result .= pack('n',128);
        $result .= pack('n',2);
        $result .= pack('n',0);

        return $result;
    }

    sub getItem
    {
        my ($self, $item, $number) = @_;
        my $result;

        my @lengths = ();
        my $fieldstr;
        foreach (@{$self->{options}->{fields}})
        {
            my $value = $item->{$_};
            my $str = $self->transformValue($value, $_)."\x00";
            push (@lengths, length($str));
            $fieldstr .= $str;
        }

        my $al = scalar(@lengths) * 2;
        for(my $i=0;$i<=$#lengths;$i++) 
        {
            $result .= pack('n', $al);
            $al += $lengths[$i];
        }
        $result .= $fieldstr;
        push (@record_lengths, length($fieldstr)+(2 * scalar(@lengths)));

        return $result;
    }

    sub getFooter
    {
        my $self = shift;
        my $result;

        return $result;
    }

    sub postProcess
    {
        my ($self, $header, $body) = @_;

        # add the index:
        my $index = "";

        my $numrecs = scalar(@record_lengths);
        my $offset = length($$header) + (8*$numrecs);

        for (my $i=0;$i<$numrecs;$i++)
        {
            $index .= pack('N', $offset);
            $index .= pack('n', 0);
            $index .= pack('n', $i);
            $offset += $record_lengths[$i];
        }

        # Insert the index into the header
        $$header = substr($$header, 0, 0x4e).$index.substr($$header,0x4e);
    }

    sub getEndInfo
    {
        my $self = shift;
        my $message;
        
        return $message;
    }


}

1;