CLI  2.9
io_device.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2006-2018, Alexis Royer, http://alexis.royer.free.fr/CLI
3 
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation
10  and/or other materials provided with the distribution.
11  * Neither the name of the CLI library project nor the names of its contributors may be used to endorse or promote products derived from this software
12  without specific prior written permission.
13 
14  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26 
27 
31 
32 #ifndef _CLI_IO_DEVICE_H_
33 #define _CLI_IO_DEVICE_H_
34 
35 #include <stdint.h> // int8_t, int16_t, int32_t, int64_t...
36 
37 #include "cli/namespace.h"
38 #include "cli/object.h"
39 #include "cli/debug.h"
40 #include "cli/tk.h"
41 #include "cli/resource_string.h"
42 
43 
45 
46 
47 
48  typedef enum _KEY
50  {
51  NULL_KEY = '\0',
52 
53  BREAK = 3,
54  LOGOUT = 4,
55  ENTER = 10,
56  ESCAPE = 27,
57  SPACE = 32,
58  BACKSPACE = 8,
59  DELETE = 127,
60  CLS = 501,
61  INSERT = 502,
62  TAB = '\t',
63 
64  KEY_0 = '0',
65  KEY_1 = '1',
66  KEY_2 = '2',
67  KEY_3 = '3',
68  KEY_4 = '4',
69  KEY_5 = '5',
70  KEY_6 = '6',
71  KEY_7 = '7',
72  KEY_8 = '8',
73  KEY_9 = '9',
74  KEY_a = 'a',
75  KEY_aacute = 0xc3a1,
76  KEY_agrave = 0xc3a0,
77  KEY_auml = 0xc3a4,
78  KEY_acirc = 0xc3a2,
79  KEY_b = 'b',
80  KEY_c = 'c',
81  KEY_ccedil = 0xc3a7,
82  KEY_d = 'd',
83  KEY_e = 'e',
84  KEY_eacute = 0xc3a9,
85  KEY_egrave = 0xc3a8,
86  KEY_euml = 0xc3ab,
87  KEY_ecirc = 0xc3aa,
88  KEY_f = 'f',
89  KEY_g = 'g',
90  KEY_h = 'h',
91  KEY_i = 'i',
92  KEY_iacute = 0xc3ad,
93  KEY_igrave = 0xc3ac,
94  KEY_iuml = 0xc3af,
95  KEY_icirc = 0xc3ae,
96  KEY_j = 'j',
97  KEY_k = 'k',
98  KEY_l = 'l',
99  KEY_m = 'm',
100  KEY_n = 'n',
101  KEY_o = 'o',
102  KEY_oacute = 0xc3b3,
103  KEY_ograve = 0xc3b2,
104  KEY_ouml = 0xc3b6,
105  KEY_ocirc = 0xc3b4,
106  KEY_p = 'p',
107  KEY_q = 'q',
108  KEY_r = 'r',
109  KEY_s = 's',
110  KEY_t = 't',
111  KEY_u = 'u',
112  KEY_uacute = 0xc3ba,
113  KEY_ugrave = 0xc3b9,
114  KEY_uuml = 0xc3bc,
115  KEY_ucirc = 0xc3bb,
116  KEY_v = 'v',
117  KEY_w = 'w',
118  KEY_x = 'x',
119  KEY_y = 'y',
120  KEY_z = 'z',
121 
122  KEY_A = 'A',
123  KEY_B = 'B',
124  KEY_C = 'C',
125  KEY_D = 'D',
126  KEY_E = 'E',
127  KEY_F = 'F',
128  KEY_G = 'G',
129  KEY_H = 'H',
130  KEY_I = 'I',
131  KEY_J = 'J',
132  KEY_K = 'K',
133  KEY_L = 'L',
134  KEY_M = 'M',
135  KEY_N = 'N',
136  KEY_O = 'O',
137  KEY_P = 'P',
138  KEY_Q = 'Q',
139  KEY_R = 'R',
140  KEY_S = 'S',
141  KEY_T = 'T',
142  KEY_U = 'U',
143  KEY_V = 'V',
144  KEY_W = 'W',
145  KEY_X = 'X',
146  KEY_Y = 'Y',
147  KEY_Z = 'Z',
148 
149  PLUS = '+',
150  MINUS = '-',
151  STAR = '*',
152  SLASH = '/',
153  LOWER_THAN = '<',
154  GREATER_THAN = '>',
155  EQUAL = '=',
156  PERCENT = '%',
157 
158  UNDERSCORE = '_',
159  AROBASE = '@',
160  SHARP = '#',
161  AMPERCENT = '&',
162  DOLLAR = '$',
163  BACKSLASH = '\\',
164  PIPE = '|',
165  TILDE = '~',
166  SQUARE = 0xc2b2,
167  EURO = 0xe282ac,
168  POUND = 0xc2a3,
169  MICRO = 0xc2b5,
170  PARAGRAPH = 0xc2a7,
171  DEGREE = 0xc2b0,
172  COPYRIGHT = 0xc2a9,
173 
174  QUESTION = '?',
175  EXCLAMATION = '!',
176  COLUMN = ':',
177  DOT = '.',
178  COMA = ',',
179  SEMI_COLUMN = ';',
180  QUOTE = '\'',
181  DOUBLE_QUOTE = '"',
182  BACK_QUOTE = '`',
183 
184  OPENING_BRACE = '(',
185  CLOSING_BRACE = ')',
186  OPENING_CURLY_BRACE = '{',
187  CLOSING_CURLY_BRACE = '}',
188  OPENING_BRACKET = '[',
189  CLOSING_BRACKET = ']',
190 
191  KEY_UP = 1001,
192  KEY_DOWN = 1002,
193  KEY_LEFT = 1003,
194  KEY_RIGHT = 1004,
195  PAGE_UP = 1005,
196  PAGE_DOWN = 1006,
197  PAGE_LEFT = 1007,
198  PAGE_RIGHT = 1008,
199 
200  KEY_BEGIN = 1020,
201  KEY_END = 1021,
202 
203  COPY = 2001,
204  CUT = 2002,
205  PASTE = 2003,
206 
207  UNDO = 2004,
208  REDO = 2005,
209  PREVIOUS = 2006,
210  NEXT = 2007,
211 
212  F1 = 0x0f000001,
213  F2 = 0x0f000002,
214  F3 = 0x0f000003,
215  F4 = 0x0f000004,
216  F5 = 0x0f000005,
217  F6 = 0x0f000006,
218  F7 = 0x0f000007,
219  F8 = 0x0f000008,
220  F9 = 0x0f000009,
221  F10 = 0x0f00000a,
222  F11 = 0x0f00000b,
223  F12 = 0x0f00000c,
224 
225  FEED_MORE = 0xffffffff,
226  } KEY;
227 
230  class IOEndl : public Object
231  {
232  public:
234  explicit IOEndl(void) : Object() {}
235 
236  private:
238  IOEndl(const IOEndl&);
240  IOEndl& operator=(const IOEndl&);
241  };
242 
247  extern const IOEndl endl;
248 
249 
250  // Forward declarations.
251  class StringEncoder;
252  class StringDecoder;
253 
256  class OutputDevice : public Object
257  {
258  protected:
260  explicit OutputDevice(
261  const char* const STR_DbgName,
262  const bool B_AutoDelete
263  );
264 
266  virtual ~OutputDevice(void);
267 
268  private:
270  explicit OutputDevice(void);
272  OutputDevice(const OutputDevice&);
274  OutputDevice& operator=(const OutputDevice&);
275 
276  public:
279  const tk::String GetDebugName(void) const;
280 
281  public:
284  const int UseInstance(
285  const CallInfo& CLI_CallInfo
286  );
287 
293  const int FreeInstance(
294  const CallInfo& CLI_CallInfo
295  );
296 
299  const int GetInstanceUsers(void) const;
300 
301  public:
307  const bool OpenUp(
308  const CallInfo& CLI_CallInfo
309  );
310 
316  const bool CloseDown(
317  const CallInfo& CLI_CallInfo
318  );
319 
322  const int GetOpenUsers(void) const;
323 
324  protected:
331  virtual const bool OpenDevice(void) = 0;
332 
339  virtual const bool CloseDevice(void) = 0;
340 
341  public:
342  #ifndef CLI_NO_STL
343  const OutputDevice& operator<<(
347  const std::string& STR_Out
348  ) const;
349  #endif
350 
353  const OutputDevice& operator<<(
354  const tk::String& STR_Out
355  ) const;
356 
359  const OutputDevice& operator<<(
360  const char* const STR_Out
361  ) const;
362 
365  const OutputDevice& operator<<(
366  const char C_Out
367  ) const;
368 
371  const OutputDevice& operator<<(
372  const KEY E_Key
373  ) const;
374 
377  const OutputDevice& operator<<(
378  const uint8_t UI8_Out
379  ) const;
380 
383  const OutputDevice& operator<<(
384  const int16_t I16_Out
385  ) const;
386 
389  const OutputDevice& operator<<(
390  const uint16_t UI16_Out
391  ) const;
392 
395  const OutputDevice& operator<<(
396  const int32_t I32_Out
397  ) const;
398 
401  const OutputDevice& operator<<(
402  const uint32_t UI32_Out
403  ) const;
404 
407  const OutputDevice& operator<<(
408  const int64_t I64_Out
409  ) const;
410 
413  const OutputDevice& operator<<(
414  const uint64_t UI64_Out
415  ) const;
416 
419  const OutputDevice& operator<<(
420  const float F_Out
421  ) const;
422 
425  const OutputDevice& operator<<(
426  const double D_Out
427  ) const;
428 
431  const OutputDevice& operator<<(
432  const void* const PV_Out
433  ) const;
434 
437  const OutputDevice& operator<<(
438  const IOEndl& CLI_IOEndl
439  ) const;
440 
441  public:
444  const ResourceString GetLastError(void) const;
445 
446  public:
449  static OutputDevice& GetNullDevice(void);
450 
453  static OutputDevice& GetStdOut(void);
454 
457  static OutputDevice& GetStdErr(void);
458 
459  public:
461  class ScreenInfo : public Object
462  {
463  public:
466  UNKNOWN = -1,
469  };
470 
471  private:
473  explicit ScreenInfo(void);
474 
475  public:
477  explicit ScreenInfo(
478  const int I_Width,
479  const int I_Height,
480  const bool B_TrueCls,
481  const bool B_WrapLines
482  ) : Object(),
483  m_iWidth(I_Width), m_iHeight(I_Height),
484  m_bTrueCls(B_TrueCls), m_bWrapLines(B_WrapLines)
485  {}
488  const ScreenInfo& CLI_Info
489  ) : Object(),
490  m_iWidth(CLI_Info.GetWidth()), m_iHeight(CLI_Info.GetHeight()),
491  m_bTrueCls(CLI_Info.GetbTrueCls()), m_bWrapLines(CLI_Info.GetbWrapLines())
492  {}
494  virtual ~ScreenInfo(void) {}
495  public:
499  const ScreenInfo& CLI_ScreenInfo
500  )
501  {
502  m_iWidth = CLI_ScreenInfo.m_iWidth;
503  m_iHeight = CLI_ScreenInfo.m_iHeight;
504  m_bTrueCls = CLI_ScreenInfo.m_bTrueCls;
505  m_bWrapLines = CLI_ScreenInfo.m_bWrapLines;
506  return *this;
507  }
508  public:
511  const int GetWidth(void) const { return ((m_iWidth > 0) ? m_iWidth : UNKNOWN); }
514  const unsigned int GetSafeWidth(void) const { return ((m_iWidth > 0) ? m_iWidth : DEFAULT_WIDTH); }
517  const int GetHeight(void) const { return ((m_iHeight > 0) ? m_iHeight : UNKNOWN); }
520  const unsigned int GetSafeHeight(void) const { return ((m_iHeight > 0) ? m_iHeight : DEFAULT_HEIGHT); }
523  const bool GetbTrueCls(void) const { return m_bTrueCls; }
526  const bool GetbWrapLines(void) const { return m_bWrapLines; }
527  private:
528  int m_iWidth;
529  int m_iHeight;
530  bool m_bTrueCls;
531  bool m_bWrapLines;
532  };
533 
534  public:
535  // Note: use of @param doxygen tag in order to avoid doxygen warnings for reimplementations in sub-classes.
538  virtual void PutString(const char* const STR_Out) const = 0;
539 
541  virtual void Beep(void) const;
542 
544  virtual void CleanScreen(void) const;
545 
548  virtual const ScreenInfo GetScreenInfo(void) const;
549 
550  // Note: use of @param doxygen tag in order to avoid doxygen warnings for reimplementations in sub-classes.
557  virtual const bool WouldOutput(const OutputDevice& CLI_Device) const;
558 
559  private:
561  const tk::String m_strDebugName;
562 
564  int m_iInstanceLock;
565 
567  int m_iOpenLock;
568 
570  StringEncoder& m_cliStringEncoder;
571 
572  protected:
576  };
577 
578 
581  class IODevice : public OutputDevice
582  {
583  public:
585  explicit IODevice(
586  const char* const STR_DbgName,
587  const bool B_AutoDelete
588  );
589 
591  virtual ~IODevice(void);
592 
593  private:
595  explicit IODevice(void);
597  IODevice(const IODevice&);
599  IODevice& operator=(const IODevice&);
600 
601  public:
605  virtual const KEY GetKey(void) const = 0;
606 
609  virtual const ResourceString GetLocation(void) const;
610 
611  // Note: use of @param doxygen tag in order to avoid doxygen warnings for reimplementations in sub-classes.
618  virtual const bool WouldInput(const IODevice& CLI_Device) const;
619 
620  public:
623  static IODevice& GetNullDevice(void);
624 
627  static IODevice& GetStdIn(void);
628 
629  protected:
634  const KEY Char2Key(
635  const int I_Char
636  ) const;
637  private:
639  StringDecoder& m_cliStringDecoder;
640  };
641 
643 
644 #endif // _CLI_IO_DEVICE_H_
645 
Generic object.
Definition: object.h:42
static OutputDevice & GetStdErr(void)
Standard error device singleton.
Cut.
Definition: io_device.h:204
Right arrow key.
Definition: io_device.h:194
static OutputDevice & GetNullDevice(void)
Null device singleton.
Main namespace of the CLI library.
Copy.
Definition: io_device.h:203
virtual ~IODevice(void)
Destructor.
virtual ~OutputDevice(void)
Destructor.
Based on utf-8 encoding for &#39;§&#39; (changed in version 2.9)
Definition: io_device.h:170
Based on utf-8 encoding for &#39;à&#39; (changed in version 2.9)
Definition: io_device.h:76
virtual void Beep(void) const
Beep handler.
const bool GetbWrapLines(void) const
Line wrapping characteristic accessor.
Definition: io_device.h:526
Based on utf-8 encoding for &#39;€&#39; (changed in version 2.9)
Definition: io_device.h:167
Based on utf-8 encoding for &#39;í&#39; (changed in version 2.9)
Definition: io_device.h:92
Debug facilities.
static OutputDevice & GetStdOut(void)
Standard output device singleton.
Redo.
Definition: io_device.h:208
Based on utf-8 encoding for &#39;ì&#39; (changed in version 2.9)
Definition: io_device.h:93
virtual const KEY GetKey(void) const =0
Input key capture handler.
Page left arrow key.
Definition: io_device.h:197
Default height constant.
Definition: io_device.h:468
virtual const bool WouldOutput(const OutputDevice &CLI_Device) const
Stack overflow protection.
static IODevice & GetNullDevice(void)
Null device singleton.
Call information object.
Definition: debug.h:43
Based on utf-8 encoding for &#39;ç&#39; (changed in version 2.9)
Definition: io_device.h:81
const bool OpenUp(const CallInfo &CLI_CallInfo)
Checks the device is opened.
virtual const bool OpenDevice(void)=0
Device opening handler.
Based on utf-8 encoding for &#39;ü&#39; (changed in version 2.9)
Definition: io_device.h:114
End key.
Definition: io_device.h:201
const bool CloseDown(const CallInfo &CLI_CallInfo)
Indicates the device the client does not need the device to opened anymore.
#define CLI_NS_END(__ns)
End a namespace definition.
Definition: namespace.h:45
const int GetInstanceUsers(void) const
Instance user count accessor.
const OutputDevice & operator<<(const std::string &STR_Out) const
Output operator.
const KEY Char2Key(const int I_Char) const
Common char translation.
Based on utf-8 encoding for &#39;£&#39; (changed in version 2.9)
Definition: io_device.h:168
Based on utf-8 encoding for &#39;á&#39; (changed in version 2.9)
Definition: io_device.h:75
Page up arrow key.
Definition: io_device.h:195
ResourceString class.
virtual const bool CloseDevice(void)=0
Device closure handler.
Based on utf-8 encoding for &#39;ô&#39; (changed in version 2.9)
Definition: io_device.h:105
ScreenInfo(const int I_Width, const int I_Height, const bool B_TrueCls, const bool B_WrapLines)
Constructor.
Definition: io_device.h:477
Based on utf-8 encoding for &#39;©&#39; (changed in version 2.9)
Definition: io_device.h:172
IOEndl(void)
Default constructor.
Definition: io_device.h:234
const int GetHeight(void) const
Screen height accessor.
Definition: io_device.h:517
Screen information.
Definition: io_device.h:461
Forward key.
Definition: io_device.h:210
const int GetOpenUsers(void) const
Open state user count accessor.
Page down arrow key.
Definition: io_device.h:196
const unsigned int GetSafeHeight(void) const
Safe screen height accessor.
Definition: io_device.h:520
Namespace management.
Based on utf-8 encoding for &#39;ï&#39; (changed in version 2.9)
Definition: io_device.h:94
End of line for input/output devices.
Definition: io_device.h:230
virtual const ResourceString GetLocation(void) const
Input location accessor.
const ResourceString GetLastError(void) const
Last error accessor.
Space.
Definition: io_device.h:57
Page right arrow key.
Definition: io_device.h:198
Based on utf-8 encoding for &#39;ü&#39; (changed in version 2.9)
Definition: io_device.h:86
_RegularValues
ScreenInfo regular values.
Definition: io_device.h:465
ScreenInfo(const ScreenInfo &CLI_Info)
Copy constructor.
Definition: io_device.h:487
Begin key.
Definition: io_device.h:200
Generic output device.
Definition: io_device.h:256
virtual void CleanScreen(void) const
Clean screen.
static IODevice & GetStdIn(void)
Standard input device singleton.
Based on utf-8 encoding for &#39;ä&#39; (changed in version 2.9)
Definition: io_device.h:77
Unknown value constant for either width or height.
Definition: io_device.h:466
Clean screen key (changed from 129 to 501 in order to avoid overlap with printable ASCII characters)...
Definition: io_device.h:60
virtual const ScreenInfo GetScreenInfo(void) const
Screen info accessor.
ResourceString m_cliLastError
Definition: io_device.h:575
Based on utf-8 encoding for &#39;ò&#39; (changed in version 2.9)
Definition: io_device.h:103
Based on utf-8 encoding for &#39;µ&#39; (changed in version 2.9)
Definition: io_device.h:169
enum _KEY KEY
Input keys.
Default width constant.
Definition: io_device.h:467
#define CLI_NS_BEGIN(__ns)
Begin a namespace definition.
Definition: namespace.h:38
const int FreeInstance(const CallInfo &CLI_CallInfo)
Releases the instance.
Based on utf-8 encoding for &#39;û&#39; (changed in version 2.9)
Definition: io_device.h:115
Based on utf-8 encoding for &#39;â&#39; (changed in version 2.9)
Definition: io_device.h:78
_KEY
Input keys.
Definition: io_device.h:49
const unsigned int GetSafeWidth(void) const
Safe screen width accessor.
Definition: io_device.h:514
Based on utf-8 encoding for &#39;é&#39; (changed in version 2.9)
Definition: io_device.h:84
const bool GetbTrueCls(void) const
True CleanScreen() characteristic accessor.
Definition: io_device.h:523
Escape.
Definition: io_device.h:56
Based on utf-8 encoding for &#39;ù&#39; (changed in version 2.9)
Definition: io_device.h:113
CLI toolkit definition.
Object class definition.
Based on utf-8 encoding for &#39;²&#39; (changed in version 2.9)
Definition: io_device.h:166
Key to chararcters encoder.
Definition: encoding.h:47
Based on utf-8 encoding for &#39;°&#39; (changed in version 2.9)
Definition: io_device.h:171
Backspace (changed from &#39;\b&#39; to 8 in version 2.7 for ASCII compliance).
Definition: io_device.h:58
Based on utf-8 encoding for &#39;è&#39; (changed in version 2.9)
Definition: io_device.h:85
Left arrow key.
Definition: io_device.h:193
Based on utf-8 encoding for &#39;ú&#39; (changed in version 2.9)
Definition: io_device.h:112
Enter.
Definition: io_device.h:55
Regular characters to key decoder.
Definition: encoding.h:77
const int UseInstance(const CallInfo &CLI_CallInfo)
Ensures instance validity.
Down arrow key.
Definition: io_device.h:192
virtual ~ScreenInfo(void)
Destructor.
Definition: io_device.h:494
Break (Ctrl+C).
Definition: io_device.h:53
ScreenInfo & operator=(const ScreenInfo &CLI_ScreenInfo)
Assignment operator.
Definition: io_device.h:498
Previous key.
Definition: io_device.h:209
Paste.
Definition: io_device.h:205
Tab key.
Definition: io_device.h:62
Undo.
Definition: io_device.h:207
Help class definition.
Logout (Ctrl+D).
Definition: io_device.h:54
Based on utf-8 encoding for &#39;î&#39; (changed in version 2.9)
Definition: io_device.h:95
Generic input/output device.
Definition: io_device.h:581
Based on utf-8 encoding for &#39;û&#39; (changed in version 2.9)
Definition: io_device.h:87
virtual void PutString(const char *const STR_Out) const =0
Output handler.
const int GetWidth(void) const
Screen width accessor.
Definition: io_device.h:511
const IOEndl endl
The common IOEndl object.
Insert key (changed from 500 to 502 in order to avoid overlap with printable ASCII characters)...
Definition: io_device.h:61
Based on utf-8 encoding for &#39;ö&#39; (changed in version 2.9)
Definition: io_device.h:104
const tk::String GetDebugName(void) const
Debug name accessor.
Return code while analyzing input characters, till the encoding sequence is not fulfilled.
Definition: io_device.h:225
Based on utf-8 encoding for &#39;ó&#39; (changed in version 2.9)
Definition: io_device.h:102
Null key.
Definition: io_device.h:51
virtual const bool WouldInput(const IODevice &CLI_Device) const
Stack overflow protection.
Up arrow key.
Definition: io_device.h:191
Delete key (changed from 128 to 127 in version 2.7 for ASCII compliance).
Definition: io_device.h:59