forked from alexpevzner/sane-airscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
airscan-id.c
181 lines (157 loc) · 4.3 KB
/
airscan-id.c
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
/* AirScan (a.k.a. eSCL) backend for SANE
*
* Copyright (C) 2019 and up by Alexander Pevzner ([email protected])
* See LICENSE for license terms and conditions
*
* Routines for SANE options handling
*/
#include "airscan.h"
#include <string.h>
/******************** Generic functions for IDs ********************/
/* Name/value mapping entry
*/
typedef struct {
int id;
const char *name;
} id_name_table;
/* Get name by ID. For unknown ID returns NULL
*/
static const char*
id_name (int id, const id_name_table *table)
{
int i;
for (i = 0; table[i].name != NULL; i ++) {
if (table[i].id == id) {
return table[i].name;
}
}
return NULL;
}
/* Get ID by name. For unknown name returns -1
*/
static int
id_by_name (const char *name, int (*cmp) (const char *s1, const char *s2),
const id_name_table *table)
{
int i;
for (i = 0; table[i].name != NULL; i ++) {
if (!cmp(name, table[i].name)) {
return table[i].id;
}
}
return -1;
}
/******************** ID_PROTO ********************/
/* id_proto_name_table table represents name mapping for ID_PROTO
*/
static id_name_table id_proto_name_table[] = {
{ID_PROTO_ESCL, "eSCL"},
{ID_PROTO_WSD, "WSD"},
{-1, NULL}
};
/* id_proto_name returns protocol name
* For unknown ID returns NULL
*/
const char*
id_proto_name (ID_PROTO proto)
{
return id_name(proto, id_proto_name_table);
}
/* id_proto_by_name returns protocol identifier by name
* For unknown name returns ID_PROTO_UNKNOWN
*/
ID_PROTO
id_proto_by_name (const char* name)
{
return id_by_name(name, strcasecmp, id_proto_name_table);
}
/******************** ID_SOURCE ********************/
/* id_source_sane_name_table represents ID_SOURCE to
* SANE name mapping
*/
static id_name_table id_source_sane_name_table[] = {
{ID_SOURCE_PLATEN, OPTVAL_SOURCE_PLATEN},
{ID_SOURCE_ADF_SIMPLEX, OPTVAL_SOURCE_ADF_SIMPLEX},
{ID_SOURCE_ADF_DUPLEX, OPTVAL_SOURCE_ADF_DUPLEX},
{-1, NULL}
};
/* id_source_sane_name returns SANE name for the source
* For unknown ID returns NULL
*/
const char*
id_source_sane_name (ID_SOURCE id)
{
return id_name(id, id_source_sane_name_table);
}
/* id_source_by_sane_name returns ID_SOURCE by its SANE name
* For unknown name returns ID_SOURCE_UNKNOWN
*/
ID_SOURCE
id_source_by_sane_name (const char *name)
{
return id_by_name(name, strcmp, id_source_sane_name_table);
}
/******************** ID_COLORMODE ********************/
/* id_colormode_sane_name_table represents ID_COLORMODE to
* SANE name mapping
*/
static id_name_table id_colormode_sane_name_table[] = {
{ID_COLORMODE_BW1, SANE_VALUE_SCAN_MODE_HALFTONE},
{ID_COLORMODE_GRAYSCALE, SANE_VALUE_SCAN_MODE_GRAY},
{ID_COLORMODE_COLOR, SANE_VALUE_SCAN_MODE_COLOR},
{-1, NULL}
};
/* id_colormode_sane_name returns SANE name for the color mode
* For unknown ID returns NULL
*/
const char*
id_colormode_sane_name (ID_COLORMODE id)
{
return id_name(id, id_colormode_sane_name_table);
}
/* id_colormode_by_sane_name returns ID_COLORMODE nu its SANE name
* For unknown name returns ID_COLORMODE_UNKNOWN
*/
ID_COLORMODE
id_colormode_by_sane_name (const char *name)
{
return id_by_name(name, strcmp, id_colormode_sane_name_table);
}
/******************** ID_FORMAT ********************/
/* id_format_mime_name_table represents ID_FORMAT to
* MIME name mapping
*/
static id_name_table id_format_mime_name_table[] = {
{ID_FORMAT_JPEG, "image/jpeg"},
{ID_FORMAT_TIFF, "image/tiff"},
{ID_FORMAT_PNG, "image/png"},
{ID_FORMAT_PDF, "application/pdf"},
{ID_FORMAT_BMP, "application/bmp"},
{-1, NULL}
};
/* id_format_mime_name returns MIME name for the image format
*/
const char*
id_format_mime_name (ID_FORMAT id)
{
return id_name(id, id_format_mime_name_table);
}
/* id_format_by_mime_name returns ID_FORMAT by its MIME name
* For unknown name returns ID_FORMAT_UNKNOWN
*/
ID_FORMAT
id_format_by_mime_name (const char *name)
{
return id_by_name(name, strcasecmp, id_format_mime_name_table);
}
/* if_format_short_name returns short name for ID_FORMAT
*/
const char*
id_format_short_name (ID_FORMAT id)
{
const char *mime = id_format_mime_name(id);
const char *name = mime ? (strchr(mime, '/') + 1) : NULL;
return name ? name : mime;
}
/* vim:ts=8:sw=4:et
*/