/
common__tools.cpp
executable file
·1425 lines (1184 loc) · 28.5 KB
/
common__tools.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 <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#include <wx/file.h>
#include "common__tools.h"
#include "common__mod.h"
#include "ob_editor.h"
#include "entity__globals.h"
#include "images__MyPalette.h"
using namespace std;
extern wxString curr_entity_filename;
/**\internal*****************
* SOME GLOBALS
* **************************
*/
wxConfigBase *config;
wxFileName dirRessources;
wxImage *noImg;
/**\internal*****************
* Miscellanious facilities
* **************************
*/
bool Frame_RestorePrevCoord( wxWindow* it, const wxString& _name )
{
int last_x, last_y, last_width, last_height;
bool b_ok = true;
b_ok = config->Read(wxT("/")+ _name + wxT("/last_x"), &last_x, 20) && b_ok;
b_ok = config->Read(wxT("/")+ _name + wxT("/last_y"), &last_y, 20) && b_ok;
b_ok = config->Read(wxT("/")+ _name + wxT("/last_width"), &last_width, 400) && b_ok;
b_ok = config->Read(wxT("/")+ _name + wxT("/last_height"), &last_height, 200) && b_ok;
it->SetSize(last_width,last_height);
it->Move( last_x,last_y );
return b_ok;
}
void Frame_SaveCoord( wxWindow* it, const wxString& name )
{
int _width, _height;
it->GetSize( &_width, &_height );
wxPoint _position = it->GetScreenPosition();
bool b_ok = true;
b_ok = config->Write(wxT("/")+ name + wxT("/last_x"), _position.x ) && b_ok;
b_ok = config->Write(wxT("/")+ name + wxT("/last_y"), _position.y ) && b_ok;
b_ok = config->Write(wxT("/")+ name + wxT("/last_width"), _width ) && b_ok;
b_ok = config->Write(wxT("/")+ name + wxT("/last_height"), _height ) && b_ok;
if( ! b_ok )
wxMessageBox(wxT("Cannot save Coords for window ") + name,
wxT("ProPlem"), wxOK | wxICON_INFORMATION, it );
}
void Frame_DestroySavedCoord( const wxString& _name )
{
config->DeleteEntry(wxT("/")+ _name + wxT("/last_x"));
config->DeleteEntry(wxT("/")+ _name + wxT("/last_y"));
config->DeleteEntry(wxT("/")+ _name + wxT("/last_width"));
config->DeleteEntry(wxT("/")+ _name + wxT("/last_height"));
return;
}
//****************************************************
void ClearMenu( wxMenu* menu )
{
if( menu == NULL || menu->GetMenuItemCount() <= 0 )
return;
wxMenuItem *t = menu->FindItemByPosition( 0 );
while( 1 )
{
menu->Delete( t );
if( menu->GetMenuItemCount() <= 0 )
break;
else
t = menu->FindItemByPosition( 0 );
}
}
void OpenInExternalEditor( wxWindow* parent, const wxString& filePath )
{
// Get the editor from the config
wxString editor_path;
if( ! config->Read( wxT("/startFrame/editor_program"), &editor_path ) )
{
wxMessageBox(wxT("Did you correctly set the editor in the start window ??"),
wxT("ProPlem"), wxOK | wxICON_EXCLAMATION, parent );
return;
}
if( ! wxFileName(editor_path).FileExists() )
{
wxMessageBox(wxT("Unable to find ")+ editor_path +wxT(" ??"),
wxT("ProPlem"), wxOK | wxICON_EXCLAMATION, parent );
return;
}
if( ! wxFileName(filePath).FileExists() )
{
wxMessageBox(wxT("Unable to open the file \n\n")+ filePath,
wxT("ProPlem"), wxOK | wxICON_EXCLAMATION, parent );
return;
}
// Launch the editor
wxExecute( wxT("\"") + editor_path + wxT("\" \"") + filePath + wxT("\"") );
}
//****************************************************
void EnableItemsMenu( wxMenu* menu, bool b_enable )
{
if( menu == NULL || menu->GetMenuItemCount() <= 0 )
return;
wxMenuItemList itemsList = menu->GetMenuItems();
wxMenuItemList::iterator iter;
for (iter = itemsList.begin(); iter != itemsList.end(); ++iter)
{
wxMenuItem *current = *iter;
current->Enable( b_enable );
}
}
//********************************
/**\internal*****************
* Images facilities
* **************************
*/
wxColour MeltColor(wxColour _c0, wxColour _c1 )
{
float _fact = 1;
if( _c1.Red() != 0 )
_fact = 1 + (float) _c0.Red() / (float) _c1.Red();
int _r = ( _c1.Red() - _c0.Red() ) + _c0.Red() * _fact;
if( _r >= 256 ) _r = 255;
_fact = 1;
if( _c1.Green() != 0 )
_fact = 1 + (float) _c0.Green() / (float) _c1.Green();
int _g = ( _c1.Green() - _c0.Green() ) + _c0.Green() * _fact;
if( _g >= 256 ) _g = 255;
_fact = 1;
if( _c1.Blue() != 0 )
_fact = 1 + (float) _c0.Blue() / (float) _c1.Blue();
int _b = ( _c1.Blue() - _c0.Blue() ) + _c0.Blue() * _fact;
if( _b >= 256 ) _b = 255;
return wxColour( _r, _g, _b );
}
Image_Holder::Image_Holder()
{
img = NULL;
png_or_gif = NULL;
}
Image_Holder::~Image_Holder()
{
RemoveImages();
}
int Image_Holder::Get_ColorIndex( const int x, const int y )
{
if( Get_GIF_or_PNG() == NULL )
return -1;
return png_or_gif->GetIndex( x, y );
}
void Image_Holder::RemoveImages()
{
if( img != NULL )
delete img;
img = NULL;
if( png_or_gif != NULL )
delete png_or_gif;
png_or_gif = NULL;
}
wxImage* Image_Holder::GetImage()
{
if( img != NULL )
return img;
wxString _path = GetFileName().GetFullPath();
if( ! wxFileName(_path).FileExists() )
return NULL;
if( IsFileEmpty( _path))
return NULL;
if( IsFileEmpty( _path))
return NULL;
img = new wxImage( _path );
if( ! img->IsOk() )
{
delete img;
img = NULL;
}
return img;
}
imgFile* Image_Holder::Get_GIF_or_PNG()
{
if( img == NULL && GetImage() == NULL )
return NULL;
wxFileName fn = GetFileName();
wxString str_path = fn.GetFullPath();
if( str_path ==wxString() )
return NULL;
if( ! fn.FileExists() )
return NULL;
// Check if the file is Zero Size
if( IsFileEmpty( str_path))
return NULL;
if( fn.GetExt().Upper() ==wxT("PNG"))
png_or_gif = new wxIndexedPNG( str_path );
else if( fn.GetExt().Upper() ==wxT("GIF"))
png_or_gif = new wxIndexedGIF( str_path );
else
return NULL;
if( ! png_or_gif->IsOk() )
{
delete png_or_gif;
png_or_gif = NULL;
return NULL;
}
return png_or_gif;
}
//---------------------------------------------------------
wxString
wxColour_ToStr( const wxColour& c )
{
return IntToStr(c.Red())
+wxT(",")+ IntToStr(c.Green())
+wxT(",")+ IntToStr(c.Blue())
;
}
//---------------------------------------------------------
wxColour
wxColour_FromStr( const wxString& s )
{
wxArrayString as = StrSplit( s,wxT(","));
return wxColour( StrToInt(as[0])
, StrToInt(as[1])
, StrToInt(as[2])
);
}
void
Image_Rescale_To_Fit( wxImage& img, size_t w, size_t h )
{
if( img.IsOk() == false )
return;
size_t img_w = img.GetWidth();
size_t img_h = img.GetHeight();
float zoom_x = 10000;
float zoom_y = 10000;
if( img_w >= w )
zoom_y = (float)w/(float)img_w;
if( img_h >= h )
zoom_x = (float)h/(float)img_h;
if( zoom_y != 10000 || zoom_x != 10000 )
{
float z = min(zoom_x,zoom_y);
img.Rescale( img_w * z, img_h * z );
return;
}
zoom_y = (float)w/(float)img_w;
zoom_x = (float)h/(float)img_h;
float z = min(zoom_x,zoom_y);
img.Rescale( img_w * z, img_h * z );
return;
}
/**\internal*****************
* Ob_objects facilities
* **************************
*/
void Sort_ob_Object_ByName( ob_object**& objs, size_t count )
{
for( size_t i =0; i < count ; i++ )
{
size_t curr_ind = i;
for( size_t j = i+1; j < count; j++ )
if( objs[j]->name.Upper() < objs[curr_ind]->name.Upper() )
curr_ind = j;
ob_object *temp = objs[i];
objs[i] = objs[curr_ind];
objs[curr_ind] = temp;
}
}
void Sort_ob_Object_ByTag( ob_object**& objs, size_t count, size_t num_tag )
{
for( size_t i =0; i < count ; i++ )
{
size_t curr_ind = i;
for( size_t j = i+1; j < count; j++ )
if( objs[j]->GetToken(num_tag).Upper() < objs[curr_ind]->GetToken(num_tag).Upper() )
curr_ind = j;
ob_object *temp = objs[i];
objs[i] = objs[curr_ind];
objs[curr_ind] = temp;
}
}
void Sort_ob_Object_ByTag( ob_anim**& objs, size_t count, size_t num_tag )
{
ob_object** temp = (ob_object**) objs;
Sort_ob_Object_ByTag( temp, count, num_tag );
}
wxString* Reverse_Null_StrArray( wxString* tokens, size_t tokens_size )
{
wxString* res = new wxString[tokens_size];
int i = 0;
int j = tokens_size -1;
while( j >=0 )
{
res[j] = tokens[i];
i++;
j--;
}
return res;
}
wxString UnTokenize( ob_object* _obj )
{
if( _obj == NULL)
return wxString();
wxString res;
int j = 0;
while(j>=0)
{
wxString _t = _obj->GetToken(j);
if( _t == wxString() )
j = -1;
else
{
res += _t + wxT(" ");
j++;
}
}
res.Trim();
return res;
}
wxArrayString ArrayString_PrependAll( const wxArrayString& arrStr, const wxString& _prefix )
{
wxArrayString res;
for( size_t i = 0; i < arrStr.Count(); i++)
{
res.Add( _prefix + arrStr[i] );
}
return res;
}
wxString ArrayString_Join( const wxArrayString& arrStr, const wxString& _jointure )
{
if( arrStr.Count() <= 0 )
return wxString();
wxString res = arrStr[0];
for( size_t i = 1; i < arrStr.Count(); i++)
{
res += _jointure + arrStr[i];
}
return res;
}
void TabStringToArrayString( wxString* tStr, size_t nb, wxArrayString& arrStr )
{
arrStr.Clear();
for( size_t i=0; i < nb; i++)
arrStr.Add( tStr[i] );
}
bool IsInArray( const wxString& _t, wxString* arr, size_t arr_tay )
{
for( size_t i = 0; i < arr_tay; i++ )
{
if( _t.Upper() == arr[i].Upper() )
return true;
}
return false;
}
/**\internal*****************
* Conversions facilities
* **************************
*/
wxString IntToStr( int i )
{
return wxString::Format(wxT("%d"), (int)i);
}
wxString IntToStr( size_t i )
{
return wxString::Format(wxT("%d"), static_cast<int>(i));
}
int StrToInt( const wxString& str, bool& ok )
{
long res = 0;
ok = str.ToLong(&res);
return (int)res;
}
int StrToInt( const wxString& str )
{
long res = 0;
bool ok;
ok = str.ToLong(&res);
return (int)res;
}
unsigned int StrToUInt( const wxString& str, bool& ok )
{
long int res = 0;
ok = str.ToLong(&res);
if( res < 0 )
{
ok = false;
return (unsigned int) -res;
}
return (unsigned int) res;
}
float StrToFloat(const wxString& str, bool& ok )
{
double res = 0;
wxString t(str);
t.Replace( wxT("."), wxT(",") );
ok = t.ToDouble(&res);
return (float)res;
}
float StrToFloat(const wxString& str )
{
bool dummy;
return StrToFloat( str, dummy );
}
wxString FloatToStr( const float f)
{
wxString t;
t.Printf( wxT("%f"), f);
t.Replace( wxT(","), wxT(".") );
if( t.Find( wxT('.') ) )
{
while( t[t.Len()-1] == wxT('0') )
t.Truncate(t.Len() -1 );
if( t[t.Len()-1] == '.' )
t.Truncate(t.Len() -1 );
}
return t;
}
bool IntToBool(const int i )
{ return i != 0; }
bool StrToBool( const wxString& str )
{
bool ok = false;
int res = StrToInt( str, ok );
if( !ok )
return false;
return IntToBool( res );
}
/**\internal*****************
* Strings facilities
* **************************
*/
bool StrIsObBool( const wxString& str )
{
if( str.Len() != 1 )
return false;
if( str[0] != wxT('0') && str[0] != wxT('1') )
return false;
return true;
}
bool StrIsInt( const wxString& str )
{
bool res;
StrToInt(str, res );
return res;
}
bool StrIsUInt( const wxString& str )
{
bool res;
StrToUInt(str, res );
return res;
}
bool StrIsFloat( const wxString& str )
{
bool res;
StrToFloat(str, res );
return res;
}
wxString ArrStrConcat( const wxArrayString& arrStr, const wxString& separator )
{
if( arrStr.Count() == 0 )
return wxString();
wxString res;
for( size_t i=0; i < arrStr.Count()-1; i++)
res += arrStr[i] + separator;
res += arrStr[arrStr.Count()-1];
return res;
}
wxArrayString StrSplit( const wxString& str, const wxString& delim, bool b_remove_empty )
{
wxArrayString res;
if( str.Len() == 0 )
{
res.Add(wxString());
return res;
}
if( delim.Len() == 0 )
{
res.Add( str );
return res;
}
wxString temp = str;
while( true )
{
int ind = temp.Find( delim );
if( ind == wxNOT_FOUND )
{
res.Add( temp );
break;
}
else
{
if( ind != 0 || ! b_remove_empty)
res.Add( temp.Left(ind) );
temp = temp.Right( temp.Len() -delim.Len() - ind );
}
}
return res;
}
inline wxArrayString Concat_ArrayString( const wxArrayString & arr1, const wxArrayString & arr2 )
{
wxArrayString res = arr1;
for( size_t i = 0; i < arr2.Count(); i++ )
res.Add( arr2[i] );
return res;
}
inline wxArrayString __Tokenize( wxArrayString& arr_str, const wxString& delim )
{
wxArrayString res;
for( size_t i = 0; i < arr_str.Count(); i++ )
res = Concat_ArrayString( res, StrSplit( arr_str[i], delim, true ) );
// removing empty elts
wxArrayString res2;
for( size_t i = 0; i < res.Count(); i++ )
if( res[i] != wxString() )
res2.Add( res[i]);
return res2;
}
wxArrayString Tokenize( const wxString& str )
{
wxArrayString res;
if( str.Len() == 0 )
return res;
res.Add( str );
res = __Tokenize( res, wxT(" ") );
res = __Tokenize( res, wxT("\t") );
return res;
}
//--------------------------------------------------------------------
void
wxArraystring__Add( wxArrayString& arr_str, size_t nb_str, ... )
{
va_list ap;
va_start (ap, nb_str);
for( size_t i= 0; i < nb_str; i++ )
arr_str.Add( wxString::FromAscii(va_arg(ap, const char* )));
va_end (ap);
}
//--------------------------------------------------------------------
wxArrayString
wxArrayString_Build( size_t nb_str, ... )
{
wxArrayString res;
va_list ap;
va_start (ap, nb_str);
for( size_t i= 0; i < nb_str; i++ )
res.Add( wxString::FromAscii(va_arg(ap, const char* )));
va_end (ap);
return res;
}
;
/**\internal*****************
* Files facilities
* **************************
*/
wxFileName MyDirAppend( const wxFileName dataDirPath, const wxString& subDir )
{
wxFileName res = dataDirPath;
wxArrayString arr_dirs = StrSplit( subDir, wxT('/') );
for( size_t i = 1; i < arr_dirs.Count(); i++)
res.AppendDir( arr_dirs[i] );
return res;
}
wxFileName MyPathAppend( const wxFileName _dataDirPath, const wxString& subPath )
{
wxFileName res = _dataDirPath;
wxArrayString arr_dirs = StrSplit( subPath, wxT('/') );
if( arr_dirs.Count() == 1 )
{
return wxFileName(res.GetPath(), arr_dirs[0] );
}
wxString filename = arr_dirs[arr_dirs.Count() -1];
for( size_t i = 0; i < arr_dirs.Count() -1; i++)
res.AppendDir( arr_dirs[i] );
return wxFileName(res.GetPath(), filename );
}
wxString
ObPath_To_FullPath( const wxString& _subPath )
{
return GetObFile( _subPath ).GetFullPath();
}
wxFileName GetObFile( const wxString& _subPath )
{
return MyPathAppend( dataDirPath, _subPath);
}
wxString Convert_To_Ob_Path( const wxString& s_path )
{
// first remove the base dir
wxString __t = dataDirPath.GetPath();
wxString _path(s_path);
//if( _path.Replace( __t , _T(""), false ) == 0 ) // FIXME: figure what _T did !
if( _path.Replace( __t , wxString(), false ) == 0 )
// No replacement made => not a valid dataDir relative path
return wxString();
wxFileName temp( _path );
wxArrayString t_arr = temp.GetDirs();
if( t_arr.Count() == 0 )
{
if( _path[0] == '/' )
return _path.Mid( 1, _path.Len() -1 );
else
return _path;
}
wxString res = ArrStrConcat( t_arr, wxT("/") );
if( res[0] == wxT('/') )
res = res.Mid( 1, res.Len() -1 );
if( res.Len() > 0 )
res += wxT('/') + temp.GetFullName();
return res;
}
wxFileName GetRessourceFile( const wxString& _strFileName )
{
wxFileName temp = dirRessources;
temp.SetFullName( _strFileName );
return temp;
}
wxString GetRessourceFile_String( const wxString& _strFileName )
{
return GetRessourceFile( _strFileName ).GetFullPath();
}
void RemoveLastAtom( wxFileName& _fn )
{
if( _fn.HasName() )
_fn = _fn.GetPath();
else
_fn.RemoveLastDir();
}
bool DirExists( wxFileName _fn )
{
wxString ttt = _fn.GetFullPath();
if( ! _fn.IsDir() )
return false ;
if( _fn.DirExists() )
return true;
return false;
}
bool __RECURS_FindFirsValidPath( wxString& path, wxArrayString& sub_dirs, int& arr_ind )
{
if( sub_dirs.Count() == 0)
return true;
wxString new_dir = path + wxFileName::GetPathSeparator() + sub_dirs[arr_ind];
wxFileName ttt(new_dir + wxFileName::GetPathSeparator());
if( ttt.DirExists() )
{
path = new_dir;
arr_ind++;
return __RECURS_FindFirsValidPath( path, sub_dirs, arr_ind );
}
return true;
}
int __apply_string_path( wxString& curr_dir, long& camino)
{
long t = camino;
size_t camino_len = 0;
while( t > 0 )
{
t = t >> 1;
camino_len++;
}
if( camino_len > curr_dir.Len() )
return false;
wxString res;
bool all_ones = true;
for( size_t i = 0; i < curr_dir.Len(); i ++)
{
long t = (camino >> i );
if( t & 1 )
res += toupper( curr_dir[i] );
else
{
res += tolower( curr_dir[i] );
all_ones = false;
}
}
camino++;
curr_dir = res;
return all_ones;
}
wxString TryRepairObPath( const wxFileName& _fn )
{
// easy repair
if( _fn.FileExists() )
return _fn.GetFullPath();
wxString _sep = wxFileName::GetPathSeparator();
wxString _fp = _fn.GetFullPath();
#ifdef OSLINUX
_fp.Replace( wxT("\\"), wxT("/") );
#endif
// Try to find the first existing dir
wxArrayString sub_dirs = StrSplit( _fp, wxFileName::GetPathSeparator() );
wxString _curr_path;
int ind_sub_dirs = 0;
bool res = __RECURS_FindFirsValidPath(_curr_path , sub_dirs, ind_sub_dirs );
// Nothing in the path exists !!
if( ! res )
return wxString();
// Try to rebuild the path dir after dir
for( size_t i = ind_sub_dirs; i < sub_dirs.Count(); i++)
{
// try every combination of upper and lowercase string with the current dir
wxString curr_subdir = sub_dirs[i];
// If the curr_subdir name is too big => abandon repair
if( curr_subdir.Len() > 32 )
return wxString();
// Build the starting path of construction
long camino = 0;
int cont = 0;
wxString curr_Try;
while( cont == 0 )
{
wxString _temp_ = curr_subdir;
cont = __apply_string_path( _temp_, camino);
curr_Try = _curr_path + _sep + _temp_;
// Dir existence check
if( i < sub_dirs.Count() -1 )
{
_temp_ = curr_Try + _sep;
wxFileName t(_temp_);
if( t.DirExists() )
cont = 2;
}
// Final Existence check
else
if( wxFileName(curr_Try).FileExists() || wxDirExists(curr_Try + _sep) )
cont = 2;
}
// Not found
if( cont != 2 )
return wxString();
// Found !!
_curr_path = curr_Try;
}
// Repaired !!!
return _curr_path;
}
bool SimpleBackUpFile( const wxString& filePath )
{
wxString backup_path = filePath +wxT(".bak");
return wxCopyFile( filePath, backup_path, true );
}
WX_DECLARE_HASH_MAP( wxString, bool, wxStringHash, wxStringEqual, Hash_File_backedup );
Hash_File_backedup hash_file_backup;
bool myBackUp( const wxString& filePath )
{
// If already backedup
if( hash_file_backup[filePath] )
return true;
if( SimpleBackUpFile( filePath ) )
{
hash_file_backup[filePath] = true;
return true;
}
return false;
}
bool myMove( const wxString& _src, const wxString& _dest )
{
wxString tempfile = wxFileName::CreateTempFileName( _src );
if( ! wxRenameFile( _src, tempfile ) )
return false;
if( ! wxRenameFile( tempfile, _dest ) )
return false;
return true;
}
bool IsFileEmpty( const wxString& _path )
{
wxFile _t(_path );
if( ! _t.IsOpened() )
return true;
int len = _t.Length();
_t.Close();
return ( len == 0);
}
/**\internal*****************
* Helper Class for repairing paths
* **************************/
Repair_Set::Repair_Set()
{
}
Repair_Set::~Repair_Set()
{
while( props.empty() == false )
{
struct d_s_i t = props.front();
delete t.prop_name;
props.pop_front();
}
}
void
Repair_Set::Add_Repairable( const wxString& _prop_name, size_t _num_token )
{
struct d_s_i add;
add.prop_name = new wxString(_prop_name.Upper());
add.num_token = _num_token;
props.push_back( add );
}
// Return count of repaired
size_t
Repair_Set::Try_Repair(ob_object* o )
{
return RECURS_Repair(o);
}
size_t
Repair_Set::RECURS_Repair(ob_object* o)
{
size_t res = 0;
list<struct d_s_i>::iterator it(props.begin()),
it_end (props.end());
for(;it!=it_end;it++)
{
if( o->name.Upper() == *((*it).prop_name) )
{
if( o->nb_tokens > (*it).num_token )
{
wxString st_file = GetObFile( o->GetToken( (*it).num_token )).GetFullPath();
if( wxFileExists(st_file) == false && wxDirExists(st_file) == false )
{
wxString repaired_path =TryRepairObPath( st_file );
if( repaired_path != wxString() )
{
o->SetToken( (*it).num_token, Convert_To_Ob_Path( repaired_path ));
res++;
}
}
}