debian: added giffgaff chatscripts
[barry.git] / src / common.cc
blobeecf5b0d5c34b14cc5afca98b61bfcb2015885e5
1 ///
2 /// \file common.cc
3 /// General Barry interface routines
4 ///
6 /*
7 Copyright (C) 2005-2013, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include "i18n.h"
23 #include "common.h"
24 #include "platform.h"
25 #include <pthread.h>
26 #include "debug.h"
27 #include "config.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
32 #ifdef USE_BARRY_SOCKETS
33 #include "usbwrap.h"
34 #endif
36 namespace Barry {
38 bool __data_dump_mode__;
40 std::ostream *LogStream = &std::cout;
41 pthread_mutex_t LogStreamMutex;
45 // Init
47 /// Barry library initializer. Call this before anything else.
48 /// This takes care of initializing the lower level libusb.
49 ///
50 /// This function is safe to be called multiple times. The
51 /// data_dump_mode and the log stream will be updated each time
52 /// it is called, but the USB library will not be re-initialized.
53 ///
54 /// \param[in] data_dump_mode If set to true, the protocol conversation
55 /// will be sent to the logStream specified
56 /// in the second argument.
57 /// \param[in] LogStream Pointer to std::ostream object to use for
58 /// debug output and logging. Defaults to
59 /// std::cout.
60 ///
61 void Init(bool data_dump_mode, std::ostream *logStream)
63 static bool initialized = false;
65 #ifdef USE_BARRY_SOCKETS
66 Usb::LibraryInterface::SetDataDump(data_dump_mode);
67 #endif
69 // perform one-time initalization
70 if( !initialized ) {
71 // initialize i18n gettext directory
72 // the rest is done in i18n.h
73 bindtextdomain(PACKAGE, LOCALEDIR);
75 #ifdef USE_BARRY_SOCKETS
76 // Should call Usb::Uninit at some point,
77 // but there isn't currently a deinit call.
78 int err = 0;
79 if( !Usb::LibraryInterface::Init(&err) ) {
80 eout(_("USB library failed to initialise with libusb error: ") << err);
81 throw Error(_("Failed to initialise USB"));
82 return;
84 #endif
86 // only need to initialize this once
87 pthread_mutex_init(&LogStreamMutex, NULL);
89 // done
90 initialized = true;
93 __data_dump_mode__ = data_dump_mode;
94 LogStream = logStream;
98 // Verbose
100 /// This API call lets the application enable / disable verbose debug
101 /// output on the fly.
103 /// \param[in] data_dump_mode If set to true, the protocol conversation
104 /// will be sent to the logStream specified
105 /// in the Barry::Init() call.
107 void Verbose(bool data_dump_mode)
109 __data_dump_mode__ = data_dump_mode;
111 #ifdef USE_BARRY_SOCKETS
112 Usb::LibraryInterface::SetDataDump(data_dump_mode);
113 #endif
117 // IsVerbose
119 /// Returns true if data dump mode is enabled.
121 bool IsVerbose()
123 return __data_dump_mode__;
126 // The following code is based on the example from the vsnprintf(3) manpage
127 std::string string_vprintf(const char *fmt, ...)
129 // Guess we need no more than 200 bytes.
130 int n, size = 200;
131 char *p, *np;
132 std::string result;
133 va_list ap;
135 if ((p = (char*)malloc(size)) == NULL)
136 return NULL;
138 for (;;) {
139 // Try to print in the allocated space.
140 va_start(ap, fmt);
141 n = vsnprintf(p, size, fmt, ap);
142 va_end(ap);
144 // If that worked, return the string.
145 if (n > -1 && n < size) {
146 result = p;
147 free(p);
148 return result;
151 // Else try again with more space.
152 if (n > -1) // glibc 2.1
153 size = n+1; // precisely what is needed
154 else // glibc 2.0
155 size *= 2; // twice the old size
157 if ((np = (char*)realloc (p, size)) == NULL) {
158 free(p);
159 return result;
160 } else {
161 p = np;
166 } // namespace Barry