Initial revision
[ssr] / StraySrc / Libraries / Steel / c / utils
1 /*
2 * utils
3 *
4 * Various miscellaneous (and largely non-WIMP) utility routines
5 *
6 * © 1993-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 "stdio.h"
29 #include "string.h"
30 #include "ctype.h"
31 #include "utils.h"
32 #include "os.h"
33 #include "werr.h"
34 #include "wimpt.h"
35 #include "swiv.h"
36 #include "swis.h"
37 #include "buffer.h"
38
39 /*
40 * int utils_caselessCmp(const char *s1,const char *s2)
41 *
42 * Use
43 * Caseless comparison between string 1 and string 2
44 *
45 * Parameters
46 * const char *s1 == source string
47 * const char *s2 == target string
48 *
49 * Returns
50 * 0 if the strings are equal, >0 if s1>s2, or <0 if s1<s2.
51 */
52
53 int utils_caselessCmp(const char *s1,const char *s2)
54 {
55 if (wimpt_getVersion()>=300)
56 return (_swi(Territory_Collate, _inr(0,3)+_return(0), -1,s1,s2,3));
57 else
58 {
59 char c1;
60 char c2;
61 while (*s1!='\0' || *s2!='\0')
62 {
63 c1=tolower(*(s1++));
64 c2=tolower(*(s2++));
65 if (c1!=c2)
66 return (c1-c2);
67 }
68 return (0);
69 }
70 }
71
72 /*
73 * char *utils_ctermToNterm(char *s)
74 *
75 * Use
76 * Changes a control-terminated string into a null-terminated string.
77 *
78 * Parameters
79 * char *s == the string to change
80 *
81 * Returns
82 * A pointer to the string.
83 */
84
85 char *utils_ctermToNterm(char *s)
86 {
87 char *p;
88 for (p=s;*p>31;p++)
89 /* blank loop - all in for statement */;
90 *p=0;
91 return (s);
92 }
93
94 /*
95 * char *utils_leafname(char *filename)
96 *
97 * Use
98 * Returns the leafname of the file whose full pathname is given in
99 * filename.
100 *
101 * Parameters
102 * char *filename == pointer to full filename string
103 *
104 * Returns
105 * Pointer to character after last '.' of string.
106 */
107
108 char *utils_leafname(char *filename)
109 {
110 char *p=filename;
111 while (*filename)
112 {
113 if (*(filename++)=='.')
114 p=filename;
115 }
116 return (p);
117 }
118
119 /*
120 * os_error *utils_complain(os error *e,char *string)
121 *
122 * Use
123 * If e is an error (i.e. not NULL) then the routine calls werr() with
124 * parameters (string,e->errmess). Ths string must contain a '%s' at some
125 * point.
126 *
127 * Parameters
128 * os_error *e == either NULL or a pointer to a standard system
129 * error structure.
130 * char *string == a string containing one %s, for which the error
131 * message from the structure passed above will be substituted.
132 *
133 * Returns
134 * The error pointer.
135 */
136
137 os_error *utils_complain(os_error *e,char *string)
138 {
139 if (e)
140 werr(FALSE,string,e->errmess);
141 return (e);
142 }
143
144 /*
145 * char *utils_cvtSize(int size)
146 *
147 * Use
148 * Converts a size in bytes into a string suitable to display the size to
149 * a user. It uses OS_ConvertFileSize to do he translation, although this
150 * is not guaranteed for future versions.
151 *
152 * Parameters
153 * int size == the size in bytes
154 *
155 * Returns
156 * A pointer to the result (read-only)
157 */
158
159 char *utils_cvtSize(int size)
160 {
161 char *buff=buffer_find();
162 _swix(XOS_ConvertFileSize,_inr(0,2),size,buff,16);
163 return (buff);
164 }