-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxfeutils.cpp
More file actions
1819 lines (1565 loc) · 39.9 KB
/
xfeutils.cpp
File metadata and controls
1819 lines (1565 loc) · 39.9 KB
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
// Helper functions for various purposes
// Some helper functions are also added to get large files support
// Also supports a timeout on the stat and lstat function (this is the reason
// why some standard FOX function cannot be used and are rewritten here)
#include "config.h"
#include "i18n.h"
#include <sys/time.h>
#include <sys/wait.h>
#include <time.h>
#include <libgen.h>
#include <fx.h>
#include <fxkeys.h>
#include <FXPNGIcon.h>
#include "xfedefs.h"
#include "icons.h"
#include "xfeutils.h"
#include "../st/st.h"
// Decode filename to get original again
FXString FXPath::dequote(const FXString& file)
{
FXString result(file);
if (0 < result.length())
{
register int e = result.length();
register int b = 0;
register int r = 0;
register int q = 0;
// Trim tail
while (0 < e && Ascii::isSpace(result[e-1]))
{
--e;
}
// Trim head
while (b < e && Ascii::isSpace(result[b]))
{
++b;
}
// Dequote the rest
while (b < e)
{
if (result[b] == '\'')
{
q = !q;
b++;
continue;
}
if ((result[b] == '\\') && (result[b+1] == '\'') && !q)
{
b++;
}
result[r++] = result[b++];
}
// Trunc to size
result.trunc(r);
}
return(result);
}
// Note : the original function from FXAccelTable is buggy!!
// Parse accelerator from string
FXHotKey _parseAccel(const FXString& string)
{
register FXuint code = 0, mods = 0;
register int pos = 0;
// Parse leading space
while (pos < string.length() && Ascii::isSpace(string[pos]))
{
pos++;
}
// Parse modifiers
while (pos < string.length())
{
// Modifier
if (comparecase(&string[pos], "ctl", 3) == 0)
{
mods |= CONTROLMASK;
pos += 3;
}
else if (comparecase(&string[pos], "ctrl", 4) == 0)
{
mods |= CONTROLMASK;
pos += 4;
}
else if (comparecase(&string[pos], "alt", 3) == 0)
{
mods |= ALTMASK;
pos += 3;
}
else if (comparecase(&string[pos], "meta", 4) == 0)
{
mods |= METAMASK;
pos += 4;
}
else if (comparecase(&string[pos], "shift", 5) == 0)
{
mods |= SHIFTMASK;
pos += 5;
}
else
{
break;
}
// Separator
if ((string[pos] == '+') || (string[pos] == '-') || Ascii::isSpace(string[pos]))
{
pos++;
}
}
// Test for some special keys
if (comparecase(&string[pos], "home", 4) == 0)
{
code = KEY_Home;
}
else if (comparecase(&string[pos], "end", 3) == 0)
{
code = KEY_End;
}
else if (comparecase(&string[pos], "pgup", 4) == 0)
{
code = KEY_Page_Up;
}
else if (comparecase(&string[pos], "pgdn", 4) == 0)
{
code = KEY_Page_Down;
}
else if (comparecase(&string[pos], "left", 4) == 0)
{
code = KEY_Left;
}
else if (comparecase(&string[pos], "right", 5) == 0)
{
code = KEY_Right;
}
else if (comparecase(&string[pos], "up", 2) == 0)
{
code = KEY_Up;
}
else if (comparecase(&string[pos], "down", 4) == 0)
{
code = KEY_Down;
}
else if (comparecase(&string[pos], "ins", 3) == 0)
{
code = KEY_Insert;
}
else if (comparecase(&string[pos], "del", 3) == 0)
{
code = KEY_Delete;
}
else if (comparecase(&string[pos], "esc", 3) == 0)
{
code = KEY_Escape;
}
else if (comparecase(&string[pos], "tab", 3) == 0)
{
code = KEY_Tab;
}
else if (comparecase(&string[pos], "return", 6) == 0)
{
code = KEY_Return;
}
else if (comparecase(&string[pos], "enter", 5) == 0)
{
code = KEY_Return;
}
else if (comparecase(&string[pos], "back", 4) == 0)
{
code = KEY_BackSpace;
}
else if (comparecase(&string[pos], "spc", 3) == 0)
{
code = KEY_space;
}
else if (comparecase(&string[pos], "space", 5) == 0)
{
code = KEY_space;
}
// Test for function keys
else if ((Ascii::toLower(string[pos]) == 'f') && Ascii::isDigit(string[pos+1]))
{
if (Ascii::isDigit(string[pos+2]))
{
// !!!! Hack to fix a bug in FOX !!!!
code = KEY_F1+10*(string[pos+1]-'0')+(string[pos+2]-'0')-1;
// !!!! End of hack !!!!
}
else
{
code = KEY_F1+string[pos+1]-'1';
}
}
// Test if hexadecimal code designator
else if (string[pos] == '#')
{
code = strtoul(&string[pos+1], NULL, 16);
}
// Test if its a single character accelerator
else if (Ascii::isPrint(string[pos]))
{
if (mods&SHIFTMASK)
{
code = Ascii::toUpper(string[pos])+KEY_space-' ';
}
else
{
code = Ascii::toLower(string[pos])+KEY_space-' ';
}
}
return(MKUINT(code, mods));
}
// Find if the specified command exists
FXbool existCommand(const FXString cmd)
{
struct stat linfo;
// Command file path
FXString cmdpath = cmd.before(' ');
// If first character is '/' then cmdpath is an absolute path
if (cmdpath[0] == PATHSEPCHAR)
{
// Check if command file name exists and is not a directory
if (!cmdpath.empty() && (lstatrep(cmdpath.text(), &linfo) == 0) && !S_ISDIR(linfo.st_mode))
{
return(true);
}
}
// If first character is '~' then cmdpath is a path relative to home directory
else if (cmdpath[0] == '~')
{
// Form command absolute path
cmdpath = FXSystem::getHomeDirectory() + cmdpath.after('~');
// Check if command file name exists and is not a directory
if (!cmdpath.empty() && (lstatrep(cmdpath.text(), &linfo) == 0) && !S_ISDIR(linfo.st_mode))
{
return(true);
}
}
// Simple command name or path relative to the exec path
else
{
// Get exec path
FXString execpath = FXSystem::getExecPath();
if (execpath != "")
{
// Number of delimiters
int nbseps = execpath.contains(':');
// Loop over path components
for (int i = 0; i <= nbseps; i++)
{
// Obtain path component
FXString path = execpath.section(':', i);
// Form command absolute path
path += PATHSEPSTRING + cmdpath;
// Check if command file name exists and is not a directory
if (!path.empty() && (lstatrep(path.text(), &linfo) == 0) && !S_ISDIR(linfo.st_mode))
{
return(true);
}
}
}
}
return(false);
}
// Get key binding string from user input
// Code adapted from FXAccelTable::unparseAccel() and modified to get strings like 'Ctrl-A' instead of 'ctrl+a'
FXString getKeybinding(FXEvent* event)
{
// Get modifiers and key
int mods = event->state;
int code = event->code;
char buffer[64];
FXString s;
// Handle modifier keys
if (mods&CONTROLMASK)
{
s += "Ctrl-";
}
if (mods&ALTMASK)
{
s += "Alt-";
}
if (mods&SHIFTMASK)
{
s += "Shift-";
}
if (mods&METAMASK)
{
s += "Meta-";
}
// Handle some special keys
switch (code)
{
case KEY_Home:
s += "Home";
break;
case KEY_End:
s += "End";
break;
case KEY_Page_Up:
s += "PgUp";
break;
case KEY_Page_Down:
s += "PgDn";
break;
case KEY_Left:
s += "Left";
break;
case KEY_Right:
s += "Right";
break;
case KEY_Up:
s += "Up";
break;
case KEY_Down:
s += "Down";
break;
case KEY_Insert:
s += "Ins";
break;
case KEY_Delete:
s += "Del";
break;
case KEY_Escape:
s += "Esc";
break;
case KEY_Tab:
s += "Tab";
break;
case KEY_Return:
s += "Return";
break;
case KEY_BackSpace:
s += "Back";
break;
case KEY_space:
s += "Space";
break;
case KEY_F1:
case KEY_F2:
case KEY_F3:
case KEY_F4:
case KEY_F5:
case KEY_F6:
case KEY_F7:
case KEY_F8:
case KEY_F9:
case KEY_F10:
case KEY_F11:
case KEY_F12:
case KEY_F13:
case KEY_F14:
case KEY_F15:
case KEY_F16:
case KEY_F17:
case KEY_F18:
case KEY_F19:
case KEY_F20:
case KEY_F21:
case KEY_F22:
case KEY_F23:
case KEY_F24:
case KEY_F25:
case KEY_F26:
case KEY_F27:
case KEY_F28:
case KEY_F29:
case KEY_F30:
case KEY_F31:
case KEY_F32:
case KEY_F33:
case KEY_F34:
case KEY_F35:
snprintf(buffer, sizeof(buffer)-1, "F%d", code-KEY_F1+1);
s += buffer;
break;
default:
if (Ascii::isPrint(code))
{
s += Ascii::toUpper(code);
}
else
{
s = ""; // Invalid case
}
break;
}
return(s);
}
// Create a directory with its path, like 'mkdir -p'
// Return 0 if success or -1 if fail
// Original author : Niall O'Higgins */
// http://niallohiggins.com/2009/01/08/mkpath-mkdir-p-alike-in-c-for-unix
int mkpath(const char* s, mode_t mode)
{
char* q, *r = NULL, *path = NULL, *up = NULL;
int rv;
rv = -1;
if ((strcmp(s, ".") == 0) || (strcmp(s, "/") == 0))
{
return(0);
}
if ((path = strdup(s)) == NULL)
{
exit(EXIT_FAILURE);
}
if ((q = strdup(s)) == NULL)
{
exit(EXIT_FAILURE);
}
if ((r = (char*)dirname(q)) == NULL)
{
goto out;
}
if ((up = strdup(r)) == NULL)
{
exit(EXIT_FAILURE);
}
if ((mkpath(up, mode) == -1) && (errno != EEXIST))
{
goto out;
}
if ((mkdir(path, mode) == -1) && (errno != EEXIST))
{
rv = -1;
}
else
{
rv = 0;
}
out:
if (up != NULL)
{
free(up);
}
free(q);
free(path);
return(rv);
}
// Obtain a unique trash files path name based on the file path name
FXString createTrashpathname(FXString pathname, FXString trashfileslocation)
{
// Initial trash files path name
FXString trashpathname = trashfileslocation+PATHSEPSTRING+FXPath::name(pathname);
// Eventually modify the trash files path name by adding a suffix like '_1', '_2', etc.,
// if the file already exists in the trash can files directory
for (int i = 1; ; i++)
{
if (::exists(trashpathname))
{
char suffix[32];
snprintf(suffix, sizeof(suffix)-1, "_%d", i);
FXString prefix = trashpathname.rbefore('_');
if (prefix == "")
{
prefix = trashpathname;
}
trashpathname = prefix+suffix;
}
else
{
break;
}
}
return(trashpathname);
}
// Create trashinfo file based on the pathname and the trashpathname
int createTrashinfo(FXString pathname, FXString trashpathname, FXString trashfileslocation, FXString trashinfolocation)
{
// Create trash can files if it doesn't exist
if (!exists(trashfileslocation))
{
int mask = umask(0);
umask(mask);
int ret = mkpath(trashfileslocation.text(), 511 & ~mask);
return(ret);
}
// Create trash can info if it doesn't exist
if (!exists(trashinfolocation))
{
int mask = umask(0);
umask(mask);
int ret = mkpath(trashinfolocation.text(), 511 & ~mask);
return(ret);
}
// Deletion date
struct timeval tv;
gettimeofday(&tv, NULL);
FXString deldate = FXSystem::time("%FT%T", tv.tv_sec);
// Trash info path name
FXString trashinfopathname = trashinfolocation+PATHSEPSTRING+FXPath::name(trashpathname)+".trashinfo";
// Create trash info file
FILE* fp;
int ret;
if ((fp = fopen(trashinfopathname.text(), "w")) != NULL)
{
fprintf(fp, "[Trash Info]\n");
fprintf(fp, "Path=%s\n", pathname.text());
fprintf(fp, "DeletionDate=%s\n", deldate.text());
fclose(fp);
ret = 0;
}
else
{
ret = -1;
}
return(ret);
}
// Return mime type of a file
// Makes use of the Unix file command, thus this function may be slow
FXString mimetype(FXString pathname)
{
FXString cmd = "/usr/bin/file -b -i " + pathname;
FILE* filecmd = popen(cmd.text(), "r");
if (!filecmd)
{
perror("popen");
exit(EXIT_FAILURE);
}
char text[128] = { 0 };
FXString buf;
while (fgets(text, sizeof(text), filecmd))
{
buf += text;
}
pclose(filecmd);
return(buf.rbefore('\n'));
}
// Quote a filename against shell substitutions
// Thanks to Glynn Clements <glynnc@users.sourceforge.net>
FXString quote(FXString str)
{
FXString result = "'";
const char* p;
for (p = str.text(); *p; p++)
{
if (*p == '\'')
{
result += "'\\''";
}
else
{
result += *p;
}
}
result += '\'';
return(result);
}
// Test if a string is encoded in UTF-8
// "length" is the number of bytes of the string to consider
// Taken from the weechat project. Original author FlashCode <flashcode@flashtux.org>
FXbool isUtf8(const char* string, FXuint length)
{
FXuint n = 0;
while (n < length)
{
// UTF-8, 2 bytes, should be: 110vvvvv 10vvvvvv
if (((FXuchar)(string[0]) & 0xE0) == 0xC0)
{
if (!string[1] || (((FXuchar)(string[1]) & 0xC0) != 0x80))
{
return(false);
}
string += 2;
n += 2;
}
// UTF-8, 3 bytes, should be: 1110vvvv 10vvvvvv 10vvvvvv
else if (((FXuchar)(string[0]) & 0xF0) == 0xE0)
{
if (!string[1] || !string[2] ||
(((FXuchar)(string[1]) & 0xC0) != 0x80) ||
(((FXuchar)(string[2]) & 0xC0) != 0x80))
{
return(false);
}
string += 3;
n += 3;
}
// UTF-8, 4 bytes, should be: 11110vvv 10vvvvvv 10vvvvvv 10vvvvvv
else if (((FXuchar)(string[0]) & 0xF8) == 0xF0)
{
if (!string[1] || !string[2] || !string[3] ||
(((FXuchar)(string[1]) & 0xC0) != 0x80) ||
(((FXuchar)(string[2]) & 0xC0) != 0x80) ||
(((FXuchar)(string[3]) & 0xC0) != 0x80))
{
return(false);
}
string += 4;
n += 4;
}
// UTF-8, 1 byte, should be: 0vvvvvvv
else if ((FXuchar)(string[0]) >= 0x80)
{
return(false);
}
// Next byte
else
{
string++;
n++;
}
}
return(true);
}
#if defined(linux)
// Stat function used to test if a mount point is up or down
// Actually, this is simply the lstat() function
int lstatmt(const char* filename, struct stat* buf)
{
return(lstat(filename, buf));
}
#endif
#if !defined (__OpenBSD__)
// Safe strcpy function (Public domain, by C.B. Falconer)
// The destination string is always null terminated
// Size sz must be equal to strlen(src)+1
size_t strlcpy(char* dst, const char* src, size_t sz)
{
const char* start = src;
if (src && sz--)
{
while ((*dst++ = *src))
{
if (sz--)
{
src++;
}
else
{
*(--dst) = '\0';
break;
}
}
}
if (src)
{
while (*src++)
{
continue;
}
return(src - start - 1);
}
else if (sz)
{
*dst = '\0';
}
return(0);
}
// Safe strcat function (Public domain, by C.B. Falconer)
// The destination string is always null terminated
size_t strlcat(char* dst, const char* src, size_t sz)
{
char* start = dst;
while (*dst++) // assumes sz >= strlen(dst)
{
if (sz)
{
sz--; // i.e. well formed string
}
}
dst--;
return(dst - start + strlcpy(dst, src, sz));
}
#endif
// Obtain the non recursive size of a directory
FXulong dirsize(const char* path)
{
DIR* dp;
struct dirent* dirp;
struct stat statbuf;
char buf[MAXPATHLEN];
FXulong dsize = 0;
int ret;
if ((dp = opendir(path)) == NULL)
{
return(0);
}
while ((dirp = readdir(dp)))
{
if (streq(dirp->d_name, ".") || streq(dirp->d_name, ".."))
{
continue;
}
if (streq(path, ROOTDIR))
{
snprintf(buf, sizeof(buf)-1, "%s%s", path, dirp->d_name);
}
else
{
snprintf(buf, sizeof(buf)-1, "%s/%s", path, dirp->d_name);
}
#if defined(linux)
// Mount points are not processed to improve performances
if (mtdevices->find(buf))
{
continue;
}
#endif
ret = lstatrep(buf, &statbuf);
if (ret == 0)
{
if (!S_ISDIR(statbuf.st_mode))
{
dsize += (FXulong)statbuf.st_size;
}
}
}
if (closedir(dp) < 0)
{
fprintf(stderr, _("Error: Can't close folder %s\n"), path);
}
return(dsize);
}
// Obtain the recursive size of a directory
// The number of files and the number of sub directories is also stored in the nbfiles and nbsubdirs pointers
// Caution: this only works if nbfiles and nbsubdirs are initialized to 0 in the calling function
// After that, nbfiles contains the total number of files (including the count of sub directories),
// nbsubdirs contains the number of sub directories and totalsize the total directory size
// The pipes are used to write partial results, for inter process communication
FXulong pathsize(char* path, FXuint* nbfiles, FXuint* nbsubdirs, FXulong *totalsize, int pipes[2])
{
struct stat statbuf;
struct dirent* dirp;
char* ptr;
DIR* dp;
FXulong dsize;
int ret;
char buf[256];
ret = lstatrep(path, &statbuf);
if (ret < 0)
{
return(0);
}
dsize = (FXulong)statbuf.st_size;
(*totalsize) += dsize;
(*nbfiles)++;
// Write to pipe, if requested
if (pipes != NULL)
{
#if __WORDSIZE == 64
{
snprintf(buf,sizeof(buf),"%lu %u %u/", *totalsize, *nbfiles, *nbsubdirs);
}
#else
{
snprintf(buf,sizeof(buf),"%llu %u %u/", *totalsize, *nbfiles, *nbsubdirs);
}
#endif
if (write(pipes[1], buf, strlen(buf)) == -1)
{
perror("write");
exit(EXIT_FAILURE);
};
}
// Not a directory
if (!S_ISDIR(statbuf.st_mode))
{
return(dsize);
}
// Directory
(*nbsubdirs)++;
ptr = (char*)path + strlen(path);
if (ptr[-1] != '/')
{
*ptr++ = '/';
*ptr = '\0';
}
if ((dp = opendir(path)) == NULL)
{
return(0);
}
while ((dirp = readdir(dp)))
{
if (streq(dirp->d_name, ".") || streq(dirp->d_name, ".."))
{
continue;
}
strlcpy(ptr, dirp->d_name, strlen(dirp->d_name)+1);
// Recursive call
dsize += pathsize(path, nbfiles, nbsubdirs, totalsize, pipes);
}
ptr[-1] = '\0'; // ??
if (closedir(dp) < 0)
{
fprintf(stderr, _("Error: Can't close folder %s\n"), path);
}
return(dsize);
}
// Write the file size in human readable form (bytes or Kbytes or Mbytes)
// We use a decimal basis for KB, MB, GB count
FXString hSize(char* size)
{
int flag = 0;
char suf[64];
char buf[128];
FXString hsize;
FXulong lsize = strtoull(size, NULL, 10);
float fsize = 0.0;
strlcpy(suf, _("bytes"), sizeof(suf));
if (lsize > 1e9)
{
fsize = lsize/1e9;
strlcpy(suf, _("GB"), sizeof(suf));
flag = 1;
}
else if (lsize > 1e6)
{
fsize = lsize/1e6;
strlcpy(suf, _("MB"), sizeof(suf));
flag = 1;
}
else if (lsize > 1e3)
{
fsize = lsize/1e3;
strlcpy(suf, _("KB"), sizeof(suf));
flag = 1;
}
if (flag)
{
if (fsize == (int)fsize)
{
snprintf(buf, sizeof(buf), "%.0f %s", fsize, suf);
}
else
{
snprintf(buf, sizeof(buf), "%.1f %s", fsize, suf);
}
}
else
#if __WORDSIZE == 64
{
snprintf(buf, sizeof(buf), "%lu %s", lsize, suf);
}
#else
{
snprintf(buf, sizeof(buf), "%llu %s", lsize, suf);
}
#endif
hsize = buf;
return(hsize);
}
// Remove terminating '/' on a path string to simplify a file or directory path
// Thus '/bla/bla////' becomes '/bla/bla'
// Special case : '/' stays to '/'
FXString cleanPath(const FXString path)
{
FXString in = path, out = path;
while (1)
{
if ((in[in.length()-1] == '/') && (in.length() != 1))
{
out = in.trunc(in.length()-1);
in = out;
}
else
{
break;
}
}
return(out);
}
// Return the absolute path, based on the current directory path
// Remove terminating '/' on a path string to simplify a file or directory path
// Thus '/bla/bla////' becomes '/bla/bla'
// Special case : '/' stays to '/'
FXString filePath(const FXString path)
{
FXString in = path, out = path;
while (1)
{
if ((in[in.length()-1] == '/') && (in.length() != 1))
{
out = in.trunc(in.length()-1);
in = out;
}