/*	Roast+ License

//	### C compilable ###

//	僼傽僀儖宯娭悢
*/
#ifndef __SFJP_OPENMGL_roast_file_H__
#define __SFJP_OPENMGL_roast_file_H__

#include "roast_common.h"
#include "roast_bufsafe.h"	/*	roast_auto_append_filesep	*/
#include "roast_str.h"		/*	roast_auto_append_filesep	*/

#include <stdio.h>


#define ROAST_DIR_SEP_CHAR_WIN		'\\'
#define ROAST_DIR_SEP_CHAR_LIN		'/'
#define ROAST_DIR_SEP_CHAR_LINUX	'/'
#define ROAST_DIR_SEP_CHAR_UNIX		'/'

#define ROAST_DIR_SEP_STR_WIN		"\\"
#define ROAST_DIR_SEP_STR_LIN		"/"
#define ROAST_DIR_SEP_STR_LINUX		"/"
#define ROAST_DIR_SEP_STR_UNIX		"/"


/*=============================================================================*/

#ifdef ROAST_WIN32
  
  #include <sys/stat.h>

  #ifdef __stat64
	typedef struct _stat32		_roast_filestat_t, ROAST_FILE_STAT, _ROAST_FILE_STAT;
  #else
	typedef struct _stat		_roast_filestat_t, ROAST_FILE_STAT, _ROAST_FILE_STAT;
  #endif
	typedef unsigned long		_roast_filestat_mode, _roast_stat_mode, ROAST_STAT_MODE, _ROAST_STAT_MODE;


	#define ROAST_DIR_SEP_CHAR	ROAST_DIR_SEP_CHAR_WIN
	#define ROAST_DIR_SEP_STR	ROAST_DIR_SEP_STR_WIN

	#define ROAST_MAX_PATH		(32000+1)


#else/* UNIX/Linux */

	#include <sys/types.h>
	#include <sys/stat.h>
	#include <unistd.h>

	typedef struct stat			_roast_filestat_t, ROAST_FILE_STAT, _ROAST_FILE_STAT;
	typedef mode_t				_roast_filestat_mode, _roast_stat_mode, ROAST_STAT_MODE, _ROAST_STAT_MODE;


	#define ROAST_DIR_SEP_CHAR	ROAST_DIR_SEP_CHAR_LINUX
	#define ROAST_DIR_SEP_STR	ROAST_DIR_SEP_STR_LINUX

	#define ROAST_MAX_PATH		(1024+1)

#endif


#define _ROAST_STAT_MODE_ERROR	(0xffffffff)

/*
#ifdef ROAST_WIN32
	#define ROAST_DIR_SEP_CHAR	'\\'
#else
	#define ROAST_DIR_SEP_CHAR	'/'
#endif


/*************************************************************************/


__ROAST_EXTERN_C_START

/*仩仭仩仭仩仭仩仭仩仭仩仭仩仭仩仭仩仭仩仭仩仭仩仭仩仭仩仭仩仭仩仭仩仭仩仭*/

/*	roast_file.c	*/

ROAST_BOOL roast_mkdir( const char* filename, unsigned long mode );



/*============================================================*/

/*	roast_file__str.c	*/




/*	\傗/偺楢懕傪柍偔偡	*/
char* RoastDeleteRedundancyFileSep( char *szStr, unsigned int nStrBufSize );


/*	.\偩偲偐..\偩偲偐偦偆偄偆偺傪張棟偡傞	*/
char* roast_fix_path( char* path, unsigned int bufsize );
#define roast_path_fix(path,bufsize)	roast_fix_path(path,bufsize);
#define RoastFixPath(path,bufsize)		roast_fix_path(path,bufsize);
#define RoastPathFix(path,bufsize)		roast_fix_path(path,bufsize);


/*	僼傽僀儖僷僗嬫愗傝暥帤偑晅偄偰偄側偐偭偨傜帺摦揑偵晅壛偡傞	*/
/*	壖憐揑側愰尵
	char* roast_auto_append_filesep( char* path, unsigned int bufsize );

	bufsize傪挻偊側偄斖埻偱晅壛丅path傪暅婣偡傞丅
*/
#define roast_auto_append_filesep( path, bufsize )	\
	(( ROAST_LAST_CHAR(path) != ROAST_DIR_SEP_CHAR_WIN &&	\
	   ROAST_LAST_CHAR(path) != ROAST_DIR_SEP_CHAR_LIN ) ?	\
	  roast_safe_strcat( path, ROAST_DIR_SEP_STR, bufsize ) : path )


/*	媡偵帺摦揑偵嶍彍	*/
/*	壖憐揑側愰尵
	char* roast_auto_remove_last_filesep( char* path );

	path傪暅婣偡傞丅
*/
#define roast_auto_remove_last_filesep( path )	\
	(( ROAST_LAST_CHAR(path) == ROAST_DIR_SEP_CHAR_WIN ||	\
	   ROAST_LAST_CHAR(path) == ROAST_DIR_SEP_CHAR_LIN ) ?	\
	  ROAST_REMOVE_LAST_CHAR( path ), path : path )
#define roast_remove_last_filesep( path )	roast_auto_remove_last_filesep(path)
#define RoastRemoveLastFileSep( path )		roast_auto_remove_last_filesep(path)



/*	僼儖僷僗傪庢摼	*/
/*	./傗../摍傕張棟偝傟傞	*/
const char* roast_fullpath( const char* filename, char* fullpathout, unsigned int bufsize );


