Allolib  1.0
C++ Components For Interactive Multimedia
al_Printing.hpp
1 #ifndef INCLUDE_AL_SYSTEM_PRINTING_H
2 #define INCLUDE_AL_SYSTEM_PRINTING_H
3 
4 /* Allocore --
5  Multimedia / virtual environment application class library
6 
7  Copyright (C) 2009. AlloSphere Research Group, Media Arts & Technology, UCSB.
8  Copyright (C) 2012. The Regents of the University of California.
9  All rights reserved.
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are met:
13 
14  Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16 
17  Redistributions in binary form must reproduce the above copyright
18  notice, this list of conditions and the following disclaimer in the
19  documentation and/or other materials provided with the distribution.
20 
21  Neither the name of the University of California nor the names of its
22  contributors may be used to endorse or promote products derived from
23  this software without specific prior written permission.
24 
25  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
29  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  POSSIBILITY OF SUCH DAMAGE.
36 
37 
38  File description:
39  Various routines for printing text to files, standard output, etc.
40 
41  File author(s):
42  Lance Putnam, 2010, putnam.lance@gmail.com
43 */
44 
45 #include <stdio.h>
46 #include <cstdint>
47 #ifdef AL_WINDOWS
48 #define AL_PRINTF_LL "I64"
49 #else
50 #define AL_PRINTF_LL "ll"
51 #endif
52 
53 // #define AL_STRINGIFY(...) #__VA_ARGS__
54 // #define AL_DEBUGLN printf("In %s: line %d\n", __FILE__, __LINE__);
55 
56 namespace al {
57 
60 char intensityToASCII(float v);
61 
63 
67 template <typename T>
68 void print(const T* arr, int size, const char* append = "");
69 
71 
74 template <typename T>
75 void println(const T* arr, int size) {
76  print(arr, size, "\n");
77 }
78 
80 
83 template <typename T>
84 void print(const T& v, const char* append = "") {
85  print(&v, 1, append);
86 }
87 
89 template <typename T>
90 void println(const T& v) {
91  print(v, "\n");
92 }
93 
95 
100 template <class T>
101 void print2D(const T* arr, int nx, int ny, FILE* fp = stdout);
102 
104 
109 void printPlot(float value, uint32_t width = 50, bool spaces = true,
110  const char* point = "o");
111 
113 void err(const char* msg, const char* src = "", bool exits = true);
114 
116 #define AL_WARN(fmt, ...) \
117  ::al::_warn(__FILE__, __LINE__, fmt "\n", ##__VA_ARGS__)
118 
120 #define AL_WARN_ONCE(fmt, ...) \
121  ::al::_warnOnce(__FILE__, __LINE__, fmt "\n", ##__VA_ARGS__)
122 
123 void _warn(const char* fileName, int lineNumber, const char* fmt, ...);
124 void _warnOnce(const char* fileName, int lineNumber, const char* fmt, ...);
125 
126 // Implementation --------------------------------------------------------------
127 
128 inline char intensityToASCII(float v) {
129  static const char map[] =
130  " .,;-~_+<>i!lI?/|)(1}{][rcvunxzjftLCJUYXZO0Qoahkbdpqwm*WMB8&%$#@";
131  //"$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ";
132  // 123456789.123456789.123456789.123456789.123456789.123456789.1234
133  static const int N = sizeof(map) - 1;
134  v = v < 0 ? 0 : (v > 0.9999999f ? 0.9999999f : v);
135  return map[int(N * v)];
136 }
137 
138 #define DEF_PRINT(T, code) \
139  template <> \
140  inline void print<T>(const T* arr, int size, const char* append) { \
141  for (int i = 0; i < size; ++i) { \
142  printf(code " ", arr[i]); \
143  } \
144  if (append[0]) printf("%s", append); \
145  }
146 
147 DEF_PRINT(float, "%g")
148 DEF_PRINT(double, "%g")
149 DEF_PRINT(char, "%d")
150 DEF_PRINT(unsigned char, "%u")
151 DEF_PRINT(short, "%d")
152 DEF_PRINT(unsigned short, "%u")
153 DEF_PRINT(int, "%d")
154 DEF_PRINT(unsigned int, "%u")
155 DEF_PRINT(long, "%ld")
156 DEF_PRINT(unsigned long, "%lu")
157 DEF_PRINT(long long, "%" AL_PRINTF_LL "d")
158 DEF_PRINT(unsigned long long, "%" AL_PRINTF_LL "u")
159 
160 #undef DEF_PRINT
161 
162 template <class T>
163 void print2D(const T* pix, int nx, int ny, FILE* fp) {
164  for (int j = 0; j < nx; ++j) {
165  for (int i = 0; i < ny; ++i) {
166  float v = pix[j * nx + i];
167  fprintf(fp, "%c ", intensityToASCII(v));
168  }
169  printf("\n");
170  }
171 }
172 
173 } // namespace al
174 
175 #endif
Definition: al_App.hpp:23
void err(const char *msg, const char *src="", bool exits=true)
Prints error message to stderr and optionally calls exit()
void println(const T *arr, int size)
Print an array of numbers with new line.
Definition: al_Printing.hpp:75
void print2D(const T *arr, int nx, int ny, FILE *fp=stdout)
Prints 2D array of intensity values.
void printPlot(float value, uint32_t width=50, bool spaces=true, const char *point="o")
Print signed unit value on a horizontal plot.
char intensityToASCII(float v)
void print(const T *arr, int size, const char *append="")
Print an array of numbers.