summaryrefslogtreecommitdiff
path: root/src/InterfaceGraphic/InterfaceGraphic_cPrintf.cxx
blob: a0d660c019ca3e2d88d9fa16c34e28c1b8787041 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

#if defined (__osf__ ) || defined ( DECOSF1 )
int DECOSF1_a_horreur_du_vide_a_la_compilation;
#endif
#if defined ( __hpux ) || defined ( HPUX ) 
int HPUX_a_horreur_du_vide_a_la_compilation;
#endif

#ifdef WNT

#define STRICT
#include <windows.h>

#include <stdio.h>
#include <string.h>

#include <InterfaceGraphic_wntio.hxx>

typedef struct _env {

  DWORD len;
  char* ptr;

} ENV, *PENV;

static DWORD tlsIndex;

class Init_ {

public:

  Init_ () { tlsIndex = TlsAlloc (); }

}; // end Init_

static Init_ init;

int cPrintf ( const char* fmt, ... ) {

  static BOOL   first = TRUE;
  static HANDLE hConsole = NULL;

  char    buffer[ 256 ];
  va_list argptr;
  int     cnt;
  DWORD   lpcchWritten;

  if ( first ) {
    hConsole = GetStdHandle ( STD_OUTPUT_HANDLE );
    if ( hConsole == NULL ) {
      AllocConsole ();
      hConsole = GetStdHandle ( STD_OUTPUT_HANDLE );
    }  /* end if */

    first = FALSE;

  }  /* end if */

  va_start( argptr, fmt );
  cnt = vsprintf ( buffer, fmt, argptr ); 
  va_end ( argptr );
  WriteConsole ( hConsole, buffer, strlen ( buffer ), &lpcchWritten, NULL );
  return cnt;

}  /* end cPrintf */

int fcPrintf ( int dummy, const char* fmt, ... ) {

  static BOOL   first = TRUE;
  static HANDLE hConsole = NULL;

  char    buffer[ 256 ];
  va_list argptr;
  int     cnt;
  DWORD   lpcchWritten;

  if ( first ) {
    hConsole = GetStdHandle ( STD_OUTPUT_HANDLE );
    if ( hConsole == NULL ) {
      AllocConsole ();
      hConsole = GetStdHandle ( STD_OUTPUT_HANDLE );
    }  /* end if */

    first = FALSE;
  }  /* end if */

  va_start( argptr, fmt );
  cnt = vsprintf ( buffer, fmt, argptr ); 
  va_end ( argptr );
  WriteConsole ( hConsole, buffer, strlen ( buffer ), &lpcchWritten, NULL );

  return cnt;

}  /* end fcPrintf */

char* GetEnv ( const char* name ) {

  DWORD dwLen;
  PENV  env = ( PENV )TlsGetValue ( tlsIndex );

  if ( env == NULL ) {
    env = ( PENV )HeapAlloc (
      GetProcessHeap (), HEAP_GENERATE_EXCEPTIONS | HEAP_ZERO_MEMORY,
      ( DWORD )sizeof ( ENV )
      );
    TlsSetValue (  tlsIndex, ( LPVOID )env  );
  }  /* end if */

  SetLastError ( ERROR_SUCCESS );
  dwLen = GetEnvironmentVariable ( name, NULL, 0 );

  if (  dwLen == 0 && GetLastError () != ERROR_SUCCESS  ) return NULL;

  ++dwLen;

  if ( env -> len < dwLen ) {
    if ( env -> ptr != NULL )
      env -> ptr = ( char* )HeapReAlloc (
      GetProcessHeap (), HEAP_GENERATE_EXCEPTIONS | HEAP_ZERO_MEMORY,
      env -> ptr, dwLen
      );
    else
      env -> ptr = ( char* )HeapAlloc (
      GetProcessHeap (), HEAP_GENERATE_EXCEPTIONS | HEAP_ZERO_MEMORY,
      dwLen
      );
    env -> len = dwLen;
  }  /* end if */

  GetEnvironmentVariable ( name, env -> ptr, dwLen );
  return env -> ptr;

}  /* end GetEnv */
#endif /* WNT */