Files
HxCFloppyEmulator/libhxcadaptor/sources/fs.c
T
Jean-François DEL NERO fd636826d4 OS "Glue" functions lib: Remove OS specific functions from lib & software.
git-svn-id: svn://svn.code.sf.net/p/hxcfloppyemu/code/HxCFloppyEmulator/libhxcadaptor/trunk@447 63fa2f70-6a9b-4bc4-b855-9c6a949b1d69
2012-07-17 10:15:06 +00:00

390 lines
8.3 KiB
C

/*
//
// Copyright (C) 2006 - 2012 Jean-François DEL NERO
//
// This file is part of the HxCFloppyEmulator library
//
// HxCFloppyEmulator may be used and distributed without restriction provided
// that this copyright statement is not removed from the file and that any
// derivative work contains the original copyright notice and the associated
// disclaimer.
//
// HxCFloppyEmulator is free software; you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// HxCFloppyEmulator is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with HxCFloppyEmulator; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
*/
///////////////////////////////////////////////////////////////////////////////////
//-------------------------------------------------------------------------------//
//-------------------------------------------------------------------------------//
//-----------H----H--X----X-----CCCCC----22222----0000-----0000------11----------//
//----------H----H----X-X-----C--------------2---0----0---0----0--1--1-----------//
//---------HHHHHH-----X------C----------22222---0----0---0----0-----1------------//
//--------H----H----X--X----C----------2-------0----0---0----0-----1-------------//
//-------H----H---X-----X---CCCCC-----222222----0000-----0000----1111------------//
//-------------------------------------------------------------------------------//
//----------------------------------------------------- http://hxc2001.free.fr --//
///////////////////////////////////////////////////////////////////////////////////
// File : fs.c
// Contains: UTF-8 / Ascii path file system functions
//
// Written by: DEL NERO Jean Francois
//
// Change History (most recent first):
///////////////////////////////////////////////////////////////////////////////////
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#ifdef WIN32
# include <windows.h>
# include <io.h>
# include <fcntl.h>
#else
# include <unistd.h>
#endif
#include "libhxcfe.h"
#include "libhxcadaptor.h"
#ifdef WIN32
int convertpath (const char * path, wchar_t * wpath)
{
if (!MultiByteToWideChar (CP_UTF8, 0, path, -1, wpath, MAX_PATH))
{
errno = ENOENT;
return -1;
}
wpath[MAX_PATH] = L'\0';
return 0;
}
#else
#endif
int hxc_open (const char *filename, int flags, ...)
{
#ifdef WIN32
wchar_t wpath[MAX_PATH+1];
#else
const char *local_name;
#endif
unsigned int mode = 0;
va_list ap;
va_start (ap, flags);
if (flags & O_CREAT)
mode = va_arg (ap, unsigned int);
va_end (ap);
#ifdef WIN32
memset(wpath,0,(MAX_PATH+1)*sizeof(wchar_t));
if(convertpath(filename, wpath)<0)
return -1;
return _wopen (wpath, flags, mode);
#else
local_name = filename;
//local_name = (const char *)ToLocale (filename);
if (local_name == NULL)
{
errno = ENOENT;
return -1;
}
int fd = open (local_name, flags, mode);
//LocaleFree (local_name);
return fd;
#endif
}
FILE *hxc_fopen (const char *filename, const char *mode)
{
int rwflags, oflags;
int append;
int fd;
const char *ptr;
FILE *stream;
// Try the classic way (ascii path)
stream = fopen(filename,mode);
if( ( stream == NULL ) && (errno == ENOENT) )
{
// Try the utf8 way
rwflags = 0;
oflags = 0;
append = 0;
for(ptr = mode;*ptr;ptr++)
{
switch (*ptr)
{
case 'r':
rwflags = O_RDONLY;
break;
#ifdef O_BINARY
case 'b':
oflags |= O_BINARY;
break;
#endif
case 'a':
rwflags = O_WRONLY;
oflags |= O_CREAT;
append = 1;
break;
case 'w':
rwflags = O_WRONLY;
oflags |= O_CREAT | O_TRUNC;
break;
case '+':
rwflags = O_RDWR;
break;
#ifdef O_TEXT
case 't':
oflags |= O_TEXT;
break;
#endif
}
}
fd = hxc_open (filename, rwflags|oflags, 0666);
if(fd==-1)
return NULL;
if(append)
{
if(lseek(fd,0,SEEK_END) == -1)
{
close (fd);
return NULL;
}
}
else
{
lseek(fd,0,SEEK_SET);
}
stream = fdopen(fd,mode);
if(stream == NULL)
close(fd);
}
return stream;
}
int hxc_fclose(FILE * f)
{
return fclose(f);
}
int hxc_statex( const char *filename, struct stat *buf)
{
#if defined (WIN32)
wchar_t wpath[MAX_PATH+1];
if( convertpath(filename, wpath) < 0 )
return -1;
return _wstat (wpath,(struct _stat *)buf);
#else
int res;
const char *local_name;
local_name = filename;//ToLocale( filename );
if( local_name != NULL )
{
res = lstat( local_name, buf );
// LocaleFree( local_name );
return res;
}
errno = ENOENT;
return -1;
#endif
}
int hxc_stat( const char *filename, struct stat *buf)
{
return hxc_statex(filename,buf);
}
long find_first_file(char *folder, char *file, filefoundinfo* fileinfo)
{
#if defined (WIN32)
HANDLE hfindfile;
char *folderstr;
WIN32_FIND_DATAW FindFileData;
wchar_t wpath[MAX_PATH+1];
if(file)
{
folderstr=(char *) malloc(strlen(folder)+strlen(file)+2);
sprintf((char *)folderstr,"%s\\%s",folder,file);
}
else
{
folderstr = (char *) malloc(strlen(folder)+1);
sprintf((char *)folderstr,"%s",folder);
}
convertpath (folderstr, wpath);
hfindfile = FindFirstFileW(wpath, &FindFileData);
if(hfindfile!=INVALID_HANDLE_VALUE)
{
WideCharToMultiByte(CP_UTF8,0,FindFileData.cFileName,-1,fileinfo->filename,sizeof(fileinfo->filename),NULL,NULL);
//sprintf(fileinfo->filename,"%s",FindFileData.cFileName);
fileinfo->isdirectory = 0;
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
fileinfo->isdirectory = 1;
}
fileinfo->size = FindFileData.nFileSizeLow;
free(folderstr);
return (long)hfindfile;
}
else
{
free(folderstr);
return -1;
}
#endif
return 0;
}
long find_next_file(long handleff, char *folder, char *file, filefoundinfo* fileinfo)
{
long ret;
#if defined (WIN32)
WIN32_FIND_DATAW FindFileData;
ret=FindNextFileW((HANDLE)handleff,&FindFileData);
if(ret)
{
WideCharToMultiByte(CP_UTF8,0,FindFileData.cFileName,-1,fileinfo->filename,sizeof(fileinfo->filename),NULL,NULL);
//sprintf(fileinfo->filename,"%s",FindFileData.cFileName);
fileinfo->isdirectory=0;
if( FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
fileinfo->isdirectory = 1;
}
fileinfo->size = FindFileData.nFileSizeLow;
}
#else
ret = 0;
#endif
return ret;
}
long find_close(long handle)
{
#if defined (WIN32)
FindClose((void*)handle);
#endif
return 0;
}
int getlistoffile(unsigned char * directorypath,unsigned char *** filelist)
{
int numberoffile;
char ** filepathtab;
#if defined (WIN32)
HANDLE findfilehandle;
WIN32_FIND_DATA FindFileData;
filepathtab = 0;
numberoffile = 0;
findfilehandle=FindFirstFile(directorypath,&FindFileData);
if(findfilehandle!=INVALID_HANDLE_VALUE)
{
do
{
filepathtab = (char **) realloc(filepathtab,sizeof(char*)*(numberoffile+1));
filepathtab[numberoffile] = (char*)malloc(strlen(FindFileData.cFileName)+1);
strcpy(filepathtab[numberoffile],FindFileData.cFileName);
numberoffile++;
}while(FindNextFile(findfilehandle,&FindFileData));
FindClose(findfilehandle);
}
*filelist=filepathtab;
#else
numberoffile = 0;
#endif
return numberoffile;
}
char * getcurrentdirectory(char *currentdirectory,int buffersize)
{
memset(currentdirectory,0,buffersize);
#if defined (WIN32)
if(GetModuleFileName(GetModuleHandle(NULL),currentdirectory,buffersize))
{
if(strrchr(currentdirectory,'\\'))
{
*((char*)strrchr(currentdirectory,'\\')) = 0;
return currentdirectory;
}
}
#endif
return 0;
}