Subversion Repositories filter_foundry

Rev

Rev 433 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  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.  
  58.         *refNum = CreateFile(spec->szName,perm[permission],0,0,OPEN_EXISTING,0,0);
  59. //      sprintf(s,"FSpOpenDF(\"%s\",\"%s\"):%#x",spec->name,perm[permission],*refNum); dbg(s);
  60.         return *refNum == INVALID_HANDLE_VALUE ? ioErr : noErr;
  61. }
  62.  
  63. OSErr FSpCreate(const FSSpec *spec, OSType creator, OSType fileType, ScriptCode scriptTag){
  64.         HANDLE h;
  65.  
  66.         UNREFERENCED_PARAMETER(creator);
  67.         UNREFERENCED_PARAMETER(scriptTag);
  68.         UNREFERENCED_PARAMETER(fileType);
  69.  
  70.         h = CreateFile(spec->szName,0,0,0,CREATE_NEW,0,0);
  71. //      sprintf(s,"FSpCreate(\"%s\"):%#x",spec->name,h); dbg(s);
  72.         if( h == INVALID_HANDLE_VALUE )
  73.                 return ioErr;
  74.         else{
  75.                 CloseHandle(h);
  76.                 return noErr;
  77.         }
  78. }
  79.  
  80. OSErr FSpDelete(const FSSpec *spec){
  81.         BOOL f;
  82.  
  83.         f = DeleteFile(spec->szName);
  84.         return f ? noErr : ioErr;
  85. }
  86.  
  87. OSErr GetFPos(FILEREF   refNum,FILEPOS *  filePos){
  88.         *filePos = SetFilePointer(refNum,0,0,FILE_CURRENT);
  89. //      sprintf(s,"GetFPos(%#x):%#x",refNum,*filePos); dbg(s);
  90.         return *filePos == INVALID_SET_FILE_POINTER ? ioErr : noErr;
  91. }
  92.  
  93. OSErr SetFPos(FILEREF   refNum,short   posMode,long    posOff){
  94.         static DWORD method[]={0,FILE_BEGIN,FILE_END,FILE_CURRENT};
  95.         DWORD res = SetFilePointer(refNum,posOff,0,method[posMode]);
  96. //      sprintf(s,"SetFPos(%#x,%d,%d):%#x",refNum,posMode,posOff,res); dbg(s);
  97.         return res == INVALID_SET_FILE_POINTER ? ioErr : noErr;
  98. }
  99. OSErr GetEOF(FILEREF   refNum,FILEPOS *  logEOF){
  100.         DWORD n = GetFileSize(refNum,NULL);
  101. //      {char s[100];sprintf(s,"GetEOF(%#x):%d",refNum,n); dbg(s);}
  102.         *logEOF = n;
  103.         return n == INVALID_FILE_SIZE ? ioErr : noErr;
  104. }
  105. OSErr SetEOF(FILEREF   refNum,FILEPOS logEOF){
  106.         return SetFilePointer(refNum,logEOF,0,FILE_BEGIN) == INVALID_SET_FILE_POINTER
  107.                 || !SetEndOfFile(refNum) ? ioErr : noErr;
  108. }
  109.  
  110. #if 0 // this code is not really cooked
  111.  
  112. /* Resources */
  113.  
  114. struct rchain_node{
  115.         struct rchain_node *next;
  116.         HMODULE hmodule;
  117. } *resource_chain = 0;
  118.  
  119. HMODULE FSpOpenResFile(const FSSpec *  spec,SignedByte      permission){
  120.         struct rchain_node *p;
  121.         HMODULE hm;
  122.         char name[MAX_PATH+1];
  123.         if( hm = LoadLibrary(myp2cstrcpy(name,spec->name)) ){
  124.                 p = (struct rchain_node*)malloc(sizeof(struct rchain_node)));
  125.                 p->hmodule = hm;
  126.                 p->next = resource_chain;
  127.                 resource_chain = p;
  128.                 return hm;
  129.         }
  130.         myc2pstr(spec->name);
  131.         return 0;
  132. }
  133.  
  134. void CloseResFile(HMODULE hm){
  135.         struct rchain_node *p;
  136.         for( p = resource_chain ; p ; p = p->next )
  137.                 if(p->hmodule == hm){
  138.                         FreeLibrary(p->hmodule);
  139.                         p->hmodule = 0;
  140.                         break;
  141.                 }
  142.         /* p now points to the node we want to delete, or NULL if none found */
  143.         if(p == resource_chain){
  144.                 resource_chain = resource_chain->next;
  145.                 free(p);
  146.         }
  147. }
  148.  
  149. HGLOBAL GetResource(ResType   theType,short     theID){
  150.         HRSRC h;
  151.         char t[5];
  152.  
  153.         if(resource_chain && resource_chain->hmodule){
  154.                 t[0] = theType>>24;
  155.                 t[1] = theType>>16;
  156.                 t[2] = theType>>8;
  157.                 t[3] = theType;
  158.                 t[4] = 0;
  159.                 if(h = FindResource(resource_chain->hmodule,theID,t))
  160.                         return LoadResource(resource_chain->hmodule,h);
  161.         }
  162.         return 0;
  163. }
  164.  
  165.  
  166. #endif
  167.