Diffstat (limited to 'noncore/apps/opie-reader/ZText.h') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/apps/opie-reader/ZText.h | 61 |
1 files changed, 46 insertions, 15 deletions
diff --git a/noncore/apps/opie-reader/ZText.h b/noncore/apps/opie-reader/ZText.h index ab81a5e..06e3bf8 100644 --- a/noncore/apps/opie-reader/ZText.h +++ b/noncore/apps/opie-reader/ZText.h | |||
@@ -1,85 +1,116 @@ | |||
1 | #ifndef __Text_h | 1 | #ifndef __Text_h |
2 | #define __Text_h | 2 | #define __Text_h |
3 | #include <stdio.h> | 3 | #include <stdio.h> |
4 | #include <zlib.h> | 4 | #include <zlib.h> |
5 | #include <sys/stat.h> | 5 | #include <sys/stat.h> |
6 | #include "useqpe.h" | 6 | #include "useqpe.h" |
7 | #include "CExpander.h" | 7 | #include "CExpander.h" |
8 | 8 | ||
9 | class Text: public CExpander { | 9 | class Text: public CExpander { |
10 | gzFile file; | 10 | gzFile file; |
11 | unsigned long fsize; | 11 | unsigned long fsize; |
12 | public: | 12 | public: |
13 | void suspend() | 13 | void suspend() |
14 | { | 14 | { |
15 | #ifdef USEQPE | 15 | #ifdef USEQPE |
16 | bSuspended = true; | 16 | if (!bSuspended) |
17 | suspos = gztell(file); | 17 | { |
18 | gzclose(file); | 18 | bSuspended = true; |
19 | file = NULL; | 19 | suspos = gztell(file); |
20 | sustime = time(NULL); | 20 | gzclose(file); |
21 | file = NULL; | ||
22 | sustime = time(NULL); | ||
23 | } | ||
21 | #endif | 24 | #endif |
22 | } | 25 | } |
23 | void unsuspend() | 26 | void unsuspend() |
24 | { | 27 | { |
25 | #ifdef USEQPE | 28 | #ifdef USEQPE |
26 | if (bSuspended) | 29 | if (bSuspended) |
27 | { | 30 | { |
28 | bSuspended = false; | 31 | bSuspended = false; |
29 | int delay = time(NULL) - sustime; | 32 | if (sustime != ((time_t)-1)) |
30 | if (delay < 10) sleep(10-delay); | 33 | { |
34 | int delay = time(NULL) - sustime; | ||
35 | if (delay < 10) sleep(10-delay); | ||
36 | } | ||
31 | file = gzopen(fname, "rb"); | 37 | file = gzopen(fname, "rb"); |
32 | for (int i = 0; file == NULL && i < 5; i++) | 38 | for (int i = 0; file == NULL && i < 5; i++) |
33 | { | 39 | { |
34 | sleep(5); | 40 | sleep(5); |
35 | file = gzopen(fname, "rb"); | 41 | file = gzopen(fname, "rb"); |
36 | } | 42 | } |
37 | if (file == NULL) | 43 | if (file == NULL) |
38 | { | 44 | { |
39 | QMessageBox::warning(NULL, PROGNAME, "Couldn't reopen file"); | 45 | QMessageBox::warning(NULL, PROGNAME, "Couldn't reopen file"); |
40 | exit(0); | 46 | exit(0); |
41 | } | 47 | } |
42 | suspos = gzseek(file, suspos, SEEK_SET); | 48 | suspos = gzseek(file, suspos, SEEK_SET); |
43 | } | 49 | } |
44 | #endif | 50 | #endif |
45 | } | 51 | } |
46 | Text() : file(NULL) {}; | 52 | Text() : file(NULL) {}; |
47 | virtual ~Text() | 53 | virtual ~Text() |
48 | { | 54 | { |
49 | if (file != NULL) gzclose(file); | 55 | if (file != NULL) |
56 | { | ||
57 | #ifdef USEQPE | ||
58 | unsuspend(); | ||
59 | #endif | ||
60 | gzclose(file); | ||
61 | } | ||
50 | } | 62 | } |
51 | int OpenFile(const char *src) | 63 | int OpenFile(const char *src) |
52 | { | 64 | { |
53 | if (file != NULL) gzclose(file); | 65 | if (file != NULL) gzclose(file); |
54 | struct stat _stat; | 66 | struct stat _stat; |
55 | stat(src,&_stat); | 67 | stat(src,&_stat); |
56 | fsize = _stat.st_size; | 68 | fsize = _stat.st_size; |
57 | return ((file = gzopen(src,"rb")) == NULL); | 69 | return ((file = gzopen(src,"rb")) == NULL); |
58 | } | 70 | } |
59 | int getch() { return gzgetc(file); } | 71 | int getch() |
60 | unsigned int locate() { return gztell(file); } | 72 | { |
61 | void locate(unsigned int n) { gzseek(file,n,SEEK_SET); } | 73 | #ifdef USEQPE |
74 | unsuspend(); | ||
75 | #endif | ||
76 | return gzgetc(file); | ||
77 | } | ||
78 | unsigned int locate() | ||
79 | { | ||
80 | #ifdef USEQPE | ||
81 | unsuspend(); | ||
82 | #endif | ||
83 | return gztell(file); | ||
84 | } | ||
85 | void locate(unsigned int n) | ||
86 | { | ||
87 | #ifdef USEQPE | ||
88 | unsuspend(); | ||
89 | #endif | ||
90 | gzseek(file,n,SEEK_SET); | ||
91 | } | ||
62 | bool hasrandomaccess() { return true; } | 92 | bool hasrandomaccess() { return true; } |
63 | void sizes(unsigned long& _file, unsigned long& _text) | 93 | void sizes(unsigned long& _file, unsigned long& _text) |
64 | { | 94 | { |
65 | _text = _file = fsize; | 95 | _text = _file = fsize; |
66 | FILE* f = fopen(fname, "rb"); | 96 | FILE* f = fopen(fname, "rb"); |
67 | if (f != NULL) | 97 | if (f != NULL) |
68 | { | 98 | { |
69 | unsigned char mn[2]; | 99 | unsigned char mn[2]; |
70 | fread(mn, 1, 2, f); | 100 | fread(mn, 1, 2, f); |
71 | if ((mn[0] == 31) && (mn[1] == 139)) | 101 | if ((mn[0] == 31) && (mn[1] == 139)) |
72 | { | 102 | { |
73 | int tmp = sizeof(_text); | 103 | int tmp = sizeof(_text); |
74 | fseek(f,-tmp,SEEK_END); | 104 | fseek(f,-tmp,SEEK_END); |
75 | fread(&_text, sizeof(_text), 1, f); | 105 | fread(&_text, sizeof(_text), 1, f); |
76 | } | 106 | } |
77 | fclose(f); | 107 | fclose(f); |
78 | } | 108 | } |
79 | } | 109 | } |
80 | MarkupType PreferredMarkup() | 110 | MarkupType PreferredMarkup() |
81 | { | 111 | { |
82 | return cTEXT; | 112 | return cTEXT; |
83 | } | 113 | } |
114 | QString about() { return QString("Text/gzipped text codec (c) Tim Wentford"); } | ||
84 | }; | 115 | }; |
85 | #endif | 116 | #endif |