Jump to content
  • Advertisement
Sign in to follow this  
Stormtrooper

Making a hiddend directory + config file

This topic is 4085 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, I have the following code which should make a hidden directory in the user's home directory along with a log and config file and sets where the assests are located...
#ifdef OS_WIN
	homeDir = "./";
	assetsDir = "./";
    logger->setLog( homeDir + "divulge.log");
	conf.setConfigFile( homeDir + "config.xml" );
#endif

#ifdef OS_LINUX
	homeDir = getenv("HOME");
	assetsDir = "/opt/divuleonline/data";
	logger->setLog( homeDir + "/.divulgeonline/divulge.log" );
	conf.setConfigFile( homeDir + "/.divulgeonline/config.xml" );
	printf( "Using Home Directory: %s", homeDir );
	printf( "Using Assets Directory: %s", assetsDir );
#endif

#ifdef OS_MAC
	homeDir = getenv("HOME");
	assetsDir = "/opt/divulgeonline/data";
	logger->setLog( homeDir + "/.divulgeonline/divulge.log" );
	conf.setConfigFile( homeDir + "/.divulgeonline/config.xml" );
#endif
But I have no way of testing the mac or linux side, can anyone give me some tips about how to accomplish this? Here is what the logger looks like
Logger::~Logger()
{
    if(mLogFile.is_open())
    {
    	mLogFile.close();
    }
}

// Set the log file to write to
void Logger::setLog(std::string filename)
{
    mLogFile.open(filename.c_str(), std::ios::trunc);
    
    if(!mLogFile.is_open())
    {
        std::cout << "Failed to open log file: " << filename << std::endl;
    }
}

// Write text to the log file with a timestamp
void Logger::write(const char *text, ...)
{
    if(!mLogFile.is_open())
    {
        return;
    }
    
    char *buf = new char[1024];
    va_list ap;
    
    va_start(ap, text);
    vsprintf(buf, text, ap);
    va_end(ap);
    
	time_t t;
	time(&t);
    tm *date = localtime(&t);
        
    mLogFile << "["
             << date->tm_hour
             << ":"
             << date->tm_min
             << ":"
             << date->tm_sec
             << "]"
             << buf
             << std::endl;

	delete buf;
}

Share this post


Link to post
Share on other sites
Advertisement
On my mac system and on my debian system:
std::cout << getenv("HOME") << std::endl;
print the right thing.

If other lines from you code snippets functions well on win32 there will be no problems on other systems. Perhaps you cannot assume that "./" is the home directory in win32. It's only the current working directory.

Pay attention to this:
void Logger::setLog(std::string filename)
you must pass a std::string as a reference in order to evitate an object copy:
void Logger::setLog(std::string& filename)

Share this post


Link to post
Share on other sites
From what I can see of Windows, you're using the current directory (which may or may not be the same as the executable directory) for the configuration directory. You will not be able to write to this directory if the application is in Program Files and the process does not have administator rights, and multiple users will not be able to have multiple settings (not to mention the settings will be ignored if you try to migrate them using the Windows tools for the job).

For these reasons, you could consider using the designated directories for application settings - and the paths to these can be derived using the SHGetSpecialFolderPath function (CSIDL_APPDATA).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!