00001 00005 /* Copyright © 2010 James Legg. 00006 This program is free software: you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation, either version 3 of the License, or 00009 (at your option) any later version. 00010 */ 00011 00012 #include "DataCheck.h" 00013 #include <fstream> 00014 00017 #include <unistd.h> 00018 00019 #include <Debug.h> 00020 00021 namespace Track 00022 { 00023 00024 DataCheck::DataCheck() 00025 { 00026 try 00027 { 00028 check_file("data/generic/booster"); 00029 } 00030 catch (FileMissingError e) 00031 { 00035 // Not found. Try using ./racer (works when in the trunk folder) 00036 if (!check_relative_directory("racer")) 00037 { 00038 // Try looking up a level? (works in racer_editor or libtrack) 00039 if (!check_relative_directory("../racer")) 00040 { 00041 // up one level? Should work in racer/data etc. 00042 if (!check_relative_directory("..")) 00043 { 00044 // Not found in current directory. Has it been installed? 00045 if (!check_directory("/usr/local/share/racer")) 00046 { 00047 // How about with a package manager? 00048 if (!check_directory("/usr/share/racer")) 00049 { 00050 // give up finding it. 00051 throw e; 00052 } 00053 } 00054 } 00055 } 00056 } 00057 } 00058 } 00059 00060 void DataCheck::check_file(std::string filename) 00061 { 00062 std::ifstream file(filename.c_str()); 00063 if (file.fail()) 00064 { 00065 FileMissingError error; 00066 error.filename = filename; 00067 throw error; 00068 } 00069 } 00070 00071 bool DataCheck::check_directory(std::string path) 00072 { 00073 try 00074 { 00075 check_file(path + "/data/generic/booster"); 00076 // must have worked, set directory. 00077 } 00078 catch (FileMissingError e) 00079 { 00080 // not here. 00081 return false; 00082 } 00083 // directory works, change it to the working directory. 00084 if (chdir(path.c_str()) == -1) 00085 { 00086 // Unusable, even though we found the file here. 00087 DEBUG_MESSAGE("Cannot change working directory, but data files found in " << path << "."); 00088 return false; 00089 } 00090 return true; 00091 } 00092 00093 bool DataCheck::check_relative_directory(std::string path_relative) 00094 { 00095 char path_cs[1024]; 00096 if (getcwd(path_cs, 1024) == 0) 00097 { 00098 DEBUG_MESSAGE("Cannot get current working directory, so cannot see if data files are somewhere relative."); 00099 FileMissingError e; 00100 e.filename = "data/generic/booster"; 00101 throw e; 00102 } 00103 std::string path_full(path_cs); 00104 path_full += "/" + path_relative; 00105 return check_directory(path_full); 00106 } 00107 00108 } // namespace Track 00109
Generated at Mon Sep 6 00:41:11 2010 by Doxygen version 1.4.7 for Racer version svn335.