<commit_before>// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
// $Id: path.C,v 1.6.20.1 2007/06/21 19:38:11 oliver Exp $
// 
// Author:
//   <NAME>
//

#include <BALL/COMMON/global.h>
#include <BALL/SYSTEM/path.h>
#include <BALL/SYSTEM/file.h>
#include <BALL/SYSTEM/fileSystem.h>
#include <fstream>

#ifdef BALL_COMPILER_MSVC
#include <windows.h>
#endif

using std::ifstream;

namespace BALL 
{
	String Path::path_ = BALL_PATH;
	bool Path::path_array_valid_ = false;
	bool Path::environment_checked_ = false;
	std::vector<String> Path::path_array_;
	bool Path::initialized_ = false;

	void Path::reset()
	{
		path_ = getDefaultDataPath();
		path_array_valid_ = false;
		environment_checked_ = false;
	}

	Path::Path()
	{
		if (!initialized_) reset();
	}

	String Path::getDataPath()
	{
		buildPathArray_();
		return path_;
	}

	void Path::setDataPath(const String& path) 
	{
		path_array_valid_ = false;
		path_ = path;
	}

	void Path::addDataPath(const String& path)
	{
		path_array_valid_ = false;
		path_ += "\n";
		path_ += path;
	}

	String Path::getDefaultDataPath()
	{	
		String path = BALL_DATA_PATH;
		if (path[path.size()-1] != FileSystem::PATH_SEPARATOR)
			path.append(String(FileSystem::PATH_SEPARATOR));
		return path;
	}

	void Path::buildPathArray_() 
	{	
		// everything is valid - return directly
		if (path_array_valid_)
		{
			return;
		}

		// remove old path array
		path_array_.clear();

		// check whether the environment variable BALL_DATA_PATH is set
		if (!environment_checked_)
		{
			char*	ball_data_path = getenv("BALL_DATA_PATH");
			if (ball_data_path != 0)
			{	
				String bdp(ball_data_path);
				bdp.append("\n");
				bdp.append(path_);
				setDataPath(bdp);
			}
#ifdef BALL_COMPILER_MSVC
			// we may have additional path information in the registry
			std::vector<unsigned char> regbuffer(MAX_PATH, 0);
			DWORD valuesize = regbuffer.size();
			DWORD regresult;

			REGSAM dSam = KEY_QUERY_VALUE;
			HKEY key;

			// first, look in the local user's part of the registry
			HKEY base = HKEY_CURRENT_USER;
			
			DWORD reg_result = RegOpenKeyEx(base, "Software\\BALL", 0, dSam, &key);
      
			if (reg_result == ERROR_SUCCESS)
				reg_result = RegQueryValueEx(key, "BALL_DATA_PATH", 0, 0, &(regbuffer[0]), &valuesize);

			// if we found BALL_DATA_PATH in the registry keys, reg_result will equal ERROR_SUCCESS
			if ((reg_result == ERROR_SUCCESS) && (valuesize > 0))
			{
				String bdp(reinterpret_cast<char*>(&(regbuffer[0])), valuesize-1);
				addDataPath(bdp);
			}

			// and also look in the system wide settings
			valuesize = regbuffer.size();

			base = HKEY_LOCAL_MACHINE;
			reg_result = RegOpenKeyEx(base, "Software\\BALL", 0, dSam, &key);

			if (reg_result == ERROR_SUCCESS)
				reg_result = RegQueryValueEx(key, "BALL_DATA_PATH", 0, 0, &(regbuffer[0]), &valuesize);
			
			if ((reg_result == ERROR_SUCCESS) && (valuesize > 0))
			{
				String bdp(reinterpret_cast<char*>(&(regbuffer[0])), valuesize-1);
				addDataPath(bdp);
			}
#endif
	
			// don`t try this again
			environment_checked_ = true;
		}
		
		// segment the path string and insert each path 
		// into the path array. append slashes where neccessary
		path_.split(path_array_, "\n");

		// rebuild the path from the array later on
		path_ = "";

		for (Position i=0; i<path_array_.size(); ++i)
		{
			String& current_path = path_array_[i];
			if (current_path.trim() != "")
			{
				// append a '/' if neccessary (just to be sure...)
				if (current_path[current_path.size()-1] != FileSystem::PATH_SEPARATOR)
				{
					current_path.append(String(FileSystem::PATH_SEPARATOR));
				}

				path_ += current_path;

				if (i < path_array_.size() - 1)
					path_ += "\n";
			}
		}


		// remember we don't have to do this again - computation on demand!
		path_array_valid_ = true;
	}
<commit_msg>Fixed reading of BALL_DATA_PATH from Registry on Windows.<commit_after>// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
// $Id: path.C,v 1.6.20.1 2007/06/21 19:38:11 oliver Exp $
// 
// Author:
//   <NAME>
//

#include <BALL/COMMON/global.h>
#include <BALL/SYSTEM/path.h>
#include <BALL/SYSTEM/file.h>
#include <BALL/SYSTEM/fileSystem.h>
#include <fstream>

#ifdef BALL_COMPILER_MSVC
#include <windows.h>
#endif

using std::ifstream;

namespace BALL 
{
	String Path::path_ = BALL_PATH;
	bool Path::path_array_valid_ = false;
	bool Path::environment_checked_ = false;
	std::vector<String> Path::path_array_;
	bool Path::initialized_ = false;

