<commit_before>#ifndef INCLUDE_AL_FILE_HPP
#define INCLUDE_AL_FILE_HPP

#include <unistd.h>
#include <stdio.h>
#include <string>
#include <sys/stat.h>
#include <list>

#include "system/al_Config.h"

#ifdef AL_WIN32 
	#define AL_FILE_DELIMITER '\\'
#else
	#define AL_FILE_DELIMITER '/'
#endif	

#define AL_PATH_MAX (4096)

namespace al{

/// Strips a qualified path to a file (src) into a path to the containing folder (dst)
void path2dir(char* dst, const char* src);

/// a pair of path (folder/directory) and filename
class FilePath {
public:
	FilePath() {};
	FilePath(std::string file, std::string path = "/") : mPath(path), mFile(file) {}
	
	const std::string& file() const { return mFile; }
	const std::string& path() const { return mPath; }
	
	std::string filepath() const { return path()+file(); }
	
	FilePath& file(const std::string& v) { mFile=v; return *this; }
	FilePath& path(const std::string& v) { mPath=v; return *this; }
	
protected:
	std::string mPath;
	std::string mFile;
};

/// A handy way to manage several possible search paths
class SearchPaths {
public:
	SearchPaths() {}
	SearchPaths(int argc, char * const argv[], bool recursive=true) { addAppPaths(argc,argv,recursive); }
	~SearchPaths() {}

	/// find a file in the searchpaths
	/// returns true if file found, and fills result with corresponding path & filename
	/// returns false if file not found
	FilePath find(const std::string& filename);
	
	/// add a path to search in; recursive searching is optional
	void addSearchPath(const std::string& path, bool recursive = true);
	
	/// adds best estimate of application launch paths (cwd etc.)
	/// can pass in argv from the main() function if desired.
	void addAppPaths(int argc, char * const argv[], bool recursive = true);
	void addAppPaths(bool recursive = true);
	
	const std::string& appPath() const { return mAppPath; }
	
	/// todo?
	//void addResourcePath();
	
	// strips trailing filename from a path; e.g. /usr/bin/man -> /usr/bin/
	static std::string stripFileName(const std::string& src);
	// ensure path ends with the proper delimiter
	static std::string conformPath(const std::string& src);
	// does a file at the given filepath exist?
	static bool fileExists(const std::string& name, const std::string& path);
	
protected:

	typedef std::pair<std::string, bool> searchpath;
	std::list<searchpath> mSearchPaths;
	
	std::string mAppPath;

};


/// File
class File{
public:

	/// @param[in] path		path of file
	/// @param[in] mode		i/o mode w, r, wb, rb
	File(std::string path, std::string mode="r", bool open=false);

	~File();

	void close();	///< Close file
	bool open();	///< Open file with specified i/o mode

	File& mode(std::string v){ mMode=v.data(); return *this; }
	File& path(std::string v){ mPath=v.data(); return *this; }

	/// Write memory elements to file
	int write(std::string v){ return fwrite(v.data(), 1, v.length(), mFP); }
	int write(const void * v, int size, int items=1){ return fwrite(v, size, items, mFP); }

	/// Quick and dirty write memory to file
	static int write(std::string path, const void * v, int size, int items=1);

	/// Returns character string of file contents (read mode only)
	char * readAll();

	/// Returns whether file is open
	bool opened() const { return 0 != mFP; }
	
	/// Returns file i/o mode string
	const char * mode() const { return mMode; }
	
	/// Returns path string
	const char * path() const { return mPath; }
	
	/// Returns size (in bytes) of file contents
	int size() const { return mSizeBytes; }

	/// Returns whether file exists
	static bool exists(std::string path);
	
	/// Return modification time of file (or 0 on failure)
	/// as number of seconds since 00:00:00 january 1, 1970 UTC
	al_sec modified();
	/// Return last access time of file (or 0 on failure)
	/// as number of seconds since 00:00:00 january 1, 1970 UTC
	al_sec accessed();
	/// Return creation time of file (or 0 on failure)
	/// as number of seconds since 00:00:00 january 1, 1970 UTC
	al_sec created();
	
	/// Return size file (or 0 on failure)
	size_t size();
	/// Return space used on disk of file (or 0 on failure)
	size_t storage();
	
	FILE * filePointer() { return mFP; }

protected:
	const char * mPath;
	const char * mMode;
	char * mContent;
	int mSizeBytes;
	FILE * mFP;
	
	class Impl; Impl * mImpl;
	
