Skip to content

Commit

Permalink
New fetish: obedience
Browse files Browse the repository at this point in the history
  • Loading branch information
Property404 committed Aug 20, 2017
1 parent 2c5433a commit 76f745b
Show file tree
Hide file tree
Showing 18 changed files with 257 additions and 1,210 deletions.
3 changes: 3 additions & 0 deletions docs/c_reference/core.md
Expand Up @@ -17,6 +17,7 @@
`public void `[`read_file_to_chain`](#chain_8h_1a008ca35b36cfa01cebd59a6dddd79671)`(`[`Chain`](#struct_chain)` * chain,`[`Chain`](#struct_chain)` filename)` | Set chain contents to file's contents
`public int `[`compare_chains`](#chain_8h_1aebea70af9ccbc3d25da67b8f836dba3d)`(`[`Chain`](#struct_chain)` a,`[`Chain`](#struct_chain)` b)` | Compare chains
`public `[`Fraction`](#struct_fraction)` `[`chain_to_fraction`](#chain_8h_1ae351d3cf295fd95456e64cb1bb9216da)`(`[`Chain`](#struct_chain)` chain)` | Convert chain to fraction
`public FILE * `[`open_file_as_stream`](#chain_8h_1a4bea31562c5e8ea9a9dbaece1203119f)`(`[`Chain`](#struct_chain)` filename,const char * mode)` |
`public void `[`runtime_error`](#error_8h_1aa4581c050961a458136ec8bdd556d5b5)`(const char * msg,...)` | Show error message and exit
`public `[`Fraction`](#struct_fraction)` `[`construct_fraction`](#fraction_8h_1affd36cedc3799420c2d6ddfbdbd81ddf)`(FractionInt num,FractionInt den)` | Construct [Fraction](#struct_fraction)
`public `[`Fraction`](#struct_fraction)` `[`random_fraction`](#fraction_8h_1aff0108be8808ee831aaac374030930b4)`()` | Get random fraction
Expand Down Expand Up @@ -196,6 +197,8 @@ Parse chain to convert to a fraction value. Return 0/1 if chain is invalid.
#### Returns
The fraction value of the chain

#### `public FILE * `[`open_file_as_stream`](#chain_8h_1a4bea31562c5e8ea9a9dbaece1203119f)`(`[`Chain`](#struct_chain)` filename,const char * mode)`

#### `public void `[`runtime_error`](#error_8h_1aa4581c050961a458136ec8bdd556d5b5)`(const char * msg,...)`

Show error message and exit
Expand Down
35 changes: 35 additions & 0 deletions docs/c_reference/obedience.md
@@ -0,0 +1,35 @@
# Summary

Members | Descriptions
--------------------------------|---------------------------------------------
`public void `[`write_chain_to_file`](#fileio_8h_1a897accbfb97acae9ff091b3dc13921c2)`(Chain chain,Chain filename)` | Set file contents to chain's contents
`public void `[`read_file_to_chain`](#fileio_8h_1a008ca35b36cfa01cebd59a6dddd79671)`(Chain * chain,Chain filename)` | Set chain contents to file's contents
`public FILE * `[`open_file_as_stream`](#fileio_8h_1a4bea31562c5e8ea9a9dbaece1203119f)`(Chain filename,const char * mode)` |

## Members

#### `public void `[`write_chain_to_file`](#fileio_8h_1a897accbfb97acae9ff091b3dc13921c2)`(Chain chain,Chain filename)`

Set file contents to chain's contents

Open a file and set its contents to that of the chain's. As with other functions, each fraction is converted to a char. An error will be thrown upon any problem opening the file

#### Parameters
* `chain` The chain being read

* `filename` A chain holding the filename of the file we're opening

#### `public void `[`read_file_to_chain`](#fileio_8h_1a008ca35b36cfa01cebd59a6dddd79671)`(Chain * chain,Chain filename)`

Set chain contents to file's contents

Open a file and read its contents to the chain's, replacing any previous values(an assign, not append). As with other functions, each char is converted to a fraction. An error will be thrown upon any problem opening the file

#### Parameters
* `chain` The chain being overwritten

* `filename` A chain holding the filename of the file we're opening

#### `public FILE * `[`open_file_as_stream`](#fileio_8h_1a4bea31562c5e8ea9a9dbaece1203119f)`(Chain filename,const char * mode)`

Generated by [Moxygen](https://sourcey.com/moxygen)
4 changes: 4 additions & 0 deletions docs/reference.md
Expand Up @@ -7,3 +7,7 @@ Core - The included-by-default fetish that defines the language
[C Reference](c_reference/core.md)
[Fetlang Reference](reference/core.md)

Obedience - File interaction
[C Reference](c_reference/obedience.md)
[Fetlang Reference](reference/obedience.md)

16 changes: 0 additions & 16 deletions docs/reference/core.md
Expand Up @@ -113,22 +113,6 @@ C Code:
/* chain/fraction overload */
append_flink_to_chain(&LHO, RHO)

### fist
Read a file to a chain, given a filename

C Code:

/* chain/chain overload */
read_file_to_chain(&LHO, RHO);

### obey
Write a chain to file, given a filename

C Code:

/* chain/chain overload */
write_chain_to_file(LHO, RHO)

## Comparison Operators
### is
Return true if LHO==RHO, else return false
Expand Down
27 changes: 27 additions & 0 deletions docs/reference/obedience.md
@@ -0,0 +1,27 @@
# Obedience
File handling
## Operators
### demand obedience of
Read a file to a chain, given a filename

C Code:

/* chain/chain overload */
read_file_to_chain(&LHO, RHO);

### obey
Write a chain to file, given a filename

C Code:

/* chain/chain overload */
write_chain_to_file(LHO, RHO)

### serve
Set LHO as a stream pointing to a file, given a filename

C Code:

/* stream/chain overload */
LHO = open_file_as_stream(RHO, "r+b")

27 changes: 0 additions & 27 deletions fetishes/core/fetish.json
Expand Up @@ -95,33 +95,6 @@
"chain/fraction": "append_flink_to_chain(&LHO, RHO)"
}
},
{
"name": "demand obedience of",
"description": "Read a file to a chain, given a filename",
"grammars": ["have"],
"code":
{
"chain/chain": "read_file_to_chain(&LHO, RHO);"
}
},
{
"name": "obey",
"description": "Write a chain to file, given a filename",
"grammars": ["have"],
"code":
{
"chain/chain": "write_chain_to_file(LHO, RHO)"
}
},
{
"name": "serve",
"description": "Set LHO as a stream pointing to a file, given a filename",
"grammars": ["make"],
"code":
{
"stream/chain": "LHO = open_file_as_stream(RHO, \"r+b\")"
}
},
{
"name": "to",
"grammars": ["bind"],
Expand Down
91 changes: 0 additions & 91 deletions fetishes/core/source/chain.c
Expand Up @@ -427,74 +427,6 @@ Fraction chain_to_fraction(Chain chain)
}


void write_chain_to_file(Chain chain, Chain filename_as_chain)
{
// Convert chain to char*
const int max_filename_size = filename_as_chain.length;
char * filename = (char*)malloc(sizeof(char*) * (max_filename_size + 1));
if(filename == NULL)
{
runtime_error("Couldn't allocate memory for filename buffer in write_chain_to_file");
}
memset(filename, 0x00, max_filename_size);
const int size = chain_to_cstr(filename_as_chain, filename);
if(size > max_filename_size)
{
runtime_error("Size is ridiculous is write_chain_to_file");
}

// Open the file for writing
FILE* fp = fopen(filename, "wb");
if(fp == NULL)
{
runtime_error("Could not open file %s for writing", filename);
}

// Write
Link * it = chain.start;
while(it != NULL)
{
fputc((char)(it->value.num/it->value.den), fp);
it = it->next;
}

fclose(fp);
}
void read_file_to_chain(Chain * chain, Chain filename_as_chain)
{
// Convert chain to char*
const int max_filename_size = filename_as_chain.length;
char * filename = (char*)malloc(sizeof(char*) * (max_filename_size + 1));
if(filename == NULL)
{
runtime_error("Couldn't allocate memory for filename buffer in read_chain_to_file");
}
memset(filename, 0x00, max_filename_size);
const int size = chain_to_cstr(filename_as_chain, filename);
if(size > max_filename_size)
{
runtime_error("Size is ridiculous is read_file_to_chain");
}

// Open the file for reading
FILE* fp = fopen(filename, "rb");
if(fp == NULL)
{
runtime_error("Could not open file %s for reading", filename);
}

// Read
while(1)
{
char c = (char)fgetc(fp);
if(c == EOF) break;
append_flink_to_chain(chain, construct_fraction(c, 1));
}

fclose(fp);
return;
}

int compare_chains(Chain a, Chain b)
{
Link *a_iterator = a.start;
Expand Down Expand Up @@ -545,26 +477,3 @@ void print_chain_numerically(Chain chain)
}
*/

FILE* open_file_as_stream(Chain filename, const char* mode)
{
int max_size = sizeof(char)*(filename.length+1);
char* buffer = (char*)malloc(max_size);
if(filename.length == 0 || buffer == NULL)
{
runtime_error("Could not allocate buffer for file");
}

int actual_size = chain_to_cstr(filename, buffer);
if(actual_size > filename.length || actual_size <= 0)
{
runtime_error("Something's terribly wrong in open_file_as_stream");
}

FILE* fp = fopen(buffer, mode);
if(fp == NULL){
perror("perror result: ");
runtime_error("Could not open file %s", buffer);
}
return fp;
}

30 changes: 0 additions & 30 deletions fetishes/core/tests/chain.cpp
Expand Up @@ -23,36 +23,6 @@ TEST_CASE("Chain Test", "[chain]"){
delete[] buffer;
}

SECTION("FILE IO")
{
std::string filename = "/tmp/boop2.txt";
FileUtil::setFileContents(filename, sample_text4);
REQUIRE(FileUtil::getFileContents(filename) == sample_text4);

Chain filename_as_chain;
init_chain(&filename_as_chain);
append_cstr_to_chain(&filename_as_chain, filename.c_str());

Chain chain;
init_chain(&chain);
REQUIRE(chain.length == 0);

FILE* stream = open_file_as_stream(filename_as_chain, "r+b");
append_stream_to_chain(&chain, stream);
REQUIRE(chain.length == strlen(sample_text4));
clear_chain(&chain);
append_cstr_to_chain(&chain, sample_text);
append_chain_to_stream(chain, stream);
clear_chain(&chain);
append_stream_to_chain(&chain, stream);
REQUIRE(chain.length== strlen(sample_text)+strlen(sample_text4));

fclose(stream);
clear_chain(&chain);
clear_chain(&filename_as_chain);

}

SECTION("Chain/stream test"){
std::string filename = "/tmp/bla.txt";
FILE* stream = NULL;
Expand Down
19 changes: 0 additions & 19 deletions fetishes/core/unit.fet
Expand Up @@ -89,25 +89,6 @@ bind reference to chain
have reference assert equality with 2
lick count

(file handling)
make hello moan "That's *burp* right\nthey blew up\n"
make filename moan "test.text"
have filename obey hello
have filename demand obedience of contents
have contents assert equality with hello

(Stream file handling)
make fp serve "test.text"
make slave scream fp
have fp assert equality with hello
make goodbye moan "I'm gonna tell mama"
make fp moan goodbye
have fp assert equality with goodbye
have hello tie up goodbye
have hello tie up fp
have fp assert equality with goodbye



(Some comparison)
make flappy moan
Expand Down

0 comments on commit 76f745b

Please sign in to comment.