Code:
/*---------------------------------------------------------
Program : eBook Catalog Maker
Author : Hans alias Kanshu / Hanabi
Description : Creates proxy / empty files for ebooks of type PDF, CHM, DJVU, RAR, ZIP
Date : 05/09/2009 2102H
Notes : Solutions provided by programmers from comp.lang.c and
cprogramming.com
*/
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
FILE *thisCard; // catalog card; proxy file
//---------------------------------------------------------
int main( int argc , char *argv[] ) {
//-------------------------------------------------------
struct dirent *thisFile;
char CatalogCard[768];
char FileType[16];
char Title[512];
char ISBN[16];
char TypePDF[] = "pdf";
char TypeCHM[] = "chm";
char TypeDJVU[] = "djvu";
char TypeZIP[] = "zip";
char TypeRAR[] = "rar";
int Matched;
int i;
DIR *thisFolder;
thisFolder = opendir( "." );
if ( thisFolder ) {
while ( ( thisFile = readdir( thisFolder ) ) != NULL ) {
// typical filename + extension
Matched = sscanf( thisFile->d_name , "%511[^.].%15s" , Title , FileType );
if ( Matched == 2 ) {
// recognize only PDF, CHM, DJVU, ZIP, RAR
for( i = 0 ; ( FileType[i] = tolower( FileType[i] ) ) ; i++ );
if ( ( strstr( FileType , TypePDF ) != NULL ) ||
( strstr( FileType , TypeCHM ) != NULL ) ||
( strstr( FileType , TypeDJVU ) != NULL ) ||
( strstr( FileType , TypeZIP ) != NULL ) ||
( strstr( FileType , TypeRAR ) != NULL ) ) {
// create 1st catalog card; ISBN card
//-----------------------------------------------------------------
if ( strstr( FileType , TypePDF ) != NULL )
snprintf( CatalogCard , sizeof CatalogCard ,
"%s.%s.txt" , Title , FileType );
// NOTE: MQH filetype is the MetaQuotes Language file
// that has a yellow icon in the file explorer
if ( ( strstr( FileType , TypeCHM ) != NULL ) ||
( strstr( FileType , TypeDJVU ) != NULL ) )
snprintf( CatalogCard , sizeof CatalogCard ,
"%s.%s.mqh" , Title , FileType );
if ( ( strstr( FileType , TypeRAR ) != NULL ) ||
( strstr( FileType , TypeZIP ) != NULL ) )
snprintf( CatalogCard , sizeof CatalogCard ,
"%s.%s.zip" , Title , FileType );
if ( ( thisCard = fopen( CatalogCard , "w+" ) ) == NULL )
printf( "Can't create file.\n" );
else
fclose( thisCard );
// create 2nd catalog card; Title + ISBN card
//-----------------------------------------------------------------
Matched = sscanf( thisFile->d_name , "%15s - %511[^.].%15s" ,
ISBN , Title , FileType );
if ( Matched == 3 ) {
for( i = 0 ; ( FileType[i] = tolower( FileType[i] ) ) ; i++ );
if ( strstr( FileType , TypePDF ) != NULL )
snprintf( CatalogCard , sizeof CatalogCard ,
"%s - %s.%s.txt" , Title , ISBN , FileType );
if ( ( strstr( FileType , TypeCHM ) != NULL ) ||
( strstr( FileType , TypeDJVU ) != NULL ) )
snprintf( CatalogCard , sizeof CatalogCard ,
"%s - %s.%s.mqh" , Title , ISBN , FileType );
if ( ( strstr( FileType , TypeRAR ) != NULL ) ||
( strstr( FileType , TypeZIP ) != NULL ) )
snprintf( CatalogCard , sizeof CatalogCard ,
"%s - %s.%s.zip" , Title , ISBN , FileType );
if ( ( thisCard = fopen( CatalogCard , "w+" ) ) == NULL )
printf( "Can't create file.\n" );
else
fclose( thisCard );
}
}
}
}
closedir( thisFolder );
}
system( "PAUSE" );
return 0;
}