Browse Source

Fix atob and added build and add build and run script

master
stew3254 1 year ago
parent
commit
951562f0a5
  1. 22
      atob.c
  2. 7
      build.sh
  3. 128
      function.c
  4. 1
      out.jabber
  5. 31
      run.sh

22
atob.c

@ -7,17 +7,17 @@
#include "unistd.h"
//Print binary
void printb(FILE *f, const char *s, const int len) {
char *out = (char*) malloc((8*sizeof(char)+1)*sizeof(char));
out[sizeof(char)] = '\0';
for (int i = 0; i < len; ++i) {
char c;
for (int j = sizeof(char)-1; j > -1; --j) {
out[sizeof(char)-1-j] = '0' + ((s[i] >> j) & 1);
}
fprintf(f, "%s", out);
}
free(out);
void printb(FILE *f, const char *s, const int len) {
char *out = (char*) malloc(9*sizeof(char));
out[8] = '\0';
for (int i = 0; i < len; ++i) {
char c;
for (int j = 7; j > -1; --j) {
out[7-j] = '0' + ((s[i] >> j) & 1);
}
fprintf(f, "%s", out);
}
free(out);
}
int main (int argc, char **argv) {

7
build.sh

@ -0,0 +1,7 @@
#!/bin/sh
mkdir -p bin
gcc atob.c -o bin/atob
gcc btoa.c -o bin/btoa
gcc jabber.c tree.h -o bin/jabber
gcc wocky.c -o bin/wocky

128
function.c

@ -1,128 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void jabber ( FILE *ifp, FILE *ofp );
int main( int argc, char *argv[] ) {
FILE *ifp, *ofp;
if (argc < 3) {
fprintf(stderr, "Not enough arguments\n");
exit(1);
}
if (!(ifp = fopen(argv[1],"r"))) {
fprintf(stderr,"Cannot open file %s\n",argv[1]);
exit(1);
}
if (!(ofp = fopen(argv[2],"w"))) {
fprintf(stderr,"Cannot open file %s\n",argv[2]);
exit(1);
}
jabber(ifp, ofp);
return 0;
}
typedef struct node_t {
int index;
// Children
struct node_t * zero;
struct node_t * one;
} node;
node * new_node( int index ) {
node * n = malloc(sizeof(node));
n -> index = index;
n -> zero = NULL;
n -> one = NULL;
return n;
}
void print_tree(node * v);
void jabber( FILE *ifp, FILE *ofp ) {
// Read the entire file into a buffer
fseek(ifp, 0L, SEEK_END);
long sz = ftell(ifp);
rewind(ifp);
char * buffer = malloc(sz);
fread(buffer, 1, sz, ifp);
// Start the tree
node * root = new_node(0);
// Start a cursor
node * cursor = root;
int i;
int index = 0;
int bits = 0;
for (i = 0; i < sz; ++i) {
char c = buffer[i];
fprintf(stderr, "%d | %d\n", index, bits);
if (c == '0' && cursor -> zero == NULL) {
cursor -> zero = new_node(++index);
cursor = root;
if ((1 << bits) & index) {
++bits;
}
int j;
for (j = bits; j >= 0; --j) {
fputc('0' + ((index >> j) & 1), ofp);
//fprintf(stderr, "%c", '0' + (index >> j) & 1);
}
fputc(buffer[++i], ofp);
} else if (c == '1' && cursor -> one == NULL) {
cursor -> one = new_node(++index);
cursor = root;
if ((1 << bits) & index) {
++bits;
}
int j;
for (j = 8; j >= 0; --j) {
fputc('0' + ((index >> j) & 1), ofp);
//fprintf(stderr, "%c", '0' + (index >> j) & 1);
}
fputc(buffer[++i], ofp);
} else if (c == '0') {
cursor = cursor -> zero;
} else if (c == '1') {
cursor = cursor -> one;
} else {
fprintf(stderr, "Invalid character: %c\n", c);
exit(1);
}
}
print_tree(root);
printf("\n");
}
void print_tree(node * v) {
printf("%d -> (", v -> index);
if (v -> zero != NULL) {
print_tree(v -> zero);
} else {
printf("NULL");
}
printf(" | ");
if (v -> one != NULL) {
print_tree(v -> one);
} else {
printf("NULL");
}
printf(")");
}

1
out.jabber
File diff suppressed because it is too large
View File

31
run.sh

@ -0,0 +1,31 @@
#!/bin/sh
mkdir -p text tino
if [ -z "`ls tino`" ]; then
cd tino
wget https://afsweb.clarkson.edu/class/cs241/as2/hollow.txt
wget https://afsweb.clarkson.edu/class/cs241/as2/bit-hollow.txt
wget https://afsweb.clarkson.edu/class/cs241/as2/zap-hollow.txt
wget https://afsweb.clarkson.edu/class/cs241/as2/kapow-hollow.txt
wget https://afsweb.clarkson.edu/class/cs241/as2/out.txt
cd ..
fi
[ -z "`ls text/hollow.txt`" ] && cp tino/hollow.txt text/
bin/atob text/hollow.txt text/bit-hollow.txt
bin/jabber text/bit-hollow.txt text/zap-hollow.txt
bin/wocky text/zap-hollow.txt text/kapow-hollow.txt
bin/btoa text/kapow-hollow.txt text/out.txt
echo "Diff bit-hollow"
[ -n "`diff text/bit-hollow.txt tino/bit-hollow.txt`" ] && echo "Files don't match"
echo "Diff zap-hollow"
[ -n "`diff text/zap-hollow.txt tino/zap-hollow.txt`" ] && echo "Files don't match"
echo "Diff kapow-hollow"
[ -n "`diff text/kapow-hollow.txt tino/kapow-hollow.txt`" ] && echo "Files don't match"
echo "Diff out"
if [ -n "`diff text/out.txt tino/out.txt`" ]; then
echo "Files don't match"
else
echo "Outputs are the same! :D"
fi
Loading…
Cancel
Save