Menu

[r25]: / trunk / uploader / src / syspil.c  Maximize  Restore  History

Download this file

126 lines (115 with data), 2.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*******************************************************************
* System Platform Independent Layer
* Distributed under GPLv3 license
* Copyright (c) 2005-11 Stanley Huang <stanleyhuangyc@gmail.com>
* All rights reserved.
*******************************************************************/
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <fcntl.h>
#include "syspil.h"
#ifndef WIN32
#include <sys/types.h>
#include <sys/time.h>
#include <dirent.h>
#include <unistd.h>
#endif
char *GetTimeString()
{
static char buf[16];
time_t tm=time(NULL);
memcpy(buf,ctime(&tm)+4,15);
buf[15]=0;
return buf;
}
int IsDir(const char* pchName)
{
#ifdef WIN32
DWORD attr=GetFileAttributes(pchName);
if (attr==INVALID_FILE_ATTRIBUTES) return 0;
return (attr & FILE_ATTRIBUTE_DIRECTORY)?1:0;
#else
struct stat stDirInfo;
if (stat( pchName, &stDirInfo) < 0) return 0;
return (stDirInfo.st_mode & S_IFDIR)?1:0;
#endif //WIN32
}
int ReadDir(const char* pchDir, char* pchFileNameBuf)
{
#ifdef WIN32
static HANDLE hFind=NULL;
WIN32_FIND_DATA finddata;
if (!pchFileNameBuf) {
if (hFind) {
FindClose(hFind);
hFind=NULL;
}
return 0;
}
if (pchDir) {
char *p;
int len;
if (hFind) FindClose(hFind);
len = strlen(pchDir);
p = malloc(len + 5);
snprintf(p, len + 5, "%s\\*.*", pchDir);
hFind=FindFirstFile(p,&finddata);
free(p);
if (hFind==INVALID_HANDLE_VALUE) {
hFind=NULL;
return -1;
}
strcpy(pchFileNameBuf,finddata.cFileName);
return 0;
}
if (!hFind) return -1;
if (!FindNextFile(hFind,&finddata)) {
FindClose(hFind);
hFind=NULL;
return -1;
}
strcpy(pchFileNameBuf,finddata.cFileName);
#else
static DIR *stDirIn=NULL;
struct dirent *stFiles;
if (!pchFileNameBuf) {
if (stDirIn) {
closedir(stDirIn);
stDirIn=NULL;
}
return 0;
}
if (pchDir) {
if (!IsDir(pchDir)) return -1;
if (stDirIn) closedir(stDirIn);
stDirIn = opendir( pchDir);
}
if (!stDirIn) return -1;
stFiles = readdir(stDirIn);
if (!stFiles) {
closedir(stDirIn);
stDirIn=NULL;
return -1;
}
strcpy(pchFileNameBuf, stFiles->d_name);
#endif
return 0;
}
int IsFileExist(const char* filename)
{
struct stat stat_ret;
if (stat(filename, &stat_ret) != 0) return 0;
return (stat_ret.st_mode & S_IFREG) != 0;
}
#ifndef WIN32
unsigned int GetTickCount()
{
struct timeval ts;
gettimeofday(&ts,0);
return ts.tv_sec * 1000 + ts.tv_usec / 1000;
}
#endif
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.