1/* FluidSynth - A Software Synthesizer
2 *
3 * Copyright (C) 2003  Peter Hanappe and others.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public License
7 * as published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 * 02111-1307, USA
19 */
20
21
22#include "fluidsynth_priv.h"
23#include "fluid_io.h"
24
25#if WITH_READLINE
26#include <readline/readline.h>
27#include <readline/history.h>
28#endif
29
30int fluid_istream_gets(fluid_istream_t in, char* buf, int len);
31
32fluid_istream_t fluid_get_stdin()
33{
34#ifdef MACOS9
35	return 0; /* to be tested - Antoine 8/3/3 */
36#else
37  return STDIN_FILENO;
38#endif
39}
40
41fluid_ostream_t fluid_get_stdout()
42{
43#ifdef MACOS9
44	return 1; /* to be tested - Antoine 8/3/3 */
45#else
46  return STDOUT_FILENO;
47#endif
48}
49
50int fluid_istream_readline(fluid_istream_t in, char* prompt, char* buf, int len)
51{
52#if WITH_READLINE
53  if (in == fluid_get_stdin()) {
54    char* line;
55
56    line = readline(prompt);
57    if (line == NULL) {
58      return -1;
59    }
60
61    snprintf(buf, len, "%s", line);
62    buf[len - 1] = 0;
63
64    free(line);
65    return 1;
66  } else {
67    return fluid_istream_gets(in, buf, len);
68  }
69#else
70  return fluid_istream_gets(in, buf, len);
71#endif
72}
73
74/* FIXME */
75int fluid_istream_gets(fluid_istream_t in, char* buf, int len)
76{
77  char c;
78  int n;
79
80  buf[len - 1] = 0;
81
82  while (--len > 0) {
83    n = read(in, &c, 1);
84    if (n == 0) {
85      *buf++ = 0;
86      return 0;
87    }
88    if (n < 0) {
89      return n;
90    }
91    if ((c == '\n') || (c == '\r')) {
92      *buf++ = 0;
93      return 1;
94    }
95    *buf++ = c;
96  }
97
98  return -1;
99}
100
101
102int fluid_ostream_printf(fluid_ostream_t out, char* format, ...)
103{
104  char buf[4096];
105  va_list args;
106  int len;
107
108  va_start(args, format);
109  len = vsnprintf(buf, 4095, format, args);
110  va_end(args);
111
112  if (len <= 0) {
113    printf("fluid_ostream_printf: buffer overflow");
114    return -1;
115  }
116
117  buf[4095] = 0;
118
119/*   return write(out, buf, len); */
120  return write(out, buf, strlen(buf));
121}
122