1414 */
1515#include " archivefile.h"
1616#include " file.h"
17+ bool Search_String_Matches (Utf8String string, Utf8String search);
1718
18- ArchivedFileInfo *ArchiveFile::Get_Archived_File_Info (Utf8String const &filename)
19+ const ArchivedFileInfo *ArchiveFile::Get_Archived_File_Info (Utf8String const &filename) const
1920{
2021 Utf8String path = filename;
2122 Utf8String token;
22- DetailedArchiveDirectoryInfo *dirp = &m_archiveInfo;
23+ const DetailedArchivedDirectoryInfo *dirp = &m_archiveInfo;
2324
2425 // Lower case for matching and get first item of the path.
2526 path.To_Lower ();
@@ -28,102 +29,102 @@ ArchivedFileInfo *ArchiveFile::Get_Archived_File_Info(Utf8String const &filename
2829 // Consider existence of '.' to indicate file as all should have .ext format
2930 // checks the remaining path does not contain one to catch directories in path
3031 // that do.
31- while (strchr (token.Str (), ' .' ) == nullptr || strchr (path.Str (), ' .' ) != nullptr ) {
32- if (dirp->directories .find (token) == dirp->directories .end ()) {
32+ while (token.Find (' .' ) == nullptr || path.Find (' .' ) != nullptr ) {
33+ auto it = dirp->directories .find (token);
34+
35+ if (!(it != dirp->directories .end ())) {
3336 return nullptr ;
3437 }
3538
36- dirp = &dirp-> directories [token] ;
39+ dirp = &it-> second ;
3740 path.Next_Token (&token, " \\ /" );
3841 }
3942
4043 // Assuming we didn't run out of directories to try, find the file
4144 // in the reached directory.
4245 auto file_it = dirp->files .find (token);
4346
44- if (file_it = = dirp->files .end ()) {
45- return nullptr ;
47+ if (file_it ! = dirp->files .end ()) {
48+ return &file_it-> second ;
4649 }
4750
48- return &file_it-> second ;
51+ return nullptr ;
4952}
5053
5154void ArchiveFile::Add_File (Utf8String const &filepath, ArchivedFileInfo const *info)
5255{
53- // DEBUG_LOG("Adding '%s' to interal archive for '%s'.\n", filepath.Str(), info->archive_name.Str());
5456 Utf8String path = filepath;
5557 Utf8String token;
56- DetailedArchiveDirectoryInfo *dirp = &m_archiveInfo;
57-
58- // Lower case for matching and get first item of the path.
58+ DetailedArchivedDirectoryInfo *dirp = &m_archiveInfo;
5959 path.To_Lower ();
60+ path.Next_Token (&token, " \\ /" );
6061
61- for (path.Next_Token (&token, " \\ /" ); !token.Is_Empty (); path.Next_Token (&token, " \\ /" )) {
62- // If an element of the path doesn't have a directory node, add it.
63- // DEBUG_LOG("Searching for path element '%s'.\n", token.Str());
62+ while (token.Get_Length () > 0 ) {
6463 if (dirp->directories .find (token) == dirp->directories .end ()) {
65- // DEBUG_LOG("Adding path element '%s'.\n", token.Str() );
64+ dirp-> directories [ token]. Clear ( );
6665 dirp->directories [token].name = token;
6766 }
6867
6968 dirp = &dirp->directories [token];
69+ path.Next_Token (&token, " \\ /" );
7070 }
7171
7272 dirp->files [info->file_name ] = *info;
7373}
7474
7575void ArchiveFile::Attach_File (File *file)
7676{
77- if (m_backingFile != nullptr ) {
78- m_backingFile ->Close ();
79- m_backingFile = nullptr ;
77+ if (m_attachedFile != nullptr ) {
78+ m_attachedFile ->Close ();
79+ m_attachedFile = nullptr ;
8080 }
8181
82- m_backingFile = file;
82+ m_attachedFile = file;
8383}
8484
85- void ArchiveFile::Get_File_List_From_Dir (Utf8String const &subdir,
85+ void ArchiveFile::Get_File_List_In_Directory (Utf8String const &subdir,
8686 Utf8String const &dirpath,
8787 Utf8String const &filter,
8888 std::set<Utf8String, rts::less_than_nocase<Utf8String>> &filelist,
8989 bool search_subdir) const
9090{
9191 Utf8String path = dirpath;
92+ path.To_Lower ();
9293 Utf8String token;
93- DetailedArchiveDirectoryInfo const *dirp = &m_archiveInfo;
94+ DetailedArchivedDirectoryInfo const *dirp = &m_archiveInfo;
95+ path.Next_Token (&token, " \\ /" );
9496
95- // Lower case for matching and get first item of the path.
96- path. To_Lower ( );
97+ while (token. Get_Length () > 0 ) {
98+ auto it = dirp-> directories . find (token );
9799
98- // Go to the last InfoNode in the path to extract file contents from.
99- for (path.Next_Token (&token, " \\ /" ); token.Is_Not_Empty (); path.Next_Token (&token, " \\ /" )) {
100- // If an element of the path doesn't have a node for our next directory, return.
101- if (dirp->directories .find (token) == dirp->directories .end ()) {
100+ if (!(it != dirp->directories .end ())) {
102101 return ;
103102 }
104103
105- dirp = &dirp->directories [token];
104+ dirp = &it->second ;
105+ path.Next_Token (&token, " \\ /" );
106106 }
107107
108- Get_File_List_From_Dir (dirp, dirpath, filter, filelist, search_subdir);
108+ Get_File_List_In_Directory (dirp, dirpath, filter, filelist, search_subdir);
109109}
110110
111- void ArchiveFile::Get_File_List_From_Dir (DetailedArchiveDirectoryInfo const *dir_info,
111+ void ArchiveFile::Get_File_List_In_Directory (DetailedArchivedDirectoryInfo const *dir_info,
112112 Utf8String const &dirpath,
113113 Utf8String const &filter,
114114 std::set<Utf8String, rts::less_than_nocase<Utf8String>> &filelist,
115115 bool search_subdir) const
116116{
117117 // Add the files from any subdirectories, recursive call.
118118 for (auto it = dir_info->directories .begin (); it != dir_info->directories .end (); ++it) {
119+ const DetailedArchivedDirectoryInfo *info = &it->second ;
119120 Utf8String path = dirpath;
120121
121- if (! path.Is_Empty () && !path.Ends_With (" \\ " ) && !path.Ends_With (" /" )) {
122- path += " /" ;
122+ if (path.Get_Length () > 0 && !path.Ends_With (" \\ " ) && !path.Ends_With (" /" )) {
123+ path. Concat ( " /" ) ;
123124 }
124125
125- path += it-> second . name ;
126- Get_File_List_From_Dir (&(it-> second ) , path, filter, filelist, search_subdir);
126+ path += info-> name ;
127+ Get_File_List_In_Directory (info , path, filter, filelist, search_subdir);
127128 }
128129
129130 // Add all the files that match the search pattern.
@@ -132,25 +133,30 @@ void ArchiveFile::Get_File_List_From_Dir(DetailedArchiveDirectoryInfo const *dir
132133 Utf8String path = dirpath;
133134
134135 if (!path.Is_Empty () && !path.Ends_With (" \\ " ) && !path.Ends_With (" /" )) {
135- path += " /" ;
136+ path. Concat ( " /" ) ;
136137 }
137138
138139 path += it->second .file_name ;
140+
139141 filelist.insert (path);
140142 }
141143 }
142144}
143145
144146// Helper funtion to check if a string matches the search string.
145- bool ArchiveFile:: Search_String_Matches (Utf8String string, Utf8String search)
147+ bool Search_String_Matches (Utf8String string, Utf8String search)
146148{
147149 // Trivial case if first string is empty.
148- if (string.Is_Empty ()) {
149- return search.Is_Empty ();
150+ if (string.Get_Length () == 0 ) {
151+ if (search.Get_Length () == 0 ) {
152+ return true ;
153+ }
154+
155+ return false ;
150156 }
151157
152158 // If there is no seach string, there cannot be a match.
153- if (search.Is_Empty () ) {
159+ if (search.Get_Length () == 0 ) {
154160 return false ;
155161 }
156162
@@ -195,5 +201,9 @@ bool ArchiveFile::Search_String_Matches(Utf8String string, Utf8String search)
195201 }
196202 }
197203
198- return *csearch == ' \0 ' ;
204+ if (*csearch == ' \0 ' ) {
205+ return true ;
206+ } else {
207+ return false ;
208+ }
199209}
0 commit comments