1 /*
2  * Files.cpp
3  * Copyright (C) 2007 by Bryan Duff <duff0097@gmail.com>
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
18  * USA
19  */
20 #include "Files.h"
21 
OpenFile(Str255 Name)22 short Files::OpenFile(Str255 Name)
23 {
24   short volume;
25   char filename[33];
26   short tFile;
27   tFile = -1;
28   Point ptOrigin = { 0, 0 };
29   volume = 0;
30   sprintf(filename, "%s", Name);
31   SetVol(nil, volume);
32   CtoPstr(filename);
33   FSOpen((Pstr) filename, volume, &tFile);
34   PtoCstr((Pstr) filename);
35   sFile = tFile;
36   return (tFile);
37 }
38 
OpenNewFile(SFReply * psfReply,OSType osTypeCreator,OSType osTypeType)39 short Files::OpenNewFile(SFReply * psfReply,
40                          OSType osTypeCreator, OSType osTypeType)
41 {
42   sFile = 0;
43   OSErr osErr;
44 
45   SetVol(nil, psfReply->vRefNum);
46   osErr = Create(psfReply->fName, psfReply->vRefNum, osTypeCreator, osTypeType);
47 
48   if(osErr == dupFNErr) {
49     FSDelete(psfReply->fName, psfReply->vRefNum);
50     Create(psfReply->fName, psfReply->vRefNum, osTypeCreator, osTypeType);
51   }
52 
53   FSOpen(psfReply->fName, psfReply->vRefNum, &sFile);
54 
55   return (sFile);
56 }
57 
58 
PromptForSaveAS(short sPromptID,short sNameID,Str255 str255NamePrompt,OSType osTypeCreator,OSType osTypeType,SFReply * psfReply)59 short Files::PromptForSaveAS(short sPromptID,
60                              short sNameID,
61                              Str255 str255NamePrompt,
62                              OSType osTypeCreator,
63                              OSType osTypeType, SFReply * psfReply)
64 {
65   Str255 str255Prompt;
66   Str255 str255Name;
67   sFile = 0;
68   Point ptOrigin = { 0, 0 };
69 
70   GetIndString(str255Prompt, FILE_STRINGS, sPromptID);
71 
72   if(!str255NamePrompt)
73     GetIndString(str255Name, FILE_STRINGS, sNameID);
74 
75   else
76     memcpy(str255Name, str255NamePrompt, *str255NamePrompt + 1);
77 
78   SFPutFile(ptOrigin, str255Prompt, str255Name, nil, psfReply);
79 
80   if(psfReply->good) {
81     sFile = OpenNewFile(psfReply, osTypeCreator, osTypeType);
82   }
83 
84   return (sFile);
85 }
86 
OpenSavedGame(Str255 Name)87 short Files::OpenSavedGame(Str255 Name)
88 {
89   Point ptOrigin = { 0, 0 };
90   sSavedGameVolume = 0;
91   sprintf(szSavedGameName, "%s", Name);
92   SetVol(nil, sSavedGameVolume);
93   CtoPstr(szSavedGameName);
94   FSOpen((Pstr) szSavedGameName, sSavedGameVolume, &sFile);
95   PtoCstr((Pstr) szSavedGameName);
96   return (sFile);
97 }
98 
OpenFileDialog()99 short Files::OpenFileDialog()
100 {
101   Point ptOrigin = { 0, 0 };
102   sFile = 0;
103   SFReply sfReply;
104   SFTypeList sfTypeList = { 'DMAP', '\p', '\p', '\p' };
105   SFGetFile(ptOrigin, "\p", nil, 1, sfTypeList, nil, &sfReply);
106 
107   if(sfReply.good) {
108     PtoCstr(sfReply.fName);
109     strcpy(szSavedGameName, (Cstr) sfReply.fName);
110   }
111 
112   if(sfReply.good) {
113     sSavedGameVolume = sfReply.vRefNum;
114     SetVol(nil, sSavedGameVolume);
115 
116     CtoPstr(szSavedGameName);
117 
118     FSOpen((Pstr) szSavedGameName, sSavedGameVolume, &sFile);
119 
120     PtoCstr((Pstr) szSavedGameName);
121   }
122 
123   return (sFile);
124 }
125 
StartSave()126 void Files::StartSave()
127 {
128   int x, y;
129   SFReply sfReply;
130   sFile = 0;
131   long lSize;
132   long lLongSize = sizeof (long);
133 
134   CtoPstr(szSavedGameName);
135 
136   sFile =
137       PromptForSaveAS(SAVE_GAME_STRING, 0, (Pstr) szSavedGameName, 'DAVD',
138                       'DMAP', &sfReply);
139 
140   PtoCstr((Pstr) szSavedGameName);
141 
142   if(sFile) {
143     sSavedGameVolume = sfReply.vRefNum;
144     PtoCstr(sfReply.fName);
145     strcpy(szSavedGameName, (Cstr) sfReply.fName);
146   }
147 
148   else {
149     sfReply.vRefNum = sSavedGameVolume;
150     strcpy((Cstr) sfReply.fName, szSavedGameName);
151     CtoPstr((Cstr) sfReply.fName);
152 
153     sFile = OpenNewFile(&sfReply, 'GLF2', 'SKLT');
154   }
155 }
156 
EndSave()157 void Files::EndSave()
158 {
159   if(sFile)
160     FSClose(sFile);
161 }
162 
StartLoad()163 void Files::StartLoad()
164 {
165   sFile = OpenFileDialog();
166 }
167 
EndLoad()168 void Files::EndLoad()
169 {
170   if(sFile)
171     FSClose(sFile);
172 }
173