KSquare Utilities
OSservices.cpp File Reference
#include "FirstIncludes.h"
#include <direct.h>
#include <windows.h>
#include <Lmcons.h>
#include <conio.h>
#include <ctype.h>
#include <errno.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include "MemoryDebug.h"
#include "OSservices.h"
#include "ImageIO.h"
#include "KKStr.h"

Go to the source code of this file.

Macros

#define _FILE_OFFSET_BITS   64
 
#define INVALID_FILE_ATTRIBUTES   ((DWORD)-1)
 
#define INVALID_FILE_ATTRIBUTES   ((DWORD)-1)
 

Functions

bool osFileNameMatchesSearchFields (const KKStr &fileName, KKStrListPtr searchFields)
 
int osLocateEnvStrStart (const KKStr &str, kkint32 startIdx)
 
KKStrListPtr osParseSearchSpec (const KKStr &searchSpec)
 

Variables

bool backGroundProcess = false
 

Macro Definition Documentation

#define _FILE_OFFSET_BITS   64

Definition at line 7 of file OSservices.cpp.

#define INVALID_FILE_ATTRIBUTES   ((DWORD)-1)
#define INVALID_FILE_ATTRIBUTES   ((DWORD)-1)

Function Documentation

bool osFileNameMatchesSearchFields ( const KKStr fileName,
KKStrListPtr  searchFields 
)

Definition at line 180 of file OSservices.cpp.

References KKB::KKStr::Concat(), KKB::KKStr::Len(), KKB::KKStr::operator==(), and KKB::KKStr::Str().

183 {
184  if (!searchFields)
185  return true;
186 
187  if (searchFields->QueueSize () < 1)
188  return true;
189 
190  KKStrConstPtr field = searchFields->IdxToPtr (0);
191 
192  if (searchFields->QueueSize () == 1)
193  {
194  if (*field == "*")
195  return true;
196 
197  if (*field == fileName)
198  return true;
199  else
200  return false;
201  }
202 
203  bool lastFieldAStar = false;
204 
205  const char* cp = fileName.Str ();
206  kkuint32 lenLeftToCheck = fileName.Len ();
207 
208  kkint32 fieldNum;
209 
210  for (fieldNum = 0; fieldNum < searchFields->QueueSize (); fieldNum++)
211  {
212  const KKStr& field = *(searchFields->IdxToPtr (fieldNum));
213 
214  if (field == "*")
215  {
216  lastFieldAStar = true;
217  }
218  else
219  {
220  if (lastFieldAStar)
221  {
222  // Since last was a '*' then we can skip over characters until we find a sequence that matches field
223 
224  bool matchFound = false;
225 
226  while ((!matchFound) && (lenLeftToCheck >= field.Len ()))
227  {
228  if (strncmp (cp, field.Str (), field.Len ()) == 0)
229  {
230  matchFound = true;
231  // lets move cp beyond where we found field in fileName
232  cp = cp + field.Len ();
233  if (field.Len() > lenLeftToCheck)
234  lenLeftToCheck = 0;
235  else
236  lenLeftToCheck = lenLeftToCheck - field.Len ();
237  }
238  else
239  {
240  ++cp;
241  --lenLeftToCheck;
242  }
243  }
244 
245  if (!matchFound)
246  {
247  // There was no match any were else in the rest of the fileName.
248  // way that this fileName is a match.
249  return false;
250  }
251  }
252  else
253  {
254  // Since last field was NOT a '*' the next field->Len () characters better be an exact match
255  if (lenLeftToCheck < field.Len ())
256  {
257  // Not enough chars left in fileName to check, can not possibly be a match
258  return false;
259  }
260 
261  if (strncmp (cp, field.Str (), field.Len ()) != 0)
262  {
263  // The next field->Len () chars were not a match. This means that fileName is
264  // not a match.
265  return false;
266  }
267  else
268  {
269  cp = cp + field.Len ();
270  lenLeftToCheck = lenLeftToCheck - field.Len ();
271  }
272  }
273 
274  lastFieldAStar = false;
275  }
276  }
277 
278 
279  if (lenLeftToCheck > 0)
280  {
281  // Since there are some char's left in fileName that we have not checked, then
282  // the last field had better been a '*'
283  if (lastFieldAStar)
284  return true;
285  else
286  return false;
287  }
288 
289  return true;
290 } /* osFileNameMatchesSearchFields */
__int32 kkint32
Definition: KKBaseTypes.h:88
EntryPtr IdxToPtr(kkuint32 idx) const
Definition: KKQueue.h:732
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
kkuint32 Len() const
Returns the number of characters in the string.
Definition: KKStr.h:366
const char * Str() const
Returns a pointer to a ascii string.
Definition: KKStr.h:422
kkint32 QueueSize() const
Definition: KKQueue.h:313
int osLocateEnvStrStart ( const KKStr str,
kkint32  startIdx 
)

