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