chiark / gitweb /
Initial revision
[ssr] / StraySrc / Libraries / Steel / c / choices
1 /*
2  * choices.c
3  *
4  * Handling the global choices repository
5  *
6  * © 1994-1998 Straylight
7  */
8
9 /*----- Licensing note ----------------------------------------------------*
10  *
11  * This file is part of Straylight's Steel library.
12  *
13  * Steel 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  * Steel 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 Steel.  If not, write to the Free Software Foundation,
25  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26  */
27
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "res.h"
32 #include "wimpt.h"
33 #include "buffer.h"
34
35 #include "choices.h"
36
37 static char choices__root[15];
38
39 /*
40  * void choices_setName(char *progname)
41  *
42  * Use
43  *  Sets the name of the application as used by choices.  The name is
44  *  truncated to 10 characters if necessary.  If no name is specified (i.e.
45  *  you don't call this routine) then the name of the application (as passed
46  *  to wimpt_init) is assumed.
47  */
48
49 void choices_setName(char *progname)
50 {
51   memcpy(choices__root,progname,10);
52   choices__root[10]=0;
53 }
54
55 /*
56  * char *choices_name(char *leaf,BOOL writable)
57  *
58  * Use
59  *  Locates the name of the specified resource.  If you want to open a file
60  *  for writing, you should set the writable flag.  Otherwise, this routine
61  *  will try to find the name of a file which already exists.
62  *
63  *  If you only want to read a file, this routine will first look at
64  *  <Choices$Dir>.leaf, and then at <resPrefix>.leaf, and return whichever
65  *  is found first.  If neither exists, a name is returned as for the
66  *  writable case.
67  *
68  *  If you want to write a file, then <Choices$Dir>.leaf is returned, unless
69  *  Choices$Dir is unset, in which case <resPrefix>.leaf is returned.
70  *
71  *  resPrefix is the prefix passed to res through res_init or res_setPrefix.
72  */
73
74 char *choices_name(char *leaf,BOOL writable)
75 {
76   char *name=buffer_find();
77   char *choices=getenv("Choices$Write");
78
79   /* --- Ensure choices__root is set up properly --- */
80
81   if (!choices__root[0])
82     choices_setName(wimpt_programname());
83
84   /* --- Just find one of the cases -- we're only reading --- */
85
86   if (!writable)
87   {
88     if (choices)
89     {
90       sprintf(name,"Choices:%s.%s",choices__root,leaf);
91       if (res_fileExists(name))
92         return (name);
93     }
94     strcpy(name,res_name(leaf));
95     if (res_fileExists(name))
96       return (name);
97   }
98
99   /* --- Find whichever is more appropriate --- */
100
101   if (choices)
102   {
103     if (writable)
104     {
105       /* --- To avoid hassle, create the directory here --- */
106
107       sprintf(name,"CDir %s.%s",choices,choices__root);
108       os_cli(name);
109     }
110     sprintf(name,"%s.%s.%s",choices,choices__root,leaf);
111   }
112   else
113     strcpy(name,res_name(leaf));
114
115   return (name);
116 }