See all articles from QL Hacker's Journal 3
Editor’s Note – Herb Schaaf has sent me a couple of Small C programs via e-mail. He did not include an article about the two programs, so I am just presenting them as is. One is an update of my previous find_c. Be aware that Herb adds a number of bells and whistles in his programs. They are not short and sweet.
find1f_c
/* find1f_c Feb 23 8:45 am
* from QL Hacker's Journal Feb 1991 page 1-4
* trying another approach seems to work ungets()?
*/
#include <stdio_h>
#define LOOKLEN 21
#define BUFSIZE LOOKLEN /* * see K&R page 79 Ex. 4-7 *
*/
char buf[BUFSIZE], *s;
int fd, b, bufp, len;
main()
{
char c, l, search[LOOKLEN], lookfor[LOOKLEN],
initok[LOOKLEN], file[42];
int file_count, str_len, i, pat_find, fd2, ;
/* abcdefghijklmnopqrstuvwxyz 0123456789 xyxyz test
string */
/* aabbccaaabbbccc a ab abc aababc vividvivid
more_tests */
/* ABC Abc ABc aBC abC aaaaaaaaaxYzaxyZaaaaaaaaaaABC
*/
file_count = pat_find = bufp = 0; /* Initialize to Zero
*/
strset(search,'\0');
strset(lookfor,'\0');
strset(initok,'\0');
strset(file,'\0');
strset(buf,'\0');
printf("Enter name of file to be searched : ");
gets(file);
printf("%s\n",file);
printf("Enter string (%d characters or less) to be
matched\n",LOOKLEN-1);
printf(" Search will be case insensitive \n");
gets(search);
if (strlen(search)>=LOOKLEN)
{
strncpy(initok,search,LOOKLEN-1);
strncpy(search,initok,LOOKLEN-1);
strset(initok,'\0');
}
fd = fopen(file,"r");
if (fd == NULL)
{
printf("Did not open file\n");
printf("file in use? example: ram3_find1e_c\n");
printf("Any key to exit\n");
getchar(c);
exit(1);
}
fd2 = fopen("ram3_find1f_log","w+");
if (fd2 == NULL)
{
printf("Did not open ram3_find1f_log file\n");
printf("Any key to exit\n");
getchar(c);
exit(1);
}
else
printf("file : ram3_find1f_log is open for w+ \n");
str_len = strlen(search);
fputs(search,fd2);
strncpy(lookfor,search,str_len);
l = lookfor[0];
printf("Searching file: %s\n",file);
fprintf(fd2,"\nSearching file: %s\n",file);
printf(" looking for %s \n",lookfor);
fprintf(fd2,"looking for %s \n",lookfor);
while(( c = getch()) != EOF)
{
++file_count;
if((lexorder(c,l)) == 0)
{
strset(initok,'\0');
initok[0] = c;
for(i=1; i<str_len; i++)
{
initok[i]=getch();
}
ungets(initok);
getch();
if(lexcmp(lookfor,initok) == 0)
{
pat_find++;
printf("%s <==> %s @ %u # %u\n",lookfor,initok
,file_count,pat_find);
fprintf(fd2,"%s <==> %s @ %u # %u\n",lookfor,initok
,file_count,pat_find);
}
}
}
printf("\nFound %d patterns that match %s\n",pat_find,
search);
fprintf(fd2,"\nFound %d patterns that match %s\n",
pat_find,search);
fclose(fd2);
fclose(fd);
printf("A log of this session is in ram3_find1f_log\nAny
key to exit\n");
getchar();
}
/* FUNCTION CALLS - getch, ungetch, ungets, strset */
getch() /* * get a pushed back character * */
{
if(bufp > 0)
{
b = buf[--bufp];
return b;
}
else
{
b = getc(fd);
return b;
}
}
ungetch(b) /* * push character back on input * */
int b;
{
if(bufp >= BUFSIZE)
{
printf("ungetch: too many characters\n");
}
else
{
buf[bufp++] = b;
}
}
ungets(s)
char *s;
{
len = strlen(s);
while( (len > 0) )
ungetch(s[--len]);
}
strset(string,chr) /* * fill string with a character *
*/
char *chr, *string;
{
int i, length;
length = strlen(string);
for(i=0; i<length; i++)
{
*string++ = *chr;
}
return *string;
}
/* * test string abAbCaababcaa
XYz vivid vivid aabababcabc
aaaaaa ababcaaabcaaaabcabbcabcabbabcABC
*/
msp1k_c
/* msp1k_c Feb 24th 1991 1pm
* try printing this file ?
* driving Citizen MSP-10 printer ( = FX-80 ) from Small-C
*/
#include <stdio_h>
char file[42], c;
int fd, *msp;
main() {
printf("Enter name of file to be sent to MSP-10
printer:\n");
gets(file);
fd = fopen(file,"r");
if(fd == NULL) {
printf("Unable to open file: %s\n",file);
printf("\nAny key to exit\n");
getchar(c);
exit(1);
}
else {
printf("Ready to send %s to MSP-10\n",file);
}
msp = fopen("ser1hc","w");
printf("fd = %d msp = %d\n",fd,msp);
printf("If above looks ok [SPACE BAR] to continue\n x to
exit\n");
c = getchar();
if(c == 'x') {
exit(1);
}
if (msp != NULL) {
stdprint(msp);
epsfont(msp);
fputc(27,msp); /* sets printer for */
fputc(109,msp); /* Standard */
fputc(0,msp); /* EPSON */
fputc(EOF,msp); /* Character Set */
printfile(fd,msp);
fclose(fd);
leave(msp);
}
printf(" + once more to really exit(1); !!\n");
getchar(c);
exit(1);
}
/* E N D O F M A I N - - - - - - - - - - - - - - */
/* FUNCTION CALLS :
stdprint(msp) standardize printer to default settings
epsfont(msp) set printer for Epson FX styles
ibmfont(msp) set printer for ibm styles
newline(msp) CR + LF + '\n'
leave(msp) close file to printer and leave this
exercise
printit(c,msp) printer does one character at a time
printfile(fd,msp) send entire file to printer
epsstd(c,msp) printer does entire Epson standard set
epsgra(c,msp) printer does entire Epson graphics set
ibmstd(c,msp) printer does entire IBM character set
ibmacc(c,msp) printer does entire IBM accented
character set
*/
/* * STDPRINT set printer to power on defaults * */
stdprint(msp)
int msp; {
printf("@ stdprint : msp = %d\n",msp);
if (msp != NULL) {
fputc(27,msp);
fputc(64,msp);
newline(msp);
}
}
/* * EPSFONT select Epson FX fonts * */
epsfont(msp)
int msp; {
printf("@ epsfont : msp = %d\n",msp);
if (msp != NULL) {
/* fputs("@ epsfont",msp); */
newline(msp);
fputc(EOF,msp);
fputc(27,msp);
fputc(126,msp);
fputc(53,msp);
fputc(0,msp);
fputc(EOF,msp);
}
}
/* * IBMFONT select IBM Graphics fonts * */
ibmfont(msp)
int msp; {
printf("@ ibmfont : msp = %d\n",msp);
if (msp != NULL) {
/* fputs("@ ibmfont",msp); */
newline(msp);
fputc(27,msp);
fputc(126,msp);
fputc(53,msp);
fputc(1,msp);
}
}
/* * NEWLINE line feed and carriage return * */
newline(msp)
int msp; {
/* printf("@ newline : msp = %d\n",msp); */
if (msp != NULL) {
fputc(10,msp);
fputc(13,msp);
fputc(138,msp);
fputc(141,msp);
printf("\n");
}
}
/* * LEAVE leave this program * */
leave(msp)
int msp; {
printf("\n@ leave : msp = %d\n",msp);
if (msp != NULL) {
printf("\n\nAny key when you're ready to leave\n");
getchar();
fclose(msp);
}
}
/* * PRINTIT print to screen and to printer * */
printit(c,msp)
char c;
int msp; {
if (msp != NULL) {
fputc(c,msp);
fputc(c,stdout);
}
}
/* * PRINTFILE prints file to stdout & to printer * */
printfile(fd,msp)
int fd, msp; {
char c;
int i;
while(((msp != NULL) && (( c = getc(fd)) != EOF))) {
fputc(c,msp);
fputc(c,stdout);
if(iscntrl(c)) {
if( c == 10 ) {
fputc(10,msp);
fputc(13,msp);
fputc(138,msp);
fputc(141,msp);
}
if( c == 9 ) {
fputc(27,msp);
fputc(102,msp);
fputc(0,msp);
fputc(3,msp); /* sets printhead 3 spaces
right */
}
}
}
}
/* * EPSON STANDARD CHARACTER SET * */
epsstd(c,msp)
char c;
int msp; {
printf("@ epsstd : msp = %d\n",msp);
if (msp != NULL) {
printf("@ epsstd Doing Standard Epson Character
Set\n");
fputc(EOF,msp);
stdprint(msp);
epsfont(msp);
fputc(27,msp);
fputc(109,msp);
fputc(0,msp);
fputc(EOF,msp);
for(c=32; c<=126; c++) {
printit(c,msp);
}
newline(msp);
for(c=161; c<=254; c++) {
printit(c,msp);
}
newline(msp);
}
}
/* * EPSON GRAPHICS SET * */
epsgra(c,msp)
char c;
int msp; {
printf("@ epsgra : msp = %d\n",msp);
if (msp != NULL) {
fputs("@ epsgra",msp);
fputc(EOF,msp);
printf("Doing Epson Graphics Only Characters\n");
stdprint(msp);
epsfont(msp);
fputc(27,msp);
fputc(109,msp);
fputc(4,msp);
fputc(EOF,msp);
for(c=128; c<=159; c++) {
printit(c,msp);
}
newline(msp);
}
}
/* * IBM STANDARD CHARACTER SET * */
ibmstd(c,msp)
char c;
int msp; {
printf("@ ibmstd : msp = %d\n",msp);
if (msp != NULL) {
fputs("@ ibmstd",msp);
newline(msp);
printf("Doing IBM Standard Character Set\n");
stdprint(msp);
ibmfont(msp);
fputc(27,msp);
fputc(55,msp);
fputc(EOF,msp);
printit(21,msp);
for(c=32; c<=126; c++) {
printit(c,msp);
}
newline(msp);
fputs("IBM Extended",msp);
newline(msp);
fputc(EOF,msp);
for(c=160; c<=254; c++) {
printit(c,msp);
}
newline(msp);
}
}
/* * IBM ACCENTED CHARACTERS * */
ibmacc(c,msp)
char c;
int msp; {
printf("@ ibmacc : msp = %d\n",msp);
if (msp != NULL) {
fputs("@ ibmacc",msp);
newline(msp);
printf("Doing IBM Accented Characters Set\n");
stdprint(msp);
ibmfont(msp);
fputc(27,msp);
fputc(54,msp);
for(c=3; c<=6; c++) {
printit(c,msp);
}
for(c=128; c<=159; c++) {
printit(c,msp);
}
newline(msp);
}
}
Products
Downloadable Media
Image Gallery
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.