Minor fixes to comments.
[AROS.git] / rom / filesys / AmberRAM / support.c
blob1bc0ea068f1111af4fe65bae87cd3dc2432b37f1
1 /*
3 File: support.c
4 Author: Neil Cafferkey
5 Copyright (C) 2001-2008 Neil Cafferkey
7 This file is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as
9 published by the Free Software Foundation; either version 2.1 of the
10 License, or (at your option) any later version.
12 This file is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with this file; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 MA 02111-1307, USA.
25 #include "handler_protos.h"
29 /****i* ram.handler/SetString **********************************************
31 * NAME
32 * SetString --
34 * SYNOPSIS
35 * block_diff = SetString(field, new_str)
37 * PINT SetString(TEXT **, TEXT *);
39 * FUNCTION
41 * INPUTS
43 * RESULT
45 * EXAMPLE
47 * NOTES
49 * BUGS
51 * SEE ALSO
53 ****************************************************************************
57 PINT SetString(struct Handler *handler, TEXT **field, const TEXT *new_str)
59 LONG error = 0;
60 UPINT size;
61 TEXT *str_copy = NULL, *old_str;
62 PINT block_diff = 0;
64 /* Allocate new string */
66 size = StrSize(new_str);
67 if(size > 1)
69 str_copy = AllocPooled(handler->muddy_pool, size);
70 if(str_copy != NULL)
72 CopyMem(new_str, str_copy, size);
74 else
75 error = ERROR_DISK_FULL;
77 else
78 size = 0;
80 if(error == 0)
82 /* Deallocate old string */
84 if(size > 0)
85 block_diff = MEMBLOCKS(size);
86 old_str = *field;
87 if(old_str != NULL)
89 size = StrSize(old_str);
90 FreePooled(handler->muddy_pool, old_str, size);
91 block_diff -= MEMBLOCKS(size);
94 /* Store new string */
96 *field = str_copy;
99 /* Store error and return difference in block utilisation */
101 SetIoErr(error);
102 return block_diff;
107 /****i* ram.handler/SwapStrings ********************************************
109 * NAME
110 * SwapStrings --
112 * SYNOPSIS
113 * block_diff = SwapStrings(field1, field2)
115 * PINT SwapStrings(TEXT **, TEXT **);
117 * FUNCTION
119 * INPUTS
121 * RESULT
123 * EXAMPLE
125 * NOTES
127 * BUGS
129 * SEE ALSO
131 ****************************************************************************
135 PINT SwapStrings(TEXT **field1, TEXT **field2)
137 TEXT *str1, *str2;
138 PINT block_diff = 0;
140 str1 = *field1;
141 if(str1 != NULL)
142 block_diff = MEMBLOCKS(StrSize(str1));
144 str2 = *field2;
145 if(str2 != NULL)
146 block_diff -= MEMBLOCKS(StrSize(str2));
148 *field1 = str2;
149 *field2 = str1;
151 /* Return difference in block utilisation */
153 return block_diff;
158 /****i* ram.handler/StrLen *************************************************
160 * NAME
161 * StrLen --
163 * SYNOPSIS
164 * length = StrLen(s)
166 * UPINT StrLen(TEXT *);
168 * FUNCTION
170 * INPUTS
172 * RESULT
174 * EXAMPLE
176 * NOTES
178 * BUGS
180 * SEE ALSO
182 ****************************************************************************
186 UPINT StrLen(const TEXT *s)
188 const TEXT *p;
190 for(p = s; *p != '\0'; p++);
191 return p - s;
196 /****i* ram.handler/StrSize ************************************************
198 * NAME
199 * StrLen --
201 * SYNOPSIS
202 * size = StrSize(s)
204 * UPINT StrSize(TEXT *);
206 * FUNCTION
208 * INPUTS
209 * s - The string (may be NULL).
211 * RESULT
212 * size - Size of the string in bytes including terminating NUL.
214 * EXAMPLE
216 * NOTES
218 * BUGS
220 * SEE ALSO
222 ****************************************************************************
226 UPINT StrSize(const TEXT *s)
228 const TEXT *p;
229 UPINT length = 0;
231 p = s;
233 if(s != NULL)
235 while(*p != '\0')
236 p++;
237 length = p - s + 1;
240 return length * sizeof(TEXT);
245 /****i* ram.handler/FindNameNoCase *****************************************
247 * NAME
248 * FindNameNoCase --
250 * SYNOPSIS
251 * node = FindNameNoCase(handler, start, name)
253 * struct Node *FindNameNoCase(struct Handler *handler, struct List *, TEXT *);
255 * FUNCTION
257 * INPUTS
259 * RESULT
261 * EXAMPLE
263 * NOTES
265 * BUGS
267 * SEE ALSO
269 ****************************************************************************
273 struct Node *FindNameNoCase(struct Handler *handler, struct List *start, const TEXT *name)
275 struct Node *node, *next_node, *matching_node;
276 TEXT *node_name;
278 matching_node = NULL;
279 node = start->lh_Head;
281 while(node != NULL)
283 next_node = node->ln_Succ;
284 if(next_node != NULL)
286 node_name = node->ln_Name;
287 if(node_name != NULL)
288 if(Stricmp(name, node_name) == 0)
290 matching_node = node;
291 next_node = NULL;
294 node = next_node;
297 return matching_node;
302 /****i* ram.handler/MyMakeDosEntry *****************************************
304 * NAME
305 * MyMakeDosEntry --
307 * SYNOPSIS
308 * dos_entry = MyMakeDosEntry(handler, name, type)
310 * struct DosList *MyMakeDosEntry(struct Handler *handler, TEXT *, LONG);
312 * FUNCTION
314 * INPUTS
316 * RESULT
318 * EXAMPLE
320 * NOTES
322 * BUGS
324 * SEE ALSO
326 ****************************************************************************
330 struct DosList *MyMakeDosEntry(struct Handler *handler, const TEXT *name, LONG type)
332 struct DosList *entry;
333 LONG error = 0;
335 entry = AllocMem(sizeof(struct DosList), MEMF_CLEAR | MEMF_PUBLIC);
337 if(entry != NULL)
339 if(!MyRenameDosEntry(handler, entry, name))
340 error = IoErr();
341 entry->dol_Type = type;
343 else
344 error = IoErr();
346 if(error != 0)
348 MyFreeDosEntry(handler, entry);
349 entry = NULL;
352 SetIoErr(error);
353 return entry;
358 /****i* ram.handler/MyFreeDosEntry *****************************************
360 * NAME
361 * MyFreeDosEntry --
363 * SYNOPSIS
364 * MyFreeDosEntry(handler, entry)
366 * VOID MyFreeDosEntry(struct Handler *handler, struct DosList *);
368 * FUNCTION
370 * INPUTS
372 * RESULT
374 * EXAMPLE
376 * NOTES
378 * BUGS
380 * SEE ALSO
382 ****************************************************************************
386 VOID MyFreeDosEntry(struct Handler *handler, struct DosList *entry)
388 if(entry != NULL)
390 MyRenameDosEntry(handler, entry, NULL);
391 FreeMem(entry, sizeof(struct DosList));
394 return;
399 /****i* ram.handler/MyRenameDosEntry ***************************************
401 * NAME
402 * MyRenameDosEntry --
404 * SYNOPSIS
405 * success = MyRenameDosEntry(entry, name)
407 * BOOL MyRenameDosEntry(struct DosList *, TEXT *);
409 * FUNCTION
411 * INPUTS
413 * RESULT
415 * EXAMPLE
417 * NOTES
419 * BUGS
421 * SEE ALSO
423 ****************************************************************************
427 #ifdef __AROS__
428 BOOL MyRenameDosEntry(struct Handler *handler, struct DosList *entry, const TEXT *name)
430 LONG error = 0;
431 UPINT length;
432 TEXT *name_copy, *old_name;
434 /* Allocate new string */
436 name_copy = NULL;
437 if(name != NULL)
439 length = StrLen(name);
440 #ifdef AROS_FAST_BPTR
441 name_copy = AllocVec(length + 1, MEMF_PUBLIC);
442 if(name_copy != NULL)
444 CopyMem(name, name_copy, length + 1);
446 #else
447 name_copy = AllocVec(length + 2, MEMF_PUBLIC);
448 if(name_copy != NULL)
450 name_copy[0] = (UBYTE)(length > 255 ? 255 : length);
451 CopyMem(name, name_copy + 1, length + 1);
453 #endif
454 else
455 error = IoErr();
458 /* Deallocate old string and set new one */
460 if(error == 0)
462 old_name = BADDR(entry->dol_Name);
463 if(old_name != NULL)
464 FreeVec(old_name);
465 entry->dol_Name = MKBADDR(name_copy);
468 /* Store error code and return success flag */
470 SetIoErr(error);
471 return error == 0;
476 #else
477 BOOL MyRenameDosEntry(struct DosList *entry, const TEXT *name)
479 LONG error = 0;
480 UPINT length;
481 TEXT *name_copy, *old_name;
483 /* Allocate new string */
485 name_copy = NULL;
486 if(name != NULL)
488 length = StrLen(name);
489 name_copy = AllocVec(length + 2, MEMF_PUBLIC);
490 if(name_copy != NULL)
492 CopyMem(name, name_copy + 1, length + 1);
493 *name_copy = length;
495 else
496 error = IoErr();
499 /* Deallocate old string and set new one */
501 if(error == 0)
503 old_name = BADDR(entry->dol_Name);
504 if(old_name != NULL)
505 FreeVec(old_name);
506 entry->dol_Name = MKBADDR(name_copy);
509 /* Store error code and return success flag */
511 SetIoErr(error);
512 return error == 0;
517 #endif