summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-reader/pdb.cpp
blob: a13b33ddd6d7e995500f44e25de195de9eb69f3f (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
#include "pdb.h"
#ifdef _WINDOWS
#include <winsock2.h>
#endif

#ifdef USEQPE
void Cpdb::suspend()
{
  CExpander::suspend(fin);
}
void Cpdb::unsuspend()
{
  CExpander::unsuspend(fin);
}
#endif

Cpdb::~Cpdb()
{
  if (fin != NULL)
    {
#ifdef USEQPE
      unsuspend();
#endif
      fclose(fin);
    }
}

size_t Cpdb::recordpos(int n)
{
    UInt16 mxn = ntohs(head.recordList.numRecords);
    if (n >= mxn)
    {
	return file_length;
    }
    else
    {
	size_t dataoffset = sizeof(DatabaseHdrType) - sizeof(UInt16);
	dataoffset += /*dataoffset%4 + */ sizeof(RecordListType) * n;
	unsuspend();
	fseek(fin, dataoffset, SEEK_SET);
	RecordListType hdr;
	fread(&hdr, 1, sizeof(hdr), fin);
	return ntohl(hdr.nextRecordListID);
    }

}

size_t Cpdb::recordlength(int n)
{
    return recordpos(n+1)-recordpos(n);
}

void Cpdb::gotorecordnumber(int n)
{
unsuspend();
    fseek(fin, recordpos(n), SEEK_SET);
}

bool Cpdb::openpdbfile(const char *src)
{
  //qDebug("cpdb openfile:%s", src);

    //  printf("In openfile\n");
    //  printf("closing fin:%x\n",fin);
    if (fin != NULL) fclose(fin);
    //  printf("opening fin\n");
    fin = fopen(src,"rb");

    if (fin==0)
    {
	return false;
    }

    // just holds the first few chars of the file
    //	char buf[0x100];
      struct stat _stat;
      stat(src,&_stat);
      file_length = _stat.st_size;
      //    fseek(fin,0,SEEK_END);
      //    file_length = ftell(fin);

    fseek(fin,0,SEEK_SET);

    fread(&head, 1, sizeof(head), fin);

    //qDebug("Database name:%s", head.name);
    //qDebug("Total number of records:%u", ntohs(head.recordList.numRecords));

    return true;
}