You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
195 lines
5.1 KiB
195 lines
5.1 KiB
/********************************************************************
|
|
* Description:
|
|
* Author: Evgeny Gagua <poocka@gmail.com>
|
|
* Created at: Mon Oct 11 20:42:02 IST 2010
|
|
* Computer: uranus
|
|
* System: Linux 2.6.24.5-smp on i686
|
|
*
|
|
* Copyright (c) 2010 Evgeny Gagua All rights reserved.
|
|
*
|
|
********************************************************************/
|
|
#include "core.h"
|
|
/*
|
|
Core of server
|
|
*/
|
|
|
|
int fileExist(const char *strFilename) {
|
|
struct stat stFileInfo;
|
|
int blnReturn;
|
|
int intStat;
|
|
|
|
intStat = stat( strFilename ,&stFileInfo);
|
|
if(intStat == 0) {
|
|
blnReturn = 1;
|
|
} else {
|
|
blnReturn = 0;
|
|
};
|
|
return(blnReturn);
|
|
};
|
|
|
|
CSTR trim( CSTR string ){
|
|
int i=0;
|
|
int j;
|
|
if ( strlen( string ) ){
|
|
while( ((unsigned char)string[i])< 0x21 ) i++;
|
|
j = strlen( string );
|
|
while( ((unsigned char)string[j])< 0x21 ) j--;
|
|
strcpy( string, string+i );
|
|
memset(string+(j-i+1),0, strlen(string)-(j-i)-1);
|
|
};
|
|
return string;
|
|
};
|
|
|
|
CArray *explode( char Delim, CSTR S_Str ){
|
|
CArray *RetVal;
|
|
CSTR b_ptr = S_Str;
|
|
U16 s_ptr = 0;
|
|
if( S_Str ){
|
|
RetVal = new CArray;
|
|
while( ( U8 ) S_Str[s_ptr] ){
|
|
if( (( U8 ) Delim ) == (( U8 )S_Str[s_ptr]) ){
|
|
S_Str[s_ptr] = ( char ) 0;
|
|
RetVal->push_back( b_ptr );
|
|
s_ptr++;
|
|
b_ptr = &S_Str[s_ptr];
|
|
} else {
|
|
s_ptr++;
|
|
};
|
|
};
|
|
RetVal->push_back( b_ptr );
|
|
};
|
|
return RetVal;
|
|
};
|
|
|
|
CSTR implode( CArray *Data, CSTR S_Str, U8 Start, U8 End ){
|
|
CSTR RetVal;
|
|
U8 RStart = Start;
|
|
U8 REnd = End;
|
|
if( Start > Data->size() ) return NULL;
|
|
if( End < Start ) return NULL;
|
|
if( End > Data->size() ) REnd = Data->size()-1;
|
|
if( strlen( (*Data).at( RStart )) ){
|
|
RetVal = (CSTR) malloc( strlen( (*Data).at( RStart )) + 1 );
|
|
sprintf( RetVal,"%s",(*Data).at( RStart ));
|
|
} else {
|
|
RetVal = (CSTR) malloc( 1 );
|
|
bzero( RetVal, 1 );
|
|
};
|
|
for( int i = RStart+1; i < REnd; i++){
|
|
RetVal = (CSTR) realloc ( RetVal, strlen( RetVal ) + strlen( (*Data).at( i )) + 1 + strlen( S_Str ) );
|
|
sprintf( RetVal, "%s%s%s", RetVal, S_Str, (*Data).at( i ) );
|
|
};
|
|
return RetVal;
|
|
};
|
|
|
|
int getIP4( if_info **arrParam, int max_ifs ){
|
|
int index = 0;
|
|
struct ifaddrs *addrs, *tmp;
|
|
getifaddrs(&addrs);
|
|
tmp = addrs;
|
|
//arrParam= malloc( 10 * sezeof(if_info) );
|
|
while (tmp){
|
|
if (tmp->ifa_addr && tmp->ifa_addr->sa_family == AF_INET){
|
|
if( strncmp( tmp->ifa_name, "lo", 2 ) != 0 ){
|
|
(*arrParam)[index].if_name = (CSTR)malloc(strlen(tmp->ifa_name)+1);
|
|
snprintf( (*arrParam)[index].if_name, strlen(tmp->ifa_name)+1,"%s", tmp->ifa_name );
|
|
(*arrParam)[index].if_addr = (CSTR)malloc(16);
|
|
struct sockaddr_in *pAddr = (struct sockaddr_in *)tmp->ifa_addr;
|
|
snprintf( (*arrParam)[index].if_addr, 16, "%s", inet_ntoa(pAddr->sin_addr) );
|
|
index++;
|
|
if( index == max_ifs ) break;
|
|
}
|
|
};
|
|
tmp = tmp->ifa_next;
|
|
};
|
|
freeifaddrs(addrs);
|
|
return index;
|
|
};
|
|
long long getFreeSpace( CSTR DirName ){
|
|
struct statvfs fiData;
|
|
if( ( statvfs( DirName, &fiData ) ) < 0 ) {
|
|
return -1;
|
|
};
|
|
return fiData.f_bsize * fiData.f_bfree;
|
|
};
|
|
long long getTotalSpace( CSTR DirName ){
|
|
struct statvfs fiData;
|
|
if( ( statvfs( DirName, &fiData ) ) < 0 ) {
|
|
return -1;
|
|
};
|
|
return fiData.f_bsize * fiData.f_blocks;
|
|
};
|
|
|
|
CSTR getTStr( CSTR Fmt ){
|
|
char DateStr[200];
|
|
CSTR RetVal;
|
|
time_t t;
|
|
struct tm *tmp;
|
|
|
|
t = time(NULL);
|
|
tmp = gmtime(&t);
|
|
if( tmp == NULL) {
|
|
perror("localtime");
|
|
return NULL;
|
|
};
|
|
if( Fmt == NULL ) {
|
|
if( strftime( DateStr, sizeof(DateStr), "%F %T GMT", tmp )==0 ){
|
|
fprintf( stderr, "strftime returned 0" );
|
|
return NULL;
|
|
};
|
|
} else {
|
|
if( strftime( DateStr, sizeof(DateStr), Fmt, tmp )==0 ){
|
|
fprintf( stderr, "strftime returned 0" );
|
|
return NULL;
|
|
};
|
|
}
|
|
RetVal =(CSTR) malloc( strlen(DateStr) +1 );
|
|
if( RetVal != NULL ) strcpy( RetVal, DateStr );
|
|
return RetVal;
|
|
};
|
|
|
|
/*long long getMilliseconds( void ){
|
|
struct timeval tData;
|
|
struct timezone tTZ;
|
|
long long tMilliseconds;
|
|
tMilliseconds = 0;
|
|
tTZ.tz_minuteswest = 0;
|
|
tTZ.tz_dsttime = 0;
|
|
if( gettimeofday( &tData, &tTZ ) == -1 ){
|
|
} else {
|
|
tMilliseconds = tData.tv_sec;
|
|
tMilliseconds = tMilliseconds *1000;
|
|
tMilliseconds += tData.tv_usec / 1000;
|
|
};
|
|
return tMilliseconds;
|
|
};*/
|
|
|
|
bool enumerateDevices( enumeratorInfo *mlist, const char *dTemplate ){
|
|
DIR *d;
|
|
struct dirent *dir;
|
|
char *tBuffer;
|
|
d = opendir("/dev");
|
|
if( d ){
|
|
while((dir = readdir(d)) != NULL){
|
|
if( dir->d_type == DT_CHR || dir->d_type == DT_BLK ){
|
|
if( strncmp( dir->d_name, dTemplate, 5 ) == 0 ) {
|
|
tBuffer = ( char * ) malloc( strlen(dir->d_name)+1 );
|
|
strncpy( tBuffer, dir->d_name, strlen(dir->d_name)+1 );
|
|
mlist->push_back( tBuffer );
|
|
//printf("%s\n", dir->d_name );
|
|
};
|
|
//if( strncmp( dir->d_name, "ttyS", 4 ) == 0 ){
|
|
// tBuffer = ( char * ) malloc( strlen(dir->d_name)+1 );
|
|
// strncpy( tBuffer, dir->d_name, strlen(dir->d_name)+1 );
|
|
// mlist->pushBack( tBuffer );
|
|
// //printf("%s\n", dir->d_name );
|
|
//};
|
|
};
|
|
};
|
|
closedir(d);
|
|
} else {
|
|
return false;
|
|
};
|
|
return true;
|
|
};
|
|
|