KSquare Utilities
Application.cpp
Go to the documentation of this file.
1 /* Application.cpp -- Generic Application class.
2  * Copyright (C) 1994-2011 Kurt Kramer
3  * For conditions of distribution and use, see copyright notice in KKB.h
4  */
5 #include "FirstIncludes.h"
6 
7 #include <stdlib.h>
8 #include <stdio.h>
9 
10 #include <string>
11 #include <iostream>
12 #include <fstream>
13 #include <vector>
14 
15 #include "MemoryDebug.h"
16 using namespace std;
17 
18 #include "Application.h"
19 #include "CmdLineExpander.h"
20 #include "DateTime.h"
21 #include "KKBaseTypes.h"
22 #include "KKQueue.h"
23 #include "OSservices.h"
24 using namespace KKB;
25 
26 
27 
28 
30  abort (false),
31  log (_log),
32  logFileName (),
33  ourLog (NULL)
34 {
35 }
36 
37 
38 
40  abort (false),
41  log (*(new RunLog ())),
42  logFileName (),
43  ourLog (NULL)
44 {
45  ourLog = &log;
46 }
47 
48 
49 
50 
51 Application::Application (const Application& _application):
52  abort (_application.abort),
53  log (_application.log),
54  logFileName (_application.logFileName),
55  ourLog (_application.ourLog)
56 {
57 }
58 
59 
60 
62 {
63  delete ourLog;
64  ourLog = NULL;
65 }
66 
67 
68 
70  char** argv
71  )
72 {
73  ProcessCmdLineParameters (argc, argv);
74  if (!logFileName.Empty ())
75  log.AttachFile (logFileName);
76 }
77 
78 
79 
81 {
82  return "No-Application-Name-Provided";
83 } /* ApplicationName */
84 
85 
86 
88 {
89  log = _log;
90 
91  if (ourLog)
92  {
93  delete ourLog;
94  ourLog = NULL;
95  }
96 } /* AssignRunLog */
97 
98 
99 
100 
101 void Application::ProcessCmdLineParameters (kkint32 argc,
102  char** argv
103  )
104 {
105  bool allParmsGood = true;
106 
107  CmdLineExpander cmdLineExpander (ApplicationName (), log, argc, argv);
108 
109  if (!cmdLineExpander.ParmsGood ())
110  Abort (true);
111 
112  if (!cmdLineExpander.LogFileName ().Empty ())
113  logFileName = cmdLineExpander.LogFileName ();
114 
115  expandedParameterPairs = cmdLineExpander.ExpandedParameterPairs ();
116 
117  vector<KKStrPair>::const_iterator idx;
118 
119  for (idx = expandedParameterPairs.begin (); idx != expandedParameterPairs.end (); ++idx)
120  {
121  const KKStr& parmSwitch = idx->first;
122  const KKStr& parmValue = idx->second;
123  bool parmGood = ProcessCmdLineParameter (parmSwitch, parmValue);
124  if (!parmGood)
125  allParmsGood = false;
126  }
127 
128  if (!allParmsGood)
129  abort = true;
130 } /* ProcessCmdLineParameters */
131 
132 
133 
135 {
136  cout << ApplicationName () << endl
137  << endl
138  << " -LogFile <FileName> File that logging messages will be written to; if left will" << endl
139  << " be written to Standard-Out (cout)." << endl
140  << endl
141  << " -CmdFile <File-Name> Filename where additional command line options are stored;" << endl
142  << " This file can also specify additional '-CmdFile parameters." << endl
143  << endl;
144 }
145 
146 
147 
148 
149 bool Application::ProcessCmdLineParameter (const KKStr& parmSwitch,
150  const KKStr& parmValue
151  )
152 {
153  log.Level (-1) << endl
154  << "Application::ProcessCmdLineParameter ***ERROR*** Unrecognized Parameter [" << parmSwitch << "] Value [" << parmValue << "]" << endl
155  << endl;
156  return false;
157 }
158 
159 
160 
161 
163 {
164  DateType buildDate (__DATE__);
165  TimeType buildTime (__TIME__);
166  return buildDate.YYYY_MM_DD () + "-" + buildTime.HH_MM_SS ();
167 } /* BuildDate */
Application(const Application &_application)
Copy Constructor for Application class.
Definition: Application.cpp:51
__int32 kkint32
Definition: KKBaseTypes.h:88
DateType(KKStr s)
Definition: DateTime.cpp:187
virtual void DisplayCommandLineParameters()
Will display Command Lone parameters that the &#39;Application&#39; class will manage.
CmdLineExpander(const KKStr &_applicationName, RunLog &_log, kkint32 argc, char **argv)
Constructor using the parameters passed through on the command line into main().
KKStr HH_MM_SS() const
Definition: DateTime.cpp:841
void Abort(bool _abort)
Used to specify that the application is been aborted.
Definition: Application.h:71
virtual const char * ApplicationName()
Definition: Application.cpp:80
KKStr YYYY_MM_DD() const
Convert into displayable string; ex: 20011/05/17.
Definition: DateTime.cpp:701
KKStr operator+(const char *right) const
Definition: KKStr.cpp:3986
Application()
Constructor for Application class that will start with a default logger(RunLog),. ...
Definition: Application.cpp:39
virtual void InitalizeApplication(kkint32 argc, char **argv)
Initialized Application Instance; 1st method to be called after instance construction.
Definition: Application.cpp:69
Application(RunLog &_log)
Constructor for Application class where we already have an existing logger &#39;_log&#39;.
Definition: Application.cpp:29
virtual bool ProcessCmdLineParameter(const KKStr &parmSwitch, const KKStr &parmValue)
This method will get called once for each parameter specified in the command line.
KKTHread * KKTHreadPtr
Expands command line parameters, by parsing for special parameters and expanding them to their full v...
KKStr(const KKStr &str)
Copy Constructor.
Definition: KKStr.cpp:561
RunLog()
Creates an instance were logging will go to the console.
Definition: RunLog.cpp:32
virtual ~Application()
Definition: Application.cpp:61
bool Empty() const
Definition: KKStr.h:241
TimeType(KKStr s)
Definition: DateTime.cpp:802
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
virtual KKStr BuildDate() const
Represents a Time, consisting of three fields, Hour, Minute, and Second. summary>Represents Date and ...
Definition: DateTime.h:149
The base class for all standalone application.
Definition: Application.h:29
KKStr operator+(const KKStr &right) const
Definition: KKStr.cpp:3998
bool ParmsGood() const
void AttachFile(const KKStr &_fileName)
Call this method to start logging messages to a different file.
Definition: RunLog.cpp:127
KKStr & operator=(const KKStr &src)
Definition: KKStr.cpp:1390
Used for logging messages.
Definition: RunLog.h:49
void AssignLog(RunLog &_log)
Replaces the Log file to write to.
Definition: Application.cpp:87
const KKStr & LogFileName() const
Represents a calendar date consisting of three fields, Year, Month, and Day.
Definition: DateTime.h:28