|
1 /* |
|
2 * Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "CmdGlobals.h" |
|
20 #ifdef __WIN__ |
|
21 #pragma warning(disable:4786) |
|
22 #endif |
|
23 |
|
24 #include <iostream> |
|
25 #include <fstream> |
|
26 #include <vector> |
|
27 |
|
28 #include "CommandFile.h" |
|
29 #include "HAException.h" |
|
30 #include "Utils.h" |
|
31 |
|
32 using namespace std; |
|
33 |
|
34 |
|
35 // ---------------------------------------------------------------------------- |
|
36 // CommandFile::CommandFile |
|
37 // File name is gien as parameter |
|
38 // |
|
39 // ---------------------------------------------------------------------------- |
|
40 // |
|
41 CommandFile::CommandFile(string aFilename) : iCmdBufLen(0), iCmdBuf(NULL) |
|
42 { |
|
43 iFilename = aFilename; |
|
44 } |
|
45 |
|
46 |
|
47 // ---------------------------------------------------------------------------- |
|
48 // CommandFile::CommandFile |
|
49 // ---------------------------------------------------------------------------- |
|
50 // |
|
51 CommandFile::~CommandFile() |
|
52 { |
|
53 if (iCmdBuf != NULL) |
|
54 { |
|
55 for (unsigned int i = 0; i < iCmdBufLen; i++) |
|
56 { |
|
57 delete [] iCmdBuf[i]; |
|
58 } |
|
59 delete [] iCmdBuf; |
|
60 iCmdBuf = NULL; |
|
61 } |
|
62 } |
|
63 |
|
64 |
|
65 // ---------------------------------------------------------------------------- |
|
66 // CommandFile::getCommandBuffer |
|
67 // ---------------------------------------------------------------------------- |
|
68 // |
|
69 char** CommandFile::getCommandBuffer() |
|
70 { |
|
71 readCommandFile(iFilename); |
|
72 return iCmdBuf; |
|
73 } |
|
74 |
|
75 |
|
76 // ---------------------------------------------------------------------------- |
|
77 // CommandFile::commandBufferLength |
|
78 // |
|
79 // ---------------------------------------------------------------------------- |
|
80 // |
|
81 size_t CommandFile::commandBufferLength() |
|
82 { |
|
83 return iCmdBufLen; |
|
84 } |
|
85 |
|
86 |
|
87 // ---------------------------------------------------------------------------- |
|
88 // CommandFile::readCommandFile |
|
89 // Reads parameters from the command file. |
|
90 // ---------------------------------------------------------------------------- |
|
91 // |
|
92 void CommandFile::readCommandFile(string filename) |
|
93 { |
|
94 #if ( defined(_DEBUG) || defined(DEBUG) ) && !defined(NO_DBG) |
|
95 cout << "Reading command file\n"; |
|
96 #endif |
|
97 ifstream input(filename.c_str(), ios::in); |
|
98 if (!input.is_open()) |
|
99 { |
|
100 throw HAException("Cannot open command file"); |
|
101 } |
|
102 char c; |
|
103 string str; |
|
104 unsigned long linecount = 1; |
|
105 bool careForSpace = true; |
|
106 bool strSpace = false; |
|
107 bool whitespace = true; |
|
108 vector<string> parmlist; |
|
109 parmlist.push_back("padding"); |
|
110 while (input.get(c)) |
|
111 { |
|
112 if (c == ' ' && careForSpace == true) |
|
113 { |
|
114 if (whitespace == false) |
|
115 { |
|
116 parmlist.push_back(str); |
|
117 } |
|
118 str = ""; |
|
119 whitespace = false; |
|
120 careForSpace = false; |
|
121 }else if (c == ' ' && strSpace == false) |
|
122 { |
|
123 }else if (c == '\\' || c == '/') |
|
124 { |
|
125 str += DIR_SEPARATOR; |
|
126 }else if (c == '"') |
|
127 { |
|
128 if (strSpace == false) |
|
129 { |
|
130 careForSpace = false; |
|
131 strSpace = true; |
|
132 } else |
|
133 { |
|
134 careForSpace = true; |
|
135 strSpace = false; |
|
136 } |
|
137 whitespace = false; |
|
138 }else if (c != '\n') |
|
139 { |
|
140 if (careForSpace == false && strSpace == false) |
|
141 { |
|
142 careForSpace = true; |
|
143 } |
|
144 str += c; |
|
145 whitespace = false; |
|
146 } else |
|
147 { |
|
148 if (strSpace == true) |
|
149 { |
|
150 string line; |
|
151 ltoa(linecount, line, 10); |
|
152 throw HAException("Syntax error: New line has come before ending quotation mark(\"). Line: " + line); |
|
153 } |
|
154 if (str.length() != 0) |
|
155 { |
|
156 parmlist.push_back(str); |
|
157 } |
|
158 linecount++; |
|
159 str = ""; |
|
160 whitespace = true; |
|
161 } |
|
162 } |
|
163 if (str.length() != 0) |
|
164 { |
|
165 parmlist.push_back(str); |
|
166 } |
|
167 |
|
168 // Store parameter information |
|
169 size_t elementcount = parmlist.size(); |
|
170 iCmdBufLen = elementcount; |
|
171 if (iCmdBuf) |
|
172 { |
|
173 delete iCmdBuf; |
|
174 iCmdBuf = NULL; |
|
175 } |
|
176 iCmdBuf = new char*[elementcount]; |
|
177 |
|
178 // Copy the parameter list from vector to char** |
|
179 // (in order to be compatible with CommandLine::parse()) |
|
180 for (unsigned int i = 0; i < parmlist.size(); i++) |
|
181 { |
|
182 size_t charcount = parmlist.at(i).length(); |
|
183 |
|
184 iCmdBuf[i] = new char[charcount+1]; |
|
185 const char* cbuf = parmlist.at(i).c_str(); |
|
186 unsigned int j = 0; |
|
187 for(j = 0; j < charcount; j++) |
|
188 { |
|
189 iCmdBuf[i][j] = cbuf[j]; |
|
190 } |
|
191 iCmdBuf[i][j] = '\0'; |
|
192 } |
|
193 } |