Commit 2d8c4921 authored by dragontamer lance's avatar dragontamer lance

added str2csv for converting arbitrary strings to csv, did some tweaks, fixed...

added str2csv for converting arbitrary strings to csv, did some tweaks, fixed a comparison between a null pointer and an int
parent 960ec593
......@@ -3,8 +3,8 @@ CFLAGS=-Wall
.PHONY: all clean
all: csv2ascii ascii2csv
make csv2ascii ascii2csv
all: csv2ascii ascii2csv str2csv
make $^
csv2ascii: csv2ascii.c
$(CC) $(CFLAGS) -o [email protected] $^
......@@ -12,5 +12,8 @@ csv2ascii: csv2ascii.c
ascii2csv: ascii2csv.c
$(CC) $(CFLAGS) -o [email protected] $^
str2csv: str2csv.c
$(CC) $(CFLAGS) -o [email protected] $^
clean:
rm ascii2csv csv2ascii
rm ascii2csv csv2ascii str2csv
......@@ -3,68 +3,80 @@
int main(int argc, char **argv)
{
FILE *infile=fopen(argv[1], "r");
int achar=0;
int i=0;
/* copy header verbatim */
/* (two lines) */
while(i<2)
if(argc == 2)
{
achar=fgetc(infile);
if(achar == EOF || achar == 0)
FILE *infile=fopen(argv[1], "r");
if(infile == NULL)
{
printf("Error: failed to open file.\n");
return -1;
}
else
int achar=0;
int i=0;
/* copy header verbatim */
/* (two lines) */
while(i<2)
{
if(achar == '\n')
achar=fgetc(infile);
if(achar == EOF || achar == 0)
{
i++;
return -1;
}
else
{
if(achar == '\n')
{
i++;
}
printf("%c",(char)achar);
}
printf("%c",(char)achar);
}
}
/* translate back into stupid integer format */
while(achar != EOF)
{
achar = fgetc(infile);
if(achar != EOF)
{
/*
this isn't actually right. It assumes that each line only contains
one string, when in fact strings are simply signified as starting
with 'S' and ending with a null (,0) and a semicolon.
*/
printf("S");
}
else
/* translate back into stupid integer format */
while(achar != EOF)
{
return 0;
}
while(achar != ';' && achar != EOF) {
if(achar != 0)
achar = fgetc(infile);
if(achar != EOF)
{
printf("%d,",achar);
/*
this isn't actually right. It assumes that each line only contains
one string, when in fact strings are simply signified as starting
with 'S' and ending with a null (,0) and a semicolon.
*/
printf("S");
}
else /* ; or eof */
else
{
if(achar==';')
return 0;
}
while(achar != ';' && achar != EOF) {
if(achar != 0)
{
putchar('0');
printf("%d,",achar);
}
printf("%d",achar);
else /* ; or eof */
{
if(achar==';')
{
putchar('0');
}
printf("%d",achar);
}
achar = fgetc(infile);
}
achar = fgetc(infile);
}
printf(";");
achar=fgetc(infile);
while(achar != '\n' && achar != EOF)
{
printf("%c",achar);
achar = fgetc(infile);
printf(";");
achar=fgetc(infile);
while(achar != '\n' && achar != EOF)
{
printf("%c",achar);
achar = fgetc(infile);
}
printf("\n");
fgetc(infile);
}
printf("\n");
fgetc(infile);
}
else
{
printf("Error: I need to be given exactly one argument, a filename.\n");
}
}
......@@ -3,76 +3,81 @@
#include <ctype.h> /* isdigit */
int main(int argc, char **argv)
{
FILE *infile=fopen(argv[1], "r");
int achar=0;
int i=0;
/* copy header verbatim */
while(i<2)
if(argc == 2)
{
achar=fgetc(infile);
if(achar == NULL)
FILE *infile=fopen(argv[1], "r");
if(infile == NULL)
{
printf("Error: Failed to open file.\n");
return -1;
}
else
int achar=0;
int i=0;
/* copy header verbatim */
while(i<2)
{
achar=fgetc(infile);
printf("%c",(char)achar);
if(achar == '\n')
{
i++;
}
}
}
/* now we are at first inv line */
/* each line starts with a letter S so we need to skip that */
char str[4]; /* ascii is limited in size */
str[3]='\0';
i=0;
if(fgetc(infile) == 'S') /* progress ahead of 'S' */
{
while(achar != EOF)
/* now we are at first inv line */
/* each line starts with a letter S so we need to skip that */
char str[4]; /* ascii is limited in size */
str[3]='\0';
i=0;
if(fgetc(infile) == 'S') /* progress ahead of 'S' */
{
while(achar != ';' && achar != EOF)
while(achar != EOF)
{
while(isdigit(achar=fgetc(infile)) && i < 3)
while(achar != ';' && achar != EOF)
{
str[i]=achar;
i++;
}
int isok=0;
i=0;
while(i<3 && str[i] != '\0' && isok == 0)
{
if(!isascii(str[i]) || str[i] == '\0')
while(isdigit(achar=fgetc(infile)) && i < 3)
{
isok=1;
str[i]=achar;
i++;
}
i++;
}
if(isok==0 && atoi(str) != '\0')
int isok=0;
i=0;
while(i<3 && str[i] != '\0' && isok == 0)
{
if(!isascii(str[i]) || str[i] == '\0')
{
isok=1;
}
i++;
}
if(isok==0 && atoi(str) != '\0')
{
printf("%c",(char)atoi(str));
}
str[0]='\0';
str[1]='\0';
str[2]='\0';
i=0;
} /* ';' reached */
if(achar == ';')
{
printf("%c",(char)atoi(str));
putchar('\0');
putchar(';');
}
str[0]='\0';
str[1]='\0';
str[2]='\0';
i=0;
} /* ';' reached */
if(achar == ';')
{
putchar('\0');
putchar(';');
}
/* print out rest of line since we don't know what it is yet */
while(achar != '\n' && achar != EOF && achar != 'S' && isascii(achar))
{
achar=fgetc(infile);
if(achar != '\0')
/* print out rest of line since we don't know what it is yet */
while(achar != '\n' && achar != EOF && achar != 'S' && isascii(achar))
{
printf("%c",achar);
achar=fgetc(infile);
if(achar != '\0')
{
printf("%c",achar);
}
}
}
} /* != EOF */
} /* != EOF */
}
return 0;
}
else
{
printf("Error: I need to be given exactly one argument, a filename.\n");
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
/* printf("length: %d", strlen(argv[1]));*/
/* str2csv*/
if(argc == 2)
{
putchar('S');
int i=0;
while( i < strlen(argv[1]))
{
printf("%d,",argv[1][i]);
i++;
}
printf("%d",argv[1][i]);
printf(";\n"); /* null terminator is encoded in ascii :| */
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment