home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.wwiv.com
/
ftp.wwiv.com.zip
/
ftp.wwiv.com
/
pub
/
UTILITY
/
CHUCKSUB.ZIP
/
CHUCKSUB.C
next >
Wrap
C/C++ Source or Header
|
1994-02-23
|
8KB
|
292 lines
/*
* Combine a group of files, sorted by a number extension. Designed
* mostly for combining WWIV Link subs listings into a single text file.
*
* This will make sure that the files are put together like 1,2,3,..etc.
* and not 1,10,11,2,3,4... etc...
*
* This program will also allow you to specify a header file to be placed
* at the begining of the subs listings.
*
* Created: 1/17/94 by Charles R. Grosvenor Jr.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <alloc.h>
#include <io.h>
#include <dos.h>
#include <dir.h>
void create_subs(char *filespec);
void add_name(char name[], struct name_rec *first);
void print_list(char *outputfilename, char *headerfilename);
void delete_rec(struct name_rec *first);
int string_num_cmp(char *name1, char *name2);
struct name_rec{
char name[121];
struct name_rec *next, *previous;
};
struct name_rec *subs_first;
void main(int argc, char **argv)
{
if (argc>3)
{
create_subs(argv[1]);
print_list(argv[2],argv[3]);
delete_rec(subs_first);
}
else
{
printf("Need command line arguments: subs.?? output.lst header.asc\n\n");
printf("Where subs.?? is the file spec for the subs to find\n");
printf("..... output.lst is the file to create\n");
printf("and header.asc is the name of a file to place before the subs lists\n\n");
}
}
void create_subs(char *filespec)
/* Name: Charles R. Grosvenor Jr.
* Date: September 14, 1993
* Purpose: Grabs the names of the files that fit the file spec.
* Revision: Level 1 -=- Charles R. Grosvenor Jr. 09/14/93
*/
{
struct ffblk fileinfo;
char tempfilename[81], path[81],specs[81];
int loop,loop2;
/* gotta seperate the path */
strcpy(path,filespec);
loop=strlen(filespec)-1;
loop2=0;
while ( (path[loop]!='\\') && (loop>=0) )
{
specs[loop2++]=path[loop];
path[loop--]='\0';
}
specs[loop2]='\0';
if (findfirst(filespec, &fileinfo, FA_RDONLY|FA_HIDDEN|FA_SYSTEM|FA_ARCH) ==-1)
{
printf("Unsuccessful attempt to create sub files for command line: %s.",filespec);
}
else
{
printf("%12s \r\n",fileinfo.ff_name);
strcpy(tempfilename,path);
strcat(tempfilename,fileinfo.ff_name);
add_name(tempfilename, subs_first);
while (findnext(&fileinfo)==0)
{
printf("%12s\r\n",fileinfo.ff_name);
strcpy(tempfilename,path);
strcat(tempfilename,fileinfo.ff_name);
add_name(tempfilename, subs_first);
}
}
}
void add_name(char name[], struct name_rec *first)
/* Name : add_name
* Dependicies: needs the name to add to the list, the pointer
* to the first data element in that list
* Purpose : Adds and sorts the names passed to it
* Revision : October 17, 1993 -=- Charles R. Grosvenor Jr. -=- Level 0
* Januray 17, 1994 -=- Charles R. Grosvenor Jr. -=- Level 1
* Changed to create just one alphebtical list
*/
{
struct name_rec *temp_ptr;
if (NULL==first)
{
/* allocate the memory */
if ( (first=(struct name_rec*)malloc(sizeof(struct name_rec))) == NULL)
{
printf("Out of memory\r\n");
exit(1);
}
else
{
strcpy(first->name,name);
first->next=NULL;
first->previous=NULL;
subs_first=first;
/* the structure is not initalized to a memory location, so one */
/* needs to be set up, when it is, we can't assign it to NULL, */
/* so we have to use the above case statements. */
}
}
else
{ /* sort it */
while ( (first->next!=NULL) && (string_num_cmp(first->name,name)<0) )
{
first=first->next;
}
if (string_num_cmp(first->name,name)<0) /* this item is the largest */
{
if ( (temp_ptr=(struct name_rec*)malloc(sizeof(struct name_rec))) == NULL)
{
printf("Out of memory\r\n");
exit(1);
}
else
{
temp_ptr->next=NULL;
temp_ptr->previous=first;
first->next=temp_ptr;
strcpy(temp_ptr->name,name);
}
}
else /* inserting inbetween */
{
if ( (temp_ptr=(struct name_rec*)malloc(sizeof(struct name_rec))) == NULL)
{
printf("Out of memory\r\n");
exit(1);
}
else
{
temp_ptr->next=first;
temp_ptr->previous=first->previous;
first->previous=temp_ptr;
strcpy(temp_ptr->name,name);
if (temp_ptr->previous!=NULL)
{
temp_ptr=temp_ptr->previous;
temp_ptr->next=first->previous;
}
else
{
subs_first=temp_ptr;
}
}
}
}
}
void delete_rec(struct name_rec *first)
/* Name : delete_rec
* Dependicies: pointer to the list to destroy
* Purpose : Frees memory at the end of the program's run
* Revision : October 17, 1993 -=- Charles R. Grosvenor Jr. -=- Level 0
*/
{
struct name_rec *temp_ptr;
while (first!=NULL)
{
temp_ptr=first->next;
free((struct name_rec *)first);
first=temp_ptr;
}
}
void print_list(char *outputfilename, char *headerfilename)
/* Name : print_list
* Dependicies: needs to already have the lists defined
* Purpose : writes the text file out to the disk
* Revision : October 17, 1993 -=- Charles R. Grosvenor Jr. -=- Level 0
*/
{
struct name_rec *tempsubs;
FILE *master, *current;
char templine[181];
master=fopen(outputfilename,"w");
if ((current=fopen(headerfilename, "r"))==NULL)
{
printf("No header description file found\n\n");
}
else
{
while (!feof(current))
{
fgets(templine, 180, current);
fputs(templine, master);
}
fclose(current);
}
tempsubs=subs_first;
while (tempsubs!=NULL)
{
printf("%s\r\n", tempsubs->name);
/* add to the master file */
current=fopen(tempsubs->name, "r");
while (!feof(current))
{
fgets(templine, 180, current);
fputs(templine, master);
}
fclose(current);
tempsubs=tempsubs->next;
}
fclose(master);
printf("end of list\r\n");
}
int string_num_cmp(char *name1, char *name2)
/* Name : string_num_cmp
* Dependicies: none that I remember
* Purpose : Compares two filenames, if the first part of the extension is
* equal, then it checks the last half numerically if they
* begin with numbers.
* Revision : January 17, 1994 -=- Charles R. Grosvenor Jr. -=- Level 0
*/
{
char string1[81], string2[81], temp_name1[81], temp_name2[81];
int number1, number2, return_value;
strcpy(temp_name1, name1);
strcpy(temp_name2, name2);
strcpy(string1,strtok(temp_name1,".\0"));
strcpy(string2,strtok(temp_name2,".\0"));
return_value=strcmpi(string1,string2);
if (return_value==0)
{ /* first part of extensions are equal */
strcpy(temp_name1, name1);
strcpy(temp_name2, name2);
strcpy(string1,strtok(temp_name1,"."));
strcpy(string1,strtok(NULL,"."));
strcpy(string2,strtok(temp_name2,"."));
strcpy(string2,strtok(NULL,"."));
if ( (string1[0]>='0') && (string1[0]<='9') &&
(string2[0]>='0') && (string2[0]<='9') )
{ /* both extensions begin with numbers */
number1=atoi(string1);
number2=atoi(string2);
if (number1<number2)
{
return_value=-1;
}
else
if (number1>number2)
{
return_value=1;
}
else
{
return_value=0;
}
}
else
{
return_value=strcmpi(temp_name1, temp_name2);
}
}
return(return_value);
}