File: adf_util.c

package info (click to toggle)
unadf 0.7.11a-7
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,660 kB
  • sloc: ansic: 5,455; cpp: 404; makefile: 127; sh: 78
file content (254 lines) | stat: -rw-r--r-- 5,013 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 *  ADF Library. (C) 1997-2002 Laurent Clevy
 *
 *  adf_util.c
 *
 *  $Id$
 *
 *  This file is part of ADFLib.
 *
 *  ADFLib is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  ADFLib is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include<stdlib.h>
#include<time.h>

#include "adf_util.h"
#include "adf_err.h"
#include "adf_disk.h"

extern struct Env adfEnv;


/*
 * swLong
 *
 * write an uint32_t value (val) (in) 
 * to an unsigned char* buffer (buf) (out)
 * 
 * used in adfWrite----Block() functions
 */
void swLong(unsigned char* buf, uint32_t val)
{
	buf[0]= (unsigned char)((val & 0xff000000) >>24UL);
	buf[1]= (unsigned char)((val & 0x00ff0000) >>16UL);
	buf[2]= (unsigned char)((val & 0x0000ff00) >>8UL);
	buf[3]= (unsigned char)(val & 0x000000ff);
}

void swShort(unsigned char* buf, uint16_t val)
{
	buf[0]= (val & 0xff00) >>8UL;
	buf[1]= (val & 0x00ff) ;
}

/*
 * newCell
 *
 * adds a cell at the end the list
 */
struct List* newCell(struct List* list, void* content)
{
    struct List* cell;

    cell=(struct List*)malloc(sizeof(struct List));
    if (!cell) {
        (*adfEnv.eFct)("newCell : malloc");
        return NULL;
    }
    cell->content = content;
    cell->next = cell->subdir = 0;
    if (list!=NULL)
        list->next = cell;

    return cell;
}


/*
 * freeList
 *
 */
void freeList(struct List* list)
{
    if (list==NULL) 
        return;
    
    if (list->next)
        freeList(list->next);
    free(list);
}




/*
 * Days2Date
 *
 * amiga disk date format (days) to normal dd/mm/yy format (out)
 */

void 
adfDays2Date(int32_t days, int *yy, int *mm, int *dd)
{
    int y,m;
    int nd;
    int jm[12]={ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    /* 0 = 1 Jan 1978,  6988 = 18 feb 1997 */

    /*--- year ---*/
    y=1978;
    if (adfIsLeap(y))
        nd=366;
    else
        nd=365;
    while( days >= nd ) {
        days-=nd;
        y  ;
        if (adfIsLeap(y))
            nd=366;
        else
            nd=365;
    }


    /*--- month ---*/
    m=1;
    if (adfIsLeap(y))
        jm[2-1]=29;
    while( days >= jm[m-1] ) {
        days-=jm[m-1];
        m  ;
    }

    *yy=y;
    *mm=m;
    *dd=days 1;
}


/*
 * IsLeap
 *
 * true if a year (y) is leap
 */

    BOOL 
adfIsLeap(int y)
{
    return( (BOOL) ( !(y0) ? !(y@0) : !(y%4) ) );
}


/*
 * adfCurrentDateTime
 *
 * return the current system date and time
 */
    struct DateTime
adfGiveCurrentTime( void )
{
    struct tm *local;
    time_t cal;
    struct DateTime r;

    time(&cal);
    local=localtime(&cal);

    r.year=local->tm_year;         /* since 1900 */
    r.mon=local->tm_mon 1;
    r.day=local->tm_mday;
    r.hour=local->tm_hour;
    r.min=local->tm_min;
    r.sec=local->tm_sec;

    return(r);
}


/*
 * adfTime2AmigaTime
 *
 * converts date and time (dt) into Amiga format : day, min, ticks
 */
    void
adfTime2AmigaTime(struct DateTime dt, int32_t *day, int32_t *min, int32_t *ticks )
{
    int jm[12]={ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};


    *min= dt.hour*60   dt.min;                /* mins */
    *ticks= dt.sec*50;                        /* ticks */

    /*--- days ---*/

    *day= dt.day-1;                         /* current month days */

    /* previous months days downto january */
    if (dt.mon>1) {                      /* if previous month exists */
        dt.mon--;
        if (dt.mon>2 && adfIsLeap(dt.year))    /* months after a leap february */
            jm[2-1]=29;
        while(dt.mon>0) {
            *day=*day jm[dt.mon-1];
            dt.mon--;
        }
    }

    /* years days before current year downto 1978 */
    if (dt.year>78) {
        dt.year--;
        while(dt.year>=78) {
            if (adfIsLeap(dt.year))
                *day=*day 366;
            else
                *day=*day 365;
            dt.year--;
        }
    }
}



/*
 * dumpBlock
 *
 * debug function : to dump a block before writing the check its contents
 *
 */
void dumpBlock(unsigned char *buf)
{
	int i, j;

	for(i=0; i<32; i  ) {
		printf("%5x ",i*16);
        for (j=0; j<4; j  ) {
			printf("x ",Long(buf j*4 i*16));
		}
        printf("    ");
        for (j=0; j<16; j  )
            if (buf[i*16 j]<32 || buf[i*16 j]>127)
                putchar('.');
            else
                putchar(buf[i*16 j]);
        putchar('\n');
    }
}



/*################################################################################*/