Skip to content

Commit

Permalink
change topt.c to use stdin and stdout. Add some documentation about it.
Browse files Browse the repository at this point in the history
  • Loading branch information
unclebob committed Aug 31, 2021
1 parent 1eba53c commit 7a80391
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 38 deletions.
8 changes: 6 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ The `Export` button at the bottom of the rack will copy a selected tape to your

The exported tape is in paper tape format and can be used by others users of the emulator. If you'd like to make a text copy of the paper tape, use the `pt2txt` utility in the `src` folder.

If you have a text file that you'd like to turn into a paper tape, use the `topt` utility.
If you have a text file that you'd like to turn into a paper tape, use the `topt` utility with the `-t` option. `topt -t <file >file.txt`. The `-t` option adds a `\r` before every `\n`.

This comment has been minimized.

Copy link
@dancrossnyc

dancrossnyc Sep 1, 2021

-t also sets the high bit on the output byte.


### Core Images
Below the racks, between the "Next" and "Prev" buttons you'll see the "Load" and "Save" buttons. If you push "Save" then the current contents of Core will be saved on an empty shelf. You'll see it appear there with the name: `Core_<Time>`. You can change the name by clicking on the shelf, and editing the text. Again, these images are saved for future emulator runs.
Expand Down Expand Up @@ -191,7 +191,11 @@ These tapes have grey icons in the shelf. They are permanent and the names cann
* `FortranCompiler` This is the 4K Fortran compiler circa 1969.
* `FortranOS` This is the Fortran OS that loads and executes Fortran programs.

If you find a binary paper tape image on the net and want to include it, you can convert it to emulator format using the `topt.c` program. Take the output and put it in your `Dropbox/Apps/Codea` directory, and then sync with Codea.
If you find a binary paper tape image on the net and want to include it, you can convert it to emulator format using the `topt.c` program.

`topt <file >file.txt`

Take the output and put it in your `Dropbox/Apps/Codea` directory, and then sync with Codea.

### Videos

Expand Down
67 changes: 31 additions & 36 deletions src/topt.c
Original file line number Diff line number Diff line change
@@ -1,67 +1,62 @@
/*
Simple utility to translate a binary file, character by character
into lines composed of three octal digits.
Simple utility to translate a binary file, character by character
into lines composed of three octal digits.
So a file that contains "ABC"
will be translated to: 101\n102\n103\n
So a file that contains "ABC"
will be translated to: 101\n102\n103\n
use -t for text files. It will add \r before each \n
usage: topt [-t] <infile >outfile
Remember that paper tapes must have a .txt extension.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

void putCharInOctal(int c, FILE* outFile) {
fputc('0'+((c>>6)&7), outFile);
fputc('0'+((c>>3)&7), outFile);
fputc('0'+(c&7), outFile);
fputc('\n', outFile);
void putCharInOctal(int c) {
putchar('0'+((c>>6)&7));

This comment has been minimized.

Copy link
@dancrossnyc

dancrossnyc Sep 1, 2021

Incorrect mask: '&7' should be '&3' for this character. Or just use printf("%03o\n", c);, as in my pull request.

putchar('0'+((c>>3)&7));
putchar('0'+(c&7));
putchar('\n');
}

void leader(int n, FILE* outFile) {
void leader(int n) {
for (int i=0; i<n; i++)
putCharInOctal(0200, outFile);
putCharInOctal(0200);
}

int main(int ac, char** av) {
int textFlag = 0;

if (ac != 2 && ac != 3) {
printf("usage topt [-t] file\n");
if (ac > 2) {

This comment has been minimized.

Copy link
@dancrossnyc

dancrossnyc Sep 1, 2021

What if the user runs, topt -t -t -t foo? Hence why I opted to use getopt() in my PR.

fprintf(stderr, "usage topt [-t] <infile >outfile\n");
exit(1);
}

av++;
av++;

if (strcmp(*av, "-t") == 0) {
printf("text mode.\n");
if (strcmp(*av, "-t") == 0) {

This comment has been minimized.

Copy link
@dancrossnyc

dancrossnyc Sep 1, 2021

What if the user specifies, topt -n? Or even just topt foo expecting it to still take a filename?

fprintf(stderr, "text mode.\n");
textFlag = 1;
av++;
}

FILE* inFile = fopen(*av, "r");
char outFileName[100];
bzero(outFileName, 100);
strcpy(outFileName, *av);
strcat(outFileName, ".txt");
printf("Creating %s\n", outFileName);
FILE* outFile = fopen(outFileName, "w");

leader(10, outFile);
leader(10);

int c;
while ((c = fgetc(inFile)) != EOF) {
if (textFlag) {
c+=0200;
if (c == 0212)
putCharInOctal(0215, outFile);
}
putCharInOctal(c, outFile);
while ((c = getchar()) != EOF) {
if (textFlag) {
c|=0200;
if (c == 0212)
putCharInOctal(0215);
}
putCharInOctal(c);
}

leader(10, outFile);
fclose(inFile);
fclose(outFile);
leader(10);

This comment has been minimized.

Copy link
@dancrossnyc

dancrossnyc Sep 1, 2021

While not strictly necessary in C99 and later, stylistically it would be nice to add return 0; at the end of main.

}


0 comments on commit 7a80391

Please sign in to comment.