The test for strtou*ll* had several times strtou*l*.
[cake.git] / workbench / tools / HDToolBox / hdtoolbox_support.c
blobcb92bf6329bda419c3e10fed758f94ae2e84aa98
1 /*
2 Copyright © 1995-2008, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <proto/alib.h>
7 #include <proto/exec.h>
8 #include <proto/gadtools.h>
9 #include <proto/intuition.h>
11 #include <intuition/intuition.h>
12 #include <libraries/gadtools.h>
14 #include <ctype.h>
15 #include <stdio.h>
16 #include <stdlib.h>
18 #include "debug.h"
20 #define HDTB_HAVE_VARARGPROTOS
21 #include "hdtoolbox_support.h"
22 #include "platform.h"
24 struct Node *getNumNode(struct List *list, int num)
26 struct Node *node;
28 D(bug("[HDToolBox] getNumNode()\n"));
30 node = list->lh_Head;
31 while ((num) && (node->ln_Succ))
33 node = node->ln_Succ;
34 num--;
36 return node->ln_Succ ? node : 0;
39 ULONG getNodeNum(struct Node *node)
41 ULONG num = 0;
43 D(bug("[HDToolBox] getNodeNum()\n"));
45 for (;;)
47 node = node->ln_Pred;
48 if (node->ln_Pred == 0)
49 return num;
50 num++;
54 ULONG countNodes(struct List *list, UBYTE type)
56 ULONG count = 0;
57 struct Node *node;
59 D(bug("[HDToolBox] countNodes()\n"));
61 node = list->lh_Head;
62 while (node->ln_Succ)
64 if ((type == (UBYTE)-1) || (node->ln_Type == type))
65 count++;
66 node = node->ln_Succ;
68 return count;
71 void typestrncpy(STRPTR dst, STRPTR src, ULONG len)
73 while (len)
75 if (isprint(*src))
76 *dst++ = *src++;
77 else
79 *dst++ = '\\';
80 sprintf(dst,"%o", *src++);
81 while (*dst)
82 dst++;
84 len--;
88 #ifndef __AROS__
89 UWORD strcpyESC(STRPTR dst, STRPTR fmt, ...){
90 #else
91 UWORD strcpyESC(STRPTR dst, STRPTR fmt){
92 #endif
93 #warning "TODO: Check varargs usage is correct"
94 UWORD count = 0;
96 while (*fmt)
98 if (*fmt == '\\')
100 fmt++;
101 if (isdigit(*fmt))
103 ULONG val=0;
105 for(;;)
107 val += (*fmt-'0');
108 fmt++;
109 if (!isdigit(*fmt))
110 break;
111 val *= 8;
113 *dst++ = (UBYTE)val;
114 count++;
116 else
118 D(bug("[HDToolBox] strcpyESC:%s-%ld: unknown escape sequence\n", __FILE__, __LINE__));
121 else
123 *dst++ = *fmt++;
124 count++;
127 return count;
130 /* size in kB */
131 void getSizeStr(STRPTR str, ULONG size)
133 UBYTE c = 'M';
134 ULONG r;
136 r = size % 1024;
137 size = size / 1024;
138 if (size > 512)
140 c='G';
141 r = size % 1024;
142 size = size / 1024;
144 r = r*10/1024;
145 sprintf(str, "%ld.%ld%c",size,r,c);
148 /* size in kB */
149 ULONG sizeStrToUL(STRPTR str)
151 char *end;
152 ULONG size;
153 ULONG value = 0;
154 ULONG div;
156 size = strtoul(str, &end, 0);
157 if (*end == '.')
159 value = strtoul(end+1, &end, 0);
161 if (*end == 'M')
163 size *= 1024;
164 div = 1024;
166 else if (*end == 'G')
168 size *= 1024*1024;
169 div = 1024*1024;
171 else
173 /* assume bytes */
174 value = 0;
175 div = 0;
176 size /= 1024; /* we want it in kB */
178 if (div)
180 ULONG d=1;
183 d *= 10;
184 } while ((value/d)>=1);
185 value *= div;
186 value /= d;
188 size += value;
189 return size;
192 LONG GetPartitionAttrsA(struct PartitionHandle *ph, IPTR tag, ... )
194 #ifdef __AROS__
195 AROS_SLOWSTACKTAGS_PRE(tag)
196 retval = GetPartitionAttrs(ph, AROS_SLOWSTACKTAGS_ARG(tag));
197 AROS_SLOWSTACKTAGS_POST
198 #else
199 return GetPartitionAttrs(ph, (struct TagItem *)&tag);
200 #endif
203 LONG SetPartitionAttrsA(struct PartitionHandle *ph, IPTR tag, ... )
205 #ifdef __AROS__
206 AROS_SLOWSTACKTAGS_PRE(tag)
207 retval = SetPartitionAttrs(ph, AROS_SLOWSTACKTAGS_ARG(tag));
208 AROS_SLOWSTACKTAGS_POST
209 #else
210 return SetPartitionAttrs(ph, (struct TagItem *)&tag);
211 #endif
214 LONG GetPartitionTableAttrsA(struct PartitionHandle *ph, IPTR tag, ... )
216 #ifdef __AROS__
217 AROS_SLOWSTACKTAGS_PRE(tag)
218 retval = GetPartitionTableAttrs(ph, AROS_SLOWSTACKTAGS_ARG(tag));
219 AROS_SLOWSTACKTAGS_POST
220 #else
221 return GetPartitionTableAttrs(ph, (struct TagItem *)&tag);
222 #endif
225 ULONG getAttrInfo(struct PartitionAttribute *attrlist, ULONG attr)
227 D(bug("[HDToolBox] getAttrInfo()\n"));
229 while (attrlist[0].attribute != PTA_DONE)
231 if (attrlist[0].attribute == attr)
232 return attrlist[0].mode;
233 attrlist++;
235 return 0;
238 UBYTE getBitNum(ULONG val)
240 UBYTE count = 0;
242 if (val==0)
243 return 0xFF;
245 for (;;)
247 val >>= 1;
248 if (val==0)
249 break;
250 count++;
252 return count;