	void Path::reset()
	{
		path_ = getDefaultDataPath();
		path_array_valid_ = false;
		environment_checked_ = false;
	}

	Path::Path()
	{
		if (!initialized_) reset();
	}

	String Path::getDataPath()
	{
		buildPathArray_();
		return path_;
	}

	void Path::setDataPath(const String& path) 
	{
		path_array_valid_ = false;
		path_ = path;
	}

	void Path::addDataPath(const String& path)
	{
		path_array_valid_ = false;
		path_ += "\n";
		path_ += path;
	}

	String Path::getDefaultDataPath()
	{	
		String path = BALL_DATA_PATH;
		if (path[path.size()-1] != FileSystem::PATH_SEPARATOR)
			path.append(String(FileSystem::PATH_SEPARATOR));
		return path;
	}

	void Path::buildPathArray_() 
	{	
		// everything is valid - return directly
		if (path_array_valid_)
		{
			return;
		}

		// remove old path array
		path_array_.clear();

		// check whether the environment variable BALL_DATA_PATH is set
		if (!environment_checked_)
		{
			char*	ball_data_path = getenv("BALL_DATA_PATH");
			if (ball_data_path != 0)
			{	
				String bdp(ball_data_path);
				bdp.append("\n");
				bdp.append(path_);
				setDataPath(bdp);
			}
#ifdef BALL_COMPILER_MSVC
			// we may have additional path information in the registry
			std::vector<unsigned char> regbuffer(MAX_PATH, 0);
			DWORD valuesize = regbuffer.size();
			DWORD regresult;

			REGSAM dSam = KEY_QUERY_VALUE;
			HKEY key;

			// first, look in the local user's part of the registry
			HKEY base = HKEY_CURRENT_USER;
			
			DWORD reg_result = RegOpenKeyEx(base, "Software\\BALL", 0, dSam, &key);
      
			if (reg_result == ERROR_SUCCESS)
				reg_result = RegQueryValueEx(key, "BALL_DATA_PATH", 0, 0, &(regbuffer[0]), &valuesize);

			// if we found BALL_DATA_PATH in the registry keys, reg_result will equal ERROR_SUCCESS
			if ((reg_result == ERROR_SUCCESS) && (valuesize > 0))
			{
				String bdp(reinterpret_cast<char*>(&(regbuffer[0])), 0, valuesize-1);
				addDataPath(bdp);
			}

			// and also look in the system wide settings
			valuesize = regbuffer.size();

			base = HKEY_LOCAL_MACHINE;
			reg_result = RegOpenKeyEx(base, "Software\\BALL", 0, dSam, &key);

			if (reg_result == ERROR_SUCCESS)
				reg_result = RegQueryValueEx(key, "BALL_DATA_PATH", 0, 0, &(regbuffer[0]), &valuesize);
			
			if ((reg_result == ERROR_SUCCESS) && (valuesize > 0))
			{
				String bdp(reinterpret_cast<char*>(&(regbuffer[0])), 0, valuesize-1);
				addDataPath(bdp);
			}
#endif
	
			// don`t try this again
			environment_checked_ = true;
		}
		
		// segment the path string and insert each path 
		// into the path array. append slashes where neccessary
		path_.split(path_array_, "\n");

		// rebuild the path from the array later on
		path_ = "";

		for (Position i=0; i<path_array_.size(); ++i)
		{
			String& current_path = path_array_[i];
			if (current_path.trim() != "")
			{
				// append a '/' if neccessary (just to be sure...)
				if (current_path[current_path.size()-1] != FileSystem::PATH_SEPARATOR)
				{
					current_path.append(String(FileSystem::PATH_SEPARATOR));
				}

				path_ += current_path;

				if (i < path_array_.size() - 1)
					path_ += "\n";
			}
		}

		// remember we don't have to do this again - computation on demand!
		path_array_valid_ = true;
	}

{'fraction_non_alphanumeric': 0.14036685312865876, 'fraction_numerical': 0.010927540002601795, 'mean_word_length': 3.5064478311840563, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '471292', 'n_tokens_mistral': 3197, 'n_tokens_neox': 2765, 'n_words': 653}