1 /* Copyright (C) 2001-2019 Artifex Software, Inc.
2    All Rights Reserved.
3 
4    This software is provided AS-IS with no warranty, either express or
5    implied.
6 
7    This software is distributed under license and may not be copied,
8    modified or distributed except as expressly authorized under the terms
9    of the license contained in the file LICENSE in this distribution.
10 
11    Refer to licensing information at http://www.artifex.com or contact
12    Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
13    CA 94945, U.S.A., +1(415)492-9861, for further information.
14 */
15 
16 
17 /* The device abstraction within the Rinkj driver. */
18 
19 #include "rinkj-device.h"
20 #include <string.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 
24 /* Deprecated */
25 int
rinkj_device_set(RinkjDevice * self,const char * config)26 rinkj_device_set (RinkjDevice *self, const char *config)
27 {
28   if (self->init_happened != 0)
29     return -1;
30   return self->set (self, config);
31 }
32 
33 /* Preferred, as it matches IJS */
34 int
rinkj_device_set_param(RinkjDevice * self,const char * key,const char * value,int value_size)35 rinkj_device_set_param (RinkjDevice *self, const char *key,
36                         const char *value, int value_size)
37 {
38   int keylen = strlen (key);
39   int bufsize = keylen + value_size + 3;
40   char *buf = malloc (bufsize);
41   int status;
42 
43   /* This implementation is in terms of device_set, but we're going to
44      change the prototype of the device so this is native. */
45   memcpy (buf, key, keylen);
46   memcpy (buf + keylen, ": ", 2);
47   memcpy (buf + keylen + 2, value, value_size);
48   buf[keylen + 2 + value_size] = 0;
49   status = rinkj_device_set (self, buf);
50   free (buf);
51   return status;
52 }
53 
54 int
rinkj_device_set_param_string(RinkjDevice * self,const char * key,const char * value)55 rinkj_device_set_param_string (RinkjDevice *self, const char *key,
56                                const char *value)
57 {
58   return rinkj_device_set_param (self, key, value, strlen (value));
59 }
60 
61 int
rinkj_device_set_param_int(RinkjDevice * self,const char * key,int value)62 rinkj_device_set_param_int (RinkjDevice *self, const char *key, int value)
63 {
64   char buf[32];
65   int value_size = sprintf (buf, "%d", value);
66   return rinkj_device_set_param (self, key, buf, value_size);
67 }
68 
69 int
rinkj_device_init(RinkjDevice * self,const RinkjDeviceParams * params)70 rinkj_device_init (RinkjDevice *self, const RinkjDeviceParams *params)
71 {
72   int status;
73 
74   if (self->init_happened != 0)
75     return -1;
76   status = self->init (self, params);
77   self->init_happened = 42;
78   return status;
79 }
80 
81 int
rinkj_device_write(RinkjDevice * self,const char ** data)82 rinkj_device_write (RinkjDevice *self, const char **data)
83 {
84   if (self->init_happened != 42)
85     return -1;
86   return self->write (self, data);
87 }
88