159617ebcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
359617ebcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
459617ebcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
559617ebcSAndrew Rist  * distributed with this work for additional information
659617ebcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
759617ebcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
859617ebcSAndrew Rist  * "License"); you may not use this file except in compliance
959617ebcSAndrew Rist  * with the License.  You may obtain a copy of the License at
1059617ebcSAndrew Rist  *
1159617ebcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1259617ebcSAndrew Rist  *
1359617ebcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
1459617ebcSAndrew Rist  * software distributed under the License is distributed on an
1559617ebcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1659617ebcSAndrew Rist  * KIND, either express or implied.  See the License for the
1759617ebcSAndrew Rist  * specific language governing permissions and limitations
1859617ebcSAndrew Rist  * under the License.
1959617ebcSAndrew Rist  *
2059617ebcSAndrew Rist  *************************************************************/
2159617ebcSAndrew Rist 
2259617ebcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <precomp.h>
25cdf0e10cSrcweir #include <cosv/commandline.hxx>
26cdf0e10cSrcweir 
27cdf0e10cSrcweir // NOT FULLY DECLARED SERVICES
28cdf0e10cSrcweir #include <cosv/file.hxx>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir 
31cdf0e10cSrcweir namespace csv
32cdf0e10cSrcweir {
33cdf0e10cSrcweir 
34cdf0e10cSrcweir namespace
35cdf0e10cSrcweir {
36cdf0e10cSrcweir 
37cdf0e10cSrcweir const intt C_nNoOption = -1;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir const char * sIncludeOptionShort = "-A:";
40cdf0e10cSrcweir const char * sIncludeOptionLong  = "--Arguments:";
41cdf0e10cSrcweir const uintt nIncludeOptionShort_Length = strlen(sIncludeOptionShort);
42cdf0e10cSrcweir const uintt nIncludeOptionLong_Length = strlen(sIncludeOptionLong);
43cdf0e10cSrcweir 
44cdf0e10cSrcweir 
45cdf0e10cSrcweir /** Analyses, if an option is the one to include a file with
46cdf0e10cSrcweir     further command line arguments.
47cdf0e10cSrcweir */
48cdf0e10cSrcweir bool                IsIncludeOption(
49cdf0e10cSrcweir                         const String &      i_option );
50cdf0e10cSrcweir 
51cdf0e10cSrcweir /** Gets the file name from an include-arguments-option.
52cdf0e10cSrcweir */
53cdf0e10cSrcweir String              IncludeFile_fromIncludeOption(
54cdf0e10cSrcweir                         const String &      i_option );
55cdf0e10cSrcweir 
56cdf0e10cSrcweir 
57cdf0e10cSrcweir bool
IsIncludeOption(const String & i_option)58cdf0e10cSrcweir IsIncludeOption(const String & i_option)
59cdf0e10cSrcweir {
60cdf0e10cSrcweir     return strncmp(i_option, sIncludeOptionShort, nIncludeOptionShort_Length) == 0
61cdf0e10cSrcweir            OR
62cdf0e10cSrcweir            strncmp(i_option, sIncludeOptionLong, nIncludeOptionLong_Length) == 0;
63cdf0e10cSrcweir }
64cdf0e10cSrcweir 
65cdf0e10cSrcweir String
IncludeFile_fromIncludeOption(const String & i_option)66cdf0e10cSrcweir IncludeFile_fromIncludeOption(const String & i_option)
67cdf0e10cSrcweir {
68cdf0e10cSrcweir     if ( strncmp(i_option, sIncludeOptionShort, nIncludeOptionShort_Length)
69cdf0e10cSrcweir          == 0 )
70cdf0e10cSrcweir     {
71cdf0e10cSrcweir         return String(i_option, nIncludeOptionShort_Length, str::maxsize);
72cdf0e10cSrcweir     }
73cdf0e10cSrcweir     else
74cdf0e10cSrcweir     if ( strncmp(i_option, sIncludeOptionLong, nIncludeOptionLong_Length)
75cdf0e10cSrcweir          == 0 )
76cdf0e10cSrcweir     {
77cdf0e10cSrcweir         return String(i_option, nIncludeOptionLong_Length, str::maxsize);
78cdf0e10cSrcweir     }
79cdf0e10cSrcweir     return String::Null_();
80cdf0e10cSrcweir }
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 
83cdf0e10cSrcweir }   // end anonymous namespace
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 
88cdf0e10cSrcweir /** Local helper class for searching a possible option name in a vector of
89cdf0e10cSrcweir     ->OptionDescription.
90cdf0e10cSrcweir */
91cdf0e10cSrcweir struct CommandLine::
92cdf0e10cSrcweir FindOptionByText
93cdf0e10cSrcweir {
operator ()csv::CommandLine::FindOptionByText94cdf0e10cSrcweir     bool                operator()(
95cdf0e10cSrcweir                             const CommandLine::OptionDescription &
96cdf0e10cSrcweir                                                 i_option )
97cdf0e10cSrcweir                         { return i_option.sText == sOption; }
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     /// @param i_searchText [i_searchText != ""]
FindOptionByTextcsv::CommandLine::FindOptionByText100cdf0e10cSrcweir                         FindOptionByText(
101cdf0e10cSrcweir                             const String &      i_option )
102cdf0e10cSrcweir                         :   sOption(i_option)   { }
103cdf0e10cSrcweir   private:
104cdf0e10cSrcweir     const String        sOption;
105cdf0e10cSrcweir };
106cdf0e10cSrcweir 
107cdf0e10cSrcweir 
108cdf0e10cSrcweir typedef std::vector<StringVector::const_iterator>   StringCIteratorList;
109cdf0e10cSrcweir typedef std::vector<intt>                           OptionIdList;
110cdf0e10cSrcweir 
111cdf0e10cSrcweir bool
Interpret(int argc,char * argv[])112cdf0e10cSrcweir CommandLine::Interpret( int    argc,
113cdf0e10cSrcweir                         char * argv[] )
114cdf0e10cSrcweir {
115cdf0e10cSrcweir     Get_Arguments(argc,argv);
116cdf0e10cSrcweir     csv_assert(aOptionPoints.size() == aOptionIds.size());
117cdf0e10cSrcweir 
118cdf0e10cSrcweir     StringVector::const_iterator
119cdf0e10cSrcweir         itNext          = aCommandLine.begin();
120cdf0e10cSrcweir         ++itNext;       // Move 1 forward from program name.
121cdf0e10cSrcweir     StringVector::const_iterator
122cdf0e10cSrcweir         itEnd           = aCommandLine.end();
123cdf0e10cSrcweir     StringCIteratorList::const_iterator
124cdf0e10cSrcweir         itOptPtsEnd     = aOptionPoints.end();
125cdf0e10cSrcweir 
126cdf0e10cSrcweir     OptionIdList::const_iterator
127cdf0e10cSrcweir         itOptIds = aOptionIds.begin();
128cdf0e10cSrcweir     for ( StringCIteratorList::const_iterator itOptPts = aOptionPoints.begin();
129cdf0e10cSrcweir           itOptPts != itOptPtsEnd AND bIsOk;
130cdf0e10cSrcweir           ++itOptPts, ++itOptIds )
131cdf0e10cSrcweir     {
132cdf0e10cSrcweir         // May be, there are arguments which do not belong to the last option:
133cdf0e10cSrcweir         // itNext != *is
134cdf0e10cSrcweir         Handle_FreeArguments(itNext, *itOptPts);
135cdf0e10cSrcweir 
136cdf0e10cSrcweir         itNext = do_HandleOption( *itOptIds,
137cdf0e10cSrcweir                                   *itOptPts + 1,
138cdf0e10cSrcweir                                   itOptPts+1 == itOptPtsEnd ? itEnd : *(itOptPts+1) );
139cdf0e10cSrcweir         csv_assert(itNext <= itEnd);
140cdf0e10cSrcweir     }   // end for (is)
141cdf0e10cSrcweir     Handle_FreeArguments(itNext, itEnd);
142cdf0e10cSrcweir 
143cdf0e10cSrcweir     return bIsOk;
144cdf0e10cSrcweir }
145cdf0e10cSrcweir 
CommandLine()146cdf0e10cSrcweir CommandLine::CommandLine()
147cdf0e10cSrcweir     :   aOptions(),
148cdf0e10cSrcweir         aCommandLine(),
149cdf0e10cSrcweir         bIsOk(false)
150cdf0e10cSrcweir {
151cdf0e10cSrcweir }
152cdf0e10cSrcweir 
153cdf0e10cSrcweir void
Add_Option(intt i_id,String i_text)154cdf0e10cSrcweir CommandLine::Add_Option( intt                i_id,
155cdf0e10cSrcweir                          String              i_text )
156cdf0e10cSrcweir {
157cdf0e10cSrcweir     aOptions.push_back(OptionDescription( i_id,
158cdf0e10cSrcweir                                           i_text ));
159cdf0e10cSrcweir }
160cdf0e10cSrcweir 
161cdf0e10cSrcweir void
Get_Arguments(int argc,char * argv[])162cdf0e10cSrcweir CommandLine::Get_Arguments( int    argc,
163cdf0e10cSrcweir                             char * argv[] )
164cdf0e10cSrcweir {
165cdf0e10cSrcweir     aCommandLine.erase(aCommandLine.begin(),aCommandLine.end());
166cdf0e10cSrcweir     aCommandLine.reserve(argc);
167cdf0e10cSrcweir 
168cdf0e10cSrcweir     char ** pArgEnd = argv + argc;
169cdf0e10cSrcweir     for ( char ** pArg = &argv[0];
170cdf0e10cSrcweir           pArg != pArgEnd;
171cdf0e10cSrcweir           ++pArg )
172cdf0e10cSrcweir     {
173cdf0e10cSrcweir         Store_Argument(*pArg);
174cdf0e10cSrcweir     }   // end for
175cdf0e10cSrcweir     Find_OptionPoints();
176cdf0e10cSrcweir     bIsOk = true;
177cdf0e10cSrcweir }
178cdf0e10cSrcweir 
179cdf0e10cSrcweir intt
Find_Option(const String & i_text) const180cdf0e10cSrcweir CommandLine::Find_Option( const String & i_text ) const
181cdf0e10cSrcweir {
182cdf0e10cSrcweir     if (i_text.empty())
183cdf0e10cSrcweir         return C_nNoOption;
184cdf0e10cSrcweir 
185cdf0e10cSrcweir     FindOptionByText aSearch(i_text);
186cdf0e10cSrcweir     OptionList::const_iterator
187cdf0e10cSrcweir         itFound = std::find_if( aOptions.begin(),
188cdf0e10cSrcweir                                 aOptions.end(),
189cdf0e10cSrcweir                                 aSearch );
190cdf0e10cSrcweir     if (itFound != aOptions.end())
191cdf0e10cSrcweir     {
192cdf0e10cSrcweir         return (*itFound).nId;
193cdf0e10cSrcweir     }
194cdf0e10cSrcweir     return C_nNoOption;
195cdf0e10cSrcweir }
196cdf0e10cSrcweir 
197cdf0e10cSrcweir bool
Store_Argument(const String & i_arg)198cdf0e10cSrcweir CommandLine::Store_Argument( const String & i_arg )
199cdf0e10cSrcweir {
200cdf0e10cSrcweir     if ( NOT IsIncludeOption(i_arg) )
201cdf0e10cSrcweir     {
202cdf0e10cSrcweir         aCommandLine.push_back(i_arg);
203cdf0e10cSrcweir         return true;
204cdf0e10cSrcweir     }
205cdf0e10cSrcweir 
206cdf0e10cSrcweir     return Try2Include_Options(i_arg);
207cdf0e10cSrcweir }
208cdf0e10cSrcweir 
209cdf0e10cSrcweir void
Find_OptionPoints()210cdf0e10cSrcweir CommandLine::Find_OptionPoints()
211cdf0e10cSrcweir {
212cdf0e10cSrcweir     StringVector::const_iterator    itEnd   = aCommandLine.end();
213cdf0e10cSrcweir     for ( StringVector::const_iterator it = aCommandLine.begin() + 1;
214cdf0e10cSrcweir           it != itEnd;
215cdf0e10cSrcweir           ++it )
216cdf0e10cSrcweir     {
217cdf0e10cSrcweir         intt    nOption = Find_Option(*it);
218cdf0e10cSrcweir         if (nOption != C_nNoOption)
219cdf0e10cSrcweir         {
220cdf0e10cSrcweir             aOptionPoints.push_back(it);
221cdf0e10cSrcweir             aOptionIds.push_back(nOption);
222cdf0e10cSrcweir         }
223cdf0e10cSrcweir     }   // end for (i)
224cdf0e10cSrcweir }
225cdf0e10cSrcweir 
226cdf0e10cSrcweir void
Handle_FreeArguments(StringVector::const_iterator i_begin,StringVector::const_iterator i_end)227cdf0e10cSrcweir CommandLine::Handle_FreeArguments( StringVector::const_iterator i_begin,
228cdf0e10cSrcweir                                    StringVector::const_iterator i_end )
229cdf0e10cSrcweir {
230cdf0e10cSrcweir     for ( StringVector::const_iterator it = i_begin;
231cdf0e10cSrcweir           it != i_end AND bIsOk;
232cdf0e10cSrcweir           ++it )
233cdf0e10cSrcweir     {
234cdf0e10cSrcweir         do_HandleFreeArgument(*it);
235cdf0e10cSrcweir     }
236cdf0e10cSrcweir }
237cdf0e10cSrcweir 
238cdf0e10cSrcweir bool
Try2Include_Options(const String & i_includeOption)239cdf0e10cSrcweir CommandLine::Try2Include_Options(const String & i_includeOption)
240cdf0e10cSrcweir {
241cdf0e10cSrcweir     static StringVector
242cdf0e10cSrcweir         aIncludedOptionFiles_;
243cdf0e10cSrcweir 
244cdf0e10cSrcweir     const String
245cdf0e10cSrcweir         aOptionFile(IncludeFile_fromIncludeOption(i_includeOption));
246cdf0e10cSrcweir 
247cdf0e10cSrcweir     // Avoid recursion deadlock 1
248cdf0e10cSrcweir     if ( std::find( aIncludedOptionFiles_.begin(),
249cdf0e10cSrcweir                     aIncludedOptionFiles_.end(),
250cdf0e10cSrcweir                     aOptionFile )
251cdf0e10cSrcweir          != aIncludedOptionFiles_.end() )
252cdf0e10cSrcweir     {
253cdf0e10cSrcweir         Cerr() << "\nError: Self inclusion of option file "
254cdf0e10cSrcweir                << aOptionFile
255cdf0e10cSrcweir                << ".\n"
256cdf0e10cSrcweir                << Endl();
257cdf0e10cSrcweir         return false;
258cdf0e10cSrcweir     }
259cdf0e10cSrcweir 
260cdf0e10cSrcweir     // Avoid recursion deadlock 2
261cdf0e10cSrcweir     aIncludedOptionFiles_.push_back(aOptionFile);
262cdf0e10cSrcweir 
263cdf0e10cSrcweir     bool ok = Include_Options(aOptionFile);
264cdf0e10cSrcweir 
265cdf0e10cSrcweir     // Avoid recursion deadlock 3
266cdf0e10cSrcweir     aIncludedOptionFiles_.pop_back();
267cdf0e10cSrcweir 
268cdf0e10cSrcweir     return ok;
269cdf0e10cSrcweir }
270cdf0e10cSrcweir 
271cdf0e10cSrcweir bool
Include_Options(const String & i_optionsFile)272cdf0e10cSrcweir CommandLine::Include_Options( const String & i_optionsFile )
273cdf0e10cSrcweir {
274cdf0e10cSrcweir     StreamStr
275cdf0e10cSrcweir         aIncludedText(500);
276cdf0e10cSrcweir     bool ok = Load_Options(aIncludedText, i_optionsFile);
277cdf0e10cSrcweir     if (NOT ok)
278cdf0e10cSrcweir         return false;
279cdf0e10cSrcweir 
280cdf0e10cSrcweir     StringVector
281cdf0e10cSrcweir         aIncludedOptions;
282cdf0e10cSrcweir     Split(aIncludedOptions, aIncludedText.c_str());
283cdf0e10cSrcweir 
284cdf0e10cSrcweir     StringVector::const_iterator itEnd = aIncludedOptions.end();
285cdf0e10cSrcweir     for ( StringVector::const_iterator it = aIncludedOptions.begin();
286cdf0e10cSrcweir           it != itEnd;
287cdf0e10cSrcweir           ++it )
288cdf0e10cSrcweir     {
289cdf0e10cSrcweir         Store_Argument(*it);
290cdf0e10cSrcweir     }   // end for
291cdf0e10cSrcweir 
292cdf0e10cSrcweir     return true;
293cdf0e10cSrcweir }
294cdf0e10cSrcweir 
295cdf0e10cSrcweir bool
Load_Options(StreamStr & o_text,const String & i_optionsFile)296cdf0e10cSrcweir CommandLine::Load_Options( StreamStr &      o_text,
297cdf0e10cSrcweir                            const String &   i_optionsFile )
298cdf0e10cSrcweir {
299cdf0e10cSrcweir     if (i_optionsFile.empty())
300cdf0e10cSrcweir         return false;
301cdf0e10cSrcweir 
302cdf0e10cSrcweir     File
303cdf0e10cSrcweir         aOptionsFile(i_optionsFile, CFM_READ);
304cdf0e10cSrcweir     OpenCloseGuard
305cdf0e10cSrcweir         aOFGuard(aOptionsFile);
306cdf0e10cSrcweir     if (NOT aOFGuard)
307cdf0e10cSrcweir     {
308cdf0e10cSrcweir         Cerr() << "\nError: Options file "
309cdf0e10cSrcweir                << i_optionsFile
310cdf0e10cSrcweir                << " not found.\n"
311cdf0e10cSrcweir                << Endl();
312cdf0e10cSrcweir         return false;
313cdf0e10cSrcweir     }
314cdf0e10cSrcweir 
315cdf0e10cSrcweir     StreamStr
316cdf0e10cSrcweir         aLoad(aOptionsFile);
317cdf0e10cSrcweir     o_text.swap(aLoad);
318cdf0e10cSrcweir     return true;
319cdf0e10cSrcweir }
320cdf0e10cSrcweir 
321cdf0e10cSrcweir 
322cdf0e10cSrcweir 
323cdf0e10cSrcweir 
324cdf0e10cSrcweir /******************         OptionDescription       ***********************/
325cdf0e10cSrcweir 
326cdf0e10cSrcweir 
327cdf0e10cSrcweir CommandLine::
OptionDescription(intt i_id,String i_text)328cdf0e10cSrcweir OptionDescription::OptionDescription( intt          i_id,
329cdf0e10cSrcweir                                       String        i_text )
330cdf0e10cSrcweir     :   nId(i_id),
331cdf0e10cSrcweir         sText(i_text)
332cdf0e10cSrcweir {
333cdf0e10cSrcweir }
334cdf0e10cSrcweir 
335cdf0e10cSrcweir 
336cdf0e10cSrcweir 
337cdf0e10cSrcweir 
338cdf0e10cSrcweir }   // namespace csv
339