	void freeContent();
	void allocContent(int n);
	void getSize();
};



//// INLINE IMPLEMENTATION ////
<commit_msg>fixed initialization order in File()<commit_after>#ifndef INCLUDE_AL_FILE_HPP
#define INCLUDE_AL_FILE_HPP

#include <unistd.h>
#include <stdio.h>
#include <string>
#include <sys/stat.h>
#include <list>

#include "system/al_Config.h"

#ifdef AL_WIN32 
	#define AL_FILE_DELIMITER '\\'
#else
	#define AL_FILE_DELIMITER '/'
#endif	

#define AL_PATH_MAX (4096)

namespace al{

/// Strips a qualified path to a file (src) into a path to the containing folder (dst)
void path2dir(char* dst, const char* src);

/// a pair of path (folder/directory) and filename
class FilePath {
public:
	FilePath() {};
	FilePath(std::string file, std::string path = "/") : mPath(path), mFile(file) {}
	
	const std::string& file() const { return mFile; }
	const std::string& path() const { return mPath; }
	
	std::string filepath() const { return path()+file(); }
	
	FilePath& file(const std::string& v) { mFile=v; return *this; }
	FilePath& path(const std::string& v) { mPath=v; return *this; }
	
protected:
	std::string mPath;
	std::string mFile;
};

/// A handy way to manage several possible search paths
class SearchPaths {
public:
	SearchPaths() {}
	SearchPaths(int argc, char * const argv[], bool recursive=true) { addAppPaths(argc,argv,recursive); }
	~SearchPaths() {}

	/// find a file in the searchpaths
	/// returns true if file found, and fills result with corresponding path & filename
	/// returns false if file not found
	FilePath find(const std::string& filename);
	
	/// add a path to search in; recursive searching is optional
	void addSearchPath(const std::string& path, bool recursive = true);
	
	/// adds best estimate of application launch paths (cwd etc.)
	/// can pass in argv from the main() function if desired.
	void addAppPaths(int argc, char * const argv[], bool recursive = true);
	void addAppPaths(bool recursive = true);
	
	const std::string& appPath() const { return mAppPath; }
	
	/// todo?
	//void addResourcePath();
	
	// strips trailing filename from a path; e.g. /usr/bin/man -> /usr/bin/
	static std::string stripFileName(const std::string& src);
	// ensure path ends with the proper delimiter
	static std::string conformPath(const std::string& src);
	// does a file at the given filepath exist?
	static bool fileExists(const std::string& name, const std::string& path);
	
protected:

	typedef std::pair<std::string, bool> searchpath;
	std::list<searchpath> mSearchPaths;
	
	std::string mAppPath;

};


/// File
class File{
public:

	/// @param[in] path		path of file
	/// @param[in] mode		i/o mode w, r, wb, rb
	File(std::string path, std::string mode="r", bool open=false);

	~File();

	void close();	///< Close file
	bool open();	///< Open file with specified i/o mode

	File& mode(std::string v){ mMode=v.data(); return *this; }
	File& path(std::string v){ mPath=v.data(); return *this; }

	/// Write memory elements to file
	int write(std::string v){ return fwrite(v.data(), 1, v.length(), mFP); }
	int write(const void * v, int size, int items=1){ return fwrite(v, size, items, mFP); }

	/// Quick and dirty write memory to file
	static int write(std::string path, const void * v, int size, int items=1);

	/// Returns character string of file contents (read mode only)
	char * readAll();

	/// Returns whether file is open
	bool opened() const { return 0 != mFP; }
	
	/// Returns file i/o mode string
	const char * mode() const { return mMode; }
	
	/// Returns path string
	const char * path() const { return mPath; }
	
	/// Returns size (in bytes) of file contents
	int size() const { return mSizeBytes; }

	/// Returns whether file exists
	static bool exists(std::string path);
	
	/// Return modification time of file (or 0 on failure)
	/// as number of seconds since 00:00:00 january 1, 1970 UTC
	al_sec modified();
	/// Return last access time of file (or 0 on failure)
	/// as number of seconds since 00:00:00 january 1, 1970 UTC
	al_sec accessed();
	/// Return creation time of file (or 0 on failure)
	/// as number of seconds since 00:00:00 january 1, 1970 UTC
	al_sec created();
	
	/// Return size file (or 0 on failure)
	size_t size();
	/// Return space used on disk of file (or 0 on failure)
	size_t storage();
	
	FILE * filePointer() { return mFP; }

protected:
	class Impl; Impl * mImpl;

	const char * mPath;
	const char * mMode;
	char * mContent;
	int mSizeBytes;
	FILE * mFP;
	
	void freeContent();
	void allocContent(int n);
	void getSize();
};



//// INLINE IMPLEMENTATION ////

{'fraction_non_alphanumeric': 0.13527008624602815, 'fraction_numerical': 0.01112119836586473, 'mean_word_length': 3.779284164859002, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '7211264', 'n_tokens_mistral': 2979, 'n_tokens_neox': 2779, 'n_words': 1070}