summaryrefslogtreecommitdiff
path: root/ACE/contrib/FaCE/FaCE.cpp
blob: bbe83ebca91f0a97d956e78b9d2739f7b2ff618d (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
#include "FaCE.h"

#ifdef NO_ACE

#include "CE_ARGV.h"

#else

#include <ace/ACE.h>
#include <ace/ARGV.h>
#include <ace/Log_Msg.h>

#endif  // NO_ACE

#include <commctrl.h>
#include <aygshell.h>
#include <sipapi.h>

// This utility does not use ACE, and shouldn't.
//FUZZ: disable check_for_lack_ACE_OS

const ACE_TCHAR* g_ParameterFileName = ACE_TEXT("Parameters.txt");

/**
 * This simple and small class manages user-input command line
 * parameters and parameter history file.
 *
 * @author Si Mong Park (spark@ociweb.com)
 */
class ParameterList
{
public:
    /**
     * Default Ctor.
     */
    ParameterList() : next_(0), param_(0) {};

    /**
     * Dtor: deletes all sub-PameterList objects as well as
     *       memory block allocated for the param_ by _wcsdup().
     */
    ~ParameterList() { free(param_); delete next_; };

    /**
     * Add a new parameter to the list.
     */
    void addParameter(char*);

    /**
     * Add a new parameter to the list.
     */
    void addParameter(ACE_TCHAR*);

    /**
     * Save all parameters stored in the list to the
     * file.
     * Note that 'outputFile' is only for the internal use
     * and user must call this function without any parameter.
     */
    void saveParameter(FILE* outputFile = 0);

    /**
     * Send out windows message to load/update parameters.
     */
    void sendParameterMSG(HWND, UINT);

private:
    /**
     * A pointer to the next ParameterList object.
     * This attribute is totally hidden from user.
     */
    ParameterList* next_;

    /**
     * User-specified command line parameter.
     * This attribute is totally hidden from user.
     */
    ACE_TCHAR* param_;
};


void ParameterList::addParameter(char* newParameter)
{
#ifdef NO_ACE
    int len = MultiByteToWideChar(CP_OEMCP, MB_PRECOMPOSED, newParameter, -1, 0, 0);
    wchar_t* w_output = new wchar_t[len];

    MultiByteToWideChar(CP_OEMCP, MB_PRECOMPOSED, newParameter, -1, w_output, len);
    this->addParameter(w_output);

    delete w_output;
#else
    this->addParameter(ACE_TEXT_CHAR_TO_TCHAR(newParameter));
#endif  // NO_ACE
}


void ParameterList::addParameter(ACE_TCHAR* newParameter)
{
    if (this->param_ == 0) {
        this->param_ = _wcsdup(newParameter);
        this->next_ = new ParameterList();  // create and add a new ParameterList object
    }
    else {
        if (wcscmp(this->param_, newParameter) != 0) {
            this->next_->addParameter(newParameter);
        }
    }
}


void ParameterList::saveParameter(FILE* outputFile)
{
    if ( (outputFile == 0) && (this->param_ != 0) ) {
        outputFile = _wfopen(g_ParameterFileName, ACE_TEXT("w+"));
    }

    if (outputFile != 0) {
        if (this->param_ != 0) {
            fwprintf(outputFile, ACE_TEXT("%s\n"), this->param_);
            this->next_->saveParameter(outputFile);
        }
        else {
            fclose(outputFile);
        }
    }
}


void ParameterList::sendParameterMSG(HWND hDlg, UINT message)
{
    if (param_ != 0) {
        SendDlgItemMessage(hDlg, IDC_CMDEDIT, message, 0, (LPARAM)this->param_);
        this->next_->sendParameterMSG(hDlg, message);
    }
}


// Global Variables:
HINSTANCE g_hInst;          // The current instance
HWND      g_hwndCB;         // The command bar handle
HWND      hWndEdit;         // Read only edit box for output display
FILE*     g_OutputFile;     // File handler for output save

ParameterList g_Parameter;  // command line parameter list

ACE_CE_Screen_Output cout;  // Replacement of std::cout

ACE_TCHAR g_CommandLine[MAX_COMMAND_LINE]; // User-specified command line parameter
ACE_TCHAR g_SaveFileName[MAX_LOADSTRING];      // Name of the output file

static SHACTIVATEINFO s_sai;

// Forward declarations of functions included in this code module:
ATOM                MyRegisterClass (HINSTANCE, ACE_TCHAR*);
BOOL                InitInstance    (HINSTANCE, int);
LRESULT CALLBACK    WndProc         (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK    About           (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK    CommandLine     (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK    SaveFileName    (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK    FileError       (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK    FileExist       (HWND, UINT, WPARAM, LPARAM);
HWND                CreateRpCommandBar(HWND);


void InitSetup()
{
    g_OutputFile    = 0;
    memset(g_CommandLine,  0, MAX_COMMAND_LINE * sizeof(ACE_TCHAR));
    memset(g_SaveFileName, 0, MAX_LOADSTRING   * sizeof(ACE_TCHAR));
}


void LoadParameterHistory()
{
    FILE* parameterFile = _wfopen(g_ParameterFileName, ACE_TEXT("r"));

    if (parameterFile != 0) {
        while (feof(parameterFile) == 0) {
            // Note: Remember that fwprintf takes wide-character format specifier but
            // save string as ASCII.  Thus, history must be read as ASCII then converted
            // to wide-character (Unicode on WinCE).
            char singleParameter[MAX_COMMAND_LINE];
            int size = 0;
            fread(&singleParameter[size], sizeof(char), 1, parameterFile);

            // WinCE does not have function that reads upto the end of line.
            while (singleParameter[size] != '\n') {
                fread(&singleParameter[++size], sizeof(char), 1, parameterFile);
            }

            if (size > 0) {
                singleParameter[size] = 0;  // NULL terminator
                g_Parameter.addParameter(singleParameter);
            }
        }
        fclose(parameterFile);
    }
}


//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    It is important to call this function so that the application
//    will get 'well formed' small icons associated with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance, ACE_TCHAR* szWindowClass)
{
    WNDCLASS    wc;

    wc.style            = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc      = (WNDPROC) WndProc;
    wc.cbClsExtra       = 0;
    wc.cbWndExtra       = 0;
    wc.hInstance        = hInstance;
    wc.hIcon            = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_FACE));
    wc.hCursor          = 0;
    wc.hbrBackground    = (HBRUSH) GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName     = 0;
    wc.lpszClassName    = szWindowClass;

    return RegisterClass(&wc);
}

//
//  FUNCTION: InitInstance(HANDLE, int)
//
//  PURPOSE: Saves instance handle and creates main window
//
//  COMMENTS:
//
//    In this function, we save the instance handle in a global variable and
//    create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    HWND hWnd = 0;

    ACE_TCHAR szTitle[MAX_LOADSTRING];            // The title bar text
    ACE_TCHAR szWindowClass[MAX_LOADSTRING];      // The window class name

    g_hInst = hInstance;        // Store instance handle in our global variable
    // Initialize global strings
    LoadString(hInstance, IDC_FACE, szWindowClass, MAX_LOADSTRING);
    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);

    //If it is already running, then focus on the window
    hWnd = FindWindow(szWindowClass, szTitle);
    if (hWnd)
    {
        // set focus to foremost child window
        // The "| 0x01" is used to bring any owned windows to the foreground and
        // activate them.
        SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
        return 0;
    }

    MyRegisterClass(hInstance, szWindowClass);

    RECT    rect;
    GetClientRect(hWnd, &rect);

    hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hInstance, 0);

    if (!hWnd)
    {
        int error = 0;
        error = GetLastError();
        return FALSE;
    }
    //When the main window is created using CW_USEDEFAULT the height of the menubar (if one
    // is created is not taken into account). So we resize the window after creating it
    // if a menubar is present
    {
        RECT rc;
        GetWindowRect(hWnd, &rc);
        rc.bottom -= MENU_HEIGHT;
        if (g_hwndCB)
            MoveWindow(hWnd, rc.left, rc.top, rc.right, rc.bottom, FALSE);
    }

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND  - process the application menu
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    int wmId, wmEvent, nCmdHt;
    PAINTSTRUCT ps;
    RECT textRect;

    switch (message)
    {
        case WM_COMMAND:
            wmId    = LOWORD(wParam);
            wmEvent = HIWORD(wParam);
            // Parse the menu selections:
            switch (wmId)
            {
                case IDM_HELP_ABOUT:
                    DialogBox(g_hInst, (const ACE_TCHAR*)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
                    break;

                case IDOK:
                    SendMessage(hWnd, WM_ACTIVATE, MAKEWPARAM(WA_INACTIVE, 0), (LPARAM)hWnd);
                    SendMessage(hWnd, WM_CLOSE, 0, 0);
                    break;

                case ID_SETTING_RUN:
                    {
#ifdef NO_ACE
                        cout << ACE_TEXT("START with command line: ") << g_CommandLine << endl;
                        CE_ARGV ce_argv(g_CommandLine);
                        main_i(ce_argv.argc(), ce_argv.argv());
                        cout << ACE_TEXT("END") << endl << endl;
#else
                        cout << ACE_TEXT("START with command line: ") << g_CommandLine << endl;
                        ACE_ARGV ce_argv(g_CommandLine);
                        ACE::init();
                        ACE_LOG_MSG->msg_callback(&cout);  // register call back
                        ACE_LOG_MSG->set_flags(ACE_Log_Msg::MSG_CALLBACK);  // set call back flag
                        ace_main_i(ce_argv.argc(), ce_argv.argv());
                        ACE::fini();
                        cout << ACE_TEXT("END") << endl << endl;
#endif  // NO_ACE
                    }
                    break;

                case ID_SETTING_EXIT:
                    SendMessage(hWnd, WM_DESTROY, 0, 0);
                    break;

                case ID_TOOLS_SAVETOFILE:
                    // create a dialog box to get the file name
                    DialogBox(g_hInst, (const ACE_TCHAR*)IDD_OUTFILE, hWnd, (DLGPROC)SaveFileName);
                    break;

                case ID_SETTING_COMMANDLINE:
                    // create a dialog box to get the command line
                    DialogBox(g_hInst, (const ACE_TCHAR*)IDD_CMDLINE, hWnd, (DLGPROC)CommandLine);
                    break;

                default:
                   return DefWindowProc(hWnd, message, wParam, lParam);
            }
            break;

        case WM_CREATE:
            SHMENUBARINFO mbi;

            memset(&mbi, 0, sizeof(SHMENUBARINFO));
            mbi.cbSize     = sizeof(SHMENUBARINFO);
            mbi.hwndParent = hWnd;
            mbi.nToolBarId = IDM_MENU;
            mbi.hInstRes   = g_hInst;
            mbi.nBmpId     = 0;
            mbi.cBmpImages = 0;

            if (!SHCreateMenuBar(&mbi))
                return 0;

            g_hwndCB = mbi.hwndMB;

            // Initialize the shell activate info structure
            memset (&s_sai, 0, sizeof (s_sai));
            s_sai.cbSize = sizeof (s_sai);

            GetClientRect(hWnd, &textRect);
            nCmdHt = CommandBar_Height(mbi.hwndMB);

            hWndEdit = CreateWindow(ACE_TEXT("EDIT"),
                                    0,
                                    WS_CHILD | WS_VISIBLE | ES_READONLY | ES_MULTILINE | WS_VSCROLL | WS_HSCROLL,
                                    0,
                                    0,
                                    textRect.right,
                                    textRect.bottom - MENU_HEIGHT,
                                    hWnd,
                                    0,
                                    g_hInst,
                                    0);
            cout.SetOutputWindow(hWndEdit);
            LoadParameterHistory();
            break;

        case WM_PAINT:
            RECT rt;
            hdc = BeginPaint(hWnd, &ps);
            GetClientRect(hWnd, &rt);
            EndPaint(hWnd, &ps);
            break;

        case WM_ACTIVATE:
            // Notify shell of our activate message
            SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
            break;

        case WM_SETTINGCHANGE:
            SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
            break;

        case WM_HIBERNATE:  // low power
        case WM_CLOSE:
        case WM_DESTROY:
            g_Parameter.saveParameter();  // save parameters to history file
            CommandBar_Destroy(g_hwndCB);
            PostQuitMessage(0);
            break;

        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}


HWND CreateRpCommandBar(HWND hwnd)
{
    SHMENUBARINFO mbi;

    memset(&mbi, 0, sizeof(SHMENUBARINFO));
    mbi.cbSize     = sizeof(SHMENUBARINFO);
    mbi.hwndParent = hwnd;
    mbi.nToolBarId = IDM_MENU;
    mbi.hInstRes   = g_hInst;
    mbi.nBmpId     = 0;
    mbi.cBmpImages = 0;

    if (!SHCreateMenuBar(&mbi))
        return 0;

    return mbi.hwndMB;
}

// Mesage handler for the About box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM)
{
    SHINITDLGINFO shidi;

    const ACE_TCHAR* copyrightNote =
ACE_TEXT("ACE and TAO are copyrighted by Dr. Douglas C. Schmidt and Center for Distributed Object") \
ACE_TEXT("Computing at Washington University, 1993-2002, all rights reserved.")  \
ACE_TEXT("FaCE is copyrighted by Object Computing, Inc., 2002,\n all rights reserved.\n") \
ACE_TEXT("See License.txt for more information.");

    switch (message)
    {
        case WM_INITDIALOG:
            // Create a Done button and size it.
            shidi.dwMask = SHIDIM_FLAGS;
            shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN;
            shidi.hDlg = hDlg;
            SHInitDialog(&shidi);
            SetDlgItemText(hDlg, IDC_COPYRIGHT, copyrightNote);
            return TRUE;

        case WM_COMMAND:
            if (LOWORD(wParam) == IDOK)
            {
                EndDialog(hDlg, LOWORD(wParam));
                return TRUE;
            }
            break;
    }
    return FALSE;
}


LRESULT CALLBACK CommandLine(HWND hDlg, UINT message, WPARAM wParam, LPARAM)
{
    int wmId;
    int wmEvent;

    switch (message)
    {
    case WM_INITDIALOG:
        g_Parameter.sendParameterMSG(hDlg, CB_INSERTSTRING);
        SetDlgItemText(hDlg, IDC_CMDEDIT, g_CommandLine);  // pass existing command line for display
        return TRUE;

    case WM_COMMAND:
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        // Parse the menu selections:
        switch (wmId)
        {
        case IDOK:
            // new command line accepted
            GetDlgItemText(hDlg, IDC_CMDEDIT, g_CommandLine, MAX_COMMAND_LINE - 1);
            EndDialog(hDlg, wmId);
            g_Parameter.addParameter(g_CommandLine);
            return TRUE;

        case IDCANCEL:
            EndDialog(hDlg, wmId);
            return TRUE;

        default:
            return FALSE;
        }
        break;
        default:
            return FALSE;
    }

    return FALSE;
}


LRESULT CALLBACK SaveFileName(HWND hDlg, UINT message, WPARAM wParam, LPARAM)
{
    int wmId;
    int wmEvent;

    ACE_TCHAR tempBuffer[MAX_LOADSTRING];
    ACE_TCHAR fileMode[3] = { 0, '+', 0 };  // mode will either be "a+" or "w+"
    FILE* tempFile;

    switch (message)
    {
    case WM_INITDIALOG:
        SetDlgItemText(hDlg, IDC_SAVEFILE, g_SaveFileName);
        return TRUE;

    case WM_COMMAND:
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        // Parse the menu selections:
        switch (wmId)
        {
        case IDOK:
            GetDlgItemText(hDlg, IDC_SAVEFILE, tempBuffer, MAX_LOADSTRING - 1);
            EndDialog(hDlg, wmId);

            tempFile = _wfopen(tempBuffer, ACE_TEXT("r"));

            if (tempFile != 0)  // if file exists
            {
                fclose(tempFile);  // close temp handler
                int choice = DialogBox(g_hInst, (const ACE_TCHAR*)IDD_FILEEXIST, hDlg, (DLGPROC)FileExist);
                switch (choice)
                {
                case IDOVERWRITE:  // overwrite existing file
                    fileMode[0] = 'w';
                    break;

                case IDC_APPEND:  // append to existing file
                    fileMode[0] = 'a';
                    break;

                case IDCANCEL:  // cancel operation without changing g_OutputFile
                    return TRUE;
                }
            }
            else  // if file does not exist
            {
                fileMode[0] = 'w';
            }

            tempFile = _wfopen(tempBuffer, fileMode);

            if (tempFile == 0)
            {
                DialogBox(g_hInst, (const ACE_TCHAR*)IDD_ERRFILE, hDlg, (DLGPROC)FileError);
            }
            else
            {
                wcscpy(g_SaveFileName, tempBuffer);

                if (g_OutputFile != 0)
                {
                    fclose(g_OutputFile);  // close any open file
                }

                g_OutputFile = tempFile;

                cout << g_OutputFile;  // update FILE* for the CE_Screen_Output class object.
            }

            return TRUE;

        case IDCANCEL:
            EndDialog(hDlg, wmId);
            return TRUE;

        default:
            return FALSE;
        }
        break;
        default:
            return FALSE;
    }

    return FALSE;
}


LRESULT CALLBACK FileError(HWND hDlg, UINT message, WPARAM wParam, LPARAM)
{
    switch (message)
    {
        case WM_INITDIALOG:
            return TRUE;

        case WM_COMMAND:
            if (LOWORD(wParam) == IDOK)
            {
                EndDialog(hDlg, LOWORD(wParam));
                return TRUE;
            }
            break;
    }

    return FALSE;
}


LRESULT CALLBACK FileExist(HWND hDlg, UINT message, WPARAM wParam, LPARAM)
{
    switch (message)
    {
    case WM_INITDIALOG:
        return TRUE;
    case WM_COMMAND:
        EndDialog(hDlg, LOWORD(wParam));
        return TRUE;
    default:
        return FALSE;
    }

    return FALSE;
}

//FUZZ: enable check_for_lack_ACE_OS