-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cpp
3569 lines (2739 loc) · 97.3 KB
/
Utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "StdFuncs.h"
#include "Lex.h"
#include "OS4Support.h"
#include "StdWindow.h"
#ifdef __amigaos__
#include <limits.h>
#include <clib/debug_protos.h>
#include <dos/dostags.h>
#include <proto/intuition.h>
#include <proto/utility.h>
#include <workbench/startup.h>
#elif defined(QT_GUI_LIB)
#include <QtWidgets/QMessageBox>
#include "Qt/QtWindow.h"
#endif /* QT_GUI_LIB */
#ifdef __unix__
#include <errno.h>
#include <syslog.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <utime.h>
#ifdef __APPLE__
#include <mach-o/dyld.h>
#endif /* __APPLE__ */
#endif /* __unix__ */
#include <stdio.h>
#include <string.h>
#include "File.h"
#ifdef __amigaos__
#define MAX_NAME_FROM_LOCK_LENGTH 1024
#ifdef __amigaos4__
#define DELETE_DIRECTORY(DirectoryName) Delete(DirectoryName)
#else /* ! __amigaos4__ */
#define DELETE_DIRECTORY(DirectoryName) DeleteFile(DirectoryName)
#endif /* ! __amigaos4__ */
#define VSNPRINTF vsnprintf
#elif defined(__unix__)
#define DELETE_DIRECTORY(DirectoryName) (rmdir(DirectoryName) == 0)
#define VSNPRINTF vsnprintf
#else /* ! __unix__ */
#define DELETE_DIRECTORY(DirectoryName) RemoveDirectory(DirectoryName)
#define VSNPRINTF _vsnprintf
#endif /* ! __unix__ */
#define PRINTF printf
/* Amiga style suffix that can be prepended to filenames to resolve */
/* them to the directory of the executable that is using the file */
static const char g_accProgDir[] = "PROGDIR:";
#define PROGDIR_LENGTH 8
/* OS4 allows using underscores to indicate hotkeys when using EasyRequest() but OS3 does not */
#ifdef __amigaos4__
static const char g_accOk[] = "_Ok";
static const char g_accOkCancel[] = "_Ok|_Cancel";
static const char g_accYesNo[] = "_Yes|_No";
static const char g_accYesNoCancel[] = "_Yes|_No|_Cancel";
#elif defined(__amigaos__)
static const char g_accOk[] = "Ok";
static const char g_accOkCancel[] = "Ok|Cancel";
static const char g_accYesNo[] = "Yes|No";
static const char g_accYesNoCancel[] = "Yes|No|Cancel";
#endif /* defined(__amigaos__) */
/* ETrue if running a GUI based program */
TBool g_bUsingGUI;
/* Pointer to root window on which all other windows open. This was defined in CWindow as a static, */
/* but this caused the GUI framework to be linked in even for command line only programs. Thus */
/* it is now kept as a global variable. Less elegent perhaps, but it significantly reduces the */
/* amount of code statically linked in */
CWindow *g_poRootWindow;
#if defined(WIN32) && !defined(QT_GUI_LIB)
/**
* Callback function used by EnumDisplayMonitors().
* This function is called by the windows EnumDisplayMonitors() function for each display that is
* attached to the host computer. This enables the client to obtain the dimensions of each display.
* It is used by the Utils::GetScreenSize() function when querying the display on which a particular
* window is opened.
*
* @date Tuesday 10-Jun-2015 6:30 am, Code HQ Ehinger Tor
* @param a_poMonitor Handle of a structure that represents a display attached to the system
* @param a_poDC Handle of the device context passed into EnumDisplayMonitors()
* @param a_poRect Pointer to the RECT passed into EnumDisplayMonitors()
* @param a_lData 32 bit user data passed into EnumDisplayMonitors(), in our case a pointer
* to the SRect structure into which to place information about the display
* @return Always FALSE to stop the scan, as we are only interested in the first display found
*/
static BOOL CALLBACK MonitorEnumProc(HMONITOR a_poMonitor, HDC /*a_poDC*/, LPRECT /*a_poRect*/, LPARAM a_lData)
{
struct SRect *ScreenSize;
MONITORINFO MonitorInfo;
/* Get a pointer to the SRect to be filled out */
ScreenSize = (struct SRect *) a_lData;
/* And query the system for the size of the display that was just passed in */
MonitorInfo.cbSize = sizeof(MonitorInfo);
if (GetMonitorInfo(a_poMonitor, &MonitorInfo))
{
ScreenSize->m_iTop = MonitorInfo.rcMonitor.top;
ScreenSize->m_iLeft = MonitorInfo.rcMonitor.left;
ScreenSize->m_iWidth = (MonitorInfo.rcMonitor.right - MonitorInfo.rcMonitor.left);
ScreenSize->m_iHeight = (MonitorInfo.rcMonitor.bottom - MonitorInfo.rcMonitor.top);
}
/* Always return FALSE to stop the scan. We are only interested in querying the first display in */
/* the system as this is the one that matches the window that was passed to EnumDisplayMonitors() */
return(FALSE);
}
#endif /* defined(WIN32) && !defined(QT_GUI_LIB) */
/**
*
* Maps the OS's last error onto one of The Framework's standard errors.
* This function will determine the last error reported by the OS and to map it onto a standard
* error.
*
* @date Thursday 27-Sep-2012 6:39 am, Code HQ Ehinger Tor
* @return KErrNone if the last OS operation was successful
* @return KErrAlreadyExists if the file already exists
* @return KErrInUse if the file or directory is in use
* @return KErrNoMemory if not enough memory was available
* @return KErrNotFound if the path is ok, but the file does not exist
* @return KErrPathNotFound if the path to the file does not exist
* @return KErrGeneral if some other unexpected error occurred
*/
TInt Utils::MapLastError()
{
TInt RetVal;
#ifdef __amigaos__
int Error = IoErr();
if (Error == ERROR_OBJECT_EXISTS)
{
RetVal = KErrAlreadyExists;
}
else if (Error == ERROR_DIR_NOT_FOUND)
{
RetVal = KErrPathNotFound;
}
else if (Error == ERROR_OBJECT_NOT_FOUND)
{
RetVal = KErrNotFound;
}
else if (Error == ERROR_OBJECT_WRONG_TYPE)
{
RetVal = KErrCorrupt;
}
else if ((Error == ERROR_DIRECTORY_NOT_EMPTY) || (Error == ERROR_OBJECT_IN_USE))
{
RetVal = KErrInUse;
}
else
{
RetVal = KErrGeneral;
}
#elif defined(__unix__)
int Error = errno;
if (Error == EEXIST)
{
RetVal = KErrAlreadyExists;
}
else if (Error == ENOENT)
{
RetVal = KErrNotFound;
}
else if ((Error == ENOTEMPTY) || (Error == EBUSY) || (Error == ETXTBSY))
{
RetVal = KErrInUse;
}
else
{
RetVal = KErrGeneral;
}
#else /* ! __unix__ */
DWORD Error = GetLastError();
if (Error == ERROR_FILE_EXISTS)
{
RetVal = KErrAlreadyExists;
}
else if (Error == ERROR_PATH_NOT_FOUND)
{
RetVal = KErrPathNotFound;
}
else if ((Error == ERROR_FILE_NOT_FOUND) || (Error == ERROR_INVALID_NAME))
{
RetVal = KErrNotFound;
}
else if (Error == ERROR_BAD_EXE_FORMAT)
{
RetVal = KErrCorrupt;
}
else if ((Error == ERROR_DIR_NOT_EMPTY) || (Error == ERROR_SHARING_VIOLATION))
{
RetVal = KErrInUse;
}
else
{
RetVal = KErrGeneral;
}
#endif /* ! __unix__ */
return(RetVal);
}
/**
* Maps the OS's last error onto one of The Framework's standard errors.
* This function is an extension to Utils::MapLastError(). It uses that function to determine the
* last error reported by the OS and to map it onto a standard error, but then performs some extra
* file related checking on some operating systems to perhaps change the error returned. This is
* because different operating systems handle errors slightly differently and thus without this
* function the results returned by The Framework's file I/O related functions would be different
* on different platforms.
*
* This function should only be used in functions that perform file or directory I/O. All other
* functions should use Utils::MapLastError() directly to do their error mapping.
*
* @date Monday 09-Oct-2012 5:47 am
* @param a_pccFileName Pointer to the name of the file to be checked
* @return KErrNone if successful
* @return KErrAlreadyExists if the file already exists
* @return KErrInUse if the file or directory is in use
* @return KErrNoMemory if not enough memory was available
* @return KErrNotFound if the path is ok, but the file does not exist
* @return KErrPathNotFound if the path to the file does not exist
* @return KErrGeneral if some other unexpected error occurred
*/
TInt Utils::MapLastFileError(const char *a_pccFileName)
{
TInt RetVal;
/* See what the last error was */
RetVal = Utils::MapLastError();
#ifdef WIN32
(void) a_pccFileName;
#else /* ! WIN32 */
char *Name;
TInt NameOffset;
TEntry Entry;
/* Unfortunately UNIX and Amiga OS don't have an error that can be mapped onto KErrPathNotFound */
/* so we must do a little extra work here. Amiga OS needs a little help too */
if (RetVal == KErrNotFound)
{
/* Determine the path to the file that was opened */
NameOffset = (Utils::filePart(a_pccFileName) - a_pccFileName);
/* If the file is not in the current directory then check if the path exists */
if (NameOffset > 0)
{
/* Strip off the trailing '/' or '\' but NOT the ':' as this is required */
/* for correct RAM disc handling on Amiga OS */
if (a_pccFileName[NameOffset - 1] != ':')
{
--NameOffset;
}
/* Allocate a buffer long enough to hold the path to the file */
if ((Name = new char[NameOffset + 1]) != NULL)
{
memcpy(Name, a_pccFileName, NameOffset);
Name[NameOffset] = '\0';
/* Now check for the existence of the file */
if (Utils::GetFileInfo(Name, &Entry) == KErrNotFound)
{
RetVal = KErrPathNotFound;
}
delete [] Name;
}
else
{
Utils::info("RFile::MapLastOpenError() => Out of memory");
RetVal = KErrNoMemory;
}
}
}
#endif /* ! WIN32 */
return(RetVal);
}
/**
* Appends a file or directory name to an existing path.
* This function will append a file or directory to an existing path, ensuring that a '/' character
* is appended as appropriate to the end of the existing path. This will ensure that a single
* slash is always present between the end of the existing path and the beginning of the newly
* added path part. It will not append an extra slash if one exists already.
*
* It will also treat Windows style directory names (such as "d:") specially in that they will not
* have a slash appended to them. This allows relative addressing of directories on drives where
* the drive letter by itself represents the current directory of that drive.
*
* @date Thursday 16-Jul-2009 3:58 pm
* @param a_pcDest Pointer to the path to which to append the file or directory name
* @param a_pccSource Pointer to the file or directory name to be appended
* @param a_stDestSize The size of the destination buffer in bytes
* @return ETrue if the file or directory was appended successfully
* @return EFalse if the destination buffer was too small to hold the resulting path
*/
TBool Utils::addPart(char *a_pcDest, const char *a_pccSource, size_t a_stDestSize)
{
TBool RetVal;
#ifdef __amigaos__
RetVal = AddPart(a_pcDest, a_pccSource, a_stDestSize);
#else /* ! __amigaos__ */
char Char;
size_t Length;
/* Assume failure */
RetVal = EFalse;
/* If there is anything already in the destination string, check there is enough space to */
/* append to it before doing anything */
if ((Length = strlen(a_pcDest)) > 0)
{
if ((Length + 1 + strlen(a_pccSource)) <= a_stDestSize)
{
RetVal = ETrue;
/* Append the source string, plus a directory separator if required */
Char = a_pcDest[Length - 1];
if ((Char != '\\') && (Char != '/') && (Char != ':'))
{
strcat(a_pcDest, "/");
}
strcat(a_pcDest, a_pccSource);
}
}
/* Otherwise just copy the source string into the destination, provided there is enough space */
/* to do so */
else
{
if (strlen(a_pccSource) <= a_stDestSize)
{
RetVal = ETrue;
strcpy(a_pcDest, a_pccSource);
}
}
#endif /* ! __amigaos__ */
return(RetVal);
}
#ifdef _DEBUG
/**
* Displays information about an assertion failure.
* This function will display information about an assertion failure, either in a message box, if
* running on a GUI based system, or as a printf() if not. It will also (on most platforms) exit
* back to the operating system after displaying this message.
*
* @date Saturday 11-Jul-2009 08:56 am
* @param a_pccMessage The message to be displayed, in printf() format
*/
void Utils::AssertionFailure(const char *a_pccMessage, ...)
{
va_list Args;
va_start(Args, a_pccMessage);
if (g_bUsingGUI)
{
MessageBox(EMBTOk, "Assertion Failure", a_pccMessage, Args);
}
else
{
PRINTF("Assertion Failure: %s\n", a_pccMessage);
}
va_end(Args);
#ifndef __amigaos__
/* When an assertion happens we want to exit the system as if we allow execution */
/* to continue then it is likely to crash anyway. We will do this on all platforms */
/* besides Amiga as the non protected Amiga OS isn't so good at handling this! */
exit(1);
#endif /* ! __amigaos__ */
}
#endif /* _DEBUG */
/**
* Parses a string to determine how many tokens it contains.
* Parses a string, identifying tokens and keeping track of a count of how many it finds. A token
* is either a single word delimited by white space (spaces or tabs), or multiple words in a quoted
* string.
*
* @date Friday 04-Jun-2010 7:58 am
* @param a_pccBuffer Pointer to buffer to parse for tokens
* @return The number of tokens found in the string
*/
TInt Utils::CountTokens(const char *a_pccBuffer)
{
char Char;
TBool FoundDelimeter, TokenStart;
TInt Source, RetVal;
/* Iterate through the string passed in and determine how many tokens are present */
FoundDelimeter = EFalse;
TokenStart = ETrue;
RetVal = 0;
for (Source = 0; a_pccBuffer[Source]; ++Source)
{
Char = a_pccBuffer[Source];
/* If the current character is a " then we have either found the start of a new token */
/* or the end of an old one */
if (Char == '"')
{
/* FoundDelimter indicates whether we have found the start of a new token or the end */
/* of an old one */
if (!(FoundDelimeter))
{
FoundDelimeter = ETrue;
}
else
{
FoundDelimeter = EFalse;
}
/* Either way we have found a new token */
TokenStart = ETrue;
}
/* If we have found white space then we have a new token, but only if the space is not inside */
/* a quoted string */
else if (((Char == ' ') || (Char == '\t')) && (!(FoundDelimeter)))
{
TokenStart = ETrue;
}
/* If this is the first character of a token then increment the token count */
else
{
if (TokenStart)
{
++RetVal;
TokenStart = EFalse;
}
}
}
return(RetVal);
}
/**
* Creates a new directory.
* Creates a directory with the name passed in. The name can be either relative to the current
* directory or a fully qualified path.
*
* @date Saturday 18-Jul-2009 8:25 am
* @param a_pccDirectoryName The name of the directory to be created
* @return KErrNone if successful
* @return KErrAlreadyExists if the dirctory already exists
* @return KErrNot found for any other error
*/
TInt Utils::CreateDirectory(const char *a_pccDirectoryName)
{
TInt RetVal;
#ifdef __amigaos__
BPTR Lock;
if ((Lock = CreateDir(a_pccDirectoryName)) != 0)
{
RetVal = KErrNone;
UnLock(Lock);
}
else
{
RetVal = (IoErr() == ERROR_OBJECT_EXISTS) ? KErrAlreadyExists : KErrNotFound;
}
#elif defined(__unix__)
if (mkdir(a_pccDirectoryName, 0755) == 0)
{
RetVal = KErrNone;
}
else
{
RetVal = (errno == EEXIST) ? KErrAlreadyExists : KErrNotFound;
}
#else /* ! __unix__ */
if (::CreateDirectory(a_pccDirectoryName, NULL))
{
RetVal = KErrNone;
}
else
{
RetVal = (GetLastError() == ERROR_ALREADY_EXISTS) ? KErrAlreadyExists : KErrNotFound;
}
#endif /* ! __unix__ */
return(RetVal);
}
/**
* Deletes a directory from the file system.
* This function will delete a directory. The directory in question must not be open for use in
* any way and must be empty.
*
* @date Friday 27-Jul-2012 10:27 am
* @param a_pccDirectoryName Name of the directory to be deleted
* @return KErrNone if successful
* @return KErrInUse if the file or directory is in use
* @return KErrNoMemory if not enough memory was available
* @return KErrPathNotFound if the path to the file does not exist
* @return KErrGeneral if some other unexpected error occurred
*/
TInt Utils::DeleteDirectory(const char *a_pccDirectoryName)
{
TInt RetVal;
/* First try to delete the directory */
if (DELETE_DIRECTORY(a_pccDirectoryName))
{
RetVal = KErrNone;
}
else
{
/* See if this was successful. If it wasn't due to path not found etc. then return this error */
RetVal = MapLastFileError(a_pccDirectoryName);
}
return(RetVal);
}
/**
* Detaches the currently running program from the CLI.
* This function is mainly for Amiga OS and relaunches the current process asynchronously before
* exiting the current process. This gives the user the impression that the process has
* automatically detached itself from the CLI. Note that if successful, this function will not
* return and will instead shut the process down using exit(). The second time it is called (by
* the newly launched process), it will detect that the new process is already detached from the
* CLI and will simply return without doing anything.
*
* @date Monday 27-Aug-2012 06:56 am
* @return Does not return if successful
* @return KErrNone if process is already detached from the CLI
* @return KErrGeneral if unable to detach the process
*/
TInt Utils::Detach()
{
TInt RetVal;
/* Assume success */
RetVal = KErrNone;
#ifdef __amigaos__
char *Command, *CommandNameBuffer;
const char *CommandName;
TInt ArgStrLength, CommandLength, CommandNameLength, Result;
BPTR InputStream;
struct CommandLineInterface *CLI;
/* If we are started from the CLI and are not already backgrounded (ie. By the "run" */
/* command or by our own relaunching of ourselves as backgrounded) then relaunch */
/* the process asynchronously */
CLI = Cli();
if ((CLI) && (!(CLI->cli_Background ) && (CLI->cli_CommandName)))
{
/* Determine the size of the buffers needed for the command name and command line */
CommandNameLength = CopyStringBSTRToC(CLI->cli_CommandName, NULL, 0);
ArgStrLength = strlen(GetArgStr());
/* Allocate a buffer long enough for the command name. This must include an extra */
/* byte for the NULL terminator */
CommandNameBuffer = new char[CommandNameLength + 1];
/* Allocate a buffer long enough to hold the command line. This must include */
/* extra bytes for the " and space characters, as well as a NULL terminator */
CommandLength = (CommandNameLength + ArgStrLength + 4);
Command = new char[CommandLength];
if ((CommandNameBuffer) && (Command))
{
/* Extract the path to the executable from the CLI structure and from that extract */
/* the name of the executable. This will be used for setting the task name so that */
/* the program shows up in the task list as something more useful than "New Process" */
CopyStringBSTRToC(CLI->cli_CommandName, CommandNameBuffer, (CommandNameLength + 1));
CommandName = Utils::filePart(CommandNameBuffer);
/* Build the string used to relaunch the process */
snprintf(Command, CommandLength,"\"%s\" %s", CommandNameBuffer, GetArgStr());
/* We want to use the parent's input stream for both input and output, so duplicate */
/* it as it will be closed when the child process exits */
if ((InputStream = Open("CONSOLE:", MODE_READWRITE)) != 0)
{
/* And launch the child process! If successful then exit as this is essentially */
/* performing a fork() and we don't want to keep the parent process alive */
#ifdef __amigaos4__
Result = SystemTags(Command, SYS_Input, (ULONG) InputStream, SYS_Output, (ULONG) NULL, SYS_Error, NULL,
SYS_Asynch, TRUE, NP_Name, CommandName, TAG_DONE);
#else /* ! __amigaos4__ */
Result = SystemTags(Command, SYS_Input, (ULONG) InputStream, SYS_Output, (ULONG) NULL,
SYS_Asynch, TRUE, NP_Name, CommandName, TAG_DONE);
#endif /* ! __amigaos4__ */
/* Free the command buffers before we exit */
delete [] Command;
delete [] CommandNameBuffer;
if (Result == 0)
{
exit(0);
}
else
{
RetVal = KErrGeneral;
Close(InputStream);
}
}
}
else
{
Utils::info("Utils::Detach() => Out of memory");
}
delete [] Command;
delete [] CommandNameBuffer;
}
#endif /* __amigaos__ */
return(RetVal);
}
/**
* Duplicates a string into a new allocated buffer.
* Allocates a buffer large enough to hold the string passed in and its NULL terminator and copies
* the string into said buffer. If a length is passed in (and 0 is a valid length) then it is
* assumed the string passed in is not NULL terminated and that length is used as the string's
* length. Otherwise if the length is -1 then the string's length is determined with strlen().
* The string returned by this function should be freed with delete [].
*
* @date 13-Feb-2013 6:48 am, Code HQ Ehinger Tor
* @param a_pccString Pointer to the string to be duplicated
* @param a_iLength Length of the string, not including NULL terminator, or -1
* @return Pointer to an allocated buffer containing the duplicated string if successful, else NULL
*/
char *Utils::DuplicateString(const char *a_pccString, TInt a_iLength)
{
char *RetVal;
size_t Length;
ASSERTM((a_pccString != NULL), "Utils::DuplicateString() => Pointer to string passed in must not be NULL");
/* If no length has been passed in then determine the length of the string */
Length = (a_iLength == -1) ? strlen(a_pccString) : a_iLength;
/* Allocate a buffer large enough to hold the string and copy the string */
if ((RetVal = new char[Length + 1]) != NULL)
{
memcpy(RetVal, a_pccString, Length);
RetVal[Length] ='\0';
}
return(RetVal);
}
/**
* Displays an error message.
* Displays a printf() style formatted message. If a GUI is in use, the message will be displayed
* in a message box. Otherwise it will be printed on the terminal. The message is displayed
* regardless of whether the code is compiled in debug or release mode, and thus this function
* should be used for displaying critical error messages.
*
* @date Saturday 11-Jul-2009 08:56 am
* @param a_pccMessage The message to be displayed, in printf() format
*/
void Utils::Error(const char *a_pccMessage, ...)
{
// TODO: CAW - Risk of overflow, here, above and in Utils::info
char Message[512];
va_list Args;
va_start(Args, a_pccMessage);
if (g_bUsingGUI)
{
MessageBox(EMBTOk, "Error", a_pccMessage, Args);
}
else
{
VSNPRINTF(Message, sizeof(Message), a_pccMessage, Args);
PRINTF("Error: %s\n", Message);
}
va_end(Args);
}
/**
* Returns the extension of a filename.
* Scans backwards from the end of string passed in, to find a '.' character. This function is
* thus a very naive method of finding the extension of a filename, although really it is just
* searching for a full stop.
*
* @date Thursday 22-Jul-2010 8:47 am
* @param a_pccFileName Pointer to the string in which to search
* @return A pointer to the first character after the full stop, if found, else NULL
*/
const char *Utils::Extension(const char *a_pccFileName)
{
char Char;
const char *RetVal;
RetVal = (a_pccFileName + strlen(a_pccFileName));
while (RetVal >= a_pccFileName)
{
Char = *RetVal;
if (Char == '.')
{
++RetVal;
break;
}
--RetVal;
}
if (RetVal < a_pccFileName)
{
RetVal = NULL;
}
return(RetVal);
}
/**
* Finds the filename contained within a fully qualified path.
* This function will scan backwards through a fully qualified path, looking for any kind of directory
* separator. When found, this separator is considered the separator between the path part of a fully
* qualified filename, and the filename part of the fully qualified filename. The pointer to this
* filename will then be returned. If the path passed in does not contain any path separators (ie. it
* contains no path) then the entire path will be returned.
*
* As an example, the following Amiga OS, UNIX and Windows paths will all result in "file.txt" being
* returned by the function. All paths are recognised by this function on all systems on which
* The Framework runs:
*
* work:file.txt
* work:path/file.txt
* /file.txt
* /path/file.txt
* c:\\file.txt
* c:\\path\\file.txt
*
* @date Thursday 22-Jul-2010 8:11 am
* @param a_pccPath Pointer to the fully qualified path to be scanned
* @return Pointer to the filename component of the path
*/
const char *Utils::filePart(const char *a_pccPath)
{
char Char;
const char *RetVal;
/* Iterate through the string passed in from its very end, looking for any kind of directory */
/* separator, whether valid for Amiga OS, UNIX or Windows */
RetVal = (a_pccPath + strlen(a_pccPath));
while (RetVal >= a_pccPath)
{
Char = *RetVal;
if ((Char == '/') || (Char == '\\') || (Char == ':'))
{
++RetVal;
break;
}
--RetVal;
}
/* If no directory separator was found, we will be pointing to just before the start of the */
/* string. In this case, return the entire string passed in */
if (RetVal < a_pccPath)
{
++RetVal;
}
return(RetVal);
}
/**
* Determines a filename from a WBArg structure.
* This function determines the fully qualified filename of the object represented by a WBArg
* structure. This is done by determining the path represented by the WBArg's lock and then
* appending the name to that path. It will also return (in the variable a_pbDirectory)
* whether or not the target of the WBArg structure is a file or a directory.
*
* @date Monday 09-Apr-2007 12:06 am
* @param a_pcFullName Pointer to a buffer into which to place the qualified filename
* @param a_poWBArg Pointer to the WBArg structure to be used
* @param a_pbDirectory Pointer to a variable into which to place a directory flag
* @return ETrue if successful, else EFalse
*/
#ifdef __amigaos__
TBool Utils::FullNameFromWBArg(char *a_pcFullName, struct WBArg *a_poWBArg, TBool *a_pbDirectory)
{
char Path[1024]; // TODO: CAW - Possible overflow here and in a_pcFullName
TBool RetVal;
struct TEntry Entry;
/* Assume failure */
RetVal = EFalse;
/* Check whether the WBArg has a valid lock and if so, determine its path */
if (a_poWBArg->wa_Lock)
{
if (NameFromLock(a_poWBArg->wa_Lock, Path, sizeof(Path)))
{
/* Create the fully qualified path from the path and filename */
strcpy(a_pcFullName, Path);
if (strlen(a_poWBArg->wa_Name) > 0)
{
DEBUGCHECK((AddPart(a_pcFullName, a_poWBArg->wa_Name, sizeof(Path)) != FALSE),
"Utils::FullNameFromWBArg() => Unable to build filename");
}
/* Is this a directory? */
if (GetFileInfo(a_pcFullName, &Entry) == KErrNone)
{
RetVal = ETrue;
*a_pbDirectory = Entry.iIsDir;
}
else
{
Utils::info("Utils::FullNameFromWBArg() => Unable to obtain information about object");
}
}
else
{
Utils::info("Utils::FullNameFromWBArg() => Unable to obtain filename from Lock");
}
}
return(RetVal);
}
#endif /* __amigaos__ */
/**
* Determines the name of the current directory.
* Queries the underlying operating system to determine the current directory, and returns the
* directory's fully qualified path.
*
* @date Sunday 26-Sep-2021 6:31 am, Code HQ Bergmannstrasse
* @return The fully qualified name of the current directory, if successful, else ""
*/
std::string Utils::GetCurrentDirectory()
{
std::string retVal;
#ifdef __amigaos__