Skip to content

Commit

Permalink
split into multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
klmanion committed Apr 29, 2018
1 parent 4a040d6 commit 55d9645
Show file tree
Hide file tree
Showing 12 changed files with 399 additions and 278 deletions.
1 change: 1 addition & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.out
coded.txt
input.txt
*.gch
85 changes: 85 additions & 0 deletions src/bitconv.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//bitconv.c
//

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

#include "bitconv.h"

/* calculates how many bits the array resulting from stobin will have
* makeing sure that it has padding bits to make it a multiple of 6 */
size_t
stobin_size(
const char *s)
{
size_t l;
l = (strlen(s))*8;
//add padding bits
if (l%6 != 0)
l += 6 - (l%6);
return l;
}

/* converts an array of chars to an array of 1s and 0s
* skips the ending newline
* returned value must be freed */
uint8_t*
stobin(
const char *s)
{
uint8_t *a;
size_t a_len;
static const int mask[] = { 0x1, 0x2, 0x4, 0x8,
0x10, 0x20, 0x40, 0x80 };
a_len = stobin_size(s);
a = (uint8_t *)malloc(a_len * sizeof(uint8_t));
memset(a, 0, a_len);
for(size_t i=0,j=0,len=strlen(s); i<len; ++i) {
//for each character to be encoded
for(int k=7; k>=0; --k,++j) {
//for each bit in that character
a[j] = (s[i] & mask[k]) >> k;
}
}
return a;
}

/* takes first 6 elements from uint8_t array and considering them as bits
* converts them to their integer equivalent */
uint8_t
bitstoc(
uint8_t *a)
{
uint8_t acc=0;
for(int i=0; i<6; ++i)
{
acc += a[i] << ((6-1)-i);
}
return acc;
}

/*
* reads at least 45 characters from the file
* seeking forward
* !!! this dose not capture the newline
* returns EOF on EOF
* returns 0 otherwise
*/
int
readline(
char **line,
FILE *fd)
{
char *x;
*line = (char *)malloc(47*sizeof(char));
(void)fgets(*line, 46, fd);
//strip newline
x = strchr(*line, '\n');
if (x != NULL)
*x = '\0';
if (feof(fd)) {
return EOF;
} else {
return 0;
}
}
19 changes: 19 additions & 0 deletions src/bitconv.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//bitconv.h
//

#ifndef __BITCONV_H__
#define __BITCONV_H__

#include <stdlib.h>

__BEGIN_DECLS
size_t stobin_size __P((const char *));
uint8_t* stobin __P((const char *));
uint8_t bitstoc __P((uint8_t *));

int readline __P((char **,FILE *));
__END_DECLS

#endif /* !__BITCONV_H__ */

/* vim: set ts=4 sw=4 noexpandtab tw=79: */
17 changes: 17 additions & 0 deletions src/cat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//cat.c
//

#include <stdio.h>

#include "cat.h"

int
cat(
const char *file)
{
char cmd[80];
snprintf(cmd, 79, "cat %s", file);
return system(cmd);
}

/* vim: set ts=4 sw=4 noexpandtab tw=79: */
15 changes: 15 additions & 0 deletions src/cat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//cat.h
//

#ifndef __CAT_H__
#define __CAT_H__

#include <stdlib.h>

__BEGIN_DECLS
int cat __P((const char *));
__END_DECLS

#endif /* !__CAT_H__ */

/* vim: set ts=4 sw=4 noexpandtab tw=79: */
18 changes: 18 additions & 0 deletions src/decode.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//decode.c
//

#include <stdio.h>

#include "decode.h"
#include "bitconv.h"

uint8_t
decode(
const char *restrict ifile)
{
printf("%s\n", "decode does nothing as of yet");
return 0; //stand-in
}

/* vim: set ts=4 sw=4 noexpandtab tw=79: */

15 changes: 15 additions & 0 deletions src/decode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//decode.h
//

#ifndef __DECODE_H__
#define __DECODE_H__

#include <stdint.h>

__BEGIN_DECLS
uint8_t decode __P((const char *restrict));
__END_DECLS

#endif /* !__DECODE_H__ */

/* vim: set ts=4 sw=4 noexpandtab tw=79: */
65 changes: 65 additions & 0 deletions src/enclist.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//enclist.c
//

#include <stdio.h>
#include <err.h>

#include "enclist.h"

