Monday, November 10, 2008

C and C++ Programming: An Example : Viewmod

#include <termios.h>

#include <stdio.h>

#include <unistd.h>

#include <fcntl.h>

#include <dirent.h>

#include <string.h>

#include <stdlib.h>

#include <time.h>

#include <sys/signal.h>

#include <sys/types.h>

#include <sys/stat.h>



#define _POSIX_SOURCE 1 //POSIX compliant source

#define FALSE 0

#define TRUE 1

#define MAX_DEVICES 64

#define MAX_IDS 64

#define LINE_LENGTH 1128





FILE *output;



char input_fname[80]; //printer file name

FILE *input_file;

mode_t mode;

char Char; //character for individual character processing

int display;
/* "-D" options: 0-ASC,1-ASC/HEX, 2-Hex 3-dec 4-dec/asc
5-DOS text 6-UNIX text 7- strip all non-asc */

int out_length; //-L options

int display_col; //display column counter

int char_len;

int asc_char; //0-non-asc, 1-asc, 2-CR, 3-LF

int nospace;

int last_char; //type of last char



int get_linestring(char *file_line, char *label_name,
int qty_statements, int strno);

int freadln(FILE *handle,char *outputline); //read a line from a file

void Process_Bufchar();
//process characters (Char) read into the serial buffer one at a time



main(int Parm_Count, char *Parms[])

{

char message[90];

int start_options, in_source, done;



char *lastslash; //string address where last slash is

char dirname[80]; //directory name used to confirm directory existence

DIR *current_directory;

int i;

char In1, Key;

char buf[255]; //buffer for where data is put



out_length = 80;

display = 1; //default to HEX/ASC display, break after CR, CR/LF, LF

start_options=0;

in_source = 0; //default, standard input

if (Parm_Count>1) //if have parameters after the program name

{

start_options = 1;

strcpy(message,Parms[1]);

if (message[0]!='-') //if is an input file name

{

if ((input_file = fopen(Parms[1], "rb")) == NULL)

{

fprintf(stderr,"%s: Unable to open the input file %s\n",Parms[0],Parms[1]);

exit(1); //exit the program with an error

}

else

{

in_source = 1; //file

}

} //end if is an input file name

} //end if have parameters on the command line

//get the parameters

if (start_options<Parm_Count) //if start options may exist

{

for (i=1; i<Parm_Count; i++)

{

strcpy(message, Parms[i]);

if (message[0]=='-')

{

if ((message[1]=='D') || (message[1]=='d')) //if display option

{

if (message[2]=='0') display=0;

if (message[2]=='1') display=1;

if (message[2]=='2') display=2;

if (message[2]=='3') display=3;

if (message[2]=='4') display=4;

if (message[2]=='5') display=5;

if (message[2]=='6') display=6;

if (message[2]=='7') display=7;

}

if ((message[1]=='L') || (message[1]=='l')) //if length option

{

out_length=atoi(&message[2]);

}

}

}

} //end if start options may exist

done = 0;

display_col=0;

nospace = 1; //we don't need space

last_char=4;

while (!done)

{

if (in_source==1) //read a char from a file

{

if ((Char=fgetc(input_file))==EOF)

{

done = 1;

}

}

else //read a char from standard input

{

Char=getchar();

if (Char==EOF)

{

done = 1;

}

}

if (done==0) //output the character to std output

{

asc_char=0; //assume not asc char

if ((Char>31) && (Char < 127)) asc_char = 1;

if (Char==13) asc_char = 2;

if (Char==10) asc_char = 3;

switch (display)

{

case 0: //ASC

if (asc_char < 2) char_len=1;

else char_len=0;

if (display_col+char_len>out_length)

{

putchar(10); //scroll the line if will be too long

display_col=0;

}

putchar(Char);

display_col++;

if (asc_char==3) display_col=0;

break;

case 1: //ASC/HEX

default:

if (asc_char==1) char_len=1;

else char_len=2;

if ((display_col==0) || (last_char==4) || ((last_char==1)
&& (asc_char==1))) nospace=1; //dont need space

else nospace=0;

if (nospace==0) char_len++; //if we need a space

if (display_col+char_len>out_length)

{

putchar(10); //scroll the line if will be too long

display_col=0;

if (nospace==0)

{

nospace=1;

char_len--;

}

}

if (nospace==0) //add a space

{

putchar(' ');

display_col++;

}

if (asc_char==1)

{

putchar(Char);

display_col++;

}

else

{

sprintf(message,"%2x",Char);

fputs(message,stdout);

display_col +=2;

if (asc_char==3)

{

putchar(10);

display_col=0;

}

}

if ((last_char==2) && (asc_char!=3)) //if had a CR with no LF

{

putchar(10);

display_col=0;

}



last_char=asc_char;

break;

case 2: //hex

char_len=2;

if (display_col==0) nospace = 1;

else nospace = 0;

if (nospace == 0) char_len++; //if we need a space

if (display_col+char_len>out_length)

{

putchar(10); //scroll the line if will be too long

display_col=0;

if (nospace==0)

{

nospace=1;

char_len--;

}

}

if (nospace==0) //add a space

{

putchar(' ');

display_col++;

}

sprintf(message,"%2x",Char);

fputs(message,stdout);

display_col +=2;

break;

case 5: //unix text - adjusts to dos text, adds CR before all LF

if (Char==10) putchar(13);

putchar(Char);

break;

case 6: //dos text - strips to unix, strips all CR

if ((last_char==2) && (asc_char !=3)) putchar(10);
/* if this char is not a LF and the last one was CR,
need a line feed for unix */

if (Char!=13) putchar(Char);

last_char=asc_char;

break;

case 3: //decimal

char_len=3;

if (display_col==0) nospace = 1;

else nospace = 0;

if (nospace == 0) char_len++; //if we need a space

if (display_col+char_len>out_length)

{

putchar(10); //scroll the line if will be too long

display_col=0;

if (nospace==0)

{

nospace=1;

char_len--;

}

}

if (nospace==0) //add a space

{

putchar(' ');

display_col++;

}

sprintf(message,"%3d",Char);

fputs(message,stdout);

display_col +=2;

break;

case 4: //decimal asc

if (asc_char==1) char_len=1;

else char_len=3;

if ((display_col==0) || (last_char==4) ||
((last_char==1) && (asc_char==1)))
nospace=1; //dont need space

else nospace=0;

if (nospace==0) char_len++; //if we need a space

if (display_col+char_len>out_length)

{

putchar(10); //scroll the line if will be too long

display_col=0;

if (nospace==0)

{

nospace=1;

char_len--;

}

}

if (nospace==0) //add a space

{

putchar(' ');

display_col++;

}

if (asc_char==1)

{

putchar(Char);

display_col++;

}

else

{

sprintf(message,"%3d",Char);

fputs(message,stdout);

display_col +=2;

if (asc_char==3)

{

putchar(10);

display_col=0;

}

}

if ((last_char==2) && (asc_char!=3)) //if had a CR with no LF

{

putchar(10);

display_col=0;

}

last_char=asc_char;

break;

case 7: //strips all non-asc chars to Unix file (all but ASC and LF)

if ((asc_char==1) || (asc_char==3)) putchar(Char);

break;

} //end of switch display

} //end of if not done

} //end of while not done

} //end of main

0 comments:

Send SMS for Free