AlcapDAQ  1
console.c
Go to the documentation of this file.
1 /*
2  -----------------------------------------------------------------------------
3 
4  --- CAEN SpA - Computing Systems Division ---
5 
6  -----------------------------------------------------------------------------
7 
8  Name : CONSOLE.c
9 
10  Description : Console library for Linux & Microsoft VISUAL C++
11  (Win32 implementation).
12  Provide a set of function which permit the console
13  screen management.
14 
15  Linux Specifications :
16 
17  Version: Linux 1.0
18  Platform: Linux 2.4.x
19  Language: GCC 2.95 and 3.0
20 
21  Date : November 2004
22  Release : 1.0
23  Author : C.Landi
24 
25 
26 
27  -----------------------------------------------------------------------------
28 
29  This file contains the following procedures and functions:
30 
31  con_init initialize the console
32  con_end close the console
33  write_log write a message into the log file
34  con_getch get a char from console without echoing
35  con_kbhit read a char from console without stopping the program
36  con_scanf read formatted data from the console
37  con_printf print formatted output to the standard output stream
38  gotoxy set the cursor position
39  con_printf_xy print formatted output on the X,Y screen position
40  clrscr clear the screen
41  clear_line clear a line
42  delay wait n milliseconds
43 
44  -----------------------------------------------------------------------------
45 */
46 
47 #include "console.h"
48 
49 #ifdef LINUX
50  #include <ncurses.h>
51  #include <unistd.h>
52 // #include <stdlib.h>
53 #else
54  #include <conio.h>
55  #include <stdio.h>
56  #include <stdarg.h>
57  #include <windows.h>
58 
59 static HANDLE ocon, icon;
60 #endif
61 
62 
63 
64 
65 /****************************************************************************/
66 /* GLOBAL VARIABLES */
67 /****************************************************************************/
68 
69 /*------------------------------- log file ---------------------------------*/
70 FILE *log_file = NULL; /* log file pointer */
71 char LOG_FILE_NAME[] = "LOG_FILE"; /* log file name */
72 
73 /*------------------------------- console info -----------------------------*/
74 
75 #ifndef LINUX
77 #endif
78 
79 
80 /****************************************************************************/
81 /* CON_INIT */
82 /*--------------------------------------------------------------------------*/
83 /* Initialize the console */
84 /****************************************************************************/
85 
86 void con_init()
87 {
88 #ifdef LINUX
89 
90  initscr();
91  cbreak();
92  noecho();
93  nodelay(stdscr, FALSE);
94  curs_set(FALSE);
95 
96 #else
97 
98  COORD coordScreen = { 0, 0 }; /* home of the cursor */
99  CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
100  BOOL bSuccess;
101  DWORD cCharsWritten;
102  DWORD dwConSize; /* number of character cells in the current buffer */
103 
104 
105  ocon = GetStdHandle(STD_OUTPUT_HANDLE); /* handle for the standard output */
106  icon = GetStdHandle(STD_INPUT_HANDLE); /* handle for the standard input */
107 
108  t_type =TERM_INIT;
109  clrscr(); /* clear the screen */
110 
111  /* information about the specified console screen buffer*/
112  bSuccess = GetConsoleScreenBufferInfo(ocon, &csbi);
113 
114  dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
115 
116  /* set the buffer's attributes */
117  bSuccess = FillConsoleOutputAttribute(ocon, csbi.wAttributes,
118  dwConSize, coordScreen, &cCharsWritten);
119 
120 #endif
121 }
122 
123 
124 /****************************************************************************/
125 /* CON_END */
126 /*--------------------------------------------------------------------------*/
127 /* Close the console */
128 /****************************************************************************/
129 
130 void con_end()
131 {
132 #ifdef LINUX
133 
134  endwin();
135 
136 #endif
137 }
138 
139 
140 /******************************************************************************/
141 /* WRITE_LOG */
142 /*----------------------------------------------------------------------------*/
143 /* parameters: msg -> log message text */
144 /*----------------------------------------------------------------------------*/
145 /* Write a messege in the log file */
146 /******************************************************************************/
147 
148 void write_log(char *msg)
149 {
150  if(log_file == NULL)
151  {
152  log_file = fopen(LOG_FILE_NAME,"w"); /* open log file */
153  if(log_file == NULL)
154  {
155  printf("\n\nCan't open log file\n\n");
156  exit(0);
157  }
158  }
159  fprintf(log_file,"%s\n",msg); /* write the error message in the log file */
160  fflush(stdout); /* empty the output buffer */
161 }
162 
163 
164 /******************************************************************************/
165 /* CON_GETCH */
166 /*----------------------------------------------------------------------------*/
167 /* return: c -> ascii code of pressed key */
168 /*----------------------------------------------------------------------------*/
169 /* Get a char from the console without echoing */
170 /* return the character read */
171 /******************************************************************************/
172 
173 int con_getch(void)
174 {
175 #ifdef LINUX
176 
177  int i;
178 
179  while( ( i = getch() ) == ERR );
180  return i;
181 
182 #else
183 
184  return _getch();
185 
186 #endif
187 }
188 
189 
190 /******************************************************************************/
191 /* CON_KBHIT */
192 /*----------------------------------------------------------------------------*/
193 /* return: 0 -> no key pressed */
194 /* c -> ascii code of pressed key */
195 /*----------------------------------------------------------------------------*/
196 /* Read the standard input buffer; if it is empty, return 0 else read and */
197 /* return the ascii code of the first char in the buffer. A call to this */
198 /* function doesn't stop the program if the input buffer is empty. */
199 /******************************************************************************/
200 
201 char con_kbhit()
202 {
203 #ifdef LINUX
204 
205  int i, g;
206 
207  nodelay(stdscr, TRUE);
208  i = ( ( g = getch() ) == ERR ? 0 : g );
209  nodelay(stdscr, FALSE);
210 
211  return i;
212 
213 #else
214 
215  char c=0;
216 
217  if(kbhit())
218  c = (char)con_getch();
219  return(c);
220 
221 #endif
222 }
223 
224 
225 /******************************************************************************/
226 /* CON_SCANF */
227 /*----------------------------------------------------------------------------*/
228 /* parameters: fmt -> format-control string */
229 /* app -> argument */
230 /*----------------------------------------------------------------------------*/
231 /* return: number of fields that were successfully converted and */
232 /* assigned (0 -> no fields were assigned) */
233 /*----------------------------------------------------------------------------*/
234 /* Read formatted data from the console into the locations given by argument */
235 /******************************************************************************/
236 
237 int con_scanf(char *fmt, void *app)
238 {
239 #ifdef LINUX
240 
241  int i;
242 
243  echo();
244  i = scanw(fmt,app);
245  refresh();
246  noecho();
247  return i;
248 
249 #else
250 
251  int res;
252 
253  res = _cscanf(fmt, app) ;
254 
255  con_kbhit() ; /* Due to input reading problem */
256 
257  return res;
258 
259 #endif
260 }
261 
262 
263 /******************************************************************************/
264 /* CON_PRINTF */
265 /*----------------------------------------------------------------------------*/
266 /* parameters: fmt -> format string: must contain specifications */
267 /* that determine the output format for the */
268 /* argument */
269 /*----------------------------------------------------------------------------*/
270 /* return: number of characters printed */
271 /* or a negative value if an error occurs */
272 /*----------------------------------------------------------------------------*/
273 /* Print formatted output to the standard output stream */
274 /******************************************************************************/
275 
276 int con_printf(char *fmt,...)
277 {
278 #ifdef LINUX
279 
280  va_list marker;
281  int i;
282 
283  va_start(marker,fmt);
284  i = vwprintw(stdscr,fmt,marker);
285  va_end(marker);
286 
287  refresh();
288  return i;
289 
290 #else
291 
292  va_list marker;
293  char buf[256];
294 
295  va_start(marker, fmt); /* Initialize variable arguments. */
296  vsprintf(buf,fmt,marker);
297  va_end(marker); /* Reset variable arguments. */
298 
299  fflush(stdout); /* Empty the output buffer */
300 
301  return _cprintf(buf);
302 
303 #endif
304 }
305 
306 
307 /****************************************************************************/
308 /* GOTOXY */
309 /*--------------------------------------------------------------------------*/
310 /* parameters: x, y -> position on the screen */
311 /*--------------------------------------------------------------------------*/
312 /* Place the cursor at position x,y on the screen */
313 /****************************************************************************/
314 
315 void gotoxy(int x, int y)
316 {
317 #ifdef LINUX
318 
319  move(y-1, x-1);
320  refresh();
321 
322 #else
323 
324  COORD coord;
325 
326  coord.X = x-1;
327  coord.Y = y-1;
328  SetConsoleCursorPosition(ocon,coord);
329 
330 #endif
331 }
332 
333 
334 /****************************************************************************/
335 /* CON_PRINTF_XY */
336 /*--------------------------------------------------------------------------*/
337 /* parameters: xpos, ypos -> position on the screen */
338 /* msg -> message text */
339 /*--------------------------------------------------------------------------*/
340 /* return: number of characters printed */
341 /* or a negative value if an error occurs */
342 /*--------------------------------------------------------------------------*/
343 /* Print formatted output on the X,Y screen position */
344 /****************************************************************************/
345 
346 int con_printf_xy(uint xpos, uint ypos, char *fmt,...)
347 {
348 #ifdef LINUX
349 
350  va_list marker;
351  int i;
352 
353  move(ypos-1, xpos-1);
354  refresh();
355 
356  va_start(marker,fmt);
357  i = vwprintw(stdscr,fmt,marker);
358  va_end(marker);
359 
360  refresh();
361  return i;
362 
363 #else
364 
365  va_list marker;
366  char buf[256];
367 
368  gotoxy(xpos, ypos); /* Set the cursor position */
369 
370  va_start(marker, fmt); /* Initialize variable arguments. */
371  vsprintf(buf,fmt,marker);
372  va_end(marker); /* Reset variable arguments. */
373 
374  fflush(stdout); /* Empty the output buffer */
375 
376  return _cprintf(buf);
377 
378 #endif
379 }
380 
381 
382 /****************************************************************************/
383 /* CLRSCR */
384 /*--------------------------------------------------------------------------*/
385 /* Clear the screen */
386 /****************************************************************************/
387 
388 void clrscr(void)
389 {
390 #ifdef LINUX
391 
392  clear();
393  move(0,0);
394  refresh();
395 
396 #else
397 
398  COORD coordScreen = { 0, 0 }; /* home of the cursor */
399  CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
400  BOOL bSuccess;
401  DWORD cCharsWritten;
402  DWORD dwConSize; /* number of character cells in the current buffer */
403 
404  /* information about the specified console screen buffer*/
405  bSuccess = GetConsoleScreenBufferInfo(ocon, &csbi);
406 
407  dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
408 
409  /* fill the entire screen with blanks */
410  bSuccess = FillConsoleOutputCharacter(ocon, (TCHAR) ' ',
411  dwConSize, coordScreen, &cCharsWritten);
412 
413  /* set the buffer's attributes */
414  bSuccess = FillConsoleOutputAttribute(ocon, csbi.wAttributes,
415  dwConSize, coordScreen, &cCharsWritten);
416 
417  /* put the cursor at (0, 0) */
418  bSuccess = SetConsoleCursorPosition(ocon, coordScreen);
419 
420 #endif
421 }
422 
423 
424 /****************************************************************************/
425 /* CLEAR_LINE */
426 /*--------------------------------------------------------------------------*/
427 /* parameters: line -> line to clear */
428 /*--------------------------------------------------------------------------*/
429 /* Clear a line of the screen */
430 /****************************************************************************/
431 
432 void clear_line(uint line)
433 {
434 #ifdef LINUX
435 
436  gotoxy(1, line);
437  clrtoeol();
438 
439 #else
440 
441  COORD coordScreen;
442  CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
443  BOOL bSuccess;
444  DWORD cCharsWritten;
445  DWORD dwLineSize; /* number of character cells in the line to clear */
446  DWORD dwConSize; /* number of character cells in the current buffer */
447 
448  /* information about the specified console screen buffer*/
449  bSuccess = GetConsoleScreenBufferInfo(ocon, &csbi);
450 
451  dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
452  dwLineSize = csbi.dwSize.X;
453 
454  coordScreen.X=0;
455  coordScreen.Y=line-1;
456 
457  /* fill the entire screen with blanks */
458  bSuccess = FillConsoleOutputCharacter(ocon, (TCHAR) ' ',
459  dwLineSize, coordScreen, &cCharsWritten);
460 
461  coordScreen.X=0;
462  coordScreen.Y=csbi.dwCursorPosition.Y;
463 
464  /* set the buffer's attributes */
465  bSuccess = FillConsoleOutputAttribute(ocon, csbi.wAttributes,
466  dwConSize, coordScreen, &cCharsWritten);
467 
468 #endif
469 }
470 
471 
472 /****************************************************************************/
473 /* DELAY */
474 /*--------------------------------------------------------------------------*/
475 /* parameters: del -> delay in millisecond */
476 /*--------------------------------------------------------------------------*/
477 /* Wait msec milliseconds */
478 /****************************************************************************/
479 
480 void delay(int msec)
481 {
482 #ifdef LINUX
483 
484  usleep(msec*1000);
485 
486 #else
487 
488  Sleep(msec);
489 
490 #endif
491 }
492 
493 
494