source: trunk/MFCtooling/replay/replay.cpp @ 163

Last change on this file since 163 was 163, checked in by sherbold, 13 years ago
  • optional parameter -p now sets the working directory of the application under test
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                return -1;
54        }
55
56        int optchar;
57        const char * optarg;
58        // set default values
59        TCHAR * resultfile = NULL;
60        int msgdelay = 0;
61        int startdelay = 5000;
62        int wait = 1;
63        TCHAR * workingPath = NULL;
64
65        int argc_char = argc-2;
66        char ** argv_char = new char*[argc_char];
67
68        // convert argv to char*[]
69        convertTCharToChar(argv[0], &argv_char[0]);
70        for( int i=3; i<argc; i++ ) {
71                convertTCharToChar(argv[i], &argv_char[i-2]);
72        }
73
74        // parse options
75        Options opts(*argv_char, optv);
76        OptArgvIter iter(--argc_char, ++argv_char);
77        while( optchar = opts(iter, optarg) ) {
78                switch(optchar) {
79                        case 'r':
80                                convertCharToTChar(optarg, &resultfile);
81                                break;
82                        case 'd':
83                                msgdelay = atoi(optarg);
84                                break;
85                        case 'w':
86                                wait = atoi(optarg);
87                                break;
88                        case 'p':
89                                convertCharToTChar(optarg, &workingPath);
90                                break;
91                        case 's':
92                                startdelay = atoi(optarg);
93                                break;
94                        default:
95                                break;
96                }
97        }
98        for( int i=0; i<argc_char; i++ ) {
99                delete[] argv_char[i];
100        }
101        //delete[] argv_char; //TODO does not work?!
102
103        if (argc<4)
104        {
105                std::wcout << L"Usage: " << argv[0] << L"<replayfile> <applicationundertest> [-r <resultfile>] [-d <useDefaultDelay>] [-w <waitAfterFinish>] [-p <workingPath>] [-s <startupDelay>]" << std::endl;
106                return 0;
107        }
108        TCHAR * replayfile = argv[1];
109        TCHAR * appUnderTest = argv[2];
110
111        // initialize COM library for the current thread
112        CoInitialize(NULL);
113        MSXML2::ISAXXMLReader* pXMLReader = NULL;
114
115        // create an instance of the XML reader
116        HRESULT hr = CoCreateInstance(
117                __uuidof(MSXML2::SAXXMLReader),
118                NULL,
119                CLSCTX_ALL,
120                __uuidof(MSXML2::ISAXXMLReader),
121                (void **)&pXMLReader);
122
123        if( !FAILED(hr) ) {
124                TestResults results(replayfile);
125                std::wcout << L"replaying sessions in " << argv[1] << std::endl;
126                LogParser * parser = new LogParser(appUnderTest, startdelay, &results, (bool) msgdelay);
127                parser->setWorkingPath(workingPath);
128                pXMLReader->putContentHandler(parser);
129                hr = pXMLReader->parseURL(replayfile);
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.