Subversion Repositories filter_foundry

Rev

Rev 198 | Rev 268 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
259 daniel-mar 1
/*
2
        This file is part of a common library
3
    Copyright (C) 2002-6 Toby Thain, toby@telegraphics.com.au
4
 
5
    This program is free software; you can redistribute it and/or modify
6
    it under the terms of the GNU General Public License as published by
7
    the Free Software Foundation; either version 2 of the License, or
8
    (at your option) any later version.
9
 
10
    This program is distributed in the hope that it will be useful,
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
    GNU General Public License for more details.
14
 
15
    You should have received a copy of the GNU General Public License
16
    along with this program; if not, write to the Free Software
17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
*/
19
 
20
#include <windows.h>
21
#include <stdio.h>
22
 
23
#include "file_compat.h"
24
#include "str.h"
25
 
26
OSErr FSClose(FILEREF f){
27
//      dbg("FSClose");
28
        return CloseHandle(f) ? noErr : ioErr;
29
}
30
 
31
OSErr FSWrite(FILEREF refNum, long *count, const void *buffPtr){
32
        DWORD n;
33
        BOOL f;
34
 
35
        f = WriteFile(refNum,buffPtr,*count,&n,0);
36
//      sprintf(s,"FSWrite(%d,%#x):%d",*count,buffPtr,n); dbg(s);
37
        *count = n;
38
        return f ? noErr : ioErr;
39
}
40
OSErr FSRead(FILEREF refNum, long *count, void *buffPtr){
41
        DWORD n;
42
        BOOL f;
43
 
44
        f = ReadFile(refNum,buffPtr,*count,&n,0);
45
//      sprintf(s,"FSRead(%d,%#x):%d",*count,buffPtr,n); dbg(s);
46
        *count = n;
47
        return f ? (n ? noErr : eofErr) : ioErr;
48
}
49
 
50
OSErr FSpOpenDF(const FSSpec *spec, int permission, FILEREF *refNum){
51
        static DWORD perm[] = {
52
                /* 0: fsCurPerm  */ GENERIC_READ | GENERIC_WRITE,
53
                /* 1: fsRdPerm   */ GENERIC_READ,
54
                /* 2: fsWrPerm   */ GENERIC_WRITE,
55
                /* 3: fsRdWrPerm */ GENERIC_READ | GENERIC_WRITE
56
        };
57
        char name[MAX_PATH+1];
58
 
59
        *refNum = CreateFile(myp2cstrcpy(name,spec->name),perm[permission],0,0,OPEN_EXISTING,0,0);
60
//      sprintf(s,"FSpOpenDF(\"%s\",\"%s\"):%#x",spec->name,perm[permission],*refNum); dbg(s);
61
        return *refNum == INVALID_HANDLE_VALUE ? ioErr : noErr;
62
}
63
 
64
OSErr FSpCreate(const FSSpec *spec, OSType creator, OSType fileType, ScriptCode scriptTag){
65
        HANDLE h;
66
        char name[MAX_PATH+1];
67
 
68
        h = CreateFile(myp2cstrcpy(name,spec->name),0,0,0,CREATE_NEW,0,0);
69
//      sprintf(s,"FSpCreate(\"%s\"):%#x",spec->name,h); dbg(s);
70
        if( h == INVALID_HANDLE_VALUE )
71
                return ioErr;
72
        else{
73
                CloseHandle(h);
74
                return noErr;
75
        }
76
}
77
 
78
OSErr FSpDelete(const FSSpec *spec){
79
        BOOL f;
80
        char name[MAX_PATH+1];
81
 
82
        f = DeleteFile(myp2cstrcpy(name,spec->name));
83
        return f ? noErr : ioErr;
84
}
85
 
86
OSErr GetFPos(FILEREF   refNum,FILEPOS *  filePos){
87
        *filePos = SetFilePointer(refNum,0,0,FILE_CURRENT);
88
//      sprintf(s,"GetFPos(%#x):%#x",refNum,*filePos); dbg(s);
89
        return *filePos == INVALID_SET_FILE_POINTER ? ioErr : noErr;
90
}
91
 
92
OSErr SetFPos(FILEREF   refNum,short   posMode,long    posOff){
93
        static DWORD method[]={0,FILE_BEGIN,FILE_END,FILE_CURRENT};
94
        DWORD res = SetFilePointer(refNum,posOff,0,method[posMode]);
95
//      sprintf(s,"SetFPos(%#x,%d,%d):%#x",refNum,posMode,posOff,res); dbg(s);
96
        return res == INVALID_SET_FILE_POINTER ? ioErr : noErr;
97
}
98
OSErr GetEOF(FILEREF   refNum,FILEPOS *  logEOF){
99
        DWORD n = GetFileSize(refNum,NULL);
100
//      {char s[100];sprintf(s,"GetEOF(%#x):%d",refNum,n); dbg(s);}
101
        *logEOF = n;
102
        return n == INVALID_FILE_SIZE ? ioErr : noErr;
103
}
104
OSErr SetEOF(FILEREF   refNum,FILEPOS logEOF){
105
        return SetFilePointer(refNum,logEOF,0,FILE_BEGIN) == INVALID_SET_FILE_POINTER
106
                || !SetEndOfFile(refNum) ? ioErr : noErr;
107
}
108
 
109
#if 0 // this code is not really cooked
110
 
111
/* Resources */
112
 
113
struct rchain_node{
114
        struct rchain_node *next;
115
        HMODULE hmodule;
116
} *resource_chain = 0;
117
 
118
HMODULE FSpOpenResFile(const FSSpec *  spec,SignedByte      permission){
119
        struct rchain_node *p;
120
        HMODULE hm;
121
        char name[MAX_PATH+1];
122
        if( hm = LoadLibrary(myp2cstrcpy(name,spec->name)) ){
123
                p = (struct rchain_node*)malloc(sizeof(struct rchain_node)));
124
                p->hmodule = hm;
125
                p->next = resource_chain;
126
                resource_chain = p;
127
                return hm;
128
        }
129
        myc2pstr(spec->name);
130
        return 0;
131
}
132
 
133
void CloseResFile(HMODULE hm){
134
        struct rchain_node *p;
135
        for( p = resource_chain ; p ; p = p->next )
136
                if(p->hmodule == hm){
137
                        FreeLibrary(p->hmodule);
138
                        p->hmodule = 0;
139
                        break;
140
                }
141
        /* p now points to the node we want to delete, or NULL if none found */
142
        if(p == resource_chain){
143
                resource_chain = resource_chain->next;
144
                free(p);
145
        }
146
}
147
 
148
HGLOBAL GetResource(ResType   theType,short     theID){
149
        HRSRC h;
150
        char t[5];
151
 
152
        if(resource_chain && resource_chain->hmodule){
153
                t[0] = theType>>24;
154
                t[1] = theType>>16;
155
                t[2] = theType>>8;
156
                t[3] = theType;
157
                t[4] = 0;
158
                if(h = FindResource(resource_chain->hmodule,theID,t))
159
                        return LoadResource(resource_chain->hmodule,h);
160
        }
161
        return 0;
162
}
163
 
164
 
165
#endif