Instrument Neutral Distributed Interface INDI  1.9.2
userio.c
Go to the documentation of this file.
1 /*
2  Copyright (C) 2021 by Pawel Soja <kernel32.pl@gmail.com>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 #include "userio.h"
19 #include <stddef.h>
20 #include <stdio.h>
21 #include <string.h>
22 
23 static size_t s_file_write(void *user, const void * ptr, size_t count)
24 {
25  return fwrite(ptr, 1, count, (FILE *)user);
26 }
27 
28 static int s_file_printf(void *user, const char * format, va_list arg)
29 {
30  return vfprintf((FILE *)user, format, arg);
31 }
32 
33 static const struct userio s_userio_file = {
34  .write = s_file_write,
35  .vprintf = s_file_printf,
36 };
37 
38 const struct userio *userio_file()
39 {
40  return &s_userio_file;
41 }
42 
43 int userio_printf(const struct userio *io, void *user, const char * format, ...)
44 {
45  int ret;
46  va_list ap;
47  va_start(ap, format);
48  ret = io->vprintf(user, format, ap);
49  va_end(ap);
50  return ret;
51 }
52 
53 int userio_vprintf(const struct userio *io, void *user, const char * format, va_list arg)
54 {
55  return io->vprintf(user, format, arg);
56 }
57 
58 size_t userio_write(const struct userio *io, void *user, const void * ptr, size_t count)
59 {
60  return io->write(user, ptr, count);
61 }
62 
63 int userio_prints(const struct userio *io, void *user, const char *str)
64 {
65  return io->write(user, str, strlen(str));
66 }
67 
68 int userio_putc(const struct userio *io, void *user, int ch)
69 {
70  char c = ch;
71  return io->write(user, &c, sizeof(c));
72 }
73 
74 size_t userio_xml_escape(const struct userio *io, void *user, const char *src)
75 {
76  size_t total = 0;
77  const char *ptr = src;
78  const char *replacement;
79 
80  for(; *ptr; ++ptr)
81  {
82  switch(*ptr)
83  {
84  case '&': replacement = "&amp;"; break;
85  case '\'': replacement = "&apos;"; break;
86  case '"': replacement = "&quot;"; break;
87  case '<': replacement = "&lt;"; break;
88  case '>': replacement = "&gt;"; break;
89  default: replacement = NULL;
90  }
91 
92  if (replacement != NULL)
93  {
94  total += userio_write(io, user, src, (size_t)(ptr - src));
95  src = ptr + 1;
96  total += userio_write(io, user, replacement, strlen(replacement));
97  }
98  }
99  total += userio_write(io, user, src, (size_t)(ptr - src));
100  return total;
101 }
102 
103 void userio_xmlv1(const userio *io, void *user)
104 {
105  userio_prints(io, user, "<?xml version='1.0'?>\n");
106 }
userio_xml_escape
size_t userio_xml_escape(const struct userio *io, void *user, const char *src)
Definition: userio.c:74
userio_write
size_t userio_write(const struct userio *io, void *user, const void *ptr, size_t count)
Definition: userio.c:58
userio_printf
int userio_printf(const struct userio *io, void *user, const char *format,...)
Definition: userio.c:43
userio_prints
int userio_prints(const struct userio *io, void *user, const char *str)
Definition: userio.c:63
userio_putc
int userio_putc(const struct userio *io, void *user, int ch)
Definition: userio.c:68
userio::write
size_t(* write)(void *user, const void *ptr, size_t count)
Definition: userio.h:34
userio_file
const struct userio * userio_file()
Definition: userio.c:38
userio.h
userio
Definition: userio.h:32
userio_vprintf
int userio_vprintf(const struct userio *io, void *user, const char *format, va_list arg)
Definition: userio.c:53
userio_xmlv1
void userio_xmlv1(const userio *io, void *user)
Definition: userio.c:103
userio::vprintf
int(* vprintf)(void *user, const char *format, va_list arg)
Definition: userio.h:35