Code:
/* Reads electrical circuit components from input file and outputs to another file, formatted*/
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "component.h"
#define MAXFILENAMELENGTH 256
#define MAXCOMPONENTS 1000
#define MAXLINELENGTH 80
int ProcessArgs(int argc, char *argv[], char **inFileNamePtr, char **outFileNamePtr);
void PrintUsage(void);
/* MAIN */
/* Main subroutine*/
int main(int argc, char *argv[])
{
int imode = 0;
char usrResponse;
char *inFileName = NULL;
char *outFileName = NULL;
FILE* inFile=NULL;
FILE* outFile=NULL;
ComPtr *Comsptr=NULL;
component comps[5];
/** Get the input and output file names **/
/* 1. First try the command line arguments */
if (argc > 1){
ProcessArgs(argc, argv, &inFileName, &outFileName);
}
do {
imode = 0;
/* 2. Get any missing file names from the user */
if (inFileName == NULL) {
if ( (inFileName = (char *) malloc(sizeof(char)*MAXFILENAMELENGTH)) == NULL ) {
fprintf(stderr,__FUNCTION__);
fprintf(stderr,"::Memory Allocation Error\n");
exit(EXIT_FAILURE);
}
printf("Enter the input file's name > ");
while (scanf("%255s",inFileName) < 1 );
while (getchar() != '\n');
imode++;
}
if (outFileName == NULL) {
if ( (outFileName = (char *) malloc(sizeof(char)*MAXFILENAMELENGTH)) == NULL ) {
fprintf(stderr,__FUNCTION__);
fprintf(stderr,"::Memory Allocation Error\n");
exit(EXIT_FAILURE);
}
printf("Enter the output file's name > ");
while (scanf("%255s",outFileName) < 1 );
while (getchar() != '\n');
imode++;
}
/* read in file, process and output results*/
printf("debug: now just before ptr assign\n");
*Comsptr = &comps[0];
printf("debug: now just before processin\n");
ProcessIn(inFileName, *Comsptr);
printf("debug: after processin\n");
/*ProcessOut(outFileName, Comsptr); */
printf("debug: after processout\n");
/* DEBUG- after run through ops*/
printf("%s\n%s\n", inFileName, outFileName);
/*****************/
/* Process files */
/*****************/
/* Clean up allocated memory */
free(inFileName);
inFileName = NULL;
free(outFileName);
outFileName = NULL;
if (inFile != NULL){
fclose(inFile);
}
if (outFile != NULL){
fclose(outFile);
}
if (imode > 0) {
printf("Do you want to process another input file? (y/n) ");
while ( scanf("%[^ynYN]",&usrResponse) == 1);
while ( scanf("%[ynYN]",&usrResponse) < 1 );
while ( getchar() != '\n' );
imode = (toupper(usrResponse) == 'Y');
}
} while (imode > 0);
return EXIT_SUCCESS;
}
int ProcessIn(const char *inFileName, ComPtr copr)
{
FILE* iFile=NULL;
char lheaderchar;
char tmpLine[MAXLINELENGTH];
Line2Type type=COM;
unsigned int linecount=0;
int compcount=0;
long *hcomments = NULL;
long *tmpcomments = NULL;
iFile = fopen(inFileName, "r");
/* open file for reading, read in each line 1by1, process lines via functions 1by1 */
while(feof(iFile)== 0)
{
linecount++; /* reinitialise line values*/
type=COM;
if(fgets(tmpLine, MAXLINELENGTH, iFile) == tmpLine)
{
if ((fscanf(iFile, "%c", &lheaderchar) == 1) && (compcount < MAXCOMPONENTS))
{
if (lheaderchar == '#') /* test for comment headers */
{
if ((tmpcomments = (long *) realloc(hcomments, (sizeof(long)* linecount))) == NULL)
{
fprintf(stderr,__FUNCTION__);
fprintf(stderr,"::Memory Allocation Error\n");
exit(EXIT_FAILURE);
}
hcomments = tmpcomments;
hcomments[linecount] = ftell(iFile); /*file pos. of h comments & add in comments later!*/
}
else
{
compcount++;
switch(lheaderchar)
{
case 'R' : case 'L' : case 'C' :
type = RLC;
break;
case 'D' : case 'I' : case 'V' :
type = DIV;
break;
default: break;
}
}
if (type >0)
{
ProcessLine(tmpLine, lheaderchar, type, compcount, copr);
/* and return comp struct vals???*/
}
}
}
}
return 0; /* return comp struct ptr for arrays?????*/
}
void ProcessLine(char *inLine, char firstchar, Line2Type type, int count, ComPtr ptr)
{ /* assign line parts to component value, array of pointers to adt*/
char tmpstring_indentifier[3];
int tmpnode1;
int tmpnode2;
char tmpval_strpid[22];
char tmpcomment[28];
ComPtr InCom= NULL;
ComPtr tmpcom = NULL;
InCom = ptr;
if ((tmpcom = (ComPtr) realloc(InCom, (sizeof(ComPtr) * count))) == NULL)
{
fprintf(stderr,__FUNCTION__);
fprintf(stderr,"::Memory Allocation Error\n");
exit(EXIT_FAILURE);
}
InCom = tmpcom;
InCom->ctype = firstchar;
if (sscanf(inLine,"%*c %s %i %i %s, %28c", tmpstring_indentifier, &tmpnode1, &tmpnode2, tmpval_strpid, tmpcomment) >0);
{
strcpy(InCom->string_indentifier, tmpstring_indentifier);
InCom->node1 = tmpnode1;
InCom->node2 = tmpnode2;
strcpy(InCom->val_strpid, tmpval_strpid);
strcpy(InCom->comment, tmpcomment);
}
/*to be continued - now calls validate function for each component for each type*/
}
void ProcessOut(const char *outFileName, ComPtr inPtr)
{
FILE* oFile=NULL;
oFile = fopen(outFileName, "w");
/*
printout??
qsort should be called in processin, not here?????
-sort by qsort & function ComPtrCompare:
then: qsort(ComPtr, count, sizeof(ComPtr), ComPtrCompare);
??? order results in: RCLDIV, ordered in group by value (RLC), or by aphlabetic model name (DIV)
print to new file in order
free(inPtr); and other mem */
}
int ComPtrCompare(const void *cp1, const void *cp2)
{ /* compares elements of components from qsort*/
int result = 0;
/*const ComPtr elem1 = (*(component **) cp1);
const ComPtr elem2 = (*(component **) cp2);
if (isParallel(cp1, cp2) !=0 )
{
compare alpha for DIV, value for RLC
if
result = strcmp( model name???)
else
{ ??
?
}
}
else
{
result = 1; more?
} */
return result;
}
int isParallel(const void *sp1, const void *sp2)
{
int isparallel = 0; /*true =1*/
int check = 0;
const ComPtr elem1 = (*(component **) sp1);
const ComPtr elem2 = (*(component **) sp2);
check = elem1->node1 - elem2->node1;
if (check == 0)
{
check = elem1->node2 - elem2->node2;
if (check == 0)
{
isparallel = 1;
}
else
{
isparallel = 0;
}
}
else if (check != 0)
{
check = elem1->node1 - elem2->node2;
if (check == 0)
{
check = elem1->node2 - elem2->node1;
if (check == 0)
{
isparallel = 1;
}
}
else
{
isparallel = 0;
}
}
return isparallel;
}
int
ProcessArgs(int argc, char *argv[], char **inFileNamePtr, char **outFileNamePtr)
{
int argCount = 0;
argc--;
argv++;
while (argc > 0) {
if ( strcmp((*argv),"-i") == 0 ) {
argc--;
argv++;
if (argc > 0) {
if ( ( (*inFileNamePtr) = (char *) malloc( sizeof(char) * (strlen( (*argv) ) + 1)) ) == NULL ) {
fprintf(stderr,__FUNCTION__);
fprintf(stderr,"::Memory Allocation Error\n");
exit(EXIT_FAILURE);
}
strcpy((*inFileNamePtr), (*argv));
argCount++;
argc--;
argv++;
}
}
else if ( strcmp((*argv),"-o") == 0 ) {
argc--;
argv++;
if (argc > 0) {
if ( ((*outFileNamePtr) = (char *) malloc(sizeof(char) * (strlen(*argv)+1)) ) == NULL ) {
fprintf(stderr,__FUNCTION__);
fprintf(stderr,"::Memory Allocation Error\n");
exit(EXIT_FAILURE);
}
strcpy((*outFileNamePtr), (*argv));
argCount++;
argc--;
argv++;
}
}
else if ( strcmp((*argv),"--help") == 0) {
PrintUsage();
exit(EXIT_FAILURE);
}
else {
fprintf(stderr,"Unknown Argument::%s\n",(*argv));
PrintUsage();
exit(EXIT_FAILURE);
}
}
return argCount;
}
void
PrintUsage(void)
{
fprintf(stderr,"usage: fileProcessor -i s -o s\n\n");
fprintf(stderr," -i 's' is the input file's name\n");
fprintf(stderr," -o 's' is the output file's name\n");
fprintf(stderr," --help prints this usage message\n\n");
}
and the .h file from before, slightly edited:
Code:
/* component.h - Component ADT */
#ifndef COMPONENT_H
#define COMPONENT_H
typedef enum {COM, RLC, DIV} Line2Type;
typedef struct {
char ctype;
char string_indentifier[3];
int node1;
int node2;
char val_strpid[22];
char comment[28];
} component;
typedef component *ComPtr;
int ProcessIn(const char *inFileName, ComPtr copr);
void ProcessLine(char *inLine, char firstchar, Line2Type type, int count, ComPtr ptr);
void ProcessOut(const char *outFileName, ComPtr inPtr);
int ComPtrCompare(const void *cp1, const void *cp2);
int isParallel(const void *sp1, const void *sp2);
#endif