KSquare Utilities
OSservices.cpp
Go to the documentation of this file.
1 /* OSservices.cpp -- O/S related functions, meant to be O/S neutral
2  * Copyright (C) 1994-2014 Kurt Kramer
3  * For conditions of distribution and use, see copyright notice in KKB.h
4  */
5 
6 // For non windows systems; will allow fseeko() and ftello() to work with 64 bit int's.
7 #define _FILE_OFFSET_BITS 64
8 
9 #include "FirstIncludes.h"
10 
11 #if defined(OS_WINDOWS)
12 #include <direct.h>
13 #include <windows.h>
14 #include <Lmcons.h>
15 #include <conio.h>
16 #else
17 #include <dirent.h>
18 #include <sys/types.h>
19 #include <sys/times.h>
20 #include <sys/time.h>
21 #include <limits.h>
22 #include <unistd.h>
23 #include <sys/stat.h>
24 #endif
25 
26 #include <ctype.h>
27 #include <errno.h>
28 #include <iostream>
29 #include <fstream>
30 #include <stdio.h>
31 #include <string.h>
32 #include <string>
33 #include <vector>
34 #include "MemoryDebug.h"
35 using namespace std;
36 
37 
38 #include "OSservices.h"
39 #include "ImageIO.h"
40 #include "KKStr.h"
41 using namespace KKB;
42 
43 
44 
46 {
47  KKStr desc;
48 
49 # ifdef WIN32
50 # ifdef USE_SECURE_FUNCS
51  char buff[100];
52  buff[0] = 0;
53  strerror_s (buff, sizeof (buff), errorNo);
54  desc = buff;
55 # else
56  const char* buff = _sys_errlist[errorNo];
57  if (buff)
58  desc = buff;
59 # endif
60 # else
61  const char* buff = strerror (errorNo);
62  if (buff)
63  desc = buff;
64 # endif
65 
66  return desc;
67 } /* osGetErrorNoDesc */
68 
69 
70 
71 
72 
73 
74 FILE* KKB::osFOPEN (const char* fileName,
75  const char* mode
76  )
77 {
78  FILE* f = NULL;
79 
80 # ifdef USE_SECURE_FUNCS
81  fopen_s (&f, fileName, mode);
82 # else
83  f = fopen (fileName, mode);
84 # endif
85 
86  return f;
87 }
88 
89 
90 
91 
92 
93 kkint64 KKB::osFTELL (FILE* f)
94 {
95 #if defined(OS_WINDOWS)
96  return _ftelli64 (f);
97 #else
98  return ftello( f);
99 #endif
100 }
101 
102 
103 
104 int KKB::osFSEEK (FILE* f,
105  kkint64 offset,
106  int origin
107  )
108 {
109 #if defined(OS_WINDOWS)
110  return _fseeki64(f, offset, origin);
111 #else
112  return fseeko (f, offset, origin);
113 #endif
114 }
115 
116 
117 
118 
119 
120 //***************************** ParseSearchSpec ********************************
121 //* Will parse the string searchSpec into separate fields using '*' as the *
122 //* delimiter character. *
123 //* *
124 //* ex: input: searchSpec = "Alpha*Right*.txt" *
125 //* returns: ("Alpha", "*", "Right", "*", ".txt") *
126 //* *
127 //* The caller is responsible for deleting the StrigList that is returned *
128 //* when no longer needed. *
129 //* *
130 //******************************************************************************
131 KKStrListPtr osParseSearchSpec (const KKStr& searchSpec)
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 */
176 
177 
178 
179 
180 bool osFileNameMatchesSearchFields (const KKStr& fileName,
181  KKStrListPtr searchFields
182  )
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 */
291 
292 
293 
294 
295 
296 
297 char KKB::osGetDriveLetter (const KKStr& pathName)
298 {
299 #ifndef WIN32
300  return 0;
301 #else
302  if (pathName.Len () < 3)
303  return 0;
304 
305  if (pathName[(kkint16)1] == ':')
306  return pathName[(kkint16)0];
307 
308  return 0;
309 #endif
310 } /* osGetDriveLetter */
311 
312 
313 
314 
315 
316 
317 
318 #ifdef WIN32
320 {
321  DWORD buffLen = 0;
322  char* buff = NULL;
323  DWORD reqLen = 1000;
324 
325  while (reqLen > buffLen)
326  {
327  buffLen = reqLen;
328 
329  if (buff)
330  delete[] buff;
331 
332  buff = new char[buffLen];
333 
334  reqLen = GetCurrentDirectory (buffLen, buff);
335  }
336 
337  KKStr curDir (buff);
338 
339  delete[] buff;
340 
341  return curDir;
342 } /* GetCurrentDirectory */
343 
344 #else
345 
346 
347 
348 
349 
350 KKStr KKB::osGetCurrentDirectory ()
351 {
352  size_t buffLen = 0;
353  char* buff = NULL;
354  char* result = NULL;
355 
356 
357  while (!result)
358  {
359  buffLen = buffLen + 1000;
360 
361  if (buff)
362  delete buff;
363 
364  buff = new char[buffLen];
365 
366  result = getcwd (buff, buffLen);
367  }
368 
369  KKStr curDir (buff);
370 
371  delete buff;
372 
373  return curDir;
374 } /* ossGetCurrentDirectory */
375 #endif
376 
377 
378 
379 
380 
381 #ifdef WIN32
382 bool KKB::osValidDirectory (KKStrConstPtr name)
383 {
384  DWORD returnCd = GetFileAttributes (name->Str ());
385 
386  if (returnCd == 0xFFFFFFFF)
387  {
388  return false;
389  }
390 
391  if ((FILE_ATTRIBUTE_DIRECTORY & returnCd) != FILE_ATTRIBUTE_DIRECTORY)
392  return false;
393  else
394  return true;
395 }
396 
397 
398 bool KKB::osValidDirectory (const KKStr& name)
399 {
400  #define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
401  DWORD returnCd = GetFileAttributes (name.Str ());
402 
403  if (returnCd == INVALID_FILE_ATTRIBUTES)
404  return false;
405 
406 
407  if ((FILE_ATTRIBUTE_DIRECTORY & returnCd) != FILE_ATTRIBUTE_DIRECTORY)
408  return false;
409  else
410  return true;
411 }
412 
413 #else
414 
415 bool KKB::osValidDirectory (KKStrConstPtr name)
416 {
417  DIR* openDir = opendir (name->Str ());
418 
419  if (!openDir)
420  return false;
421 
422  closedir (openDir);
423  return true;
424 }
425 
426 
427 
428 bool KKB::osValidDirectory (const KKStr& name)
429 {
430  DIR* openDir = opendir (name.Str ());
431 
432  if (!openDir)
433  return false;
434 
435  closedir (openDir);
436  return true;
437 }
438 
439 #endif
440 
441 
442 
443 bool KKB::osValidFileName (const KKStr& _name)
444 {
445  KKStrListPtr errors = osValidFileNameErrors (_name);
446  if (errors == NULL)
447  return true;
448  else
449  {
450  delete errors;
451  errors = NULL;
452  return false;
453  }
454 }
455 
456 
457 
458 KKStrListPtr KKB::osValidFileNameErrors (const KKStr& _name)
459 {
460  const char* invalidChars = "\\\" :<>!?*";
461  const char* invalidNames[] = {"CON", "PRN", "AUX", "NUL", "COM1", "COM2",
462  "COM3", "COM4", "COM5", "COM6", "COM7", "COM8",
463  "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5",
464  "LPT6", "LPT7", "LPT8", "LPT9",
465  NULL
466  };
467 
468  KKStrListPtr errors = new KKStrList (true);
469  if (_name.Empty ())
470  {
471  errors->PushOnBack (new KKStr ("Blank names are invalid!"));
472  }
473 
474  else
475  {
476  // Check for invalid names.
477  kkuint32 x = 0;
478  while (invalidNames[x] != NULL)
479  {
480  if (_name.EqualIgnoreCase (invalidNames[x]))
481  errors->PushOnBack (new KKStr ("Can not use \"" + _name + "\""));
482  ++x;
483  }
484 
485  // Check for invalid characters
486  for (x = 0; x < _name.Len (); ++x)
487  {
488  char c = _name[x];
489  if (c == 0)
490  errors->PushOnBack (new KKStr ("Null character at position: " + StrFormatInt (x, "##0")));
491 
492  else if (c == ' ')
493  errors->PushOnBack (new KKStr ("No spaces allowed."));
494 
495  else if (c < 32)
496  {
497  errors->PushOnBack (new KKStr ("Character at position: " + StrFormatInt (x, "##0") + " has ordinal value of: " + StrFormatInt (c, "##0")));
498  }
499 
500  else if (strchr (invalidChars, c) != NULL)
501  {
502  KKStr invalidStr (2);
503  invalidStr.Append (c);
504  errors->PushOnBack (new KKStr ("Invalid character: " + invalidStr + " at position: " + StrFormatInt (x, "##0")));
505  }
506  }
507  }
508 
509  if (errors->QueueSize () < 1)
510  {
511  delete errors;
512  errors = NULL;
513  }
514 
515  return errors;
516 } /* osValidFileNameErrors */
517 
518 
519 
520 
521 
522 
523 bool KKB::osDeleteFile (KKStr _fileName)
524 {
525  #ifdef WIN32
526 
527  const char* fileName = _fileName.Str ();
528 
529  if (DeleteFile (fileName))
530  {
531  return true;
532  }
533  else
534  {
535  DWORD fileAttributes = GetFileAttributes (fileName);
536  fileAttributes = FILE_ATTRIBUTE_NORMAL;
537  if (!SetFileAttributes (fileName, fileAttributes))
538  {
539  return false;
540  }
541 
542  else
543  {
544  // At this point we can either delete this file or not.
545  return (DeleteFile (fileName) != 0);
546  }
547  }
548 
549 
550  #else
551  kkint32 returnCd;
552 
553  // We are in Unix Environment.
554  returnCd = unlink (_fileName.Str ());
555  return (returnCd == 0);
556  #endif
557 } /* DeleteFile */
558 
559 
560 
561 
562 
563 
564 
565 
566 #ifdef WIN32
567 
568 bool KKB::osFileExists (const KKStr& _fileName)
569 {
570  const char* fileName = _fileName.Str ();
571 
572  #define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
573 
574  DWORD fileAttributes = GetFileAttributes (fileName);
575  if (fileAttributes == INVALID_FILE_ATTRIBUTES)
576  {
577  // Error getting Attributes. File may not exist.
578  DWORD lastErrorNum = GetLastError ();
579  if ((lastErrorNum == ERROR_FILE_NOT_FOUND) || (lastErrorNum == 3))
580  return false;
581  }
582 
583  return true;
584 }
585 
586 
587 #else
588 
589 
590 bool KKB::osFileExists (const KKStr& _fileName)
591 {
592  struct stat buff;
593  kkint32 result;
594 
595  result = stat (_fileName.Str (), &buff);
596  if (result == 0)
597  return true;
598  else
599  return false;
600 }
601 
602 #endif
603 
604 
605 
606 
607 
608 
609 bool KKB::osMoveFileBetweenDirectories (const KKStr& _fileName,
610  const KKStr& _srcDir,
611  const KKStr& _destDir
612  )
613 {
614  #ifdef WIN32
615 
616  KKStr srcName (_srcDir);
617  KKStr destName (_destDir);
618 
619  if (srcName.LastChar () != '\\')
620  srcName << "\\";
621 
622  if (destName.LastChar () != '\\')
623  destName << "\\";
624 
625  srcName << _fileName;
626  destName << _fileName;
627 
628  return (MoveFile (srcName.Str (), destName.Str ()) != 0);
629 
630  #else
631  cerr << std::endl;
632  cerr << "*** osMoveFileBetweenDirectories ***" << std::endl;
633  cerr << std::endl;
634  cerr << "*** Not yet implemented ***" << std::endl;
635  osWaitForEnter ();
636  exit (-1);
637  #endif
638 }
639 
640 
641 
642 #ifdef WIN32
643 bool KKB::osCopyFileBetweenDirectories (const KKStr& _fileName,
644  const KKStr& _srcDir,
645  const KKStr& _destDir
646  )
647 {
648  KKStr existingFile (_srcDir);
649  KKStr destinationName (_destDir);
650  BOOL fail = 1;
651 
652  osAddLastSlash (existingFile);
653  existingFile << _fileName;
654 
655  osAddLastSlash (destinationName);
656  destinationName << _fileName;
657 
658  bool result = (CopyFile (existingFile.Str (),
659  destinationName.Str (),
660  fail) != 0);
661 
662  if (result)
663  return true;
664  else
665  return false;
666 }
667 
668 #else
669 
670 bool KKB::osCopyFileBetweenDirectories (const KKStr& _fileName,
671  const KKStr& _srcDir,
672  const KKStr& _destDir
673  )
674 {
675  cerr << std::endl;
676  cerr << "*** osCopyFileBetweenDirectories ***" << std::endl;
677  cerr << std::endl;
678  cerr << "*** Not yet implemented ***" << std::endl;
679  osWaitForEnter ();
680  exit (-1);
681 }
682 
683 #endif
684 
685 
686 
687 
688 #ifdef WIN32
689 bool KKB::osCopyFile (const KKStr& srcFileName,
690  const KKStr& destFileName
691  )
692 {
693  BOOL fail = 1;
694 
695  bool result = (CopyFile (srcFileName.Str (),
696  destFileName.Str (),
697  fail) != 0);
698 
699 
700  if (result)
701  {
702  return true;
703  }
704 
705  else
706  {
707  DWORD lastError = GetLastError ();
708 
709  KKStr errorMessage = StrFormatInt (lastError, "#,###,##0");
710 
711  cerr << std::endl
712  << "osCopyFile *** ERROR ***" << std::endl
713  << " srcFileName [" << srcFileName << "]" << std::endl
714  << " destFileName[" << destFileName << "]" << std::endl
715  << " Error Code [" << lastError << "]" << std::endl
716  << " Error Msg [" << errorMessage << "]" << std::endl;
717  return false;
718  }
719 }
720 
721 #else
722 
723 
724 
725 bool KKB::osCopyFile (const KKStr& srcFileName,
726  const KKStr& destFileName
727  )
728 {
729  cerr << std::endl;
730  cerr << "*** osCopyFile ***" << std::endl;
731  cerr << std::endl;
732  cerr << "*** Not yet implemented ***" << std::endl;
733  osWaitForEnter ();
734  exit (-1);
735 }
736 
737 #endif
738 
739 
740 
741 #ifdef WIN32
742 
743 bool KKB::osRenameFile (const KKStr& oldName,
744  const KKStr& newName
745  )
746 {
747  bool returnCd = (MoveFile (oldName.Str (), newName.Str ()) != 0);
748 
749  if (!returnCd)
750  {
751  DWORD lastError = GetLastError ();
752 
753  cerr << std::endl
754  << "osRenameFile *** ERROR ***, Rename Failed" << std::endl
755  << std::endl
756  << " oldName[" << oldName << "] NewName[" << newName << "]" << std::endl
757  << " LastError[" << lastError << "]" << std::endl
758  << std::endl;
759 
760  }
761 
762  return returnCd;
763 
764 } /* osRenameFile */
765 #else
766 
767 
768 bool KKB::osRenameFile (const KKStr& oldName,
769  const KKStr& newName
770  )
771 {
772 
773  kkint32 returnCd = rename (oldName.Str (), newName.Str ());
774 
775  if (returnCd == 0)
776  return true;
777 
778  kkint32 errorCode = errno;
779 
780  cerr << std::endl
781  << "osRenameFile *** ERROR ***, Rename Failed" << std::endl
782  << " oldName[" << oldName << "] NewName[" << newName << "]" << std::endl
783  << " errno[" << errorCode << "]" << std::endl
784  << std::endl;
785 
786  return false;
787 } /* osRenameFile */
788 #endif
789 
790 
791 
792 
793 void KKB::osChangeDir (const KKStr& dirName,
794  bool& successful
795  )
796 {
797 #ifdef WIN32
798 
799 
800  BOOL changeOk = SetCurrentDirectory(dirName.Str ());
801  if (changeOk)
802  successful = true;
803  else
804  successful = false;
805 
806 #else
807  kkint32 errorCd = chdir (dirName.Str ());
808  successful = (errorCd == 0);
809 
810  if (!successful)
811  {
812  cerr << std::endl << std::endl << std::endl
813  << "osChangeDir *** ERROR *** DirPath[" << dirName << "]" << std::endl
814  << std::endl;
815  }
816 
817 #endif
818 
819 
820  return;
821 } /* osChangeDir */
822 
823 
824 
825 bool KKB::osCreateDirectoryPath (KKStr _pathName)
826 {
827  KKStr nextPartOfPath;
828 
829  if (_pathName.FirstChar () == DSchar)
830  {
831  nextPartOfPath = DS;
832  _pathName = _pathName.SubStrPart (1);
833  nextPartOfPath << _pathName.ExtractToken (DS);
834  }
835  else
836  {
837  nextPartOfPath = _pathName.ExtractToken (DS);
838  }
839 
840  KKStr pathNameSoFar;
841 
842  while (!nextPartOfPath.Empty ())
843  {
844  if (!pathNameSoFar.Empty ())
845  {
846  if (pathNameSoFar.LastChar () != DSchar)
847  pathNameSoFar << DS;
848  }
849 
850  pathNameSoFar << nextPartOfPath;
851 
852  if (!KKB::osValidDirectory (pathNameSoFar))
853  {
854  bool createdSucessfully = osCreateDirectory (pathNameSoFar);
855  if (!createdSucessfully)
856  {
857  cerr << std::endl
858  << "osCreateDirectoryPath Error Creating Directory[" << pathNameSoFar << "]"
859  << std::endl;
860  return false;
861  }
862  }
863 
864  nextPartOfPath = _pathName.ExtractToken (DS);
865  }
866 
867 
868  return true;
869 } /* osCreateDirectoryPath */
870 
871 
872 
873 
874 
875 bool KKB::osCreateDirectory (const KKStr& _dirName)
876 {
877  #ifdef WIN32
878  return (CreateDirectory (_dirName.Str (), NULL) != 0);
879 
880  #else
881 
882  kkint32 result;
883 
884  mode_t mode = S_IRWXU + S_IRWXG;
885 
886  result = mkdir (_dirName.Str (), mode);
887  return result == 0;
888 
889  #endif
890 }
891 
892 
893 
894 
895 #ifdef WIN32
896 
897 KKStrPtr KKB::osGetEnvVariable (const KKStr& _varName)
898 {
899  char buff[1024];
900  DWORD varLen;
901 
902  varLen = GetEnvironmentVariable (_varName.Str (), buff, sizeof (buff));
903 
904  if (varLen == 0)
905  {
906  return NULL;
907  }
908  else
909  {
910  return new KKStr (buff);
911  }
912 } /* GetEnvVariable */
913 
914 #else
915 
916 
917 KKStrPtr KKB::osGetEnvVariable (const KKStr& _varName)
918 {
919  char* envStrValue = NULL;
920 
921  envStrValue = getenv (_varName.Str ());
922 
923  if (envStrValue)
924  return new KKStr (envStrValue);
925  else
926  return NULL;
927 
928 } /* GetEnvVariable */
929 #endif
930 
931 
932 
933 /**
934  * Searches a string starting from a specified index for the start of an Environment String Specification.
935  * @code
936  * 1 2 3 4 5 6
937  * ex: 0123456789012345678901234567890123456789012345678901234567890123456789
938  * str = "TrainingDirectory\${LarcosHomeDir}\Classifiers\${CurTrainLibrary"
939  *
940  * idx = osLocateEnvStrStart (str, 0); // returns 18
941  * idx = osLocateEnvStrStart (str, 34) // returns 47
942  * @endcode
943  *
944  * @param str Starting that is to be searched.
945  * @param startIdx Index search is to start at.
946  * @returns Index of 1st character of a Environment String specifier or -1 if none was found.
947  */
948 int osLocateEnvStrStart (const KKStr& str,
949  kkint32 startIdx /**< Index in 'str' to start search from. */
950  )
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 */
974 
975 
976 
978 {
980  if (x < 0) return src;
981 
982  KKStr str (src);
983 
984  while (x >= 0)
985  {
986  char startChar = src[(kkint16)(x + 1)];
987  char endChar = ')';
988 
989  if (startChar == '(') endChar = ')';
990  else if (startChar == '{') endChar = '}';
991  else if (startChar == '[') endChar = ']';
992 
993  KKStr beforeEnvStr = str.SubStrPart (0, x - 1);
994  str = str.SubStrPart (x + 2);
995  x = str.LocateCharacter (endChar);
996  if (x < 0) return src;
997 
998  KKStr envStrName = str.SubStrPart (0, x - 1);
999  KKStr afterStrName = str.SubStrPart (x + 1);
1000 
1001  KKStrPtr envStrValue = osGetEnvVariable (envStrName);
1002  if (envStrValue == NULL)
1003  envStrValue = new KKStr ("${" + envStrName + "}");
1004 
1005  kkuint32 idxToStartAtNextTime = beforeEnvStr.Len () + envStrValue->Len ();
1006  str = beforeEnvStr + (*envStrValue) + afterStrName;
1007  delete envStrValue;
1008  x = osLocateEnvStrStart (str, idxToStartAtNextTime);
1009  }
1010 
1011  return str;
1012 } /* osSubstituteInEnvironmentVariables */
1013 
1014 
1015 
1016 
1018 {
1019  kkint32 lastLeftSlash = fileSpec.LocateLastOccurrence ('\\');
1020  kkint32 lastRightSlash = fileSpec.LocateLastOccurrence ('/');
1021 
1022  return Max (lastLeftSlash, lastRightSlash);
1023 } /* LastSlashChar */
1024 
1025 
1026 
1028 {
1029  kkint32 firstForewardSlash = fileSpec.LocateCharacter ('/');
1030  kkint32 firstBackSlash = fileSpec.LocateCharacter ('\\');
1031 
1032  if (firstForewardSlash < 0)
1033  return firstBackSlash;
1034 
1035  else if (firstBackSlash < 0)
1036  return firstForewardSlash;
1037 
1038  return Min (firstForewardSlash, firstBackSlash);
1039 } /* LastSlashChar */
1040 
1041 
1042 
1043 
1044 
1045 void KKB::osAddLastSlash (KKStr& fileSpec)
1046 {
1047  char c = fileSpec.LastChar ();
1048 
1049  if ((c != '\\') && (c != '/'))
1050  fileSpec << DS;
1051 } /* osAddLastSlash */
1052 
1053 
1054 
1055 KKStr KKB::osAddSlash (const KKStr& fileSpec)
1056 {
1057  KKStr result (fileSpec);
1058  if ((result.LastChar () != '\\') && (result.LastChar () != '/'))
1059  result << DS;
1060  return result;
1061 } /* OsAddLastSlash */
1062 
1063 
1064 
1065 
1066 KKStr KKB::osMakeFullFileName (const KKStr& _dirName,
1067  const KKStr& _fileName
1068  )
1069 {
1070  KKStr fullFileName (_dirName);
1071 
1072  osAddLastSlash (fullFileName);
1073 
1074  fullFileName << _fileName;
1075 
1076  return fullFileName;
1077 }
1078 
1079 
1080 
1082 {
1083  if (dirPath.LastChar () == DSchar)
1084  dirPath.ChopLastChar ();
1085 
1086  KKStr path, root, ext;
1087  osParseFileName (dirPath, path, root, ext);
1088 
1089  if (ext.Empty ())
1090  return root;
1091  else
1092  return root + "." + ext;
1093 } /* osGetDirNameFromPath */
1094 
1095 
1096 
1097 void KKB::osParseFileName (KKStr _fileName,
1098  KKStr& _dirPath,
1099  KKStr& _rootName,
1100  KKStr& _extension
1101  )
1102 {
1103  kkint32 x;
1104 
1105  x = osLocateLastSlashChar (_fileName);
1106 
1107  if (x < 0)
1108  {
1109  _dirPath = "";
1110  }
1111 
1112  else
1113  {
1114  _dirPath = _fileName.SubStrPart (0, x - 1);
1115  _fileName = _fileName.SubStrPart (x + 1);
1116  }
1117 
1118 
1119  x = _fileName.LocateLastOccurrence ('.');
1120  if (x < 0)
1121  {
1122  _rootName = _fileName;
1123  _extension = "";
1124  }
1125  else
1126  {
1127  _rootName = _fileName.SubStrPart (0, x - 1);
1128  _extension = _fileName.SubStrPart (x + 1);
1129  }
1130 
1131  return;
1132 } /* ParseFileName */
1133 
1134 
1135 
1136 KKStr KKB::osRemoveExtension (const KKStr& _fullFileName)
1137 {
1138  kkint32 lastSlashChar = osLocateLastSlashChar (_fullFileName);
1139 
1140  kkint32 lastPeriodChar = _fullFileName.LocateLastOccurrence ('.');
1141 
1142  if (lastPeriodChar < lastSlashChar)
1143  return KKStr (_fullFileName);
1144 
1145  if (lastPeriodChar <= 1)
1146  return KKStr (_fullFileName);
1147 
1148  return _fullFileName.SubStrPart (0, lastPeriodChar - 1);
1149 } /* osRemoveExtension */
1150 
1151 
1152 
1153 
1154 KKStr KKB::osGetRootName (const KKStr& fullFileName)
1155 {
1156  kkint32 lastSlashChar = osLocateLastSlashChar (fullFileName);
1157  kkint32 lastColon = fullFileName.LocateLastOccurrence (':');
1158 
1159  kkint32 lastSlashOrColon = Max (lastSlashChar, lastColon);
1160 
1161  KKStr lastPart;
1162  if (lastSlashOrColon < 0)
1163  {
1164  lastPart = fullFileName;
1165  }
1166  else
1167  {
1168  lastPart = fullFileName.SubStrPart (lastSlashOrColon + 1);
1169  }
1170 
1171  kkint32 period = lastPart.LocateLastOccurrence ('.');
1172 
1173  if (period < 0)
1174  return lastPart;
1175 
1176  return lastPart.SubStrPart (0, period - 1);
1177 } /* osGetRootName */
1178 
1179 
1180 
1182 {
1183  if (fullDirName.LastChar () == DSchar)
1184  fullDirName.ChopLastChar ();
1185 
1186  kkint32 lastSlashChar = osLocateLastSlashChar (fullDirName);
1187  kkint32 lastColon = fullDirName.LocateLastOccurrence (':');
1188 
1189  kkint32 lastSlashOrColon = Max (lastSlashChar, lastColon);
1190 
1191  KKStr lastPart;
1192  if (lastSlashOrColon < 0)
1193  lastPart = fullDirName;
1194  else
1195  lastPart = fullDirName.SubStrPart (lastSlashOrColon + 1);
1196 
1197  return lastPart;
1198 } /* osGetRootNameOfDirectory */
1199 
1200 
1201 
1203 {
1204  if (path.LastChar () == DSchar)
1205  path.ChopLastChar ();
1206 
1208  kkint32 x2 = path.LocateLastOccurrence (':');
1209 
1210  kkint32 x = Max (x1, x2);
1211  if (x < 1)
1212  return KKStr::EmptyStr ();
1213 
1214  return path.SubStrPart (0, x);
1215 } /* osGetParentDirectoryOfDirPath */
1216 
1217 
1218 
1220 {
1221  kkint32 lastSlashChar = osLocateLastSlashChar (fullFileName);
1222  kkint32 lastColon = fullFileName.LocateLastOccurrence (':');
1223 
1224  kkint32 lastSlashOrColon = Max (lastSlashChar, lastColon);
1225 
1226  KKStr lastPart;
1227  if (lastSlashOrColon < 0)
1228  {
1229  lastPart = fullFileName;
1230  }
1231  else
1232  {
1233  lastPart = fullFileName.SubStrPart (lastSlashOrColon + 1);
1234  }
1235 
1236  return lastPart;
1237 } /* osGetRootNameWithExtension */
1238 
1239 
1240 
1241 
1242 
1243 void KKB::osParseFileSpec (KKStr fullFileName,
1244  KKStr& driveLetter,
1245  KKStr& path,
1246  KKStr& root,
1247  KKStr& extension
1248  )
1249 {
1250  path = "";
1251  root = "";
1252  extension = "";
1253  driveLetter = "";
1254 
1255  // Look for Drive Letter
1256  kkint32 driveLetterPos = fullFileName.LocateCharacter (':');
1257  if (driveLetterPos >= 0)
1258  {
1259  driveLetter = fullFileName.SubStrPart (0, driveLetterPos - 1);
1260  fullFileName = fullFileName.SubStrPart (driveLetterPos + 1);
1261  }
1262 
1263  KKStr fileName;
1264 
1265  if (fullFileName.LastChar () == DSchar)
1266  {
1267  // FileSpec must look like 'c:\xxx\xx\'
1268  path = fullFileName;
1269  root = "";
1270  extension = "";
1271  return;
1272  }
1273 
1274  kkint32 lastSlash = osLocateLastSlashChar (fullFileName);
1275  if (lastSlash < 0)
1276  {
1277  path = "";
1278  fileName = fullFileName;
1279  }
1280  else
1281  {
1282  path = fullFileName.SubStrPart (0, lastSlash - 1);
1283  fileName = fullFileName.SubStrPart (lastSlash + 1);
1284  }
1285 
1286  kkint32 period = fileName.LocateLastOccurrence ('.');
1287  if (period <= 0)
1288  {
1289  root = fileName;
1290  extension = "";
1291  }
1292  else
1293  {
1294  root = fileName.SubStrPart (0, period - 1);
1295  extension = fileName.SubStrPart (period + 1);
1296  }
1297 
1298  return;
1299 } /* osParseFileSpec */
1300 
1301 
1302 
1303 
1304 
1306 {
1307  kkint32 lastSlash = osLocateLastSlashChar (fullFileName);
1308 
1309  if (lastSlash >= 0)
1310  {
1311  return fullFileName.SubStrPart (0, lastSlash - 1);
1312  }
1313 
1314  kkint32 lastColon = fullFileName.LocateLastOccurrence (':');
1315  if (lastColon >= 0)
1316  return fullFileName.SubStrPart (0, lastColon);
1317  else
1318  return KKStr ("");
1319 } /* GetPathPartOfFile */
1320 
1321 
1322 
1324 {
1325  KKStr fileName, dirPath, rootName, extension;
1326  osParseFileName (fullFileName, dirPath, rootName, extension);
1327  return extension;
1328 } /* osGetFileExtension */
1329 
1330 
1331 
1332 
1334 {
1335  if ((dirPath.LastChar () == '\\') || (dirPath.LastChar () == '/'))
1336  dirPath.ChopLastChar ();
1337 
1338  int x = Max (dirPath.LocateLastOccurrence ('\\'), dirPath.LocateLastOccurrence ('/'));
1339 
1340  if (x < 0)
1341  {
1342  x = dirPath.LocateLastOccurrence (':');
1343  if (x >= 0)
1344  return dirPath.SubStrPart (0, x - 1);
1345  else
1346  return KKStr::EmptyStr ();
1347  }
1348 
1349  return dirPath.SubStrPart (0, x - 1);
1350 }
1351 
1352 
1353 
1355 {
1356 #if defined(OS_WINDOWS)
1357  char buff[1024];
1358  DWORD buffSize = sizeof (buff) - 1;
1359  memset (buff, 0, sizeof(buff));
1360 
1361  KKStr compName = "";
1362 
1363  //BOOL returnCd = GetComputerNameEx (ComputerNameDnsFullyQualified, buff, &buffSize);
1364  BOOL returnCd = GetComputerNameA (buff, &buffSize);
1365  if (returnCd != 0)
1366  {
1367  compName = buff;
1368  }
1369  else
1370  {
1371  KKStrPtr compNameStr = osGetEnvVariable ("COMPUTERNAME");
1372  if (compNameStr)
1373  {
1374  compName = *compNameStr;
1375  delete compNameStr;
1376  compNameStr = NULL;
1377  }
1378  else
1379  {
1380  compName = "";
1381  }
1382  }
1383 
1384  return compName;
1385 
1386 
1387 #else
1388 
1389  char buff[1024];
1390  memset (buff, 0, sizeof (buff));
1391  kkint32 returnCd = gethostname (buff, sizeof (buff) - 2);
1392  if (returnCd != 0)
1393  return "";
1394  else
1395  return buff;
1396 
1397 #endif
1398 } /* osGetHostName */
1399 
1400 
1401 
1403 {
1404 #if defined(OS_WINDOWS)
1405  KKStr progName;
1406 
1407  char filename[ MAX_PATH ];
1408  DWORD size = GetModuleFileNameA (NULL, filename, MAX_PATH);
1409  if (size)
1410  progName = filename;
1411  else
1412  progName = "";
1413 
1414  return progName;
1415 
1416 #else
1417  return "NotImplemented";
1418 #endif
1419 }
1420 
1421 
1422 
1423 
1424 
1425 
1427 {
1428 #if defined(OS_WINDOWS)
1429  TCHAR name [ UNLEN + 1 ];
1430  DWORD size = UNLEN + 1;
1431 
1432  KKStr userName = "";
1433  if (GetUserName ((TCHAR*)name, &size))
1434  userName = name;
1435  else
1436  userName = "***ERROR***";
1437 
1438  return userName;
1439 #else
1440 
1441  return "NoImplemented";
1442 #endif
1443 } /* osGetUserName */
1444 
1445 
1446 
1447 
1448 
1450 {
1451 #if defined(OS_WINDOWS)
1452  KKStrPtr numProcessorsStr = osGetEnvVariable ("NUMBER_OF_PROCESSORS");
1453  kkuint32 numOfProcessors = -1;
1454  if (numProcessorsStr)
1455  {
1456  numOfProcessors = numProcessorsStr->ToInt32 ();
1457  delete numProcessorsStr;
1458  numProcessorsStr = NULL;
1459  }
1460 
1461  return numOfProcessors;
1462 #else
1463  /** @todo Need to implement 'osGetNumberOfProcessors' for linux. */
1464  return 1;
1465 #endif
1466 } /* osGetNumberOfProcessors */
1467 
1468 
1469 
1470 
1472 {
1473  if (fullFileName.LastChar () == DSchar)
1474  return KKStr ("");
1475 
1476  kkint32 lastSlash = osLocateLastSlashChar (fullFileName);
1477  if (lastSlash < 0)
1478  {
1479  kkint32 colon = fullFileName.LocateCharacter (':');
1480  if (colon < 0)
1481  return fullFileName;
1482  else
1483  return fullFileName.SubStrPart (colon + 1);
1484  }
1485  else
1486  {
1487  return fullFileName.SubStrPart (lastSlash + 1);
1488  }
1489 } /* FileNamePartOfFile */
1490 
1491 
1492 
1493 
1494 
1495 bool backGroundProcess = false;
1496 
1497 
1498 
1500 {
1501  backGroundProcess = true;
1502 }
1503 
1504 
1505 
1507 {
1508  return backGroundProcess;
1509 }
1510 
1511 
1512 
1514 {
1515  if (backGroundProcess)
1516  return;
1517 
1518  cout << std::endl
1519  << std::endl
1520  << "Press Enter To Continue"
1521  << std::endl;
1522 
1523  while (getchar () != '\n');
1524 
1525 } /* osWaitForEnter */
1526 
1527 
1528 
1529 
1530 #ifdef WIN32
1531 KKStrListPtr KKB::osGetListOfFiles (const KKStr& fileSpec)
1532 {
1533  WIN32_FIND_DATA wfd;
1534 
1535  HANDLE handle = FindFirstFile (fileSpec.Str (), &wfd);
1536 
1537  if (handle == INVALID_HANDLE_VALUE)
1538  {
1539  return NULL;
1540  }
1541 
1542  KKStrListPtr nameList = new KKStrList (true);
1543 
1544  BOOL moreFiles = true;
1545  while (moreFiles)
1546  {
1547  if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) == 0)
1548  {
1549  if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
1550  {
1551  nameList->PushOnBack (new KKStr (wfd.cFileName));
1552  }
1553  }
1554 
1555  moreFiles = FindNextFile (handle, &wfd);
1556  }
1557 
1558  FindClose (handle);
1559 
1560  return nameList;
1561 } /* osGetListOfFiles */
1562 
1563 #else
1564 
1565 
1566 
1567 
1568 KKStrListPtr osDirectoryList (KKStr dirName) /* Unix Version of Function */
1569 {
1570  if (dirName.Empty ())
1571  {
1572  dirName = osGetCurrentDirectory ();
1573  }
1574  else
1575  {
1576  osAddLastSlash (dirName);
1577  }
1578 
1579  KKStrListPtr nameList = new KKB::KKStrList (true);
1580 
1581  DIR* openDir = opendir (dirName.Str ());
1582  if (openDir == NULL)
1583  return NULL;
1584 
1585  struct dirent *de;
1586  de = readdir (openDir);
1587 
1588  while (de)
1589  {
1590  KKStr rootName (de->d_name);
1591  if ((rootName != ".") && (rootName != ".."))
1592  {
1593  KKStr fullName (dirName);
1594  fullName << rootName;
1595  struct stat fs;
1596 
1597 
1598  if ((fs.st_mode & S_IFDIR) == 0)
1599  {
1600  nameList->PushOnBack (new KKStr (rootName));
1601  }
1602  }
1603 
1604  de = readdir (openDir);
1605  }
1606 
1607  closedir (openDir);
1608 
1609  return nameList;
1610 } /* osDirectoryList */
1611 
1612 
1613 
1614 
1615 
1616 KKStrListPtr KKB::osGetListOfFiles (const KKStr& fileSpec)
1617 {
1618  KKStr afterLastSlash;
1619  KKStr afterStar;
1620  KKStr beforeStar;
1621  KKStr dirPath;
1622 
1623  kkint32 lastSlash = osLocateLastSlashChar (fileSpec);
1624 
1625  if (lastSlash < 0)
1626  {
1627  dirPath = osGetCurrentDirectory ();
1628  afterLastSlash = fileSpec;
1629  }
1630  else
1631  {
1632  dirPath = fileSpec.SubStrPart (0, lastSlash);
1633  afterLastSlash = fileSpec.SubStrPart (lastSlash + 1);
1634  }
1635 
1636  osAddLastSlash (dirPath);
1637 
1638  KKStrListPtr allFilesInDirecory = osDirectoryList (dirPath);
1639 
1640  if (!allFilesInDirecory)
1641  return NULL;
1642 
1643  KKStrListPtr searchSpecParms = osParseSearchSpec (afterLastSlash);
1644 
1645  KKStrListPtr resultList = new KKStrList (true);
1646 
1647  KKStrPtr name = NULL;
1648  KKStrList::iterator nameIDX;
1649  for (nameIDX = allFilesInDirecory->begin (); nameIDX != allFilesInDirecory->end (); ++nameIDX)
1650  {
1651  name = *nameIDX;
1652  if (osFileNameMatchesSearchFields (*name, searchSpecParms))
1653  resultList->PushOnBack (new KKStr (*name));
1654  }
1655 
1656 
1657  delete allFilesInDirecory;
1658  delete searchSpecParms;
1659 
1660  return resultList;
1661 } /* osGetListOfFiles */
1662 
1663 #endif
1664 
1665 
1666 
1667 
1669  KKStr fileSpec,
1670  VectorKKStr& fileNames // The file names include full path.
1671  )
1672 {
1673  if (fileSpec.Empty ())
1674  fileSpec = "*.*";
1675 
1676  {
1677  KKStrListPtr filesInThisDirectory = osGetListOfFiles (osAddSlash (rootDir) + fileSpec);
1678  if (filesInThisDirectory)
1679  {
1680  KKStrList::iterator idx;
1681  for (idx = filesInThisDirectory->begin (); idx != filesInThisDirectory->end (); idx++)
1682  {
1683  KKStrPtr fn = *idx;
1684  KKStr fullName = osAddSlash (rootDir) + (*fn);
1685  fileNames.push_back (fullName);
1686  }
1687  delete filesInThisDirectory; filesInThisDirectory = NULL;
1688  }
1689  }
1690 
1691  // Lets now process all sub directories below 'rootDir'
1692  KKStrListPtr subDirectories = osGetListOfDirectories (osAddSlash (rootDir) + "*.*");
1693  if (subDirectories)
1694  {
1695  KKStrList::iterator idx;
1696  for (idx = subDirectories->begin (); idx != subDirectories->end (); idx++)
1697  {
1698  KKStr subDirName = **idx;
1699  if ((subDirName == ".") || (subDirName == ".."))
1700  continue;
1701 
1702 
1703  KKStr dirToSearch = osAddSlash (rootDir) + subDirName;
1704  osGetListOfFilesInDirectoryTree (dirToSearch, fileSpec, fileNames);
1705  }
1706 
1707  delete subDirectories; subDirectories = NULL;
1708  }
1709 
1710  return;
1711 } /* osGetListOfFilesInDirectoryTree */
1712 
1713 
1714 
1715 
1716 
1717 KKStrListPtr KKB::osGetListOfImageFiles (KKStr fileSpec)
1718 {
1719  KKStrListPtr imageFileNames = new KKStrList (true);
1720 
1721  KKStrListPtr filesInDir = osGetListOfFiles (fileSpec);
1722  if (filesInDir)
1723  {
1724  KKStrList::iterator fnIDX;
1725  for (fnIDX = filesInDir->begin (); fnIDX != filesInDir->end (); ++fnIDX)
1726  {
1727  KKStr fileName (**fnIDX);
1728  if (SupportedImageFileFormat (fileName))
1729  imageFileNames->PushOnBack (new KKStr (fileName));
1730  }
1731 
1732  delete filesInDir;
1733  filesInDir = NULL;
1734  }
1735 
1736  return imageFileNames;
1737 } /* osGetListOfImageFiles */
1738 
1739 
1740 
1741 
1742 #ifdef WIN32
1743 KKStrListPtr KKB::osGetListOfDirectories (KKStr fileSpec)
1744 {
1745  WIN32_FIND_DATA wfd;
1746 
1747 
1748  if (fileSpec.LastChar () == DSchar)
1749  {
1750  fileSpec << "*.*";
1751  }
1752 
1753  else if (fileSpec.LocateCharacter ('*') < 0)
1754  {
1755  if (osValidDirectory (&fileSpec))
1756  {
1757  fileSpec << "\\*.*";
1758  }
1759  }
1760 
1761  KKStrListPtr nameList = new KKStrList (true);
1762 
1763  HANDLE handle = FindFirstFile (fileSpec.Str (), &wfd);
1764 
1765  if (handle == INVALID_HANDLE_VALUE)
1766  {
1767  delete nameList;
1768  return NULL;
1769  }
1770 
1771 
1772  BOOL moreFiles = true;
1773  while (moreFiles)
1774  {
1775  if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) == 0)
1776  {
1777  if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
1778  {
1779  KKStrPtr dirName = new KKStr (wfd.cFileName);
1780 
1781  if ((*dirName != ".") && (*dirName != ".."))
1782  nameList->PushOnBack (dirName);
1783  else
1784  delete dirName;
1785  }
1786  }
1787 
1788  moreFiles = FindNextFile (handle, &wfd);
1789  }
1790 
1791  FindClose (handle);
1792 
1793  return nameList;
1794 } /* osGetListOfDirectories */
1795 
1796 
1797 
1798 
1799 #else
1800 KKStrListPtr KKB::osGetListOfDirectories (KKStr fileSpec)
1801 {
1802 
1803  KKStr rootDirName;
1804  kkint32 x = fileSpec.LocateCharacter ('*');
1805  if (x > 0)
1806  rootDirName = fileSpec.SubStrPart (0, x - 1);
1807  else
1808  rootDirName = fileSpec;
1809 
1810 
1811  osAddLastSlash (rootDirName);
1812 
1813  KKStrListPtr nameList = new KKStrList (true);
1814 
1815  DIR* openDir = opendir (rootDirName.Str ());
1816  if (openDir == NULL)
1817  return NULL;
1818 
1819  struct dirent *de;
1820  de = readdir (openDir);
1821 
1822  while (de)
1823  {
1824  KKStr rootName (de->d_name);
1825  if ((rootName != ".") && (rootName != ".."))
1826  {
1827  KKStr fullName (rootDirName);
1828  fullName << rootName;
1829  struct stat fs;
1830 
1831 
1832  if ((fs.st_mode & S_IFDIR) != 0)
1833  {
1834  nameList->PushOnBack (new KKStr (rootName));
1835  }
1836  }
1837 
1838  de = readdir (openDir);
1839  }
1840 
1841  closedir (openDir);
1842 
1843  return nameList;
1844 } /* osGetListOfDirectories */
1845 
1846 #endif
1847 
1848 
1849 
1850 
1851 
1852 
1853 
1854 #ifdef WIN32
1856 {
1857  HANDLE h = GetCurrentProcess ();
1858 
1859  FILETIME creationTime, exitTime, kernelTime, userTime;
1860 
1861  BOOL ok = GetProcessTimes (h, &creationTime, &exitTime, &kernelTime, &userTime);
1862 
1863  if (!ok)
1864  return 0;
1865 
1866  SYSTEMTIME st;
1867 
1868  FileTimeToSystemTime(&kernelTime, &st);
1869  double kt = st.wHour * 3600 + st.wMinute * 60 + st.wSecond;
1870  kt += ((double)st.wMilliseconds / 1000.0);
1871 
1872  FileTimeToSystemTime(&userTime, &st);
1873  double ut = st.wHour * 3600 + st.wMinute * 60 + st.wSecond;
1874  ut += ((double)st.wMilliseconds / 1000.0);
1875 
1876  double numOfSecs = kt + ut;
1877 
1878  // (kernelTime.dwLowDateTime + userTime.dwLowDateTime) / 10000000 + 0.5;
1879  return numOfSecs;
1880 } /* osGetSystemTimeUsed */
1881 
1882 #else
1883 double KKB::osGetSystemTimeUsed ()
1884 {
1885  struct tms buff;
1886  times (&buff);
1887  double totalTime = (double)(buff.tms_utime + buff.tms_stime);
1888  return (totalTime / (double)(sysconf (_SC_CLK_TCK)));
1889 }
1890 #endif
1891 
1892 
1893 
1894 
1895 
1896 #ifdef WIN32
1898 {
1899  HANDLE h = GetCurrentProcess ();
1900 
1901  FILETIME creationTime, exitTime, kernelTime, userTime;
1902 
1903  BOOL ok = GetProcessTimes (h, &creationTime, &exitTime, &kernelTime, &userTime);
1904 
1905  if (!ok)
1906  return 0;
1907 
1908 
1909  SYSTEMTIME st;
1910 
1911  FileTimeToSystemTime(&userTime, &st);
1912  double ut = st.wHour * 3600 +
1913  st.wMinute * 60 +
1914  st.wSecond +
1915  st.wMilliseconds / 1000.0;
1916 
1917  return ut;
1918 } /* osGetSystemTimeUsed */
1919 
1920 #else
1921 
1922 
1923 
1924 
1925 double KKB::osGetUserTimeUsed ()
1926 {
1927  struct tms buff;
1928  times (&buff);
1929  double totalTime = (double)(buff.tms_utime);
1930  return (totalTime / (double)(sysconf (_SC_CLK_TCK)));
1931 }
1932 #endif
1933 
1934 
1935 
1936 
1937 #ifdef WIN32
1939 {
1940  HANDLE h = GetCurrentProcess ();
1941 
1942  FILETIME creationTime, exitTime, kernelTime, userTime;
1943 
1944  BOOL ok = GetProcessTimes (h, &creationTime, &exitTime, &kernelTime, &userTime);
1945 
1946  if (!ok)
1947  return 0.0;
1948 
1949  SYSTEMTIME st;
1950 
1951  FileTimeToSystemTime(&kernelTime, &st);
1952  double kt = st.wHour * 3600 +
1953  st.wMinute * 60 +
1954  st.wSecond +
1955  st.wMilliseconds / 1000.0;
1956 
1957 
1958  return kt;
1959 } /* osGetSystemTimeUsed */
1960 
1961 #else
1962 double KKB::osGetKernalTimeUsed ()
1963 {
1964  struct tms buff;
1965  times (&buff);
1966  double totalTime = (double)(buff.tms_stime);
1967  return (totalTime / (double)(sysconf (_SC_CLK_TCK)));
1968 } /* osGetSystemTimeUsed */
1969 #endif
1970 
1971 
1972 
1973 
1974 
1975 #ifdef WIN32
1977 {
1978  return timeGetTime();
1979 } /* osGetSystemTimeInMiliSecs */
1980 
1981 #else
1982 kkuint64 KKB::osGetSystemTimeInMiliSecs ()
1983 {
1984  struct timeval now;
1985  gettimeofday(&now, NULL);
1986  return now.tv_usec/1000;
1987 } /* osGetSystemTimeInMiliSecs */
1988 #endif
1989 
1990 
1991 
1992 #ifdef WIN32
1994 {
1995  SYSTEMTIME sysTime;
1996 
1997  GetLocalTime(&sysTime);
1998 
1999  DateTime dateTime ((short)sysTime.wYear,
2000  (uchar)sysTime.wMonth,
2001  (uchar)sysTime.wDay,
2002  (uchar)sysTime.wHour,
2003  (uchar)sysTime.wMinute,
2004  (uchar)sysTime.wSecond
2005  );
2006 
2007  return dateTime;
2008 } /* osGetCurrentDateTime */
2009 
2010 
2011 
2012 
2013 #else
2014 DateTime KKB::osGetLocalDateTime ()
2015 {
2016  struct tm *curTime;
2017  time_t long_time;
2018 
2019  time (&long_time); /* Get time as long integer. */
2020  curTime = localtime (&long_time); /* Convert to local time. */
2021 
2022  DateTime dateTime (curTime->tm_year + 1900,
2023  curTime->tm_mon + 1,
2024  curTime->tm_mday,
2025  curTime->tm_hour,
2026  curTime->tm_min,
2027  curTime->tm_sec
2028  );
2029 
2030  return dateTime;
2031 } /* osGetCurrentDateTime */
2032 #endif
2033 
2034 
2035 
2036 #ifdef WIN32
2038 {
2039  WIN32_FIND_DATA wfd;
2040 
2041  HANDLE handle = FindFirstFile (fileName.Str (), &wfd);
2042  if (handle == INVALID_HANDLE_VALUE)
2043  {
2044  return DateTime (0, 0, 0, 0 ,0 ,0);
2045  }
2046 
2047 
2048  SYSTEMTIME fileTime;
2049  SYSTEMTIME stLocal;
2050 
2051 
2052  FileTimeToSystemTime (&(wfd.ftLastWriteTime), &fileTime);
2053  SystemTimeToTzSpecificLocalTime(NULL, &fileTime, &stLocal);
2054 
2055  return DateTime ((short)stLocal.wYear,
2056  (uchar)stLocal.wMonth,
2057  (uchar)stLocal.wDay,
2058  (uchar)stLocal.wHour,
2059  (uchar)stLocal.wMinute,
2060  (uchar)stLocal.wSecond
2061  );
2062 
2063 } /* osGetFileDateTime */
2064 
2065 
2066 
2067 #else
2068 
2069 DateTime KKB::osGetFileDateTime (const KKStr& fileName)
2070 {
2071  struct stat buf;
2072 
2073  kkint32 returnCd = stat (fileName.Str (), &buf);
2074 
2075  if (returnCd != 0)
2076  {
2077  return DateTime (0, 0, 0, 0, 0, 0);
2078  }
2079 
2080 
2081  struct tm* dt = localtime (&(buf.st_mtime));
2082 
2083  return DateTime (1900 + dt->tm_year,
2084  dt->tm_mon + 1,
2085  dt->tm_mday,
2086  dt->tm_hour,
2087  dt->tm_min,
2088  dt->tm_sec
2089  );
2090 }
2091 #endif
2092 
2093 
2094 
2095 #ifdef WIN32
2096 kkint64 KKB::osGetFileSize (const KKStr& fileName)
2097 {
2098  WIN32_FIND_DATA wfd;
2099 
2100  HANDLE handle = FindFirstFile (fileName.Str (), &wfd);
2101  if (handle == INVALID_HANDLE_VALUE)
2102  {
2103  return -1;
2104  }
2105 
2106  return (kkint64)(wfd.nFileSizeHigh) * (kkint64)MAXDWORD + (kkint64)(wfd.nFileSizeLow);
2107 }
2108 
2109 
2110 #else
2111 
2112 KKB::kkint64 KKB::osGetFileSize (const KKStr& fileName)
2113 {
2114  struct stat buf;
2115 
2116  kkint32 returnCd = stat (fileName.Str (), &buf);
2117 
2118  if (returnCd != 0)
2119  {
2120  return -1;
2121  }
2122 
2123  return buf.st_size;
2124 }
2125 #endif
2126 
2127 
2128 
2129 
2130 #if defined(WIN32)
2131 void KKB::osDisplayWarning (KKStr _message)
2132 {
2133  MessageBox (NULL,
2134  _message.Str (),
2135  "Warning",
2136  (MB_OK + MB_SETFOREGROUND)
2137  );
2138 /*
2139  cerr << endl
2140  << " *** WARNING ***" << endl
2141  << endl
2142  << _message << endl
2143  << endl;
2144  osWaitForEnter ();
2145 */
2146 }
2147 
2148 
2149 #else
2150 void KKB::osDisplayWarning (KKStr _message)
2151 {
2152  cerr << std::endl
2153  << " *** WARNING ***" << std::endl
2154  << std::endl
2155  << _message << std::endl
2156  << std::endl;
2157 
2158  if (!backGroundProcess)
2159  osWaitForEnter ();
2160 }
2161 #endif
2162 
2163 
2164 
2165 
2166 //*******************************************************************
2167 //* fileName - Name of file we are looking for. *
2168 //* srcDir - Sub Directory tree we want to search. *
2169 //* *
2170 //* Returns - Full directory path to where first occurrence of *
2171 //* fileName is located. If not found will return *
2172 //* back an empty string. *
2173 //*******************************************************************
2174 KKStr KKB::osLookForFile (const KKStr& fileName,
2175  const KKStr& srcDir
2176  )
2177 {
2178  KKStr fileNameUpper (fileName);
2179  fileNameUpper.Upper ();
2180 
2181  KKStr fileSpec = osAddSlash (srcDir) + fileName;
2182  //KKStr fileSpec = osAddSlash (srcDir) + "*.*";
2183 
2184  // We will first look at contents of 'srcDir' and if not there then look at sub directories
2185  KKStrListPtr files = osGetListOfFiles (fileSpec);
2186  if (files)
2187  {
2188  for (KKStrList::iterator nIDX = files->begin (); nIDX != files->end (); nIDX++)
2189  {
2190  KKStrPtr fnPtr = *nIDX;
2191  if (KKStr::StrEqualNoCase (fileName.Str (), fnPtr->Str ()))
2192  {
2193  delete files;
2194  files = NULL;
2195  return srcDir;
2196  }
2197  }
2198  delete files;
2199  files = NULL;
2200  }
2201 
2202  KKStrListPtr subDirs = osGetListOfDirectories (srcDir);
2203  if (subDirs)
2204  {
2205  for (KKStrList::iterator sdIDX = subDirs->begin (); sdIDX != subDirs->end (); sdIDX++)
2206  {
2207  KKStr subDirName = osAddSlash (srcDir) + **sdIDX;
2208  KKStr resultDir = osLookForFile (fileName, subDirName);
2209  if (!resultDir.Empty ())
2210  {
2211  delete subDirs;
2212  subDirs = NULL;
2213  return resultDir;
2214  }
2215  }
2216 
2217  delete subDirs; subDirs = NULL;
2218  }
2219 
2220  return "";
2221 } /* osLookForFile */
2222 
2223 
2224 
2225 
2227 {
2228  if (fileName.Empty ())
2229  fileName = "Temp.txt";
2230 
2231  KKStr dirPath, rootName, extension;
2232 
2233  osParseFileName (fileName, dirPath, rootName, extension);
2234 
2235  kkint32 seqNum = 0;
2236  bool fileNameExists = osFileExists (fileName);
2237  while (fileNameExists)
2238  {
2239  if (dirPath.Empty ())
2240  {
2241  fileName = rootName + "_" + StrFormatInt (seqNum, "ZZ00") + "." + extension;
2242  }
2243  else
2244  {
2245  fileName = osAddSlash (dirPath) +
2246  rootName + "_" + StrFormatInt (seqNum, "ZZ00") +
2247  "." + extension;
2248  }
2249 
2250  fileNameExists = osFileExists (fileName);
2251  seqNum++;
2252  }
2253 
2254  return fileName;
2255 } /* osCreateUniqueFileName */
2256 
2257 
2258 
2259 
2260 
2261 KKStrPtr KKB::osReadNextLine (FILE* in)
2262 {
2263  if (feof (in))
2264  return NULL;
2265 
2266  KKStrPtr buff = new KKStr (100);
2267  while (true)
2268  {
2269  if (feof (in))
2270  break;
2271 
2272  char ch = fgetc (in);
2273  if (ch == '\r')
2274  {
2275  if (!feof (in))
2276  {
2277  char nextCh = fgetc (in);
2278  if (nextCh != '\n')
2279  ungetc (nextCh, in);
2280  break;
2281  }
2282  }
2283  else if (ch == '\n')
2284  {
2285  break;
2286  }
2287 
2288  buff->Append (ch);
2289  if (buff->Len () >= uint16_max)
2290  break;
2291  }
2292 
2293  return buff;
2294 } /* osReadNextLine */
2295 
2296 
2297 
2298 
2299 
2300 
2301 KKStr KKB::osReadNextToken (std::istream& in,
2302  const char* delimiters,
2303  bool& eof,
2304  bool& eol
2305  )
2306 {
2307  eof = false;
2308  eol = false;
2309 
2310  char token[1024];
2311  kkint32 maxTokenLen = sizeof (token) - 1;
2312 
2313  //kkint32 ch = fgetc (in); eof = (feof (in) != 0);
2314  kkint32 ch = in.get ();
2315  eof = in.eof ();
2316  if (eof)
2317  {
2318  eol = true;
2319  return "";
2320  }
2321 
2322  // lets skip leading white space
2323  while ((!eof) && ((ch == ' ') || (ch == '\r')) && (ch != '\n'))
2324  {
2325  ch = in.get ();
2326  eof = in.eof ();
2327  }
2328 
2329  if (ch == '\n')
2330  {
2331  eol = true;
2332  return "";
2333  }
2334 
2335  kkint32 tokenLen = 0;
2336 
2337  // Read till first delimiter or eof
2338  while ((!eof) && (!strchr (delimiters, ch)))
2339  {
2340  if (ch == '\n')
2341  {
2342  in.putback (ch);
2343  break;
2344  }
2345  else
2346  {
2347  token[tokenLen] = ch;
2348  tokenLen++;
2349 
2350  if (tokenLen >= maxTokenLen)
2351  break;
2352 
2353  ch = in.get ();
2354  eof = in.eof ();
2355  }
2356  }
2357 
2358  token[tokenLen] = 0; // Terminating NULL character.
2359 
2360  // Remove Trailing whitespace
2361  while (tokenLen > 0)
2362  {
2363  if (strchr (" \r", token[tokenLen - 1]) == 0)
2364  break;
2365  tokenLen--;
2366  token[tokenLen] = 0;
2367  }
2368 
2369  return token;
2370 } /* osReadNextToken */
2371 
2372 
2373 
2375  const char* delimiters,
2376  bool& eof,
2377  bool& eol
2378  )
2379 {
2380  eof = false;
2381  eol = false;
2382 
2383  char token[1024];
2384  kkint32 maxTokenLen = sizeof (token) - 1;
2385 
2386  kkint32 ch = fgetc (in); eof = (feof (in) != 0);
2387 
2388  if (eof)
2389  {
2390  eol = true;
2391  return "";
2392  }
2393 
2394  // lets skip leading white space
2395  while ((!eof) && ((ch == ' ') || (ch == '\r')) && (ch != '\n'))
2396  {ch = fgetc (in); eof = (feof (in)!= 0);}
2397 
2398  if (ch == '\n')
2399  {
2400  eol = true;
2401  return "";
2402  }
2403 
2404  kkint32 tokenLen = 0;
2405 
2406  // Read till first delimiter or eof
2407  while ((!eof) && (!strchr (delimiters, ch)))
2408  {
2409  if (ch == '\n')
2410  {
2411  ungetc (ch, in);
2412  break;
2413  }
2414  else
2415  {
2416  token[tokenLen] = ch;
2417  tokenLen++;
2418 
2419  if (tokenLen >= maxTokenLen)
2420  break;
2421 
2422  ch = fgetc (in); eof = (feof (in)!= 0);
2423  }
2424  }
2425 
2426  token[tokenLen] = 0; // Terminating NULL character.
2427 
2428 
2429  // Remove Trailing whitespace
2430  while (tokenLen > 0)
2431  {
2432  if (strchr (" \r", token[tokenLen - 1]) == 0)
2433  break;
2434  tokenLen--;
2435  token[tokenLen] = 0;
2436  }
2437 
2438  return token;
2439 } /* ReadNextToken */
2440 
2441 
2442 
2443 
2445  const char* delimiters,
2446  bool& eof
2447  )
2448 {
2449  eof = false;
2450  char token[1024];
2451  kkint32 maxTokenLen = sizeof (token) - 1;
2452 
2453  kkint32 ch = fgetc (in); eof = (feof (in) != 0);
2454 
2455  if (eof)
2456  return "";
2457 
2458  // lets skip leading white space
2459  while ((!eof) && ((ch == ' ') || (ch == '\r')) && (ch != '\n'))
2460  {ch = fgetc (in); eof = (feof (in)!= 0);}
2461 
2462  kkint32 tokenLen = 0;
2463 
2464  // Read till first delimiter or eof
2465  while ((!eof) && (!strchr (delimiters, ch)))
2466  {
2467  token[tokenLen] = ch;
2468  tokenLen++;
2469 
2470  if (tokenLen >= maxTokenLen)
2471  break;
2472 
2473  ch = fgetc (in); eof = (feof (in)!= 0);
2474  }
2475 
2476  token[tokenLen] = 0; // Terminating NULL character.
2477 
2478 
2479  // Remove Trailing whitespace
2480  while (tokenLen > 0)
2481  {
2482  if (strchr (" \n\r", token[tokenLen - 1]) == 0)
2483  break;
2484  tokenLen--;
2485  token[tokenLen] = 0;
2486  }
2487 
2488  return token;
2489 } /* ReadNextToken */
2490 
2491 
2492 
2493 KKB::KKStr KKB::osReadRestOfLine2 (std::istream& in,
2494  bool& eof
2495  )
2496 {
2497  KKStrPtr l = osReadRestOfLine (in, eof);
2498  if (l) {
2499  KKStr result(*l);
2500  delete l;
2501  l = NULL;
2502  return result;
2503  }
2504  else {
2505  return "";
2506  }
2507 }
2508 
2509 
2510 
2511 KKStrPtr KKB::osReadRestOfLine (std::istream& in,
2512  bool& eof
2513  )
2514 {
2515  eof = false;
2516 
2517  kkint32 ch = in.get ();
2518  eof = in.eof ();
2519 
2520  if (eof)
2521  return NULL;
2522 
2523  KKStrPtr result = new KKStr (1024);
2524 
2525  // Read till first delimiter or eof
2526  while (!eof)
2527  {
2528  if (ch == '\n')
2529  {
2530  break;
2531  }
2532  else
2533  {
2534  result->Append (ch);
2535  if (result->Len () >= (result->MaxLenSupported ()))
2536  break;
2537  ch = in.get (); eof = in.eof ();
2538  }
2539  }
2540 
2541  result->Trim (" \n\r");
2542 
2543  return result;
2544 } /* osReadRestOfLine */
2545 
2546 
2547 
2548 
2549 KKStrPtr KKB::osReadRestOfLine (FILE* in,
2550  bool& eof
2551  )
2552 {
2553  eof = false;
2554 
2555  kkint32 ch = fgetc (in);
2556  eof = (feof (in) != 0);
2557  if (eof)
2558  return NULL;
2559 
2560  KKStrPtr result = new KKStr(1024);
2561  // Read till first delimiter or eof
2562  while (!eof)
2563  {
2564  if (ch == '\n')
2565  {
2566  break;
2567  }
2568  else
2569  {
2570  result->Append (ch);
2571  if (result->Len () >= result->MaxLenSupported ())
2572  break;
2573  ch = fgetc (in); eof = (feof (in) != 0);
2574  }
2575  }
2576 
2577  result->TrimRight (" \r\n");
2578 
2579  return result;
2580 } /* osReadRestOfLine */
2581 
2582 
2583 
2585  bool& eof
2586  )
2587 {
2588  KKStrPtr l = osReadRestOfLine (in, eof);
2589  KKStr result(*l);
2590  delete l;
2591  l = NULL;
2592  return result;
2593 }
2594 
2595 
2596 
2597 
2599  const char* whiteSpaceCharacters,
2600  bool& eof
2601  )
2602 {
2603  if (feof (in))
2604  {
2605  eof = true;
2606  return KKStr::EmptyStr ();
2607  }
2608 
2609  // Skip leading white space and find first character in Token
2610  kkint32 ch = fgetc (in);
2611 
2612  while ((!feof (in)) && (strchr (whiteSpaceCharacters, ch) != NULL))
2613  {
2614  ch = fgetc (in);
2615  }
2616 
2617  if (feof (in))
2618  {
2619  eof = true;
2620  return KKStr::EmptyStr ();
2621  }
2622 
2623 
2624  KKStr result (10);
2625 
2626  bool lookForTerminatingQuote = false;
2627 
2628  if (ch == '"')
2629  {
2630  // We are going to read in a quoted string. In this case we include all characters until
2631  // we find the terminating quote
2632  lookForTerminatingQuote = true;
2633  ch = fgetc (in);
2634  }
2635 
2636  // Search for matching terminating Quote
2637 
2638  while (!feof (in))
2639  {
2640  if (lookForTerminatingQuote)
2641  {
2642  if (ch == '"')
2643  {
2644  break;
2645  }
2646  }
2647 
2648  else
2649  {
2650  if (strchr (whiteSpaceCharacters, ch))
2651  {
2652  // We found the next terminating white space character.
2653  break;
2654  }
2655  }
2656 
2657  if ((ch == '\\') && (lookForTerminatingQuote))
2658  {
2659  if (!feof (in))
2660  {
2661  ch = fgetc (in);
2662  switch (ch)
2663  {
2664  case '"': result.Append ('"'); break;
2665  case 't': result.Append ('\t'); break;
2666  case 'n': result.Append ('\n'); break;
2667  case 'r': result.Append ('\r'); break;
2668  case '\\': result.Append ('\\'); break;
2669  case '0': result.Append (char (0)); break;
2670  case 0: break;
2671  default: result.Append (ch); break;
2672  }
2673  }
2674  }
2675  else
2676  {
2677  result.Append (ch);
2678  }
2679 
2680  ch = fgetc (in);
2681  }
2682 
2683  // Eliminate all trailing white space
2684  if (!feof (in))
2685  {
2686  ch = fgetc (in);
2687  while ((!feof (in)) && (strchr (whiteSpaceCharacters, ch) != NULL))
2688  {
2689  ch = fgetc (in);
2690  }
2691 
2692  if (!feof (in))
2693  {
2694  ungetc (ch, in);
2695  }
2696  }
2697 
2698 
2699  return result;
2700 } /* osReadNextQuotedStr */
2701 
2702 
2703 
2704 
2705 void KKB::osSkipRestOfLine (FILE* in,
2706  bool& eof
2707  )
2708 {
2709  eof = false;
2710  kkint32 ch = fgetc (in); eof = (feof (in) != 0);
2711  while ((ch != '\n') && (!eof))
2712  {
2713  ch = fgetc (in); eof = (feof (in) != 0);
2714  }
2715 } /* osSkipRestOfLine */
2716 
2717 
2718 
2719 
2720 void KKB::osSkipRestOfLine (std::istream& in,
2721  bool& eof
2722  )
2723 {
2724  kkint32 ch = in.get ();
2725  eof = in.eof ();
2726 
2727  while ((ch != '\n') && (!eof))
2728  {
2729  ch = in.get ();
2730  eof = in.eof ();
2731  }
2732 } /* osSkipRestOfLine */
2733 
2734 
2735 
2737 {
2738 #ifdef WIN32
2739  //DWORD WINAPI processId = GetCurrentProcessId ();
2740  DWORD processId = GetCurrentProcessId();
2741  return processId;
2742 
2743 #else
2744  pid_t processID = getpid ();
2745  return processID;
2746 
2747 #endif
2748 }
2749 
2750 
2751 
2753 {
2754 #ifdef WIN32
2755  //DWORD WINAPI threadId = GetCurrentThreadId ();
2756  DWORD threadId = GetCurrentThreadId();
2757  return threadId;
2758 #else
2759  return 0;
2760 #endif
2761 }
2762 
2763 
2764 void KKB::osSleep (float secsToSleep)
2765 {
2766  #ifdef WIN32
2767  kkint32 miliSecsToSleep = (kkint32)(1000.0f * secsToSleep + 0.5f);
2768  Sleep (miliSecsToSleep);
2769  #else
2770  kkint32 secsToSleepInt = (kkint32)(0.5f + secsToSleep);
2771 
2772  if (secsToSleepInt < 1)
2773  secsToSleepInt = 1;
2774 
2775  else if (secsToSleepInt > 3600)
2776  cout << "osSleep secsToSleep[" << secsToSleepInt << "]" << std::endl;
2777 
2778  sleep (secsToSleepInt);
2779  #endif
2780 }
2781 
2782 
2783 
2784 
2785 void KKB::osSleepMiliSecs (kkuint32 numMiliSecs)
2786 {
2787  #ifdef WIN32
2788  Sleep (numMiliSecs);
2789  #else
2790  int numSecsToSleep = (numMiliSecs / 1000);
2791  sleep (numSecsToSleep);
2792  #endif
2793 }
2794 
2795 
2796 
2798 {
2799  VectorKKStr parts;
2800 
2801  if (path.Len () == 0)
2802  return parts;
2803 
2804  kkuint32 zed = 0;
2805 
2806  if (path[(kkuint16)1] == ':')
2807  {
2808  parts.push_back (path.SubStrPart (0, 1));
2809  zed += 2;
2810  }
2811 
2812  while (zed < path.Len ())
2813  {
2814  if (path[zed] == '\\')
2815  {
2816  parts.push_back ("\\");
2817  zed++;
2818  }
2819  else if (path[zed] == '/')
2820  {
2821  parts.push_back ("/");
2822  zed++;
2823  }
2824  else
2825  {
2826  // Scan until we come up to another separator or end of string
2827  kkint32 startPos = zed;
2828  while (zed < path.Len ())
2829  {
2830  if ((path[zed] == '\\') || (path[zed] == '/'))
2831  break;
2832  zed++;
2833  }
2834 
2835  parts.push_back (path.SubStrPart (startPos, zed - 1));
2836  }
2837  }
2838 
2839  return parts;
2840 } /* osSplitDirectoryPathIntoParts */
2841 
2842 
2844 {
2845 #if defined(WIN32)
2846  char szAppPath[MAX_PATH] = "";
2847  DWORD result = ::GetModuleFileName (0, szAppPath, MAX_PATH);
2848  return szAppPath;
2849 #else
2850  return KKStr::EmptyStr ();
2851 #endif
2852 }
__int16 kkint16
16 bit signed integer.
Definition: KKBaseTypes.h:85
KKStr osGetFullPathOfApplication()
returns the name and path of the current running application.
char osGetDriveLetter(const KKStr &pathName)
Given a full drive with path and name return the drive letter specified.
Definition: OSservices.cpp:297
KKStr(kkint32 size)
Creates a KKStr object that pre-allocates space for &#39;size&#39; characters.
Definition: KKStr.cpp:655
kkuint64 osGetSystemTimeInMiliSecs()
Returns mili-secs that system (Windows = has been started, Linux time in epoch).
bool osDeleteFile(KKStr _fileName)
Definition: OSservices.cpp:523
bool osValidFileName(const KKStr &_name)
Definition: OSservices.cpp:443
bool backGroundProcess
KKB::KKStr osReadNextToken(FILE *in, const char *delimiters, bool &eof, bool &eol)
Read the next logical token from a file using characters in &#39;delimiters&#39; to separate tokens...
char operator[](kkint16 i) const
Definition: KKStr.cpp:3379
#define INVALID_FILE_ATTRIBUTES
KKStr & Trim(const char *whiteSpaceChars="\n\r\t ")
Definition: KKStr.cpp:1686
KKB::KKStr osReadNextQuotedStr(FILE *in, const char *whiteSpaceCharacters, bool &eof)
Read the next Quoted String from the file.
static bool StrEqualNoCase(const char *s1, const char *s2)
Definition: KKStr.cpp:408
KKStr osRemoveExtension(const KKStr &_fullFileName)
__int32 kkint32
Definition: KKBaseTypes.h:88
KKB::DateTime osGetLocalDateTime()
Returned the current local date and time.
KKB::KKStrPtr osReadRestOfLine(std::istream &in, bool &eof)
Extracts text up to and including the next end-of-line character and returns pointer to dynamical all...
bool osValidDirectory(const KKStr &_name)
Definition: OSservices.cpp:398
kkint32 osGetNumberOfProcessors()
returns the number of CPU&#39;s or number of simultaneous threads that you can have.
KKStr osGetParentDirPath(KKStr dirPath)
Returns the Parent directory path to &#39;dirPath&#39;.
KKStr osGetPathPartOfFile(KKStr fullFileName)
Get the path part of a full file name specification.
kkint64 osFTELL(FILE *f)
Calls the appropriate 64 bit function for operating system.
Definition: OSservices.cpp:93
KKStr & TrimRight(const char *whiteSpaceChars="\n\r\t ")
Definition: KKStr.cpp:1695
KKStr osCreateUniqueFileName(KKStr fileName)
Get a unique file name based off file spec &#39;fileName&#39;.
KKStr osMakeFullFileName(const KKStr &_dirName, const KKStr &_fileName)
KKStrPtr osReadNextLine(FILE *in)
Read the next line from the file and return a c-String(NULL terminated).
void osWaitForEnter()
void ChopLastChar()
Definition: KKStr.cpp:1668
KKStr ExtractToken(const char *delStr="\n\t\r ")
Definition: KKStr.cpp:2969
KKB::KKStr osReadNextToken(std::istream &in, const char *delimiters, bool &eof, bool &eol)
Read the next logical token from a file using characters in &#39;delimiters&#39; to separate tokens...
void osGetListOfFilesInDirectoryTree(const KKStr &rootDir, KKStr fileSpec, VectorKKStr &fileNames)
void osSkipRestOfLine(std::istream &in, bool &eof)
Skips rest of the characters in the current line in the input stream.
KKStr & operator=(const char *src)
Definition: KKStr.cpp:1442
kkint32 osGetThreadId()
bool operator==(const char *rtStr) const
Definition: KKStr.cpp:1588
char FirstChar() const
Definition: KKStr.cpp:1970
kkuint32 MaxLenSupported() const
Definition: KKStr.cpp:2510
KKStrListPtr osGetListOfImageFiles(KKStr fileSpec)
KKStrListPtr osGetListOfFiles(const KKStr &fileSpec)
Returns a list of files that match the provided file specification.
KKStr operator+(const char *right) const
Definition: KKStr.cpp:3986
double osGetKernalTimeUsed()
kkint32 osLocateLastSlashChar(const KKStr &fileSpec)
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
KKStr osReadNextToken(FILE *in, const char *delimiters, bool &eof)
Read the next logical token from a file using characters in &#39;delimiters&#39; to separate tokens where &#39; &#39;...
void osSleep(float numOfSecs)
KKStr osGetFileExtension(KKStr fullFileName)
bool osCopyFileBetweenDirectories(const KKStr &_fileName, const KKStr &_srcDir, const KKStr &_destDir)
Definition: OSservices.cpp:643
__int64 kkint64
Definition: KKBaseTypes.h:90
char operator[](kkuint32 i) const
Definition: KKStr.cpp:3430
KKStr & operator=(KKStr &&src)
Definition: KKStr.cpp:1369
double osGetUserTimeUsed()
KKStrListPtr osValidFileNameErrors(const KKStr &_name)
Returns list of errors in &#39;_name&#39; with respect to it being a valid file name for the O/S...
Definition: OSservices.cpp:458
#define DS
Definition: OSservices.h:505
KKB::KKStr osAddSlash(const KKStr &fileSpec)
KKB::KKStr osReadRestOfLine2(FILE *in, bool &eof)
char LastChar() const
Definition: KKStr.cpp:2007
kkuint32 Len() const
Returns the number of characters in the string.
Definition: KKStr.h:366
KKTHread * KKTHreadPtr
void osParseFileName(KKStr _fileName, KKStr &_dirPath, KKStr &_rootName, KKStr &_extension)
KKStr osLookForFile(const KKStr &fileName, const KKStr &srcDir)
Look for a specified file in a sub-directory structure.
void Append(char ch)
Definition: KKStr.cpp:1863
KKStr operator+(const char *left, const KKStr &right)
Definition: KKStr.cpp:3976
void osParseFileSpec(KKStr fullFileName, KKStr &driveLetter, KKStr &path, KKStr &root, KKStr &extension)
bool osFileNameMatchesSearchFields(const KKStr &fileName, KKStrListPtr searchFields)
Definition: OSservices.cpp:180
KKStr(const KKStr &str)
Copy Constructor.
Definition: KKStr.cpp:561
kkint32 osGetProcessId()
void osChangeDir(const KKStr &dirName, bool &successful)
Definition: OSservices.cpp:793
bool Empty() const
Definition: KKStr.h:241
KKStr osGetHostName()
kkint32 osLocateFirstSlashChar(const KKStr &fileSpec)
VectorKKStr osSplitDirectoryPathIntoParts(const KKStr &path)
KKStr SubStrPart(kkint32 firstChar, kkint32 lastChar) const
returns a SubString consisting of all characters starting at index &#39;firstChar&#39; and ending at &#39;lastInd...
Definition: KKStr.cpp:2802
#define uint16_max
Definition: KKBaseTypes.h:118
kkint32 ToInt32() const
Definition: KKStr.cpp:3587
bool osValidDirectory(KKStrConstPtr _name)
Definition: OSservices.cpp:382
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
void Upper()
Converts all characters in string to their Upper case equivalents via &#39;toupper&#39;.
Definition: KKStr.cpp:2461
KKB::KKStr osReadRestOfLine2(std::istream &in, bool &eof)
kkint32 LocateLastOccurrence(char ch) const
Returns index of last occurrence of &#39;ch&#39; otherwise -1.
Definition: KKStr.cpp:2118
void osSleepMiliSecs(kkuint32 numMiliSecs)
kkint32 LocateCharacter(char ch) const
Returns index of 1st occurrence of &#39;ch&#39; otherwise -1.
Definition: KKStr.cpp:2021
static const KKStr & EmptyStr()
Static method that returns an Empty String.
Definition: KKStr.cpp:3453
KKStr osGetRootNameOfDirectory(KKStr fullDirName)
unsigned __int64 kkuint64
Definition: KKBaseTypes.h:91
KKStr StrFormatInt(kkint32 val, const char *mask)
Definition: KKStr.cpp:5004
KKStr(const char *str)
Definition: KKStr.cpp:537
int osFSEEK(FILE *f, kkint64 offset, int origin)
Definition: OSservices.cpp:104
KKStrPtr osGetEnvVariable(const KKStr &_varName)
Definition: OSservices.cpp:897
KKStr operator+(const KKStr &right) const
Definition: KKStr.cpp:3998
bool osCreateDirectory(const KKStr &dirName)
Definition: OSservices.cpp:875
const char * Str() const
Returns a pointer to a ascii string.
Definition: KKStr.h:422
KKB::KKStr osSubstituteInEnvironmentVariables(const KKStr &src)
Substitute in the value of the environment variables into &#39;src&#39;.
Definition: OSservices.cpp:977
double osGetSystemTimeUsed()
Returns the number of CPU seconds used by current process.
KKStr osGetUserName()
bool osFileExists(const KKStr &_fileName)
Definition: OSservices.cpp:568
KKStrListPtr osGetListOfDirectories(KKStr fileSpec)
FILE * osFOPEN(const char *fileName, const char *mode)
Definition: OSservices.cpp:74
KKStr osGetParentDirectoryOfDirPath(KKStr path)
int osLocateEnvStrStart(const KKStr &str, kkint32 startIdx)
Definition: OSservices.cpp:948
KKStrList(bool owner)
Definition: KKStr.cpp:4485
KKStr osGetFileNamePartOfFile(KKStr fullFileName)
osGetFileNamePartOfFile, retrieves the file name part of the file spec.
bool osCopyFile(const KKStr &srcFileName, const KKStr &destFileName)
Definition: OSservices.cpp:689
bool osIsBackGroundProcess()
#define OS_WINDOWS
Definition: FirstIncludes.h:11
KKStr & operator=(const KKStr &src)
Definition: KKStr.cpp:1390
bool osMoveFileBetweenDirectories(const KKStr &_fileName, const KKStr &_srcDir, const KKStr &_destDir)
Definition: OSservices.cpp:609
KKB::DateTime osGetFileDateTime(const KKStr &fileName)
KKStr osGetProgName()
KKStr osGetCurrentDirectory()
Definition: OSservices.cpp:319
KKStr osGetRootNameWithExtension(const KKStr &fullFileName)
void osSkipRestOfLine(FILE *in, bool &eof)
Skips rest of the characters in the current line in the file.
#define DSchar
Definition: OSservices.h:506
bool osCreateDirectoryPath(KKStr _pathName)
Will create the whole Directory Path not just the final part of the path.
Definition: OSservices.cpp:825
KKStr osGetDirNameFromPath(KKStr dirPath)
Extracts the final sub-directory name of the fill directory specification.
KKStr SubStrPart(kkint32 firstChar) const
returns a SubString consisting of all characters starting at index &#39;firstChar&#39; until the end of the s...
Definition: KKStr.cpp:2780
KKStrListPtr osParseSearchSpec(const KKStr &searchSpec)
Definition: OSservices.cpp:131
bool osRenameFile(const KKStr &oldName, const KKStr &newName)
Definition: OSservices.cpp:743
char operator[](kkuint16 i) const
Definition: KKStr.cpp:3396
kkint64 osGetFileSize(const KKStr &fileName)
KKStr osGetErrorNoDesc(kkint32 errorNo)
Definition: OSservices.cpp:45
KKB::KKStrPtr osReadRestOfLine(FILE *in, bool &eof)
KKStr osGetRootName(const KKStr &fullFileName)
void osDisplayWarning(KKStr _message)
void osAddLastSlash(KKStr &fileSpec)
Will add the appropriate Directory separator character to the end of fileSpec if one is not there alr...
void osRunAsABackGroundProcess()