/*
	Rogue Trader.
	Copyright (c) 2007, Darragh Coy.
	All rights reserved.
	Original work(s), source code & author(s) details available at: http://www.darraghcoy.com

	Redistribution and use in source and binary forms, with or without modification, are permitted 
	provided that the following conditions are met:

		* 	Redistributions of source code must retain the above copyright notice, this list of 
			conditions and the following disclaimer.
		* 	Redistributions in binary form must reproduce the above copyright notice, this list of 
			conditions and the following disclaimer in the documentation.
		* 	Neither the name of the author nor the names of contributors may be used to endorse or 
			promote products derived from this software without specific prior written permission.
		* 	The use of any third party libraries, modules or data files provided with the source 
			form of the program must follow and respect the individual licensing terms of these 
			components. You do not have permission to modify or circumvent the licensing terms for 
			provided third party components unless the third party licenses specifically allow this. 
			If you do not agree to the terms of provided third party components then you do not have 
			permission to use them in your derived work(s).
		* 	The terms of this license do not apply to separate (but related) distributions if supplied 
			by the author(s), only to the contents of this source distribution, derived works, and the 
			binaries produced by either modified or unmodified versions of the source code. The terms 
			of this license do not also apply to any third party libraries or components provided with 
			the source distribution. Such third party elements are licensed under their own separate 
			agreements.

	THIS SOFTWARE IS PROVIDED BY DARRAGH COY ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
	ARE DISCLAIMED. IN NO EVENT SHALL DARRAGH COY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
	OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################
//
//
// Class Name:   SaveDataFolder
// Purpose:      This handles creation of application save data folder and 
//				 returns it's path.
//
//
//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################

#include "SaveDataFolder.h"
#include <Windows.h>
#include <shlobj.h>
#include <list> 

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Name:     SaveDataFolder::GetLocalAppDataFolder()
Purpose:  Creates the application save data folder if it does not already exist.
Params:   Save folder to create.
Returns:  none

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/

void SaveDataFolder::CreateSaveFolder( const char * name )
{
	// Abort if no name given:
	
	if ( name == 0 || name[0] == 0 ) return;

	// Get the local app data folder:
	
	std::string app_folder = GetLocalAppDataFolder();
	
	// Need to split up the given folder name if there is multiple sub-folders:
	
	std::list<std::string> folders;
	
	{
		unsigned beg = 0;				// Beginning of the current folder name
		unsigned end = 0;				// End of the current folder name
		unsigned len = strlen(name);	// Length of the name string
		
		for ( unsigned i = 0 ; i < len ; i++ )
		{
			// See if there is a backslash or forward slash at current location:
			
			if ( name[i] == '\\' || name[i] == '/' )
			{
				// End of this particular path part
			
				end = i;
				
				// Make sure ok:
				
				if ( beg != end && beg < end )
				{
					// Extra safety checks:
					
					if ( beg < len && end <= len )
					{
						// Add this path part to the list of strings:
						
						int l = end - beg; char * folder = new char[ l + 1 ]; folder[l] = 0;
						
						// Fill it in:
						
						for ( unsigned j = beg ; j < end ; j++ ) folder[j-beg] = name[j];
						
						// Add:
						
						folders.push_back( std::string(folder) ); delete[] folder;
					}
				}
				
				// Set the beginning for the next string: 
				
				beg = i + 1;
				end = i + 1;
			}
		}
		
		end = len;
		
		// Make sure ok:
		
		if ( beg != end && beg < end )
		{
			// Extra safety checks:
			
			if ( beg < len && end <= len )
			{
				// Add this path part to the list of strings:
				
				int l = end - beg; char * folder = new char[ l + 1 ]; folder[l] = 0;
				
				// Fill it in:
				
				for ( unsigned j = beg ; j < end ; j++ )
				{
					folder[j-beg] = name[j];
				}
				
				// Add:
				
				folders.push_back( std::string(folder) ); delete[] folder;
			}
		}
	}
	
	// Now make sure all the folders are made:
	
	{
		std::list<std::string>::iterator i = folders.begin();
		std::list<std::string>::iterator e = folders.end();
		
		while ( i != e )
		{
			// Append to the name: 
			
			app_folder += std::string("\\") + *i; i++;
			
			// Make the folder:
			
			CreateDirectoryA(app_folder.c_str(),NULL); 
		}
	}
}

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Name:     SaveDataFolder::GetLocalAppDataFolder()
Purpose:  Gets the path to local application data on the system
Params:   none
Returns:  The path to the application local data on the system

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/

std::string SaveDataFolder::GetLocalAppDataFolder()
{
	// Store the path to the save folder here:
	
	char path[MAX_PATH]; path[0] = 0;
	
	// Get it:

	HRESULT result = SHGetFolderPathA
	(          
		0										,		// No owner window: do not care about this
		CSIDL_LOCAL_APPDATA	| CSIDL_FLAG_CREATE	,		// CSIDL for the folder: local (non-roaming) application data. Create if not there.
		0										,		// Token for the current user: let windows fill this in
		SHGFP_TYPE_CURRENT						,		// Retrieve the current path
		path											// Where to save the path
	);
    
    // See if that was ok:
    
    if ( result == S_OK ) 
    {
		return std::string(path);    
	}
	else
	{
		return std::string("");
	}
}
