When autodetecting, don't segfault if no item was previously selected and a device...
[Rockbox.git] / rbutil / installlog.cpp
blob7f6fa999f6bc86dfc2701555a74d2603e46f7833
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * Module: rbutil
9 * File: installlog.cpp
11 * Copyright (C) 2006 Christi Alice Scarborough
13 * All files in this archive are subject to the GNU General Public License.
14 * See the file COPYING in the source tree root for full license agreement.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
21 #include "installlog.h"
22 #include "rbutil.h"
24 InstallLog::InstallLog(wxString logname, bool CreateLog)
26 wxString buf;
27 dirtyflag = true;
29 if (! CreateLog && ! wxFileExists(logname) ) return;
31 logfile = new wxFileConfig(wxEmptyString, wxEmptyString, logname);
34 if (!logfile)
36 wxLogWarning(_("Failed to create install log file: ") + logname);
37 return;
40 logfile->SetPath(wxT("/InstallLog"));
41 if (logfile->Exists(wxT("Version")) &&
42 logfile->Read(wxT("Version"), 0l) != LOGFILE_VERSION )
44 wxLogWarning(_("Logfile version mismatch: ") + logname);
45 delete logfile;
46 return;
49 logfile->Write(wxT("Version"), LOGFILE_VERSION);
50 dirtyflag = false;
53 InstallLog::~InstallLog()
55 if (dirtyflag) return;
57 delete logfile;
60 unsigned int InstallLog::WriteFile(wxString filepath, bool isDir)
62 wxString key, buf;
63 long installcount = 0;
65 if (dirtyflag) return true;
67 filepath.Replace(PATH_SEP, wxT("/") );
69 if (filepath.GetChar(0) == '/')
70 filepath = filepath.Right(filepath.Len() - 1);
72 logfile->SetPath(wxT("/FilePaths"));
73 installcount = logfile->Read(filepath, 0l);
75 if (isDir)
77 filepath.Append(wxT("/" DIRECTORY_KLUDGE) ); // Needed for empty dirs
80 logfile->Write(filepath, ++installcount);
82 return false;
85 unsigned int InstallLog::WriteFile(wxArrayString filepaths)
87 unsigned long i;
88 unsigned int finalrc = false;
89 wxString thisone;
91 if (dirtyflag) return true;
93 for (i = 0; i < filepaths.GetCount(); i++);
95 if ( WriteFile(filepaths[i]) )
97 finalrc++;
101 return finalrc;
104 wxArrayString* InstallLog::GetInstalledFiles()
106 wxString curdir = wxT("");
108 if (dirtyflag) return NULL;
109 workingAS.Clear();
111 EnumerateCurDir(wxT(""));
113 wxArrayString* out = new wxArrayString(workingAS);
114 return out;
117 void InstallLog::EnumerateCurDir(wxString curdir)
119 bool contflag;
120 wxString curname, buf, buf2, pathcache;
121 long dummy;
123 buf = wxT("/FilePaths/") + curdir;
124 pathcache = logfile->GetPath();
125 logfile->SetPath(buf);
127 contflag = logfile->GetFirstGroup(curname, dummy);
128 while (contflag)
130 buf = curdir + wxT("/") + curname;
131 buf2 = buf; buf2.Replace(wxT("/"), PATH_SEP);
132 workingAS.Add(buf2);
133 EnumerateCurDir(buf);
134 contflag = logfile->GetNextGroup(curname, dummy);
137 contflag = logfile->GetFirstEntry(curname, dummy);
138 while (contflag)
140 if (curname != wxT(DIRECTORY_KLUDGE) )
142 buf = curdir + wxT("" PATH_SEP) + curname;
143 workingAS.Add(buf);
145 contflag = logfile->GetNextEntry(curname, dummy);
148 logfile->SetPath(pathcache);