summaryrefslogtreecommitdiff
path: root/src/plugin-luaiup.cc
blob: c1a07ff10df271fa9667f9bca55765f9d48d520c (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
#include "BLua.h"
#include "Input.h"
#include "Output.h"
#include "iup.h"
#include "iuplua.h"
#include "iupcontrols.h"
#include "iupluacontrols.h"
#include "iup_pplot.h"
#include "iuplua_pplot.h"
#include <cd.h>
#include <cdgdiplus.h>
#include <cdlua.h>
#include <cdluaiup.h>
#include "iupluaim.h"
#include <im.h>
#include <im_image.h>
#include <imlua.h>
#include <cdluaim.h>
#include <im_binfile.h>

#ifdef FROM_LUAINTERFACE
#define NO_DLL
#endif

#ifndef WIN32
#define WEAK __attribute__ ((weak))
#else
#define WEAK
#endif

class imBinFileHandle : public imBinFileBase {
  public:
      imBinFileHandle() : h(NULL), hasError(0) {}
    virtual int HasError() const { return hasError; }
    virtual void Open(const char* pFileName) {
        try {
            h = new Input(pFileName);
        } catch (...) {
            hasError = 1;
        }
    }
    virtual void New(const char* pFileName) {
        try {
            h = new Output(pFileName);
        } catch (...) {
            hasError = 1;
        }
    }
    virtual void Close() {
        try {
            delete h;
            h = 0;
        } catch (...) {
            hasError = 1;
        }
    }
    virtual unsigned long FileSize() {
        unsigned long r = 0;
        try {
            r = h->GetSize();
        } catch (...) {
            hasError = 1;
        }
        
        return r;
    }
    virtual void SeekTo(unsigned long pOffset) {
        try {
            h->seek(pOffset, SEEK_SET);
        } catch (...) {
            hasError = 1;
        }
    }
    virtual void SeekOffset(long pOffset) {
        try {
            h->seek(pOffset, SEEK_CUR);
        } catch (...) {
            hasError = 1;
        }
    }
    virtual void SeekFrom(long pOffset) {
        try {
            h->seek(pOffset, SEEK_END);
        } catch (...) {
            hasError = 1;
        }
    }
    virtual unsigned long Tell() const {
        unsigned long r = 0;
        try {
            r = h->tell();
        } catch (...) {
            hasError = 1;
        }
        
        return r;
    }
    virtual int EndOfFile() const {
        int r = 0;
        try {
            r = h->tell() == h->GetSize();
        } catch (...) {
            hasError = 1;
        }
        
        return r;
    }
  protected:
    virtual unsigned long ReadBuf(void* pValues, unsigned long pSize) {
        unsigned long r = 0;
        try {
            r = h->read(pValues, pSize);
        } catch (...) {
            hasError = 1;
        }
        
        return r;
    }
    virtual unsigned long WriteBuf(void* pValues, unsigned long pSize) {
        unsigned long r = 0;
        try {
            r = h->write(pValues, pSize);
        } catch (...) {
            hasError = 1;
        }
        
        return r;
    }
  private:
    Handle * h;
    mutable int hasError;
};

static imBinFileBase * imBinFileHandleFunc() { return new imBinFileHandle(); }

static void _init_plugin(Lua * L) {
    static bool done = false;
    if (done) return;
    done = true;
    L->wrap_open(iuplua_open);
    L->wrap_open(iupkey_open);
    L->wrap_open(iupcontrolslua_open);
    L->wrap_open(cdlua_open);
    L->wrap_open(cdluaiup_open);
    L->wrap_open(iupimlua_open);
    L->wrap_open(imlua_open);
    L->wrap_open(imlua_open_process);
    L->wrap_open(cdluaim_open);
    
    int id = imBinFileRegisterModule(imBinFileHandleFunc);
    if (id >= 0)
        imBinFileSetCurrentModule(id);
}

extern "C" {

#ifndef NO_DLL
WEAK void init_plugin(Lua * L) {
    _init_plugin(L);
}
#endif

void luaiup_init(Lua * L) {
    _init_plugin(L);
}
    
}