Panda3D

load_prc_file.cxx

00001 // Filename: load_prc_file.cxx
00002 // Created by:  drose (22Oct04)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "load_prc_file.h"
00016 #include "configPageManager.h"
00017 #include "configVariableManager.h"
00018 #include "virtualFileSystem.h"
00019 #include "config_express.h"
00020 #include "config_util.h"
00021 #include "hashVal.h"
00022 
00023 ////////////////////////////////////////////////////////////////////
00024 //     Function: load_prc_file
00025 //  Description: A convenience function for loading explicit prc files
00026 //               from a disk file or from within a multifile (via the
00027 //               virtual file system).  Save the return value and pass
00028 //               it to unload_prc_file() if you ever want to unload
00029 //               this file later.
00030 //
00031 //               The filename is first searched along the default prc
00032 //               search path, and then also along the model path, for
00033 //               convenience.
00034 //
00035 //               This function is defined in putil instead of in dtool
00036 //               with the read of the prc stuff, so that it can take
00037 //               advantage of the virtual file system (which is
00038 //               defined in express), and the model path (which is in
00039 //               putil).
00040 ////////////////////////////////////////////////////////////////////
00041 ConfigPage *
00042 load_prc_file(const Filename &filename) {
00043   Filename path = filename;
00044   path.set_text();
00045 
00046   ConfigPageManager *cp_mgr = ConfigPageManager::get_global_ptr();
00047 
00048   VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr();
00049   vfs->resolve_filename(path, cp_mgr->get_search_path()) ||
00050     vfs->resolve_filename(path, get_model_path());
00051   
00052   istream *file = vfs->open_read_file(path, true);
00053   if (file == (istream *)NULL) {
00054     util_cat.error()
00055       << "Unable to open " << path << "\n";
00056     return NULL;
00057   }
00058   
00059   util_cat.info()
00060     << "Reading " << path << "\n";
00061   
00062   ConfigPage *page = cp_mgr->make_explicit_page(path);
00063   bool read_ok = page->read_prc(*file);
00064   vfs->close_read_file(file);
00065   
00066   if (read_ok) {
00067     return page;
00068     
00069   } else {
00070     util_cat.info()
00071       << "Unable to read " << path << "\n";
00072     cp_mgr->delete_explicit_page(page);
00073     return NULL;
00074   }
00075 }
00076 
00077 ////////////////////////////////////////////////////////////////////
00078 //     Function: load_prc_file_data
00079 //  Description: Another convenience function to load a prc file from
00080 //               an explicit string, which represents the contents of
00081 //               the prc file.
00082 //
00083 //               The first parameter is an arbitrary name to assign to
00084 //               this in-memory prc file.  Supply a filename if the
00085 //               data was read from a file, or use any other name that
00086 //               is meaningful to you.  The name is only used when the
00087 //               set of loaded prc files is listed.
00088 ////////////////////////////////////////////////////////////////////
00089 EXPCL_PANDA_PUTIL ConfigPage *
00090 load_prc_file_data(const string &name, const string &data) {
00091   istringstream strm(data);
00092 
00093   ConfigPageManager *cp_mgr = ConfigPageManager::get_global_ptr();
00094 
00095   ConfigPage *page = cp_mgr->make_explicit_page(name);
00096   bool read_ok = page->read_prc(strm);
00097   
00098   if (read_ok) {
00099     page->set_trust_level(1);  // temp hack
00100     return page;
00101     
00102   } else {
00103     util_cat.info()
00104       << "Unable to read explicit prc data " << name << "\n";
00105     cp_mgr->delete_explicit_page(page);
00106     return NULL;
00107   }
00108 }
00109 
00110 ////////////////////////////////////////////////////////////////////
00111 //     Function: unload_prc_file
00112 //  Description: Unloads (and deletes) a ConfigPage that represents a
00113 //               prc file that was previously loaded by
00114 //               load_prc_file().  Returns true if successful, false
00115 //               if the file was unknown.
00116 //
00117 //               After this function has been called, the ConfigPage
00118 //               pointer is no longer valid and should not be used
00119 //               again.
00120 ////////////////////////////////////////////////////////////////////
00121 bool
00122 unload_prc_file(ConfigPage *page) {
00123   ConfigPageManager *cp_mgr = ConfigPageManager::get_global_ptr();
00124   return cp_mgr->delete_explicit_page(page);
00125 }
00126 
00127 
00128 #ifdef HAVE_OPENSSL
00129 
00130 ////////////////////////////////////////////////////////////////////
00131 //     Function: hash_prc_variables
00132 //  Description: Fills HashVal with the hash from the current prc file
00133 //               state as reported by
00134 //               ConfigVariableManager::write_prc_variables().
00135 ////////////////////////////////////////////////////////////////////
00136 void
00137 hash_prc_variables(HashVal &hash) {
00138   ostringstream strm;
00139   ConfigVariableManager *cv_mgr = ConfigVariableManager::get_global_ptr();
00140   cv_mgr->write_prc_variables(strm);
00141   hash.hash_string(strm.str());
00142 }
00143 
00144 #endif  // HAVE_OPENSSL
 All Classes Functions Variables Enumerations