File: util.h

package info (click to toggle)
cbmconvert 2.1.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 380 kB
  • sloc: ansic: 5,084; perl: 42; makefile: 4
file content (198 lines) | stat: -rw-r--r-- 5,646 bytes parent folder | download
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
/**
 * @file util.h
 * Definitions of data types and utility functions
 * @author Marko Mäkelä (marko.makela at iki.fi)
 */

/*
** Copyright © 1993-1997,2001,2021 Marko Mäkelä
**
**     This program 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.
**
**     This program 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 this program; if not, write to the Free Software
**     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifndef UTIL_H
#  define UTIL_H

/**
 * Rounded integer division
 * @param a     the numerator
 * @param b     the denominator
 * @return      a divided by b, rounded up to the next integer value
 */
#  define rounddiv(a,b) ((a + b - 1U) / b)
/**
 * Determine the number of elements in an array
 * @param a     an array
 * @return      number of elements in the array
 */
#  define elementsof(a) ((sizeof a) / (sizeof *a))

#  if defined(__MSDOS__) || defined(MSDOS)
/** Directory path separator character */
#    define PATH_SEPARATOR '\\'
#  else
/** Directory path separator character */
#    define PATH_SEPARATOR '/'
#  endif

#  include <limits.h>

/* Common data types */

#  if UCHAR_MAX != 255
#    error "Wrong unsigned char range!"
#  endif
/** A data type of exactly one byte */
typedef unsigned char byte_t;
#  if UINT_MAX < 65535
#    error "Insufficient unsigned int range!"
#  endif
/** An unsigned data type with at least 16 bits of precision */
typedef unsigned int word_t;
#  if UINT_MAX >= 16777216
/** An unsigned data type with at least 24 bits of precision */
typedef unsigned int tbyte_t;
#  else
#    if ULONG_MAX < 16777216
#      error "Insufficient unsigned long range!"
#    endif
/** An unsigned data type with at least 24 bits of precision */
typedef unsigned long int tbyte_t;
#  endif

#if __STDC_VERSION__ < 201100L
/** Truth value */
typedef enum
{
  false = 0,    /**< false, binary digit '0' */
  true          /**< true, binary digit '1' */
} bool;
#else
# include <stdbool.h>
#endif

/** whether to allow duplicate file names */
extern bool allowDuplicates;

/** Commodore file types */
enum Filetype
{
  DEL = 0x80,   /**< Deleted (sequential) file */
  SEQ,          /**< Sequential data file */
  PRG,          /**< Sequential program file */
  USR,          /**< Sequential data file with user-defined structure */
  REL,          /**< Random-access data file */
  CBM           /**< 1581 partition */
};

/** Commodore file name */
struct Filename
{
  /** The file name, padded with shifted spaces */
  unsigned char name[16];
  /** The file type */
  enum Filetype type;
  /** Record length for random-access (relative) files */
  byte_t recordLength;
};

/** Disk image types */
enum ImageType
{
  ImUnknown,    /**< Unknown or unrecognized image */
  Im1541,       /**< 35-track 1541, 3040 or 4040 disk image */
  Im1571,       /**< 70-track 1571 disk image */
  Im1581        /**< 80-track 1581 disk image */
};

/** Options for getDirEnt () */
enum DirEntOpts
{
  DirEntDontCreate, /**< only try to find the file name */
  DirEntUniqCreate, /**< only create a new slot with unique name */
  DirEntFindOrCreate,/**< create the directory entry if it doesn't exist */
  DirEntDupCreate   /**< create new directory entries if the name exists */
};

/** Disk image */
struct Image
{
  /** type of disk image */
  enum ImageType type;
  /** getDirEnt() behaviour */
  enum DirEntOpts direntOpts;
  /** (active) directory track number */
  byte_t dirtrack;
  /** disk image file name on the host system */
  unsigned char* name;
  /** disk image data */
  byte_t* buf;
  /** lower limits of partitions (for the 1581) */
  byte_t partBots[80];
  /** upper limits of partitions (for the 1581) */
  byte_t partTops[80];
  /** parent partitions (for the 1581) */
  byte_t partUpper[80];
};

/** An entry in a file archive */
struct ArchiveEntry
{
  /** Pointer to next archive entry */
  struct ArchiveEntry* next;
  /** The file name of the entry */
  struct Filename name;
  /** Length of the entry in bytes */
  size_t length;
  /** The contents of the entry */
  byte_t* data;
};

/** A file archive */
struct Archive
{
  /** The first archive entry */
  struct ArchiveEntry* first;
  /** The last archive entry */
  struct ArchiveEntry* last;
};

/* Utility functions */

/** Convert a file name to a printable null-terminated string.
 * @param name  the PETSCII file name to be converted
 * @return      the corresponding ASCII file name
 */
const char*
getFilename (const struct Filename* name);

/** Verbosity level of diagnostic output */
enum Verbosity
{
  Errors,       /**< Display only errors; report an error */
  Warnings,     /**< Display errors and warnings; report a warning */
  Everything    /**< Display everything; report an informational message */
};

/** Call-back function for diagnostic output
 * @param verbosity     the verbosity level
 * @param name          the file name associated with the message (or NULL)
 * @param format        printf-like format string followed by arguments
 */
typedef void log_t (enum Verbosity verbosity,
                    const struct Filename* name,
                    const char* format, ...);

#endif /* UTIL_H */