- Set a default PCM volume so that something can be heard when driver is
[AROS.git] / tools / adflib / adf_util.c
blob120b207db8deb00a40bf0881e587a11928f04ec3
1 /*
2 * ADF Library. (C) 1997-1999 Laurent Clevy
4 * adf_util.c
6 */
8 #include<stdlib.h>
9 #include<time.h>
11 #include "adf_util.h"
12 #include "adf_err.h"
13 #include "adf_disk.h"
15 extern struct Env adfEnv;
19 * swLong
21 * write an unsigned long value (val) (in)
22 * to an unsigned char* buffer (buf) (out)
24 * used in adfWrite----Block() functions
26 void swLong(unsigned char* buf, unsigned long val)
28 buf[0]= (unsigned char)((val & 0xff000000) >>24UL);
29 buf[1]= (unsigned char)((val & 0x00ff0000) >>16UL);
30 buf[2]= (unsigned char)((val & 0x0000ff00) >>8UL);
31 buf[3]= (unsigned char)(val & 0x000000ff);
34 void swShort(unsigned char* buf, unsigned short val)
36 buf[0]= (val & 0xff00) >>8UL;
37 buf[1]= (val & 0x00ff) ;
41 * newCell
43 * adds a cell at the end the list
45 struct List* newCell(struct List* list, void* content)
47 struct List* cell;
49 cell=(struct List*)malloc(sizeof(struct List));
50 if (!cell) {
51 (*adfEnv.eFct)("newCell : malloc");
52 return NULL;
54 cell->content = content;
55 cell->next = cell->subdir = 0;
56 if (list!=NULL)
57 list->next = cell;
59 return cell;
64 * freeList
67 void freeList(struct List* list)
69 if (list==NULL)
70 return;
72 if (list->next)
73 freeList(list->next);
74 free(list);
81 * Days2Date
83 * amiga disk date format (days) to normal dd/mm/yy format (out)
86 void
87 adfDays2Date(long days, int *yy, int *mm, int *dd)
89 int y,m;
90 int nd;
91 int jm[12]={ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
93 /* 0 = 1 Jan 1978, 6988 = 18 feb 1997 */
95 /*--- year ---*/
96 y=1978;
97 if (adfIsLeap(y))
98 nd=366;
99 else
100 nd=365;
101 while( days >= nd ) {
102 days-=nd;
103 y++;
104 if (adfIsLeap(y))
105 nd=366;
106 else
107 nd=365;
111 /*--- month ---*/
112 m=1;
113 if (adfIsLeap(y))
114 jm[2-1]=29;
115 while( days >= jm[m-1] ) {
116 days-=jm[m-1];
117 m++;
120 *yy=y;
121 *mm=m;
122 *dd=days+1;
127 * IsLeap
129 * true if a year (y) is leap
132 BOOL
133 adfIsLeap(int y)
135 return( (BOOL) ( !(y%100) ? !(y%400) : !(y%4) ) );
140 * adfCurrentDateTime
142 * return the current system date and time
144 struct DateTime
145 adfGiveCurrentTime( void )
147 struct tm *local;
148 time_t cal;
149 struct DateTime r;
151 time(&cal);
152 local=localtime(&cal);
154 r.year=local->tm_year; /* since 1900 */
155 r.mon=local->tm_mon+1;
156 r.day=local->tm_mday;
157 r.hour=local->tm_hour;
158 r.min=local->tm_min;
159 r.sec=local->tm_sec;
161 return(r);
166 * adfTime2AmigaTime
168 * converts date and time (dt) into Amiga format : day, min, ticks
170 void
171 adfTime2AmigaTime(struct DateTime dt, long *day, long *min, long *ticks )
173 int jm[12]={ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
176 *min= dt.hour*60 + dt.min; /* mins */
177 *ticks= dt.sec*50; /* ticks */
179 /*--- days ---*/
181 *day= dt.day-1; /* current month days */
183 /* previous months days downto january */
184 if (dt.mon>1) { /* if previous month exists */
185 dt.mon--;
186 if (dt.mon>2 && adfIsLeap(dt.year)) /* months after a leap february */
187 jm[2-1]=29;
188 while(dt.mon>0) {
189 *day=*day+jm[dt.mon-1];
190 dt.mon--;
194 /* years days before current year downto 1978 */
195 if (dt.year>78) {
196 dt.year--;
197 while(dt.year>=78) {
198 if (adfIsLeap(dt.year))
199 *day=*day+366;
200 else
201 *day=*day+365;
202 dt.year--;
210 * dumpBlock
212 * debug function : to dump a block before writing the check its contents
215 void dumpBlock(unsigned char *buf)
217 int i, j;
219 for(i=0; i<32; i++) {
220 printf("%5x ",i*16);
221 for (j=0; j<4; j++) {
222 printf("%08x ",Long(buf+j*4+i*16));
224 printf(" ");
225 for (j=0; j<16; j++)
226 if (buf[i*16+j]<32 || buf[i*16+j]>127)
227 putchar('.');
228 else
229 putchar(buf[i*16+j]);
230 putchar('\n');
236 /*################################################################################*/