From 15fdeb2be03ae9f38f20068c1b3bb786d4c426de Mon Sep 17 00:00:00 2001 From: Viktor Szakats Date: Fri, 30 Mar 2012 17:06:43 +0000 Subject: [PATCH] 2012-03-30 19:06 UTC+0200 Viktor Szakats (harbour syenar.net) * contrib/xhb/filestat.c + switched from HB_TCHAR_* macros to Str API --- harbour/ChangeLog | 4 + harbour/contrib/xhb/filestat.c | 225 ++++++++++++++++----------------- 2 files changed, 112 insertions(+), 117 deletions(-) diff --git a/harbour/ChangeLog b/harbour/ChangeLog index 84fc4abef5..137710cc6a 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -16,6 +16,10 @@ The license applies to all entries newer than 2009-04-28. */ +2012-03-30 19:06 UTC+0200 Viktor Szakats (harbour syenar.net) + * contrib/xhb/filestat.c + + switched from HB_TCHAR_* macros to Str API + 2012-03-30 18:27 UTC+0200 Viktor Szakats (harbour syenar.net) * contrib/hbct/ctnet.c + switched from HB_TCHAR_* macros to Str API diff --git a/harbour/contrib/xhb/filestat.c b/harbour/contrib/xhb/filestat.c index 49785a185b..08c4a3f10a 100644 --- a/harbour/contrib/xhb/filestat.c +++ b/harbour/contrib/xhb/filestat.c @@ -67,120 +67,128 @@ #if ! defined( INVALID_FILE_ATTRIBUTES ) #define INVALID_FILE_ATTRIBUTES ((DWORD)(-1)) #endif + #include "hbwinuni.h" #endif -static HB_BOOL hb_fsFileStats( - const char * pszFileName, - char * szAttr, - HB_FOFFSET * llSize, - long * lcDate, - long * lcTime, - long * lmDate, - long * lmTime ) +HB_FUNC( FILESTATS ) { HB_BOOL fResult = HB_FALSE; + char szAttr[ 21 ]; + HB_FOFFSET llSize = 0; + long lcDate = 0, lcTime = 0, lmDate = 0, lmTime = 0; + + /* Parameter checking */ + if( hb_parclen( 1 ) == 0 ) + { + hb_errRT_BASE_SubstR( EG_ARG, 3012, NULL, HB_ERR_FUNCNAME, 1, + hb_paramError(1) ); + return; + } #if defined( HB_OS_UNIX ) - - struct stat statbuf; - - if( stat( pszFileName, &statbuf ) == 0 ) { - // determine if we can read/write/execute the file - HB_FATTR usAttr, ushbAttr = 0; - time_t ftime; + struct stat statbuf; + + if( stat( hb_parc( 1 ), &statbuf ) == 0 ) + { + // determine if we can read/write/execute the file + HB_FATTR usAttr, ushbAttr = 0; + time_t ftime; #if _POSIX_C_SOURCE >= 199506L - struct tm tms; + struct tm tms; #endif - struct tm *ptms; + struct tm *ptms; + char * pszAttr = szAttr; - /* See which attribs are applicable */ - if( statbuf.st_uid == geteuid() ) - { - usAttr = - ((statbuf.st_mode & S_IRUSR ) ? 1 << 2 : 0) | - ((statbuf.st_mode & S_IWUSR ) ? 1 << 1 : 0) | - ((statbuf.st_mode & S_IXUSR ) ? 1 : 0); - } - else if( statbuf.st_gid == getegid() ) - { - usAttr = - ((statbuf.st_mode & S_IRGRP ) ? 1 << 2 : 0) | - ((statbuf.st_mode & S_IWGRP ) ? 1 << 1 : 0) | - ((statbuf.st_mode & S_IXGRP ) ? 1 : 0); - } - else - { - usAttr = - ((statbuf.st_mode & S_IROTH ) ? 1 << 2 : 0) | - ((statbuf.st_mode & S_IWOTH ) ? 1 << 1 : 0) | - ((statbuf.st_mode & S_IXOTH ) ? 1 : 0); - } + /* See which attribs are applicable */ + if( statbuf.st_uid == geteuid() ) + { + usAttr = + ((statbuf.st_mode & S_IRUSR ) ? 1 << 2 : 0) | + ((statbuf.st_mode & S_IWUSR ) ? 1 << 1 : 0) | + ((statbuf.st_mode & S_IXUSR ) ? 1 : 0); + } + else if( statbuf.st_gid == getegid() ) + { + usAttr = + ((statbuf.st_mode & S_IRGRP ) ? 1 << 2 : 0) | + ((statbuf.st_mode & S_IWGRP ) ? 1 << 1 : 0) | + ((statbuf.st_mode & S_IXGRP ) ? 1 : 0); + } + else + { + usAttr = + ((statbuf.st_mode & S_IROTH ) ? 1 << 2 : 0) | + ((statbuf.st_mode & S_IWOTH ) ? 1 << 1 : 0) | + ((statbuf.st_mode & S_IXOTH ) ? 1 : 0); + } - /* Standard characters */ - if( (usAttr & 4) == 0 ) /* Hidden (can't read)*/ - ushbAttr |= HB_FA_HIDDEN; + /* Standard characters */ + if( (usAttr & 4) == 0 ) /* Hidden (can't read)*/ + ushbAttr |= HB_FA_HIDDEN; - if( (usAttr & 2) == 0 ) /* read only (can't write)*/ - ushbAttr |= HB_FA_READONLY; + if( (usAttr & 2) == 0 ) /* read only (can't write)*/ + ushbAttr |= HB_FA_READONLY; - if( (usAttr & 1) == 1 ) /* executable? (xbit)*/ - ushbAttr |= HB_FA_SYSTEM; + if( (usAttr & 1) == 1 ) /* executable? (xbit)*/ + ushbAttr |= HB_FA_SYSTEM; - /* Extension characters */ + /* Extension characters */ - if( ( statbuf.st_mode & S_IFLNK ) == S_IFLNK) - *szAttr++ = 'Z'; /* Xharbour extension */ + if( ( statbuf.st_mode & S_IFLNK ) == S_IFLNK) + *pszAttr++ = 'Z'; /* Xharbour extension */ - if( ( statbuf.st_mode & S_IFSOCK ) == S_IFSOCK ) - *szAttr++ = 'K'; /* Xharbour extension */ + if( ( statbuf.st_mode & S_IFSOCK ) == S_IFSOCK ) + *pszAttr++ = 'K'; /* Xharbour extension */ - /* device */ - if( ( statbuf.st_mode & S_IFBLK ) == S_IFBLK || - ( statbuf.st_mode & S_IFCHR ) == S_IFCHR ) - ushbAttr |= HB_FA_DEVICE; /* Xharbour extension */ + /* device */ + if( ( statbuf.st_mode & S_IFBLK ) == S_IFBLK || + ( statbuf.st_mode & S_IFCHR ) == S_IFCHR ) + ushbAttr |= HB_FA_DEVICE; /* Xharbour extension */ - if( ( statbuf.st_mode & S_IFIFO ) == S_IFIFO ) - *szAttr++ = 'Y'; /* Xharbour extension */ + if( ( statbuf.st_mode & S_IFIFO ) == S_IFIFO ) + *pszAttr++ = 'Y'; /* Xharbour extension */ - if( S_ISDIR( statbuf.st_mode ) ) - ushbAttr |= HB_FA_DIRECTORY; /* Xharbour extension */ - /* Give the ARCHIVE if readwrite, not executable and not special */ - else if( S_ISREG( statbuf.st_mode ) && ushbAttr == 0 ) - ushbAttr |= HB_FA_ARCHIVE; + if( S_ISDIR( statbuf.st_mode ) ) + ushbAttr |= HB_FA_DIRECTORY; /* Xharbour extension */ + /* Give the ARCHIVE if readwrite, not executable and not special */ + else if( S_ISREG( statbuf.st_mode ) && ushbAttr == 0 ) + ushbAttr |= HB_FA_ARCHIVE; - *llSize = ( HB_FOFFSET ) statbuf.st_size; + llSize = ( HB_FOFFSET ) statbuf.st_size; - ftime = statbuf.st_mtime; + ftime = statbuf.st_mtime; #if defined( HB_HAS_LOCALTIME_R ) - ptms = localtime_r( &ftime, &tms ); + ptms = localtime_r( &ftime, &tms ); #else - ptms = localtime( &ftime ); + ptms = localtime( &ftime ); #endif - *lcDate = hb_dateEncode( ptms->tm_year + 1900, - ptms->tm_mon + 1, ptms->tm_mday ); - *lcTime = ptms->tm_hour*3600 + ptms->tm_min * 60 + ptms->tm_sec; + lcDate = hb_dateEncode( ptms->tm_year + 1900, + ptms->tm_mon + 1, ptms->tm_mday ); + lcTime = ptms->tm_hour*3600 + ptms->tm_min * 60 + ptms->tm_sec; - ftime = statbuf.st_atime; + ftime = statbuf.st_atime; #if defined( HB_HAS_LOCALTIME_R ) - ptms = localtime_r( &ftime, &tms ); + ptms = localtime_r( &ftime, &tms ); #else - ptms = localtime( &ftime ); + ptms = localtime( &ftime ); #endif - *lmDate = hb_dateEncode( ptms->tm_year + 1900, - ptms->tm_mon + 1, ptms->tm_mday ); - *lmTime = ptms->tm_hour*3600 + ptms->tm_min * 60 + ptms->tm_sec; + lmDate = hb_dateEncode( ptms->tm_year + 1900, + ptms->tm_mon + 1, ptms->tm_mday ); + lmTime = ptms->tm_hour*3600 + ptms->tm_min * 60 + ptms->tm_sec; - hb_fsAttrDecode( ushbAttr, szAttr ); + hb_fsAttrDecode( ushbAttr, szAttr ); - fResult = HB_TRUE; + fResult = HB_TRUE; + } } #elif defined( HB_OS_WIN ) { - LPTSTR lpFileName = HB_TCHAR_CONVTO( pszFileName ); + void * hFileName; + LPCTSTR lpFileName = HB_PARSTR( 1, &hFileName, NULL ); DWORD dwAttribs; WIN32_FIND_DATA ffind; HANDLE hFind; @@ -200,56 +208,56 @@ static HB_BOOL hb_fsFileStats( CloseHandle( hFind ); /* get file times and work them out */ - *llSize = ( HB_FOFFSET ) ffind.nFileSizeLow + ( ( HB_FOFFSET ) ffind.nFileSizeHigh << 32 ); + llSize = ( HB_FOFFSET ) ffind.nFileSizeLow + ( ( HB_FOFFSET ) ffind.nFileSizeHigh << 32 ); if( FileTimeToLocalFileTime( &ffind.ftCreationTime, &filetime ) && FileTimeToSystemTime( &filetime, &time ) ) { - *lcDate = hb_dateEncode( time.wYear, time.wMonth, time.wDay ); - *lcTime = time.wHour * 3600 + time.wMinute * 60 + time.wSecond; + lcDate = hb_dateEncode( time.wYear, time.wMonth, time.wDay ); + lcTime = time.wHour * 3600 + time.wMinute * 60 + time.wSecond; } else { - *lcDate = hb_dateEncode( 0, 0, 0 ); - *lcTime = 0; + lcDate = hb_dateEncode( 0, 0, 0 ); + lcTime = 0; } if( FileTimeToLocalFileTime( &ffind.ftLastAccessTime, &filetime ) && FileTimeToSystemTime( &filetime, &time ) ) { - *lmDate = hb_dateEncode( time.wYear, time.wMonth, time.wDay ); - *lmTime = time.wHour * 3600 + time.wMinute * 60 + time.wSecond; + lmDate = hb_dateEncode( time.wYear, time.wMonth, time.wDay ); + lmTime = time.wHour * 3600 + time.wMinute * 60 + time.wSecond; } else { - *lcDate = hb_dateEncode( 0, 0, 0 ); - *lcTime = 0; + lcDate = hb_dateEncode( 0, 0, 0 ); + lcTime = 0; } fResult = HB_TRUE; } } - HB_TCHAR_FREE( lpFileName ); + hb_strfree( hFileName ); } #else /* Generic algorithm based on findfirst */ { - PHB_FFIND findinfo = hb_fsFindFirst( pszFileName, HB_FA_ALL ); + PHB_FFIND findinfo = hb_fsFindFirst( hb_parc( 1 ), HB_FA_ALL ); if( findinfo ) { hb_fsAttrDecode( findinfo->attr, szAttr ); - *llSize = ( HB_FOFFSET ) findinfo->size; - *lcDate = findinfo->lDate; - *lcTime = (findinfo->szTime[0] - '0') * 36000 + - (findinfo->szTime[1] - '0') * 3600 + - (findinfo->szTime[3] - '0') * 600 + - (findinfo->szTime[4] - '0') * 60 + - (findinfo->szTime[6] - '0') * 10 + - (findinfo->szTime[7] - '0'); - *lmDate = hb_dateEncode( 0, 0, 0 ); - *lmTime = 0; + llSize = ( HB_FOFFSET ) findinfo->size; + lcDate = findinfo->lDate; + lcTime = (findinfo->szTime[0] - '0') * 36000 + + (findinfo->szTime[1] - '0') * 3600 + + (findinfo->szTime[3] - '0') * 600 + + (findinfo->szTime[4] - '0') * 60 + + (findinfo->szTime[6] - '0') * 10 + + (findinfo->szTime[7] - '0'); + lmDate = hb_dateEncode( 0, 0, 0 ); + lmTime = 0; hb_fsFindClose( findinfo ); fResult = HB_TRUE; } @@ -258,25 +266,8 @@ static HB_BOOL hb_fsFileStats( #endif hb_fsSetIOError( fResult, 0 ); - return fResult; -} -HB_FUNC( FILESTATS ) -{ - char szAttr[ 21 ]; - const char * szFileName = hb_parc( 1 ); - HB_FOFFSET llSize = 0; - long lcDate = 0, lcTime = 0, lmDate = 0, lmTime = 0; - - /* Parameter checking */ - if( !szFileName || !*szFileName ) - { - hb_errRT_BASE_SubstR( EG_ARG, 3012, NULL, HB_ERR_FUNCNAME, 1, - hb_paramError(1) ); - return; - } - - if( hb_fsFileStats( szFileName, szAttr, &llSize, &lcDate, &lcTime, &lmDate, &lmTime ) ) + if( fResult ) { hb_storc ( szAttr, 2 ); hb_stornint( llSize, 3 );