aboutsummaryrefslogtreecommitdiff
path: root/dla2csv.c
diff options
context:
space:
mode:
Diffstat (limited to 'dla2csv.c')
-rw-r--r--dla2csv.c83
1 files changed, 71 insertions, 12 deletions
diff --git a/dla2csv.c b/dla2csv.c
index 4605761..734ec57 100644
--- a/dla2csv.c
+++ b/dla2csv.c
@@ -36,25 +36,33 @@ char *eoltypes[][2] = {
{ NULL, NULL }
};
-/* read a string from stdin (E: on the Atari). there could
- be error checking here, but there's not. */
+/* read a string from stdin (E: on the Atari).
+ exit on EOF (or Break key on the Atari). */
void readstring(void) {
- fgets(stringbuf, 256, stdin);
+ if(fgets(stringbuf, 256, stdin) == NULL)
+ exit(1);
}
/* prompt for a filename, try to open it. if there's an error,
show error message and retry. will not return until it
- opens the file.
- there could be code here to prepend D: if it's missing, but
- there isn't.
- */
+ opens the file. */
FILE *prompt_filename(const char *name, const char *mode) {
FILE *f = NULL;
+ putchar('\n');
while(f == NULL) {
- printf("\n%s file: ", name);
+ printf("%s file: ", name);
fflush(stdout);
readstring();
- stringbuf[strlen(stringbuf) - 1] = '\0';
+ stringbuf[strlen(stringbuf) - 1] = '\0'; /* kill trailing \n */
+ if(strlen(stringbuf) == 0) continue;
+#ifdef __ATARI__
+ /* if there's no device spec (D: or D1: etc), prepend D: */
+ if(!strchr(stringbuf, ':')) {
+ memmove(stringbuf+2, stringbuf, strlen(stringbuf));
+ stringbuf[0] = 'D';
+ stringbuf[1] = ':';
+ }
+#endif
f = fopen(stringbuf, mode);
if(!f) perror(stringbuf);
}
@@ -96,10 +104,31 @@ void backspace3(void) {
int main(int argc, char **argv) {
char *inp, *eol;
- int bytes = 0, x, y, xmask;
+ int i, bytes = 0, x, y, xmask, warn;
+
+#ifdef __ATARI__
+ /* cc65's startup code turns off caps lock. turn it back on,
+ since we're typing DOS filenames. in BASIC this would be:
+ POKE 702,64 */
+ *((char *)0x02be) = 0x40;
+
+ /* clear the screen */
+ putchar(0x7d);
+#endif
+
+ printf("DLA to CSV converter.\n\n");
+
+#ifdef __ATARI__
+ /* Atari users aren't used to typing an EOF to exit */
+ printf("Note: Press Ctrl-3 or Break at any\nprompt to exit this program.\n");
+#endif
- printf("DLA to CSV converter.\n");
while(1) {
+ warn = 0;
+
+ /* in case of short read on 2nd or later conversion: */
+ memset(inbuf, 0, INBUF_SIZE);
+
/* read whole input file into memory */
inf = prompt_filename("Input DLA", "rb");
printf("Reading...");
@@ -109,9 +138,39 @@ int main(int argc, char **argv) {
perror(stringbuf);
continue;
}
- fclose(inf);
printf("Read %d bytes.\n", bytes);
+ /* warn if the file size is wrong... */
+ if(bytes < INBUF_SIZE) {
+ printf("Warning: File too short!\n");
+ warn = 1;
+ } else if(fgetc(inf) != EOF) {
+ printf("Warning: File too long!\n");
+ warn = 1;
+ } else {
+ printf("File size is OK.\n");
+ }
+ fclose(inf);
+
+ /* a DLA file never has non-zero bytes at the beginning. */
+ for(i = 0; i < 32; i++) {
+ if(inbuf[i]) {
+ printf("Warning: File doesn't look like a DLA!\n");
+ warn = 1;
+ break;
+ }
+ }
+
+ /* if anything looks wrong, we shouldn't proceed... but the user
+ is boss, so give him the choice to shoot himself in the foot. */
+ if(warn) {
+ printf("Try to convert anyway[y/N]? ");
+ fflush(stdout);
+ readstring();
+ if(stringbuf[0] != 'y' && stringbuf[0] != 'Y')
+ continue;
+ }
+
eol = prompt_eol();
outf = prompt_filename("Output CSV", "wb");