#define roast_del_rfs			RoastDeleteRedundancyFileSep
#define roast_drfs				RoastDeleteRedundancyFileSep
#define roast_del_red_file_sep	RoastDeleteRedundancyFileSep



/*============================================================*/

/*	roast_file__stat.c	*/

ROAST_BOOL _roast_file_stat32( const char *path, _roast_filestat_t *p_stat_out );
_roast_stat_mode _roast_get_file_mode( const char* filename );
long roast_filesize32( const char* filename );

#define roast_filesize	roast_filesize32



/*============================================================*/

/*	roast_file__access.c	*/


/*	僼傽僀儖偺懚嵼桳柍傪挷傋傞
//	ret:  ROAST_TRUE:懚嵼  /  ROAST_FALSE:懚嵼偟側偄	*/
ROAST_BOOL roast_exist_file( const char* filename );	

/*	僼傽僀儖撉傔傞丠  */
ROAST_BOOL roast_canread( const char* filename );

/*	僼傽僀儖彂偒崬傔傞丠	*/
ROAST_BOOL roast_canwrite( const char* filename );

/*	幚峴弌棃傞丠	*/
ROAST_BOOL roast_canexec( const char* filename );




/*============================================================*/

/*	Windows: roast_file_win32__windows_h.c	*/
/*	Linux:   roast_file_unix__isdirectory.c	*/


ROAST_BOOL roast_is_directory( const char* filename );
ROAST_BOOL roast_is_file( const char* filename );
ROAST_BOOL roast_is_directory_ex( const char* filename, ROAST_BOOL *pbRetIs );
ROAST_BOOL roast_is_file_ex( const char* filename, ROAST_BOOL *pbRetIs );


/*	Windows: roast_file_win32__windows_h.c	*/
char* RoastGetModuleCurDir( char* szFilePathOut, unsigned int nBufSize );	/* <- 偄傫偺偐側偙傟 */


/*	Windows: roast_file_win32__windows_h.c	*/
/*	Linux:   roast_file_unix__isdirectory.c	*/

/*	僶僢僼傽偑懌傝側偄応崌偵偼NULL	*/
char* roast_curdir( char* buf, unsigned int bufsize );

ROAST_BOOL roast_chdir(const char* path);



/*============================================================*/

/*	roast_file_win32__imagehlp.c	*/

ROAST_BOOL RoastCreateDirectoryR( const char* szDirectory );





/*仩仭仩仭仩仭仩仭仩仭仩仭仩仭仩仭仩仭仩仭仩仭仩仭仩仭仩仭仩仭仩仭仩仭仩仭*/


/*	傑偩愰尵偩偗丅乮懡暘 roast_file_win32__windows_h2.c?乯  */


/*	儕僱乕儉	*/
ROAST_BOOL roast_rename_file(const char* from, const char* to);

/*	嶍彍	*/
ROAST_BOOL roast_del_file( const char* filename );

/*	僐僺乕	*/
ROAST_BOOL roast_copy_file( const char* from, const char* to );

/*	堏摦	*/
ROAST_BOOL roast_move_file( const char* from, const char* to );



/*////////////////////////////////////////////////////////////////////*/
/*  撉傒彂偒                                                          */


/*	彂偒崬傒	*/
ROAST_BOOL roast_write_file( const char* filename, unsigned char* membuf, unsigned int length);

/*	捛婰彂偒崬傒	*/
ROAST_BOOL roast_append_file( const char* filename, unsigned char* membuf, unsigned int length);

/*	撉傒崬傒	*/
unsigned char* roast_read_file_alloc( const char* filename );
unsigned char* roast_read_file( const char* filename, unsigned char* membuf, unsigned int length);



////////////////////////////////////////////////////////

//	僪儔僀僽僞僀僾
#define ROAST_DRIVE_TYPE_UNKNOWN	(0)
#define ROAST_DRIVE_TYPE_FIXED 		(1)		//	屌掕僪儔僀僽乮HDD摍乯
#define ROAST_DRIVE_TYPE_HDD 		(ROAST_DRIVE_TYPE_FIXED)

#define ROAST_DRIVE_TYPE_REMOVABLE 	(2)		//	偙偺僨傿僗僋偼丄僪儔僀僽偐傜庢傝弌偣傑偡丅
#define ROAST_DRIVE_TYPE_REMOVABLE 	(2)		//	偙偺僨傿僗僋偼丄僪儔僀僽偐傜庢傝弌偣傑偡丅

#define ROAST_DRIVE_TYPE_CDROM 		(4)		//	偙偺僪儔僀僽偼丄CD-ROM 僪儔僀僽偱偡丅
#define ROAST_DRIVE_TYPE_RAMDISK 	(8)		//	偙偺僪儔僀僽偼丄RAM 僨傿僗僋偱偡丅

#define ROAST_DRIVE_TYPE_REMOTE 	(32)		//	偙偺僪儔僀僽偼丄儕儌乕僩乮僱僢僩儚乕僋乯僪儔僀僽偱偡丅
#define ROAST_DRIVE_TYPE_NETWORK 	(32)		//	偙偺僪儔僀僽偼丄儕儌乕僩乮僱僢僩儚乕僋乯僪儔僀僽偱偡丅

int roast_get_drive_type(const char *path);


__ROAST_EXTERN_C_END


#endif/*__SFJP_OPENMGL_roast_file_H__*/
