For the amusement of all concerned I attach indent5.c which is lying
around here at the moment. I'm actually busy and don't have a lot of
time to fart with it. This is a debugging and elaboration of David Fix's
indent program offered here earlier by a self confessed C illiterate.
This is more or less doing the business. I get in files on a long line
(up to 2595 characters long) preprocess and indent5 them. I don't
understand all of the code, but the mouse still works :-). I'm caught
with this line
char c;
if (c == ',' || c == ':' || c == ':')
as it always passes as true. Then it folds the line in some specious way
and I lose three letters. I did try going to the hex code
if (c == '0x3b' || c == '0x3a' || c == '0x2c')
and gcc got really annoyed with me - it always passes as false, it
appears wrong syntax?
indent6.c:79: warning: comparison is always false due to limited range
of data type
indent6.c:79:39: warning: multi-character character constant
indent6.c:79: warning: comparison is always false due to limited range
of data type
indent6.c:79:54: warning: multi-character character constant
indent6.c:79: warning: comparison is always false due to limited range
of data type
That aside, I'm basically running.
Input line Up to 2595 chars (2171 with control chars stripped)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~etc.
Output line (115 , then 65-115)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ (115 of them)
~~~~~~~~~~~~~~~~~etc
~~~~~~~~~~~~~~~~~
This C is handy stuff, worth the effort imho. Can anyone throw me a bone
on the line above? I'm trying to find ';' ':' or ',' to fold the line
there. But the test is bad. BTW, for beginners, I would reccomend gnu
indent, which passes over code and lines it up.
--
With best Regards,
Declan Moriarty.
#include <stdio.h>
#include <ctype.h>
/* Make this how long you want the lines to be
This works but loses characters off the end of some lines D.M.*/
#define MAX_LINE_LENGTH 115
int
main (int argc, char *argv[])
{
FILE *fp1;
FILE *fp2;
char c;
char c2;
int position;
int counter;
int gottawrap;
if (argc != 3)
{
printf ("Usage: %s <input filename> <output filename>", argv[0]);
return -1;
}
if ((fp1 = fopen (argv[1], "r")) == NULL)
{
puts ("Damn, couldn't open input file!");
return -1;
}
if ((fp2 = fopen (argv[2], "w")) == NULL)
{
puts ("Damn, couldn't open output file!");
return -1;
}
position = 0;
gottawrap = 0;
while (1)
{
for (counter = 0;; counter++)
{
c = fgetc (fp1);
position = position + 1;
if (c == EOF)
{
fclose (fp1);
fclose (fp2);
return 0;
}
if (position > MAX_LINE_LENGTH - 2)
{
if (c == ' ' || c == '(')
{
/* This line here will insert a tab when it's split the line
*/
fputs ("\n", fp2);
fputs(" ",
fp2);
position = 65;
break;
}
}
if (c == '\r' || c == '\n')
{
c2 = fgetc (fp1);
while (c2 == ' ')
{
c2 = fgetc (fp1);
}
if (c2 == '\r' || c2 == '\n')
{
fputc ('\n', fp2);
position = 0;
}
ungetc (c2, fp1);
}
if (position > MAX_LINE_LENGTH + 2)
{
if (c == ',' || c == ':' || c == ':')
{
fputc (c, fp2);
fputs ("\n", fp2);
fputs(" ",
fp2);
position = 65;
break;
}
}
else
{
fputc (c, fp2);
}
}
}
}
--
http://linuxfromscratch.org/mailman/listinfo/blfs-support
FAQ: http://www.linuxfromscratch.org/blfs/faq.html
Unsubscribe: See the above information page