/* debugging purposes only */
int __attribute__((unused))
enclist_length(
enclist_t *el)
{
if (el && el->cdr) {
return 1 + enclist_length(el->cdr);
} else if (el) {
return 1;
} else {
return 0;
}
}


pthread_t*
newthread(
enclist_t *el)
{
if (el && el->cdr != NULL) {
return newthread(el->cdr);
} else if (el) {
(*el).cdr = (enclist_t *)malloc(enclist_sz);
el->cdr->cdr = NULL;
return &el->cdr->thread;
} else {
warnx("%s\n", "newthread() called with null enclist_t");
return NULL;
}
}


void
writeenc(
enclist_t *el,
FILE * fd)
{
void *enc = NULL;
pthread_join(el->thread, &enc);
if (enc)
fprintf(fd, "%s", (char *)enc);
free(enc);
if (el->cdr)
writeenc(el->cdr, fd);
}

void*
freeenc(
enclist_t *el)
{
if (el->cdr)
el->cdr = freeenc(el->cdr);
free(el);
return el = NULL;
}

/* vim: set ts=4 sw=4 noexpandtab tw=79: */
28 changes: 28 additions & 0 deletions src/enclist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//enclist.h
//

#ifndef __ENCLIST_H__
#define __ENCLIST_H__

#include <stdlib.h>
#include <pthread.h>

/*
* linked-list structure to dynamically hold encoded lines
*/
typedef
struct _enclist {
pthread_t thread;
struct _enclist *cdr;
} enclist_t;
#define enclist_sz (sizeof(enclist_t))

__BEGIN_DECLS
pthread_t* newthread __P((enclist_t *));
void writeenc __P((enclist_t *,FILE *));
void* freeenc __P((enclist_t *));
__END_DECLS

#endif /* !__ENCLIST_H__ */

/* vim: set ts=4 sw=4 noexpandtab tw=79: */
111 changes: 111 additions & 0 deletions src/encode.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
//encode.c
//

#include <stdio.h>
#include <err.h>
#include <string.h>

#include "encode.h"
#include "bitconv.h"
#include "enclist.h"

void
header(
FILE * fd, //output file written to in encode funct
const char *perm, //octal UNIX permissions, usualy 644
const char *filename) //filename to be written to
{
fprintf(fd, "begin %s %s\n", perm, filename);
}

void
footer(
FILE *fd)
{
fprintf(fd, "`\nend\n");
}

/* power-house function that actualy converts text to binary
* this is now the thread function
* --> dose sprintf require the void ?
* return value needs to be freed
*/
void*
encode_line(
void *arg)
{
char *line = (char *)arg;
uint8_t *bin;
//the 45->60 chars and the length newline and null
char *enc = (char *)malloc(63*sizeof(char));

//encode the number of characters
sprintf(enc, "%c", (int)(strlen(line)+32));

//convert the string of chars into an array of bits
bin = stobin(line);//must be freed
for(size_t i=0,len=stobin_size(line); i<len; i+=6)
{
sprintf(enc, "%s%c", enc, bitstoc(&bin[i]) + 32);
}
sprintf(enc, "%s\n", enc);
free(bin);
return (void *)enc;
}

/* start a thread for each line
* storing the thread ids in order
* in a linked list
* then join them, writting their return value to f_write
* change the encode_line funct to only return an encoded version
* of line (having no aftereffects, since it no longer writes to the file
* itself
*/
uint8_t
encode(
const char *restrict ifile,
const char *restrict ofile)
{
FILE * f_read;
FILE * f_write;
char *line = NULL;

if (ifile == NULL) {
ifile = "stdin";
f_read = stdin;
} else {
f_read = fopen(ifile, "r");
}

if (ofile == NULL)
f_write = stdout;
else
f_write = fopen(ofile, "w");

if (!f_read)
errx(EBADIN, "%s\n", "could not open input_file");
if (!f_write)
errx(EBADOUT, "%s\n", "could not open output_file");

header(f_write, DEFAULT_PERM, ifile);

enclist_t *el = (enclist_t *)malloc(enclist_sz);
el->cdr = NULL;
while (readline(&line, f_read) != EOF)
{
pthread_create(newthread(el), NULL, &encode_line, (void *)line);

free(line);
}
writeenc(el, f_write);
el = freeenc(el);

//add footer
footer(f_write);

fclose(f_read);
fclose(f_write);
return 0; //stand-in
}

/* vim: set ts=4 sw=4 noexpandtab tw=79: */
Loading

0 comments on commit 55d9645

Please sign in to comment.