Bumped copyright dates for 2013
[barry.git] / src / common.cc
blobe04fb595165254486332ef604a4b0feea79d8c74
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 <pthread.h>
25 #include "debug.h"
26 #include "config.h"
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
31 #ifdef USE_BARRY_SOCKETS
32 #include "usbwrap.h"
33 #endif
35 namespace Barry {
37 bool __data_dump_mode__;
39 std::ostream *LogStream = &std::cout;
40 pthread_mutex_t LogStreamMutex;
44 // Init
46 /// Barry library initializer. Call this before anything else.
47 /// This takes care of initializing the lower level libusb.
48 ///
49 /// This function is safe to be called multiple times. The
50 /// data_dump_mode and the log stream will be updated each time
51 /// it is called, but the USB library will not be re-initialized.
52 ///
53 /// \param[in] data_dump_mode If set to true, the protocol conversation
54 /// will be sent to the logStream specified
55 /// in the second argument.
56 /// \param[in] LogStream Pointer to std::ostream object to use for
57 /// debug output and logging. Defaults to
58 /// std::cout.
59 ///
60 void Init(bool data_dump_mode, std::ostream *logStream)
62 static bool initialized = false;
64 #ifdef USE_BARRY_SOCKETS
65 Usb::LibraryInterface::SetDataDump(data_dump_mode);
66 #endif
68 // perform one-time initalization
69 if( !initialized ) {
70 // initialize i18n gettext directory
71 // the rest is done in i18n.h
72 bindtextdomain(PACKAGE, LOCALEDIR);
74 #ifdef USE_BARRY_SOCKETS
75 // Should call Usb::Uninit at some point,
76 // but there isn't currently a deinit call.
77 int err = 0;
78 if( !Usb::LibraryInterface::Init(&err) ) {
79 eout(_("USB library failed to initialise with libusb error: ") << err);
80 throw Error(_("Failed to initialise USB"));
81 return;
83 #endif
85 // only need to initialize this once
86 pthread_mutex_init(&LogStreamMutex, NULL);
88 // done
89 initialized = true;
92 __data_dump_mode__ = data_dump_mode;
93 LogStream = logStream;
97 // Verbose
99 /// This API call lets the application enable / disable verbose debug
100 /// output on the fly.
102 /// \param[in] data_dump_mode If set to true, the protocol conversation
103 /// will be sent to the logStream specified
104 /// in the Barry::Init() call.
106 void Verbose(bool data_dump_mode)
108 __data_dump_mode__ = data_dump_mode;
110 #ifdef USE_BARRY_SOCKETS
111 Usb::LibraryInterface::SetDataDump(data_dump_mode);
112 #endif
116 // IsVerbose
118 /// Returns true if data dump mode is enabled.
120 bool IsVerbose()
122 return __data_dump_mode__;
125 // The following code is based on the example from the vsnprintf(3) manpage
126 std::string string_vprintf(const char *fmt, ...)
128 // Guess we need no more than 200 bytes.
129 int n, size = 200;
130 char *p, *np;
131 std::string result;
132 va_list ap;
134 if ((p = (char*)malloc(size)) == NULL)
135 return NULL;
137 for (;;) {
138 // Try to print in the allocated space.
139 va_start(ap, fmt);
140 n = vsnprintf(p, size, fmt, ap);
141 va_end(ap);
143 // If that worked, return the string.
144 if (n > -1 && n < size) {
145 result = p;
146 free(p);
147 return result;
150 // Else try again with more space.
151 if (n > -1) // glibc 2.1
152 size = n+1; // precisely what is needed
153 else // glibc 2.0
154 size *= 2; // twice the old size
156 if ((np = (char*)realloc (p, size)) == NULL) {
157 free(p);
158 return result;
159 } else {
160 p = np;
165 } // namespace Barry