F
Frank
It's hard for me to really know when to start a new thread. I think,
in general, shorter threads promote better communication and less
arguing or mudslinging. The sixtieth to the eightieth messages will
tend to have the uncharitable exchanges.
I'm having trouble adapting the wiki soln to K&R 1-24 to my fledgling
indent program. The way I see it, I've got line defined in main and
in getline. My compiler disagrees:
F:\gfortran\dan>gcc bb10.c -Wall -o indent.exe
C:\DOCUME~1\dan\LOCALS~1\Temp/ccym7Q6M.o:bb10.c
.text+0x35):
undefined referenc
e to `line'
C:\DOCUME~1\dan\LOCALS~1\Temp/ccym7Q6M.o:bb10.c
.text+0x50):
undefined referenc
e to `line'
C:\DOCUME~1\dan\LOCALS~1\Temp/ccym7Q6M.o:bb10.c
.text+0x62):
undefined referenc
e to `line'
collect2: ld returned 1 exit status
F:\gfortran\dan>type bb10.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#define MAXLINE 1000 /* max input line size */
int getline(FILE * p1)
{
int c, i;
extern char line[];
for ( i=0;i<MAXLINE-1 && ( c=fgetc(p1)) != EOF && c != '\n'; ++i)
line = c;
if(c == '\n')
{
line = c;
++i;
}
line = '\0';
return i;
}
int main(int argc, char *argv[])
{
int n;
FILE *fin, *fout;
char *path_exp, *exp_suffix = ".exp";
const char *path;
int c, nc;
char d;
int getline(FILE *);
int len=0;
int t=0;
int brace=0, bracket=0, parenthesis=0;
int s_quote=1, d_quote=1;
char line[MAXLINE]; /*current input line*/
path=argv[2];
if (argc != 3)
{
fprintf(stderr, "usage: \n");
fprintf(stderr, "%s <num> <filename>\n", argv[0]);
return EXIT_FAILURE;
}
errno = 0;
n = strtod(argv[1], NULL);
if (n < 0 || n > 999 )
{
fprintf(stderr, "%s: give me a natural number\
less than 1000\n", argv[0]);
return EXIT_FAILURE;
}
// if detab
if (n == 0)
{
fprintf(stderr, "this will detab your file\n");
fprintf(stderr, "If that's what you want, enter zero again\n");
fprintf(stderr, "To abort, enter anything else\n");
d = getchar();
if (d == '0')
fprintf(stderr, "detabbing %s \n", argv[2]);
else
{
fprintf(stderr, "Aborting");
return EXIT_FAILURE;
}
}
// Process path and open file
nc = strlen (path);
if (nc <= 0 || nc > 999)
{
fprintf(stderr, "%s: nc screwed up\n", argv[0]);
return EXIT_FAILURE;
}
c = strlen (exp_suffix);
path_exp = malloc(strlen(path) + strlen(exp_suffix) + 1);
if (!path_exp)
{
fprintf(stderr, "%s: %s\n", __func__, "malloc error");
return EXIT_FAILURE;
}
strcat(strcpy(path_exp, path), exp_suffix);
fin = fopen(path, "r");
if (!fin)
{
fprintf(stderr, "%s %s\n", "fopen failed to open", argv[2] );
fclose(fout);
return EXIT_FAILURE;
}
// main control
while ((len = getline(fin)) > 0 )
{
t=0;
while(t < len)
{
if( line[t] == '[')
{
brace++;
}
if( line[t] == ']')
{
brace--;
}
if( line[t] == '(')
{
parenthesis++;
}
if( line[t] == ')')
{
parenthesis--;
}
if( line[t] == '\'')
{
s_quote *= -1;
}
if( line[t] == '"')
{
d_quote *= -1;
}
t++;
}
}
if(d_quote !=1)
printf ("Mismatching double quote mark\n");
if(s_quote !=1)
printf ("Mismatching single quote mark\n");
if(parenthesis != 0)
printf ("Mismatching parenthesis\n");
if(brace != 0)
printf ("Mismatching brace mark\n");
if(bracket != 0)
printf ("Mismatching bracket mark\n");
if( bracket==0 && brace==0 && parenthesis==0 && s_quote == 1 &&
d_quote == 1)
printf ("Syntax appears to be correct.\n");
// print some values and exit
printf(" argv1 is %d\n" , n);
printf(" argv2 is %s\n" , path);
fout = fopen(path_exp, "w");
if (!fout)
{
fprintf(stderr, "%s \n", "fout failed");
return EXIT_FAILURE;
}
fclose(fin);
fclose(fout);
free(path_exp);
return EXIT_SUCCESS;
}
// gcc bb10.c -Wall -o indent.exe
F:\gfortran\dan>
My bigger concern here is that I don't see the logic here that would
decide whether these characters occured in quotes or comments. Does
someone else?
in general, shorter threads promote better communication and less
arguing or mudslinging. The sixtieth to the eightieth messages will
tend to have the uncharitable exchanges.
I'm having trouble adapting the wiki soln to K&R 1-24 to my fledgling
indent program. The way I see it, I've got line defined in main and
in getline. My compiler disagrees:
F:\gfortran\dan>gcc bb10.c -Wall -o indent.exe
C:\DOCUME~1\dan\LOCALS~1\Temp/ccym7Q6M.o:bb10.c
undefined referenc
e to `line'
C:\DOCUME~1\dan\LOCALS~1\Temp/ccym7Q6M.o:bb10.c
undefined referenc
e to `line'
C:\DOCUME~1\dan\LOCALS~1\Temp/ccym7Q6M.o:bb10.c
undefined referenc
e to `line'
collect2: ld returned 1 exit status
F:\gfortran\dan>type bb10.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#define MAXLINE 1000 /* max input line size */
int getline(FILE * p1)
{
int c, i;
extern char line[];
for ( i=0;i<MAXLINE-1 && ( c=fgetc(p1)) != EOF && c != '\n'; ++i)
line = c;
if(c == '\n')
{
line = c;
++i;
}
line = '\0';
return i;
}
int main(int argc, char *argv[])
{
int n;
FILE *fin, *fout;
char *path_exp, *exp_suffix = ".exp";
const char *path;
int c, nc;
char d;
int getline(FILE *);
int len=0;
int t=0;
int brace=0, bracket=0, parenthesis=0;
int s_quote=1, d_quote=1;
char line[MAXLINE]; /*current input line*/
path=argv[2];
if (argc != 3)
{
fprintf(stderr, "usage: \n");
fprintf(stderr, "%s <num> <filename>\n", argv[0]);
return EXIT_FAILURE;
}
errno = 0;
n = strtod(argv[1], NULL);
if (n < 0 || n > 999 )
{
fprintf(stderr, "%s: give me a natural number\
less than 1000\n", argv[0]);
return EXIT_FAILURE;
}
// if detab
if (n == 0)
{
fprintf(stderr, "this will detab your file\n");
fprintf(stderr, "If that's what you want, enter zero again\n");
fprintf(stderr, "To abort, enter anything else\n");
d = getchar();
if (d == '0')
fprintf(stderr, "detabbing %s \n", argv[2]);
else
{
fprintf(stderr, "Aborting");
return EXIT_FAILURE;
}
}
// Process path and open file
nc = strlen (path);
if (nc <= 0 || nc > 999)
{
fprintf(stderr, "%s: nc screwed up\n", argv[0]);
return EXIT_FAILURE;
}
c = strlen (exp_suffix);
path_exp = malloc(strlen(path) + strlen(exp_suffix) + 1);
if (!path_exp)
{
fprintf(stderr, "%s: %s\n", __func__, "malloc error");
return EXIT_FAILURE;
}
strcat(strcpy(path_exp, path), exp_suffix);
fin = fopen(path, "r");
if (!fin)
{
fprintf(stderr, "%s %s\n", "fopen failed to open", argv[2] );
fclose(fout);
return EXIT_FAILURE;
}
// main control
while ((len = getline(fin)) > 0 )
{
t=0;
while(t < len)
{
if( line[t] == '[')
{
brace++;
}
if( line[t] == ']')
{
brace--;
}
if( line[t] == '(')
{
parenthesis++;
}
if( line[t] == ')')
{
parenthesis--;
}
if( line[t] == '\'')
{
s_quote *= -1;
}
if( line[t] == '"')
{
d_quote *= -1;
}
t++;
}
}
if(d_quote !=1)
printf ("Mismatching double quote mark\n");
if(s_quote !=1)
printf ("Mismatching single quote mark\n");
if(parenthesis != 0)
printf ("Mismatching parenthesis\n");
if(brace != 0)
printf ("Mismatching brace mark\n");
if(bracket != 0)
printf ("Mismatching bracket mark\n");
if( bracket==0 && brace==0 && parenthesis==0 && s_quote == 1 &&
d_quote == 1)
printf ("Syntax appears to be correct.\n");
// print some values and exit
printf(" argv1 is %d\n" , n);
printf(" argv2 is %s\n" , path);
fout = fopen(path_exp, "w");
if (!fout)
{
fprintf(stderr, "%s \n", "fout failed");
return EXIT_FAILURE;
}
fclose(fin);
fclose(fout);
free(path_exp);
return EXIT_SUCCESS;
}
// gcc bb10.c -Wall -o indent.exe
F:\gfortran\dan>
My bigger concern here is that I don't see the logic here that would
decide whether these characters occured in quotes or comments. Does
someone else?