Browse Source

Make atob and btoa. Tree not tested

master
stew3254 2 years ago
commit
aed1a144ef
  1. 6
      .gitignore
  2. 57
      atob.c
  3. 55
      btoa.c
  4. 51
      tree.c
  5. 17
      tree.h

6
.gitignore

@ -0,0 +1,6 @@
atob
btoa
jabber
wocky
Session.vim
test*

57
atob.c

@ -0,0 +1,57 @@
#include "assert.h"
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "unistd.h"
//Print binary
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) {
assert(argc == 3);
struct stat sbuf = {};
if (stat(argv[1], &sbuf) != 0) {
fprintf(stderr, "Error finding file\n");
return -1;
}
//Assume we can read the file
char *buff = (char*) malloc(sbuf.st_size * sizeof(char));
FILE *f = fopen(argv[1], "r");
if (f == NULL) {
fprintf(stderr, "Error opening input file\n");
return -1;
}
if (fread(buff, sizeof(char), sbuf.st_size, f) != sbuf.st_size) {
fclose(f);
fprintf(stderr, "Error reading file\n");
return -1;
}
//Close file when done reading
fclose(f);
f = fopen(argv[2], "w");
if (f == NULL) {
fprintf(stderr, "Error opening output file\n");
return -1;
}
printb(f, buff, sbuf.st_size);
return 0;
}

55
btoa.c

@ -0,0 +1,55 @@
#include "assert.h"
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "unistd.h"
//Print binary
void printa(FILE *f, const char *s, const int len) {
char *out = (char*) calloc(len/8 + 1, sizeof(char));
for (int i = 0; i < len/8; ++i) {
for (int j = 7; j > -1; --j) {
out[i] += (s[8*i + (7-j)] - '0') << j;
}
}
fprintf(f, "%s", out);
free(out);
}
int main (int argc, char **argv) {
assert(argc == 3);
struct stat sbuf = {};
if (stat(argv[1], &sbuf) != 0) {
fprintf(stderr, "Error finding file\n");
return -1;
}
//Assume we can read the file
char *buff = (char*) malloc(sbuf.st_size * sizeof(char));
FILE *f = fopen(argv[1], "r");
if (f == NULL) {
fprintf(stderr, "Error opening input file\n");
return -1;
}
if (fread(buff, sizeof(char), sbuf.st_size, f) != sbuf.st_size) {
fclose(f);
fprintf(stderr, "Error reading file\n");
return -1;
}
//Close file when done reading
fclose(f);
f = fopen(argv[2], "w");
if (f == NULL) {
fprintf(stderr, "Error opening output file\n");
return -1;
}
printa(f, buff, sbuf.st_size);
return 0;
}

51
tree.c

@ -0,0 +1,51 @@
#include "stdbool.h"
#include "string.h"
#include "tree.h"
static inline int isleaf(node *n) {return (n->left == NULL && n->right == NULL);}
node *newn(char bit, int index) {
node *n = (node*) malloc(sizeof(node));
n->left = NULL;
n->right = NULL;
n->bit = bit;
n->index = index;
}
int tadd(tree *t, char *tag, int index) {
node *current = t->root, *prev = NULL;
int i;
for (i = 0; i < strlen(tag); ++i) {
if (current == NULL) {
++t->size;
if (t->root == NULL) {
t->root = newn(tag[i], 1);
return t-size;
}
else {
if (tag[i] == '0')
prev->left = newn(tag[i], t->size);
else
prev->right = newn(tag[i], t->size);
}
}
else if (tag[i] == '0') {
prev = current;
current = current->left;
}
else {
prev = current;
current = current->right;
}
}
if (current == NULL) {
if (tag[i] == '0')
prev->left = newn(tag[i], t->size);
else
prev->right = newn(tag[i], t->size);
return i;
}
else {
return -1;
}
}

17
tree.h

@ -0,0 +1,17 @@
typedef struct Node {
struct Node *left;
struct Node *right;
char bit;
int index;
} node;
typedef struct Tree {
node *root;
int size;
} tree;
char *tadd(tree *t, char *tag, int index);
static inline tree *newt() {return (tree*) calloc(1, sizeof(tree))}
static inline tree *tisempty(tree *t) {return (t->root->left == NULL && t->root->right == NULL);}
static inline tree *tsize(tree *t) {return t->root->size;}
Loading…
Cancel
Save