Searches a string starting from a specified index for the start of an Environment String Specification.

1  1 2 3 4 5 6
2 ex: 0123456789012345678901234567890123456789012345678901234567890123456789
3  str = "TrainingDirectory\${LarcosHomeDir}\Classifiers\${CurTrainLibrary"
4 
5  idx = osLocateEnvStrStart (str, 0); // returns 18
6  idx = osLocateEnvStrStart (str, 34) // returns 47
Parameters
strStarting that is to be searched.
startIdxIndex search is to start at.
Returns
Index of 1st character of a Environment String specifier or -1 if none was found.
Parameters
startIdxIndex in 'str' to start search from.

Definition at line 948 of file OSservices.cpp.

References KKB::KKStr::Len(), and KKB::KKStr::Str().

Referenced by KKB::osSubstituteInEnvironmentVariables().

951 {
952  int x = startIdx;
953  int y = startIdx + 1;
954  int len = str.Len ();
955  const char* s = str.Str ();
956 
957  while (y < len)
958  {
959  if (s[y] == 0)
960  return -1;
961 
962  if (s[x] == '$')
963  {
964  if ((s[y] == '(') || (s[y] == '{') || (s[y] == '['))
965  return x;
966  }
967 
968  ++x;
969  ++y;
970  }
971 
972  return -1;
973 } /* LocateEnvStrStart */
kkuint32 Len() const
Returns the number of characters in the string.
Definition: KKStr.h:366
const char * Str() const
Returns a pointer to a ascii string.
Definition: KKStr.h:422
KKStrListPtr osParseSearchSpec ( const KKStr searchSpec)

Definition at line 131 of file OSservices.cpp.

References KKB::KKStr::Empty(), and KKB::KKStrList::KKStrList().

132 {
133  KKStrListPtr searchFields = new KKStrList (true);
134 
135  if (searchSpec.Empty ())
136  {
137  searchFields->PushOnBack (new KKStr ("*"));
138  return searchFields;
139  }
140 
141 # ifdef USE_SECURE_FUNCS
142  char* workStr = _strdup (searchSpec.Str ());
143 # else
144  char* workStr = strdup (searchSpec.Str ());
145 # endif
146 
147  char* cp = workStr;
148 
149  char* startOfField = cp;
150 
151  while (*cp != 0)
152  {
153  if (*cp == '*')
154  {
155  *cp = 0;
156  if (startOfField != cp)
157  searchFields->PushOnBack (new KKStr (startOfField));
158 
159  searchFields->PushOnBack (new KKStr ("*"));
160  startOfField = cp + 1;
161  }
162  cp++;
163  }
164 
165  if (cp != startOfField)
166  {
167  // There is one last field that we need to add, meaning that there was no '*'s or
168  // that the last char in the searchField was not a '*'.
169  searchFields->PushOnBack (new KKStr (startOfField));
170  }
171 
172  delete workStr;
173 
174  return searchFields;
175 } /* osParseSearchSpec */
bool Empty() const
Definition: KKStr.h:241
virtual void PushOnBack(EntryPtr _entry)
Definition: KKQueue.h:398
const char * Str() const
Returns a pointer to a ascii string.
Definition: KKStr.h:422

Variable Documentation

bool backGroundProcess = false