chiark / gitweb /
Initial revision
[ssr] / StraySrc / Libraries / Sapphire / s / res
1 ;
2 ; res.s
3 ;
4 ; Locating resources (MDW)
5 ;
6 ; © 1994-1998 Straylight
7 ;
8
9 ;----- Licensing note -------------------------------------------------------
10 ;
11 ; This file is part of Straylight's Sapphire library.
12 ;
13 ; Sapphire is free software; you can redistribute it and/or modify
14 ; it under the terms of the GNU General Public License as published by
15 ; the Free Software Foundation; either version 2, or (at your option)
16 ; any later version.
17 ;
18 ; Sapphire is distributed in the hope that it will be useful,
19 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 ; GNU General Public License for more details.
22 ;
23 ; You should have received a copy of the GNU General Public License
24 ; along with Sapphire.  If not, write to the Free Software Foundation,
25 ; 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26
27 ;----- Standard header ------------------------------------------------------
28
29                 GET     libs:header
30                 GET     libs:swis
31
32 ;----- External dependencies ------------------------------------------------
33
34                 GET     sapphire:string
35                 GET     sapphire:sapphire
36
37 ;----- Main code ------------------------------------------------------------
38
39                 AREA    |Sapphire$$Code|,CODE,READONLY
40
41 ; --- res_exists ---
42 ;
43 ; On entry:     R0 == pointer to pathname
44 ;
45 ; On exit:      CS if the file exists, CC otherwise
46 ;
47 ; Use:          Tries to find the named file.  The file is deemed to exist
48 ;               if OS_File can return a valid object type for it (i.e. not
49 ;               `non-existant object' or actually raising errors).
50
51                 EXPORT  res_exists
52 res_exists      ROUT
53
54                 STMFD   R13!,{R0-R5,R14}        ;Stack some registers
55                 MOV     R1,R0                   ;Point to the filename
56                 MOV     R0,#17                  ;Find information about file
57                 SWI     XOS_File                ;Try to find the file
58                 MOVVS   R0,#0                   ;If error, say didn't exist
59                 CMP     R0,#0                   ;Is it a nonexistant thing?
60                 LDMFD   R13!,{R0-R5,R14}        ;Restore the registers
61                 ORRNES  PC,R14,#C_flag          ;If it is there, set C
62                 BICEQS  PC,R14,#C_flag          ;Otherwise clear it
63
64                 LTORG
65
66 ; --- res__trySuffix ---
67 ;
68 ; On entry:     R0 == pointer to pathname to try
69 ;               R1 == pointer to end of pathname
70 ;
71 ; On exit:      R0 == pointer to pathname start (same as entry)
72 ;               R1 == pointer to pathname end (may be different)
73 ;               If C is set on exit, the pathname is updated to show the
74 ;                 suffix which was found successfully.
75 ;
76 ; Use:          Tries to find a named file with either a suffix
77 ;               (Wimp_ReadSysInfo-style) or no suffix with the given
78 ;               basename.
79
80 res__trySuffix  ROUT
81
82                 STMFD   R13!,{R0,R2-R4,R14}     ;Stack some registers again
83
84                 ; --- Try to read the suffix string ---
85
86                 MOV     R4,R1                   ;Keep end pointer
87                 MOV     R3,R0                   ;Keep start pointer too!
88                 MOV     R0,#-1                  ;Use the current mode
89                 MOV     R1,#5                   ;Read the y-EIG factor
90                 SWI     OS_ReadModeVariable     ;Read that then, please
91                 CMP     R2,#2                   ;Is this a low-res mode?
92                 ADRCS   R1,res__lowRes          ;Yes -- point to the suffix
93                 ADRCC   R1,res__hiRes           ;No -- point to other suffix
94
95                 ; --- Tack the suffix on the end of the string ---
96
97                 MOV     R0,R4                   ;Point to the main string
98                 BL      str_cpy                 ;Copy the suffix on
99
100                 ; --- Find out whether the file exists ---
101
102                 MOV     R1,R0                   ;Point to null terminator
103                 MOV     R0,R3                   ;Point to the string base
104                 BL      res_exists              ;Does it exist?
105                 LDMCSFD R13!,{R0,R2-R4,PC}      ;Yes -- return with C set
106                 MOV     R0,#0                   ;Chop off terminator again
107                 STRB    R0,[R4,#0]              ;Store it on the end
108
109                 ; --- See if the basic file exists ---
110
111                 MOV     R0,R3                   ;Point to the base name
112                 MOV     R1,R4                   ;Point to the terminator
113                 LDMFD   R13!,{R0,R2-R4,R14}     ;Unstack all the registers
114                 B       res_exists              ;And see if it exists
115
116 res__lowRes     DCB     "24",0
117 res__hiRes      DCB     "22",0
118
119                 LTORG
120
121 ; --- res_country ---
122 ;
123 ; On entry:     R0 == pointer to a buffer to use
124 ;
125 ; On exit:      R0 == pointer to country name (may not be in the buffer)
126 ;
127 ; Use:          Reads the name of the current country.  If no name can be
128 ;               found, it returns a pointer to the string `UK' which should
129 ;               do as a suitable default
130
131                 EXPORT  res_country
132 res_country     ROUT
133
134                 STMFD   R13!,{R0-R5,R14}        ;Save loads of registers
135
136                 ; --- Read the country number ---
137
138                 MOV     R0,#240                 ;Read country number
139                 MOV     R1,#0                   ;Read, don't write
140                 MOV     R2,#255                 ;Read, don't write
141                 SWI     OS_Byte                 ;Find the country number
142
143                 ; --- Now get someone to translate it for us ---
144
145                 MOV     R3,R1                   ;Get the country number
146                 MOV     R1,#&43                 ;Service_International
147                 MOV     R2,#2                   ;The magic reason code
148                 LDR     R4,[R13],#4             ;Get the buffer pointer
149                 MOV     R5,#20                  ;A fair size for the name
150                 SWI     OS_ServiceCall          ;Try to find the name
151
152                 CMP     R1,#0                   ;Was the service call claimed
153                 ADRNE   R0,res__uk              ;No -- point to `UK'
154                 STREQB  R1,[R4,R5]              ;Yes -- null terminate string
155                 MOVEQ   R0,R4                   ;... and point to the name
156                 LDMFD   R13!,{R1-R5,PC}^        ;Return to the caller
157
158 res__uk         DCB     "UK",0
159
160                 LTORG
161
162 ; --- res_find ---
163 ;
164 ; On entry:     R0 == pointer to resource filename
165 ;               R1 == pointer to buffer to build filename in
166 ;
167 ; On exit:      R0 == pointer to start of full pathname (R1 on entry)
168 ;               R1 == pointer to terminating null character
169 ;               CS if the file could actually be found, CC otherwise
170 ;
171 ; Use:          Locates a resource file.  It searches, in order:
172 ;
173 ;               * resPrefix.Resources.leaf[suffix]
174 ;               * resPrefix.Resources.country.leaf[suffix]
175 ;               * resPrefix.leaf[suffix]
176 ;
177 ;               returning the last if none of them could be found.  Note
178 ;               that `country' here is the currently configured country
179 ;               setting, and `suffix' is the WIMP mode aspect ratio suffix
180 ;               for the current mode (RISC OS 3 only).
181
182                 EXPORT  res_find
183 res_find        ROUT
184
185                 STMFD   R13!,{R7-R10,R12,R14}   ;Save some registers
186
187                 ; --- First build the resource prefix ---
188
189                 WSPACE  res_wspace              ;Find the workspace
190                 MOV     R10,R1                  ;Keep buffer pointer
191                 MOV     R7,R0                   ;Keep leafname pointer
192                 MOV     R0,R1                   ;Point to the buffer
193                 ADR     R1,res_prefix           ;Point to resource root
194                 BL      str_cpy                 ;Copy it over
195
196                 ; --- Now build resPrefix.Resources.leaf ---
197
198                 MOV     R1,#'.'                 ;Put a dot in there
199                 STRB    R1,[R0],#1              ;Insert after res prefix
200                 MOV     R9,R0                   ;Keep this pointer
201                 ADR     R1,res__resources       ;Point to `Resources'
202                 BL      str_cpy                 ;Copy it over
203                 MOV     R8,R0                   ;Keep this pointer for later
204                 MOV     R1,R7                   ;Point to leafname pointer
205                 BL      str_cpy                 ;Copy it over
206                 MOV     R1,R0                   ;Keep terminator pointer
207                 MOV     R0,R10                  ;Point to buffer
208                 BL      res__trySuffix          ;Try adding a suffix then
209                 BCS     %10res_find             ;If it's there return
210
211                 ; --- Try resPrefix.Resources.country.leaf ---
212
213                 SUB     R13,R13,#20             ;Drop the stack a bit
214                 MOV     R0,R13                  ;Point to this new buffer
215                 BL      res_country             ;Read the country name
216                 MOV     R1,R0                   ;Keep the pointer
217                 MOV     R0,R8                   ;We remembered this pointer
218                 BL      str_cpy                 ;Copy the country name over
219                 ADD     R13,R13,#20             ;Reclaim the stack
220                 MOV     R1,#'.'                 ;Put a dot in there
221                 STRB    R1,[R0],#1              ;Insert after country name
222                 MOV     R1,R7                   ;Point to leafname
223                 BL      str_cpy                 ;Copy it over
224                 MOV     R1,R0                   ;Keep the end pointer
225                 MOV     R0,R10                  ;Point to buffer
226                 BL      res__trySuffix          ;Try adding a suffix then
227                 BCS     %10res_find             ;If it's there return
228
229                 ; --- Try resPrefix.Resources.UK.leaf ---
230
231                 MOV     R0,R8                   ;We remembered this pointer
232                 ADR     R1,res__uk              ;Point to the UK string
233                 BL      str_cpy                 ;Copy the country name over
234                 MOV     R1,#'.'                 ;Put a dot in there
235                 STRB    R1,[R0],#1              ;Insert after country name
236                 MOV     R1,R7                   ;Point to leafname
237                 BL      str_cpy                 ;Copy it over
238                 MOV     R1,R0                   ;Keep the end pointer
239                 MOV     R0,R10                  ;Point to buffer
240                 BL      res__trySuffix          ;Try adding a suffix then
241                 BCS     %10res_find             ;If it's there return
242
243                 ; --- No joy so just build resPrefix.leafname and quit ---
244
245                 MOV     R0,R9                   ;We remembered this point
246                 MOV     R1,R7                   ;Point to the leafname
247                 BL      str_cpy                 ;Copy it over
248                 MOV     R1,R0                   ;Keep the end pointer
249                 MOV     R0,R10                  ;Point to buffer
250                 BL      res__trySuffix          ;Try adding a suffix then
251                 BCS     %10res_find             ;If it's there return
252
253                 ; --- Couldn't find it at all -- return with C clear ---
254
255                 LDMFD   R13!,{R7-R10,R12,R14}   ;Unstack loads of registers
256                 BICS    PC,R14,#C_flag          ;Return with C clear
257
258                 ; --- We found a resource name -- return with C set ---
259
260 10res_find      LDMFD   R13!,{R7-R10,R12,R14}   ;Unstack loads of registers
261                 ORRS    PC,R14,#C_flag          ;Return with C set
262
263 res__resources  DCB     "Resources.",0
264
265                 LTORG
266
267 ; --- res_init ---
268 ;
269 ; On entry:     R0 == pointer to application name
270 ;
271 ; On exit:      --
272 ;
273 ; Use:          Initialises the resource prefix to <appname$Dir>
274
275                 EXPORT  res_init
276 res_init        ROUT
277
278                 STMFD   R13!,{R0-R2,R12,R14}    ;Save some registers
279                 WSPACE  res_wspace              ;Get my workspace pointer
280                 LDR     R14,res_wstart          ;Get the first word
281                 CMP     R14,#0                  ;Are we initialised?
282                 LDMNEFD R13!,{R0-R2,R12,PC}^    ;Yes -- return
283                 MOV     R2,R0                   ;Point to application name
284                 ADR     R1,res_prefix           ;Point to my buffer
285                 ADR     R0,res__skeleton        ;Point to skeleton for this
286                 BL      str_subst               ;Build the string up
287                 LDMFD   R13!,{R0-R2,R12,PC}^    ;Return to caller
288
289 res__skeleton   DCB     "<%0$Dir>",0
290
291                 LTORG
292
293 res_wspace      DCD     0
294
295 ;----- Workspace ------------------------------------------------------------
296
297                 ^       0,R12
298 res_wstart      #       0
299
300 res_prefix      #       40                      ;Prefix string
301
302 res_wsize       EQU     {VAR}-res_wstart
303
304                 AREA    |Sapphire$$LibData|,CODE,READONLY
305
306                 DCD     res_wsize
307                 DCD     res_wspace
308                 DCD     0
309                 DCD     res_init
310
311 ;----- That's all, folks ----------------------------------------------------
312
313
314                 END