1 /*
2     TiMidity++ -- MIDI to WAVE converter and player
3     Copyright (C) 1999-2002 Masanao Izumo <mo@goice.co.jp>
4     Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>
5 
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 
20 	Macintosh interface for TiMidity
21 	by T.Nogami	<t-nogami@happy.email.ne.jp>
22 
23     mac_util.c
24 */
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif /* HAVE_CONFIG_H */
28 #include 	<stdio.h>
29 #include	<stdlib.h>
30 #include	<string.h>
31 #include	<ctype.h>
32 #include	"mac_util.h"
33 
34 
GetFullPath(const FSSpec * spec,Str255 fullPath)35 OSErr GetFullPath( const FSSpec* spec, Str255 fullPath)
36 {
37 	CInfoPBRec		myPB;
38 	Str255		dirName;
39 	OSErr		myErr;
40 
41 	fullPath[0] = '\0';
42 	BlockMoveData(spec->name, fullPath, spec->name[spec->name[0]]+1 );
43 
44 	myPB.dirInfo.ioNamePtr = dirName;
45 	myPB.dirInfo.ioVRefNum = spec->vRefNum;
46 	myPB.dirInfo.ioDrParID = spec->parID;
47 	myPB.dirInfo.ioFDirIndex = -1;
48 
49 	do{
50 		myPB.dirInfo.ioDrDirID = myPB.dirInfo.ioDrParID;
51 		myErr = PBGetCatInfoSync(&myPB);
52 
53 		dirName[0]++;
54 		dirName[ dirName[0] ]=':';
55 		if( dirName[0]+fullPath[0]>=254 ){ fullPath[0]=0; return 1; }
56 		BlockMoveData(fullPath+1, dirName+dirName[0]+1, fullPath[0]);
57 		dirName[0]+=fullPath[0];
58 		BlockMoveData(dirName, fullPath, dirName[0]+1);
59 	}while( !(myPB.dirInfo.ioDrDirID == fsRtDirID) );
60 	return noErr;
61  }
62 
StopAlertMessage(Str255 s)63 void StopAlertMessage(Str255 s)
64 {
65 	ParamText(s,"\p","\p","\p");
66 	StopAlert(128,0);
67 }
68 
LDeselectAll(ListHandle lHandle)69 void	LDeselectAll(ListHandle lHandle)
70 {
71 	Cell  cell={0,0};
72 	while(LGetSelect(true, &cell, lHandle)){
73 		LSetSelect(false, cell, lHandle);
74 	}
75 }
76 
TEReadFile(char * filename,TEHandle te)77 void	TEReadFile(char* filename, TEHandle te)
78 {
79 	FILE	*in;
80 
81 		//clear TE
82 	TESetSelect(0, (**te).teLength, te);
83 	TEDelete(te);
84 
85 		//read
86 	if( (in=fopen(filename, "rb"))==0 ){
87 		char *s="No document.";
88 		TEInsert(s, strlen(s), te);
89 	}else{
90 		char	buf[80];
91 		int		len;
92 		while(fgets( buf, 80, in)){
93 			len=strlen(buf);
94 			if( buf[len-1]==0xA ){
95 				buf[len-1]=0xD; //len--;
96 			}
97 			TEInsert(buf, len, te);
98 		}
99 		fclose(in);
100 	}
101 }
102 
103 /***********************/
104 #pragma	mark	-
105 
SetDialogItemValue(DialogPtr dialog,short item,short value)106 void SetDialogItemValue(DialogPtr dialog, short item, short value)
107 {
108 	short			itemType;
109 	ControlHandle	itemHandle;
110 	Rect			itemRect;
111 
112 	GetDialogItem(dialog, item, &itemType, (Handle*)&itemHandle, &itemRect);
113 	SetControlValue(itemHandle, value);
114 }
115 
GetDialogItemValue(DialogPtr dialog,short item)116 short GetDialogItemValue(DialogPtr dialog, short item )
117 {
118 	short			itemType;
119 	ControlHandle	itemHandle;
120 	Rect			itemRect;
121 
122 	GetDialogItem(dialog, item, &itemType, (Handle*)&itemHandle, &itemRect);
123 	return GetControlValue(itemHandle);
124 }
125 
SetDialogTEValue(DialogRef dialog,short item,int value)126 void SetDialogTEValue(DialogRef dialog, short item, int value)
127 {
128 	Str255		s;
129 
130 	NumToString(value, s);
131 	mySetDialogItemText(dialog, item, s);
132 }
133 
GetDialogTEValue(DialogRef dialog,short item)134 int GetDialogTEValue(DialogRef dialog, short item )
135 {
136 	Str255		s;
137 
138 	myGetDialogItemText(dialog, item, s);
139 	return atof(p2cstr(s));
140 }
141 
ToggleDialogItem(DialogPtr dialog,short item)142 short ToggleDialogItem(DialogPtr dialog, short item )
143 {
144 	short			itemType, value;
145 	ControlHandle	itemHandle;
146 	Rect			itemRect;
147 
148 	GetDialogItem(dialog, item, &itemType, (Handle*)&itemHandle, &itemRect);
149 	value=GetControlValue(itemHandle);
150 	SetControlValue(itemHandle, !value);
151 	return !value;
152 }
153 
myGetDialogItemText(DialogPtr theDialog,short itemNo,Str255 s)154 void myGetDialogItemText(DialogPtr theDialog, short itemNo, Str255 s)
155 {
156 	short	itemType;
157 	Handle	item;
158 	Rect	box;
159 
160 	GetDialogItem(theDialog, itemNo, &itemType, &item, &box);
161 	GetDialogItemText(item, s);
162 }
163 
mySetDialogItemText(DialogRef theDialog,short itemNo,const Str255 text)164 void mySetDialogItemText(DialogRef theDialog, short itemNo, const Str255 text)
165 {
166 	short	itemType;
167 	Handle	item;
168 	Rect	box;
169 
170 	GetDialogItem(theDialog, itemNo, &itemType, &item, &box);
171 	SetDialogItemText(item, text);
172 }
173 
SetDialogControlTitle(DialogRef theDialog,short itemNo,const Str255 text)174 void SetDialogControlTitle(DialogRef theDialog, short itemNo, const Str255 text)
175 {
176 	short	itemType;
177 	Handle	item;
178 	Rect	box;
179 
180 	GetDialogItem(theDialog, itemNo, &itemType, &item, &box);
181 	SetControlTitle((ControlRef)item, text);
182 }
183 
SetDialogItemHilite(DialogRef dialog,short item,short value)184 void SetDialogItemHilite(DialogRef dialog, short item, short value)
185 {
186 	short		itemType;
187 	ControlRef	itemHandle;
188 	Rect		itemRect;
189 
190 	GetDialogItem(dialog, item, &itemType, (Handle *)&itemHandle, &itemRect);
191 	HiliteControl(itemHandle, value);
192 }
193 
194 // ************************************************
mac_TransPathSeparater(const char str[],char out[])195 void    mac_TransPathSeparater(const char str[], char out[])
196 {
197 	int i;
198 	for( i=0; str[i]!=0; i++ ){
199 		if( str[i]=='/' ){
200 			out[i]=':';
201 		} else {
202 			out[i]=str[i];
203 		}
204 	}
205 }
206 #if 0
207 
208 char** sys_errlist_()
209 {
210 	static char	s[80];
211 	static char*	ps=s;
212 	static char**	ret=&ps;
213 
214 	/* if( errno==ENOENT )  strcpy(s,"File not Found.");
215 	else */   /* ENOENT vanished at CWPro2 */
216 	if( errno==ENOSPC )
217 		strcpy(s,"Out of Space.");
218 	else
219 		snprintf(s, 80, "error no.%d", errno);
220 
221 	return(ret-errno);
222 }
223 #endif /*0*/
224 // **************************************************
225 #pragma mark -
226 
227 /* special fgets */
228 /* allow \r as return code */
mac_fgets(char buf[],int n,FILE * file)229 char* mac_fgets( char buf[], int n, FILE* file)
230 {
231 
232 	int c,i;
233 
234 	if( n==0 )  return 0;
235 	for( i=0; i<n-1; ){
236 		buf[i++]=c=fgetc(file);
237 		if( c=='\n' || c=='\r' || c==EOF ) break;
238 	}
239 
240 	buf[i]=0;
241 	if( i>0 && buf[i-1]==EOF ){ buf[i-1]=0; i--; }
242 
243 	if( i==0 && c==EOF && feof(file) ) return 0;
244 
245 
246 	if( i==1 && buf[0]=='\n' ){ /* probably dos LF (Unix blank line?)*/
247 		return mac_fgets( buf, n, file);
248 	}
249 	else if( buf[i-1]=='\n' ){	/*unix*/
250 		return buf;
251 	}
252 	else if( buf[i-1]=='\r' ){ /*mac or dos*/
253 		buf[i-1]='\n';
254 		return buf;
255 	}
256 	return buf;  /* for safty*/
257 }
258 
mac_memchr(const void * s,int c,size_t n)259 void *mac_memchr (const void *s, int c, size_t n)
260 {
261 	char* buf=(char*)s;
262 	int i;
263 
264 	for( i=0; i<n; i++ ){
265 		if( buf[i]==c || (c=='\n' && buf[i]=='\r') ){
266 			return buf+i;
267 		}
268 	}
269 	return 0;
270 }
271 
p2cstrcpy(char * dst,ConstStr255Param src)272 void p2cstrcpy(char* dst, ConstStr255Param src)
273 {
274 	int i;
275 	for( i=0; i<src[0]; i++){
276 		dst[i]=src[i+1];
277 	}
278 	dst[i]=0;
279 }
280 
mac_strcasecmp(const char * s1,const char * s2)281 int mac_strcasecmp(const char *s1, const char *s2)
282 {
283 	int	c1,c2,i;
284 
285 	for( i=0; ; i++){
286 		if( !s1[i] && !s2[i] ) return 0; //equal
287 		if( !s1[i] || !s2[i] ) return 1;
288 		c1= ( isupper(s1[i]) ? tolower(s1[i]) : s1[i] );
289 		c2= ( isupper(s2[i]) ? tolower(s2[i]) : s2[i] );
290 		if( c1 != c2 )	return 1;
291 	}
292 	return 0; //equal
293 }
294 
295 #define CHAR_CASECMP(c1,c2) ((isupper(c1)?tolower(c1):(c1))!=(isupper(c2)?tolower(c2):(c2)))
296 
mac_strncasecmp(const char * s1,const char * s2,size_t n)297 int mac_strncasecmp(const char *s1, const char *s2, size_t n )
298 {
299 	char	c1,c2;
300 	int i;
301 
302 	for( i=0; i<n; i++){
303 		if( !s1[i] && !s2[i] ) return 0; //equal
304 		if( !s1[i] || !s2[i] ) return 1;
305 		c1= ( isupper(s1[i]) ? tolower(s1[i]) : s1[i] );
306 		c2= ( isupper(s2[i]) ? tolower(s2[i]) : s2[i] );
307 		if( c1 != c2 )	return 1;
308 	}
309 	return 0; //equal
310 }
311 
312 #define min(a,b)  (((a) < (b)) ? (a) : (b))
313 
314 
strtailcasecmp(const char * s1,const char * s2)315 int strtailcasecmp(const char *s1, const char *s2)
316 {
317 	int	len1, len2, cmplen,
318 		i, i1,i2;
319 
320 	len1=strlen(s1); len2=strlen(s2);
321 	cmplen= min(len1,len2);
322 	for( i=0, i1=len1-1, i2=len2-1; i<cmplen; i++, i1--, i2--){
323 		if( CHAR_CASECMP(s1[i1],s2[i2])!=0 ){
324 			return 1;  //differ
325 		}
326 	}
327 	return 0; //equal
328 }
329 
330 
331 
332