/*
* |P|I|X|O| Image cataloging using html and thumb nails
* Released under GPL http://www.gnu.org/copyleft/gpl.html
*
* kishan@hackorama.com www.hackorama.com 2001 MAY
*
* Class "Files"
* Scans a given directory for image files
*
*/
#include "files.h"
Files::Files()
{
}
Files::Files(char* _dir )
{
dir = _dir;
strcpy( pixo_tag , "pixo-" );
file_count = fileCount(dir);
if( file_count > 0 ){
file_list = new char* [ file_count ];
init();
}else{
file_list = NULL;
}
}
Files::~Files()
{
}
void
Files::init()
{
int file_type = 0;
int fcount = 0;
#ifndef _WINDOWS
DIR *dirp = NULL;
struct dirent *dp = NULL;
char subdir[PATH_MAX + 1];
dirp = opendir(dir);
if( dirp != NULL ){
while ((dp = readdir(dirp)) != NULL){
strcpy( subdir , dir );
strcat( strcat(subdir,"/"), dp->d_name );
if( opendir(subdir) != NULL ){
//cout << "skipped " << dp->d_name << endl ;
} else {
file_type = ftype( dp->d_name );
if( file_type == JPG_IMAGE ){
file_list[fcount] = new char[ strlen(dp->d_name) + 1];
strcpy ( file_list[fcount], dp->d_name ) ;
fcount++;
}else{
//cout << "skipped " << dp->d_name << endl ;
}
}
}
(void)closedir(dirp);
}else{
cout << endl << "PIXO ERROR: Failed opening :"<< dir << endl ;
}
#else
WIN32_FIND_DATA fd;
HANDLE h;
char tmp[ MAX_PATH+1 ];
sprintf( tmp, "%s\\*.*", dir );
h = FindFirstFile( tmp , &fd);
if( h != INVALID_HANDLE_VALUE){
int okidoki = 1;
while ( okidoki != 0 ){
file_type = ftype( fd.cFileName );
if( file_type == JPG_IMAGE ){
file_list[fcount] = new char[ strlen(fd.cFileName) + 1];
strcpy ( file_list[fcount], fd.cFileName ) ;
fcount++;
}else{
//cout << "skipped " << fd.cFileName << endl ;
}
okidoki = FindNextFile( h, &fd );
}
FindClose( h );
} else {
cout << endl << "PIXO ERROR: Failed opening :"<< dir << endl ;
}
#endif /* ! _WINDOWS */
// sanity check - this should not ever happen normally
if ( fcount > file_count ){
cout << "WARNING:FLLE COUNT MISMATCH " << file_count << " - " << fcount << endl;
cout << "SOME IMAGES (" << fcount - file_count <<") MAY NOT BE PROCESSED" << endl;
}else if ( fcount < file_count ) {
cout << "WARNING: CORRECTED FLLE COUNT " << file_count << " - " << fcount << endl;
file_count = fcount ;
}
}
int
Files::fileCount(char* dir)
{
int count = 0;
int file_type = 0;
#ifndef _WINDOWS
DIR *dirp = NULL;
struct dirent *dp = NULL;
dirp = opendir(dir);
if( dirp != NULL ){
while ((dp = readdir(dirp)) != NULL){
if( ftype( dp->d_name ) == JPG_IMAGE )
count++;
}
(void)closedir(dirp);
}
return ( count );
#else
WIN32_FIND_DATA fd;
HANDLE h;
char tmp[ MAX_PATH+1];
sprintf( tmp, "%s\\*.*", dir );
h = FindFirstFile( tmp , &fd);
if( h != INVALID_HANDLE_VALUE){
int okidoki = 1;
while ( okidoki != 0 ){
file_type = ftype( fd.cFileName );
if( file_type == JPG_IMAGE ){
count++;
}
okidoki = FindNextFile( h, &fd );
}
FindClose( h );
}
return ( count );
#endif /* ! _WINDOWS */
}
int
Files::ftype( char* fname)
{
unsigned int i = 0 , j = 0;
int dotat = 0 ;
char ext[ PATH_MAX + 1] = { "" };
if( isPixoFile(fname) ){
return NOT_IMAGE;
}
for( i = 1 ; i < strlen(fname) ; i++ ){
if( (int)fname[i] == 46 )
dotat = i ;
}
if( dotat > 0 ){
for( i = dotat ; i < strlen(fname) ; i++ ){
ext[j] = fname[i]; j++;
}
}
if( ext != NULL && strcmp( ext, "") != 0 ){
if( strcmp( ext, ".jpg") == 0 || strcmp( ext, ".JPG") == 0 )
return JPG_IMAGE;
if( strcmp( ext, ".gif") == 0 )
return GIF_IMAGE;
}
return NOT_IMAGE;
}
bool
Files::isPixoFile(char *fname)
{
int tag_len = strlen(pixo_tag);
if( (signed int) strlen(fname) < tag_len ){
return false;
}else{
for( int i = 0 ; i < tag_len ; i++ ){
tmp_tag[i] = fname[i];
}
tmp_tag[tag_len] = '\0';
if( strcmp(tmp_tag, pixo_tag) == 0 ){
return true;
}
}
return false;
}
void
Files::cleanUp()
{
if( file_count > 0 ){
for( int i = 0 ; i < file_count ; i++ ){
delete file_list[i] ;
}
}
delete[] file_list;
}
int
Files::getCount()
{
return file_count;
}
char**
Files::getList()
{
return file_list;
}
char*
Files::getBase()
{
return dir;
}
syntax highlighted by Code2HTML, v. 0.9