Standardize License information
[openafs.git] / src / WINNT / client_exp / msgs.cpp
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 extern "C" {
11 #include <afs/param.h>
12 #include <afs/stds.h>
13 }
14
15 #include <string.h>
16 #include <stdarg.h>
17
18 #include "stdafx.h"
19 #include "msgs.h"
20
21
22
23 /* 
24         ShowMessageBox:
25                 
26                 This function takes three main arguements, the stringtable ID, the button types
27                 to be displayed (default = MB_OK) and the help table reference (default = 0, no 
28                 help) and then a variable amount of arguements. The variable list does not need 
29                 a special ending flag/character/number. The list is read only as needed, which 
30                 is defined by the string table and the presence of any "%X" characters, where X 
31                 is one of the printf format types. The order of the variable list MUST 
32                 correspond to the order of types in the string table entry. If the string table 
33                 calls for INT INT UINT CHAR* DOUBLE, then the arguement list had better be INT 
34                 INT UINT CHAR* DOUBLE or else there will be serious problems (stack will be 
35                 misread, general protection faults, garbage output, and other errors).
36                 
37                 This function takes the arguements passed in the list and inserts them by 
38                 parsing and pszcut/pszpaste the string table entry to add all the arguements passed 
39                 in. This allows for any generic message to be created.
40                 
41                 %i,d    = Integer
42                 %u              = unsigned int
43                 %x,X    = Hex (takes an integer arguement, pszconverts it)
44                 %g,f,e  = Double
45                 %s              = String (char*)
46                 %l
47                         d       = Long int
48                         x       = long hex
49                 %c              = character (one)
50                 %a              = String Table Ref. (UINT)
51                 %o              = CString object (prints the string of the object)
52                 default = prints out string so far, with error message attached at place of error.
53         
54         Return type is the button pressed in the message box.
55  
56 */
57
58 UINT ShowMessageBox (UINT Id, UINT Button, UINT Help, ...) {
59
60         CString 
61                  temp;
62         char *pszstring, 
63                  *pszpaste, 
64                  *pszcut, 
65                  *pszdone,
66                  *pszconvert;
67         char
68                  chread;
69         va_list 
70                  params;
71         int 
72                  x;
73                    
74         pszconvert = new char[255];     
75         va_start(params, Help);
76         LoadString (temp, Id);
77         pszstring = temp.GetBuffer(512);
78         strcpy(pszstring,pszstring);
79         temp.ReleaseBuffer();
80                         // Look and see - is there a need to insert chars (95% of the time, there won't)
81                 if (!strstr(pszstring, "%")) {
82                         delete pszconvert;
83                         return AfxMessageBox(pszstring, Button, Help);
84                 }   
85                 
86         x = strcspn(pszstring, "%");
87         pszdone = new char[512];
88         pszcut = new char[512];
89         pszpaste = new char[512];
90         strcpy(pszcut, &pszstring[x+2]);
91         strncpy(pszpaste, pszstring, x);
92         pszpaste[x] = '\0';
93         chread = pszstring[x+1];
94         
95                 for ( ; ; ) {
96
97                                 switch (chread) { 
98                                         case    'i' :
99                                         case    'd' :
100                                                                 {           
101                                                                 int anint = va_arg(params, int);
102                                                                 _itoa( anint, pszconvert, 10);
103                                                                 break;
104                                                                 }
105                                         case    'u' :
106                                                                 {       
107                                                                 UINT anuint = va_arg(params, UINT);
108                                                                 _itoa( anuint, pszconvert, 10);
109                                                                 break;
110                                                                 }
111                                         
112                                         case    'x' :
113                                         case    'X' :   
114                                                                 {
115                                                                 int ahex = va_arg(params, int);
116                                                                 _itoa( ahex, pszconvert, 16);
117                                                                 break;
118                                                                 }
119                                         case    'g' :
120                                         case    'f' :
121                                         case    'e' :   
122                                                                 {
123                                                                 double adbl = va_arg(params, double);
124                                                                 _gcvt( adbl, 10, pszconvert);
125                                                                 break;
126                                                                 }
127                                         case    's' :   
128                                                                 {
129                                                                 char *pStr = va_arg(params, char*);
130                                                                 ASSERT(strlen(pStr) <= 255);
131                                                                 strcpy(pszconvert, pStr);
132                                                         break;
133                                                         }
134                                         case    'l' :   
135                                                                 {
136                                                                 chread = pszdone[x+2];
137                                                                         switch(chread) {
138                                                                                 case    'x'     :
139                                                                                                         {
140                                                                                                         long int alhex = va_arg(params, long int);
141                                                                                                         _ltoa(alhex, pszconvert, 16);
142                                                                                                         strcpy(pszcut, &pszcut[1]);
143                                                                                                         break;
144                                                                                                         }
145                                                                                 case    'd'     :
146                                                                                 default         :
147                                                                                                     {
148                                                                                                         long int along = va_arg(params, long int);
149                                                                                                         _ltoa( along, pszconvert, 10);
150                                                                                                         // For the L, there will be one character after it,
151                                                                                                         //   so move ahead another letter
152                                                                                                         strcpy(pszcut, &pszcut[1]);
153                                                                                                         break;
154                                                                                                         }
155                                                                         }
156                                                                 break;
157                                                                 }
158
159                                         case    'c' :   
160                                                                 {
161                                                                 int letter = va_arg(params, int);
162                                 pszconvert[0] = (char)letter;
163                                 pszconvert[1] = '\0'; 
164                                                                 break;
165                                                                 }
166                                         case    'a'     :
167                                                                 {
168                                                                 CString zeta;
169                                                                 char* lsc;
170                                                                 UINT ls = va_arg(params, UINT);
171                                                                 LoadString (zeta, ls);
172                                                                 lsc = zeta.GetBuffer(255);
173                                                                 strcpy(pszconvert, lsc);
174                                                                 zeta.ReleaseBuffer();
175                                                                 break;
176                                                                 }
177                                         case    'o'     :
178                                                                 {
179                                                                 CString get = va_arg(params, CString);
180                                                                 char* ex = get.GetBuffer(255);
181                                                                 strcpy(pszconvert,ex);
182                                                                 get.ReleaseBuffer();
183                                                                 break;
184                                                                 }
185                                         default         :
186                                                                 {       
187                                                                 strcpy(pszconvert, " Could not load message. Invalid %type in string table entry. ");
188                                                                 delete pszdone;
189                                                                 pszdone = new char[strlen(pszpaste)+strlen(pszcut)+strlen(pszconvert)+5];
190                                                                 strcpy(pszdone, pszpaste);
191                                                                 strcat(pszdone, pszconvert);
192                                                                 strcat(pszdone, pszcut);
193                                                                 AfxMessageBox(pszdone, Button, Help);
194                                                                 delete pszcut;
195                                                                 delete pszpaste;
196                                                                 delete pszconvert;
197                                                                 delete pszdone;
198                                                                 ASSERT(FALSE);
199                                                                 return 0;
200                                                                 }               
201                                 } // case
202                 
203                 delete pszdone;
204                         pszdone = new char[strlen(pszpaste)+strlen(pszcut)+strlen(pszconvert)+5];
205                         strcpy(pszdone, pszpaste);
206                         strcat(pszdone, pszconvert);
207                         strcat(pszdone, pszcut);
208                         // Now pszdone holds the entire message.
209                         // Check to see if there are more insertions to be made or not
210         
211                                 if (!strstr(pszdone, "%"))      {
212                                         UINT rt_type = AfxMessageBox(pszdone, Button, Help);
213                                         delete pszcut;
214                                         delete pszpaste;
215                                         delete pszconvert;
216                                         delete pszdone;
217                                         return rt_type;
218                                 } // if
219                                 
220                         // there are more insertions to make, prepare the strings to use.
221                         x = strcspn(pszdone, "%");
222                         strcpy(pszcut, &pszdone[x+2]);
223                         strncpy(pszpaste, pszdone, x); 
224             pszpaste[x] = '\0';
225             chread = pszdone[x+1];
226         
227                 } // for
228         ASSERT(FALSE);          
229         return 0;
230
231 } // ShowMessageBox
232
233 CString GetMessageString(UINT Id, ...)
234 {
235         CString 
236                  temp;
237         char *pszstring, 
238                  *pszpaste, 
239                  *pszcut, 
240                  *pszdone,
241                  *pszconvert;
242         char
243                  chread;
244         va_list 
245                  params;
246         int 
247                  x;
248         CString strMsg;
249
250         pszconvert = new char[255];     
251         va_start(params, Id);
252         LoadString (temp, Id);
253         pszstring = temp.GetBuffer(512);
254         strcpy(pszconvert,pszstring);
255         temp.ReleaseBuffer();
256
257         // Look and see - is there a need to insert chars (95% of the time, there won't)
258         if (!strstr(pszstring, "%")) {
259                 strMsg = pszconvert;
260                 delete pszconvert;
261                 return strMsg;
262         }   
263                 
264         x = strcspn(pszstring, "%");
265         pszdone = new char[512];
266         pszcut = new char[512];
267         pszpaste = new char[512];
268         strcpy(pszcut, &pszstring[x+2]);
269         strncpy(pszpaste, pszstring, x);
270         pszpaste[x] = '\0';
271         chread = pszstring[x+1];
272         
273                 for ( ; ; ) {
274
275                                 switch (chread) { 
276                                         case    'i' :
277                                         case    'd' :
278                                                                 {           
279                                                                 int anint = va_arg(params, int);
280                                                                 _itoa( anint, pszconvert, 10);
281                                                                 break;
282                                                                 }
283                                         case    'u' :
284                                                                 {       
285                                                                 UINT anuint = va_arg(params, UINT);
286                                                                 _itoa( anuint, pszconvert, 10);
287                                                                 break;
288                                                                 }
289                                         
290                                         case    'x' :
291                                         case    'X' :   
292                                                                 {
293                                                                 int ahex = va_arg(params, int);
294                                                                 _itoa( ahex, pszconvert, 16);
295                                                                 break;
296                                                                 }
297                                         case    'g' :
298                                         case    'f' :
299                                         case    'e' :   
300                                                                 {
301                                                                 double adbl = va_arg(params, double);
302                                                                 _gcvt( adbl, 10, pszconvert);
303                                                                 break;
304                                                                 }
305                                         case    's' :   
306                                                                 {
307                                                                 char *pStr = va_arg(params, char*);
308                                                                 ASSERT(strlen(pStr) <= 255);
309                                                                 strcpy(pszconvert, pStr);
310                                                         break;
311                                                         }
312                                         case    'l' :   
313                                                                 {
314                                                                 chread = pszdone[x+2];
315                                                                         switch(chread) {
316                                                                                 case    'x'     :
317                                                                                                         {
318                                                                                                         long int alhex = va_arg(params, long int);
319                                                                                                         _ltoa(alhex, pszconvert, 16);
320                                                                                                         strcpy(pszcut, &pszcut[1]);
321                                                                                                         break;
322                                                                                                         }
323                                                                                 case    'd'     :
324                                                                                 default         :
325                                                                                                     {
326                                                                                                         long int along = va_arg(params, long int);
327                                                                                                         _ltoa( along, pszconvert, 10);
328                                                                                                         // For the L, there will be one character after it,
329                                                                                                         //   so move ahead another letter
330                                                                                                         strcpy(pszcut, &pszcut[1]);
331                                                                                                         break;
332                                                                                                         }
333                                                                         }
334                                                                 break;
335                                                                 }
336
337                                         case    'c' :   
338                                                                 {
339                                                                 int letter = va_arg(params, int);
340                                 pszconvert[0] = (char)letter;
341                                 pszconvert[1] = '\0'; 
342                                                                 break;
343                                                                 }
344                                         case    'a'     :
345                                                                 {
346                                                                 CString zeta;
347                                                                 char* lsc;
348                                                                 UINT ls = va_arg(params, UINT);
349                                                                 LoadString (zeta, ls);
350                                                                 lsc = zeta.GetBuffer(255);
351                                                                 strcpy(pszconvert, lsc);
352                                                                 zeta.ReleaseBuffer();
353                                                                 break;
354                                                                 }
355                                         case    'o'     :
356                                                                 {
357                                                                 CString get = va_arg(params, CString);
358                                                                 char* ex = get.GetBuffer(255);
359                                                                 strcpy(pszconvert,ex);
360                                                                 get.ReleaseBuffer();
361                                                                 break;
362                                                                 }
363                                         default         :
364                                                                 {       
365                                                                 strcpy(pszconvert, " Could not load message. Invalid %type in string table entry. ");
366                                                                 delete pszdone;
367                                                                 pszdone = new char[strlen(pszpaste)+strlen(pszcut)+strlen(pszconvert)+5];
368                                                                 strcpy(pszdone, pszpaste);
369                                                                 strcat(pszdone, pszconvert);
370                                                                 strcat(pszdone, pszcut);
371                                                                 strMsg = pszdone;
372                                                                 delete pszcut;
373                                                                 delete pszpaste;
374                                                                 delete pszconvert;
375                                                                 delete pszdone;
376                                                                 ASSERT(FALSE);
377                                                                 return strMsg;
378                                                                 }               
379                                 } // case
380                 
381                 delete pszdone;
382                         pszdone = new char[strlen(pszpaste)+strlen(pszcut)+strlen(pszconvert)+5];
383                         strcpy(pszdone, pszpaste);
384                         strcat(pszdone, pszconvert);
385                         strcat(pszdone, pszcut);
386                         // Now pszdone holds the entire message.
387                         // Check to see if there are more insertions to be made or not
388         
389                                 if (!strstr(pszdone, "%"))      {
390                                         strMsg = pszdone;
391                                         delete pszcut;
392                                         delete pszpaste;
393                                         delete pszconvert;
394                                         delete pszdone;
395                                         return strMsg;
396                                 } // if
397                                 
398                         // there are more insertions to make, prepare the strings to use.
399                         x = strcspn(pszdone, "%");
400                         strcpy(pszcut, &pszdone[x+2]);
401                         strncpy(pszpaste, pszdone, x); 
402             pszpaste[x] = '\0';
403             chread = pszdone[x+1];
404         
405                 } // for
406         ASSERT(FALSE);          
407         return strMsg;
408 }
409
410 void LoadString (CString &Str, UINT id)
411 {
412    TCHAR szString[ 256 ];
413    GetString (szString, id);
414    Str = szString;
415 }
416