File: preferences.c

package info (click to toggle)
sane 0.72-1.1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 3,008 kB
  • ctags: 5,180
  • sloc: ansic: 42,911; sh: 4,584; java: 1,421; makefile: 720
file content (181 lines) | stat: -rw-r--r-- 4,409 bytes parent folder | download | duplicates (3)
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
/* sane - Scanner Access Now Easy.
   Copyright (C) 1997 David Mosberger-Tang
   This file is part of the SANE package.

   SANE 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.

   SANE 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 sane; see the file COPYING.  If not, write to the Free
   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <preferences.h>
#include <sane/sane.h>
#include <sane/sanei.h>
#include <sane/sanei_wire.h>
#include <sane/sanei_codec_ascii.h>

#define POFFSET(field)	((char *) &((Preferences *) 0)->field - (char *) 0)
#define PFIELD(p,offset,type)	(*((type *)(((char *)(p)) + (offset))))

Preferences preferences =
  {
       0,		/* no preferred device (must be 0 or malloced!) */
       0,		/* no default filename */
       0,		/* advanced user */
       1,		/* tooltips enabled */
    10.0,		/* length unit */
       1,		/* preserve_preview */
       0,		/* preview_own_cmap */
     1.0		/* preview_gamma */
  };

static void w_string (Wire *w, Preferences *p, long offset);
static void w_double (Wire *w, Preferences *p, long offset);
static void w_int (Wire *w, Preferences *p, long offset);

static struct
  {
    SANE_String name;
    void (*codec) (Wire *w, Preferences *p, long offset);
    long offset;
  }
desc[] =
  {
    {"device", w_string, POFFSET(device)},
    {"filename", w_string, POFFSET(filename)},
    {"advanced", w_int, POFFSET(advanced)},
    {"tool-tips", w_int, POFFSET(tooltips_enabled)},
    {"length-unit", w_double, POFFSET(length_unit)},
    {"preserve-preview", w_int, POFFSET(preserve_preview)},
    {"preview-own-cmap", w_int, POFFSET(preview_own_cmap)},
    {"preview-gamma", w_double, POFFSET(preview_gamma)},
  };

static void
w_string (Wire *w, Preferences *p, long offset)
{
  SANE_String string;

  if (w->direction == WIRE_ENCODE)
    string = PFIELD (p, offset, char *);

  sanei_w_string (w, &string);

  if (w->direction == WIRE_DECODE)
    {
      if (w->status == 0)
	{
	  const char **field;

	  field = &PFIELD (p, offset, const char *);
	  if (*field)
	    free ((char *) *field);
	  *field = string ? strdup (string) : 0;
	}
      sanei_w_free (w, (WireCodecFunc) sanei_w_string, &string);
    }
}

static void
w_double (Wire *w, Preferences *p, long offset)
{
  SANE_Word word;

  if (w->direction == WIRE_ENCODE)
    word = SANE_FIX (PFIELD (p, offset, double));

  sanei_w_word (w, &word);

  if (w->direction == WIRE_DECODE)
    {
      if (w->status == 0)
	PFIELD (p, offset, double) = SANE_UNFIX (word);
      sanei_w_free (w, (WireCodecFunc) sanei_w_word, &word);
    }
}

static void
w_int (Wire *w, Preferences *p, long offset)
{
  SANE_Word word;

  if (w->direction == WIRE_ENCODE)
    word = PFIELD (p, offset, int);

  sanei_w_word (w, &word);

  if (w->direction == WIRE_DECODE)
    {
      if (w->status == 0)
	PFIELD (p, offset, int) = word;
      sanei_w_free (w, (WireCodecFunc) sanei_w_word, &word);
    }
}

void
preferences_save (int fd)
{
  Wire w;
  int i;

  w.io.fd = fd;
  w.io.read = read;
  w.io.write = write;
  sanei_w_init (&w, sanei_codec_ascii_init);
  sanei_w_set_dir (&w, WIRE_ENCODE);

  for (i = 0; i < NELEMS (desc); ++i)
    {
      sanei_w_string (&w, &desc[i].name);
      (*desc[i].codec) (&w, &preferences, desc[i].offset);
    }

  sanei_w_set_dir (&w, WIRE_DECODE);	/* flush it out */
}

void
preferences_restore (int fd)
{
  SANE_String name;
  Wire w;
  int i;

  w.io.fd = fd;
  w.io.read = read;
  w.io.write = write;
  sanei_w_init (&w, sanei_codec_ascii_init);
  sanei_w_set_dir (&w, WIRE_DECODE);

  while (1)
    {
      sanei_w_space (&w, 3);
      if (w.status)
	return;

      sanei_w_string (&w, &name);
      if (w.status || !name)
	return;

      for (i = 0; i < NELEMS (desc); ++i)
	{
	  if (strcmp (name, desc[i].name) == 0)
	    {
	      (*desc[i].codec) (&w, &preferences, desc[i].offset);
	      break;
	    }
	}
    }
}