source: trunk/MFCtooling/replay/replay.cpp

Last change on this file was 169, checked in by sherbold, 13 years ago

+ replay.exe now return 1 in case of errors, e.g., failures parsing the replay XML

File size: 4.0 KB
Line 
1// replay.cpp : Defines the entry point for the console application.
2//
3
4#include "stdafx.h"
5
6#include "LogParser.h"
7#include "SAXContentHandlerImpl.h"
8#include <iostream>
9
10#include "options.h"
11
12const char * optv[] = {
13        "r:resultfile <string>",
14        "d:msgdelay <number>",
15        "w:wait <number>",
16        "s:startdelay <number>",
17        "p:path <string>"
18};
19
20void convertTCharToChar(const TCHAR * source, char ** dest) {
21#ifdef UNICODE
22        std::wstring wstr(source);
23    int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
24    std::string strTo( size_needed, 0 );
25    WideCharToMultiByte                  (CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
26        *dest = new char[strTo.size()+1];
27        memcpy((void*)*dest, strTo.c_str(), strTo.size()+1);
28#else
29        std::string str(source);
30        *dest = new char[strTo.size()+1];
31        memcpy(*dest, strTo.c_str(), strTo.size()+1);
32#endif
33}
34
35void convertCharToTChar(const char * source, TCHAR ** dest) {
36#ifdef UNICODE
37        std::string str(source);
38        int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
39        std::wstring wstrTo( size_needed, 0 );
40        MultiByteToWideChar                  (CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
41        *dest = new TCHAR[wstrTo.size()+1];
42        memcpy((void*)*dest, wstrTo.c_str(), (wstrTo.size()+1)*sizeof(TCHAR));
43#else
44        std::string str(source);
45        *dest = new char[strTo.size()+1];
46        memcpy(*dest, strTo.c_str(), strTo.size()+1);
47#endif
48}
49
50int _tmain(int argc, _TCHAR* argv[])
51{
52        if( argc < 3 ) {
53                std::wcout << L"Usage: " << argv[0] << L"<replayfile> <applicationundertest> [-r <resultfile>] [-d <useDefaultDelay>] [-w <waitAfterFinish>] [-p <workingPath>] [-s <startupDelay>]" << std::endl;
54                return 1;
55        }
56
57        int optchar = 0;
58        const char * optarg;
59        // set default values
60        TCHAR * resultfile = NULL;
61        int msgdelay = 0;
62        int startdelay = 5000;
63        int wait = 1;
64        TCHAR * workingPath = NULL;
65
66        int argc_char = argc-2;
67        char ** argv_char = new char*[argc_char];
68
69        // convert argv to char*[]
70        convertTCharToChar(argv[0], &argv_char[0]);
71        for( int i=3; i<argc; i++ ) {
72                convertTCharToChar(argv[i], &argv_char[i-2]);
73        }
74
75        // parse options
76        Options opts(*argv_char, optv);
77        OptArgvIter iter(--argc_char, ++argv_char);
78        while( optchar = opts(iter, optarg) ) {
79                switch(optchar) {
80                        case 'r':
81                                convertCharToTChar(optarg, &resultfile);
82                                break;
83                        case 'd':
84                                msgdelay = atoi(optarg);
85                                break;
86                        case 'w':
87                                wait = atoi(optarg);
88                                break;
89                        case 'p':
90                                convertCharToTChar(optarg, &workingPath);
91                                break;
92                        case 's':
93                                startdelay = atoi(optarg);
94                                break;
95                        default:
96                                break;
97                }
98        }
99        for( int i=0; i<argc_char; i++ ) {
100                delete[] argv_char[i];
101        }
102        //delete[] argv_char; //TODO does not work?!
103
104        TCHAR * replayfile = argv[1];
105        TCHAR * appUnderTest = argv[2];
106
107        // initialize COM library for the current thread
108        CoInitialize(NULL);
109        MSXML2::ISAXXMLReader* pXMLReader = NULL;
110
111        // create an instance of the XML reader
112        HRESULT hr = CoCreateInstance(
113                __uuidof(MSXML2::SAXXMLReader),
114                NULL,
115                CLSCTX_ALL,
116                __uuidof(MSXML2::ISAXXMLReader),
117                (void **)&pXMLReader);
118
119        if( !FAILED(hr) ) {
120                TestResults results(replayfile);
121                std::wcout << L"replaying sessions in " << argv[1] << std::endl;
122                LogParser * parser = new LogParser(appUnderTest, startdelay, &results, (bool) msgdelay);
123                parser->setWorkingPath(workingPath);
124                pXMLReader->putContentHandler(parser);
125                hr = pXMLReader->parseURL(replayfile);
126                if( hr!=S_OK ) {
127                        std::wcout << L"failure parsing XML file" << std::endl;
128                        return 1;
129                }
130                pXMLReader->Release();
131                std::wcout << L"================================================" << std::endl;
132                std::wcout << L"replay completed" << std::endl;
133                if( resultfile!=NULL ) {
134                        results.write(resultfile);
135                        std::wcout << L"results written to " << resultfile << std::endl;
136                }
137        }
138
139        CoUninitialize();
140       
141        if( wait!=0 ) {
142                std::wcout << L"press enter to exit ...";
143                getchar();
144        }
145
146        delete resultfile;
147
148        return 0;
149}
150
Note: See TracBrowser for help on using the repository browser.