+
| | supio.cpp
|
supio.cpp --- supio.cpp Linux
+++ supio.cpp Windows
@@ -7,19 +7,40 @@
#ifdef USE_WINDOWS_API
-#include <windows.h>
+#include <windows.h>
+#include <direct.h> // Windows ディレクトリ関係
+#else
+// gettime
+#include <sys/time.h>
+#include <time.h>
+// mkdir stat
+#include <sys/stat.h>
+#include <sys/types.h>
+// changedir delfile get_current_dir_name stat
+#include <unistd.h>
+// dirlist
+#include <dirent.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
-#include <direct.h>
#include <ctype.h>
#include "supio.h"
#include "dpmread.h"
#include "strbuf.h"
+
+#define Linux
+#ifdef Linux
+enum {
+ _MAX_PATH = 256,
+ _MAX_DIR = 256,
+ _MAX_EXT = 256,
+ _MAX_FNAME = 256
+};
+#endif
//
// basic C I/O support
@@ -157,6 +178,31 @@
return res;
}
+void _splitpath2( char *path, char *dir, char *fname, char *ext )
+{
+ // Linux用ファイルパス切り出し
+ //
+ char *p, pathtmp[256];
+ int len;
+
+ strcpy( pathtmp, path );
+
+ p = strrchr( pathtmp, '.' );
+ if (p != NULL) {
+ len = p - pathtmp;
+ sprintf( ext, "%s", p+1 );
+ memset( pathtmp, 0, strlen(pathtmp));
+ strncpy( pathtmp, path, len );
+ }
+ p = strrchr( pathtmp, '/' );
+ if (p != NULL) {
+ len = p - pathtmp;
+ sprintf( fname, "%s", p+1 );
+ memset( pathtmp, 0, strlen(pathtmp));
+ strncpy( pathtmp, path, len );
+ }
+ sprintf( dir, "%s/", pathtmp );
+}
void getpath( char *stmp, char *outbuf, int p2 )
{
@@ -167,8 +213,12 @@
char p_ext[_MAX_EXT];
p = outbuf;
- if (p2&16) strcase( stmp );
- _splitpath( stmp, p_drive, p_dir, p_fname, p_ext );
+ if (p2&16) strcase( stmp );
+#ifdef Linux
+ _splitpath2( stmp, p_dir, p_fname, p_ext );
+#else
+ _splitpath( stmp, p_drive, p_dir, p_fname, p_ext );
+#endif
strcat( p_drive, p_dir );
if ( p2&8 ) {
strcpy( stmp, p_fname ); strcat( stmp, p_ext );
@@ -194,8 +244,9 @@
{
#ifdef USE_WINDOWS_API
return _mkdir( name );
-#else
- return 0;
+#else
+ //return 0;
+ return mkdir( name, 0777 );
#endif
}
@@ -205,7 +256,8 @@
#ifdef USE_WINDOWS_API
return _chdir( name );
#else
- return 0;
+ //return 0;
+ return chdir( name );
#endif
}
@@ -215,7 +267,9 @@
#ifdef USE_WINDOWS_API
return DeleteFile( name );
#else
- return 0;
+ //return 0;
+ return unlink( name );
+ //return remove( name ); // ディレクトリにもファイルにも対応
#endif
}
@@ -260,11 +314,93 @@
}
FindClose(sh);
return stat_main;
-#else
- return 0;
+#else
+ // Linux System
+ //
+ enum { MASK = 3 }; // mode 3までのビット反転用
+ char *p;
+ unsigned int fl;
+ unsigned int stat_main;
+ unsigned int fmask;
+ DIR *sh;
+ struct dirent *fd;
+ struct stat st;
+
+ stat_main=0;
+
+ sh = opendir( get_current_dir_name() );
+ fd = readdir( sh );
+ while( fd != NULL ) {
+ p = fd->d_name; fl = 1;
+ if ( *p==0 ) fl=0; // 空行を除外
+ if ( *p=='.') { // '.','..'を除外
+ if ( p[1]==0 ) fl=0;
+ if ((p[1]=='.')&&(p[2]==0)) fl=0;
+ }
+ // 表示/非表示のマスク
+ // Linux用なのでシステム属性は考慮しない
+ if (p3!=0 && fl==1) {
+ stat( p, &st );
+ fmask=0;
+ if (p3&4) { // 条件反転
+ if (S_ISREG( st.st_mode )&&( *p!='.' )) {
+ fl=0;
+ } else {
+ fmask=MASK;
+ }
+ }
+ if ( fl==1 ) {
+ if ((p3^fmask)&1 && S_ISDIR( st.st_mode )) fl=0; //ディレクトリ
+ if ((p3^fmask)&2 && ( *p=='.' )) fl=0; //隠しファイル
+ }
+ }
+ // ワイルドカード処理
+ //
+ if (fl) {
+ fl=wildcard( p, fname );
+ }
+
+ if (fl) {
+ stat_main++;
+ sbStrAdd( target, p );
+ sbStrAdd( target, "\n" );
+ }
+ fd = readdir( sh );
+ }
+ closedir( sh );
+ //return 0;
+ return stat_main;
#endif
-}
-
+}
+
+int wildcard( char *text, char *wc )
+{
+ // textに対してワイルドカード処理を適応
+ // return value: yes 1, no 0
+ //
+ if ( wc[0]=='\0' && *text=='\0' ) {
+ return 1;
+ }
+ if ( wc[0]=='*' ) {
+ if ( *text=='\0' && wc[1]=='\0' ) {
+ return 1;
+ } else if ( *text=='\0' ) {
+ return 0;
+ }
+ if ( wc[1]==*text | wc[1]=='*' ) {
+ if (wildcard( text, wc+1 )) {
+ return 1;
+ }
+ }
+ if ( *text!='\0' ) {
+ return wildcard( text+1, wc );
+ }
+ }
+ if ( (*text!='\0')&&(wc[0]==*text) ) {
+ return wildcard( text+1, wc+1 );
+ }
+ return 0;
+}
int gettime( int index )
{
@@ -286,8 +422,36 @@
GetLocalTime( &st );
a=(short *)&st;
return (int)(a[index]);
-#else
- return 0;
+#else
+ struct timeval tv;
+ struct tm *lt;
+
+ gettimeofday( &tv, NULL );
+ lt = localtime( &tv.tv_sec );
+
+ switch( index ) {
+ case 0:
+ return lt->tm_year+1900;
+ case 1:
+ return lt->tm_mon+1;
+ case 2:
+ return lt->tm_wday;
+ case 3:
+ return lt->tm_mday;
+ case 4:
+ return lt->tm_hour;
+ case 5:
+ return lt->tm_min;
+ case 6:
+ return lt->tm_sec;
+ case 7:
+ return (int)tv.tv_usec/10000;
+/* 一応マイクロ秒まで取れる
+ case 8:
+ return (int)tv.tv_usec%10000;
+*/
+ }
+ //return 0;
#endif
}
|