~ubuntu-branches/debian/squeeze/sword/squeeze

« back to all changes in this revision

Viewing changes to tests/compnone.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Glassey
  • Date: 2004-01-15 15:50:07 UTC
  • Revision ID: james.westby@ubuntu.com-20040115155007-n9mz4x0zxrs1isd3
Tags: upstream-1.5.7
ImportĀ upstreamĀ versionĀ 1.5.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
 
 
3
#include <ctype.h>
 
4
#include <stdio.h>
 
5
#include <fcntl.h>
 
6
#include <errno.h>
 
7
#include <stdlib.h>
 
8
#include <string.h>
 
9
 
 
10
#ifndef __GNUC__
 
11
#include <io.h>
 
12
#else
 
13
#include <unistd.h>
 
14
#endif
 
15
 
 
16
#include <swcomprs.h>
 
17
#ifndef NO_SWORD_NAMESPACE
 
18
using namespace sword;
 
19
#endif
 
20
 
 
21
class FileCompress: public SWCompress {
 
22
        int ifd;
 
23
        int ofd;
 
24
        int ufd;
 
25
        int zfd;
 
26
public:
 
27
        FileCompress(char *);
 
28
        ~FileCompress();
 
29
        unsigned long GetChars(char *, unsigned long len);
 
30
        unsigned long SendChars(char *, unsigned long len);
 
31
        void Encode();
 
32
        void Decode();
 
33
};
 
34
 
 
35
 
 
36
FileCompress::FileCompress(char *fname) 
 
37
{
 
38
        char buf[256];
 
39
 
 
40
#ifndef O_BINARY
 
41
#define O_BINARY 0
 
42
#endif
 
43
 
 
44
        ufd  = open(fname, O_RDWR|O_CREAT|O_BINARY, 00644);
 
45
 
 
46
        sprintf(buf, "%s.zzz", fname);
 
47
        zfd = open(buf, O_RDWR|O_CREAT|O_BINARY, 00644);
 
48
}
 
49
 
 
50
        
 
51
FileCompress::~FileCompress() 
 
52
{
 
53
        close(ufd);
 
54
        close(zfd);
 
55
}
 
56
 
 
57
 
 
58
unsigned long FileCompress::GetChars(char *buf, unsigned long len) 
 
59
{
 
60
        return read(ifd, buf, len);
 
61
}
 
62
 
 
63
 
 
64
unsigned long FileCompress::SendChars(char *buf, unsigned long len) 
 
65
{
 
66
        return write(ofd, buf, len);
 
67
}
 
68
 
 
69
 
 
70
void FileCompress::Encode() 
 
71
{
 
72
        ifd = ufd;
 
73
        ofd = zfd;
 
74
 
 
75
        SWCompress::Encode();
 
76
}
 
77
 
 
78
 
 
79
void FileCompress::Decode() 
 
80
{
 
81
        ifd = zfd;
 
82
        ofd = ufd;
 
83
 
 
84
        SWCompress::Decode();
 
85
}
 
86
 
 
87
 
 
88
int main(int argc, char **argv)
 
89
{
 
90
        int decomp = 0;
 
91
        SWCompress *fobj;
 
92
        
 
93
        if (argc != 2) {
 
94
                fprintf(stderr, "usage: %s <filename|filename.zzz>\n", argv[0]);
 
95
                exit(1);
 
96
        }
 
97
 
 
98
        if (strlen(argv[1]) > 4) {
 
99
                if (!strcmp(&argv[1][strlen(argv[1])-4], ".zzz")) {
 
100
                        argv[1][strlen(argv[1])-4] = 0;
 
101
                        decomp = 1;
 
102
                }
 
103
        }
 
104
                        
 
105
        fobj = new FileCompress(argv[1]);
 
106
        
 
107
        if (decomp)
 
108
                fobj->Decode();
 
109
        else fobj->Encode();
 
110
 
 
111
        delete fobj;
 
112
}