Skip to content

Commit

Permalink
refactored stream write, wip #109
Browse files Browse the repository at this point in the history
move code to write to stream out of process.c by
adding generic tree to stream writing function _t_write
and _st_write
also adds LINES symbol for writing multiple lines at once
  • Loading branch information
zippy committed Feb 29, 2016
1 parent d5929c7 commit 2c650a1
Show file tree
Hide file tree
Showing 13 changed files with 151 additions and 36 deletions.
12 changes: 12 additions & 0 deletions doxy/sys_defs.html
Original file line number Diff line number Diff line change
Expand Up @@ -2369,6 +2369,18 @@ <h4>DEV_COMPOSITORY_CONTEXT</h4>
<div class="def-sym-def"><a href="ref_sys_structures.html#CSTRING">CSTRING</a></div>
<div class="def-comment"> text lines from unix streams</div>
</div>
<div class="def-item def-structure">
<div class="def-type">Structure:</div>
<div class="def-name"><a name="ZERO_OR_MORE_OF_LINE"></a>ZERO-OR-MORE-OF-LINE</div>
<div class="def-struc-def">*(<a href="ref_sys_symbols.html#LINE">LINE</a>)</div>
<div class="def-comment"></div>
</div>
<div class="def-item def-symbol">
<div class="def-type">Symbol:</div>
<div class="def-name"><a name="LINES"></a>LINES</div>
<div class="def-sym-def"><a href="ref_sys_structures.html#ZERO_OR_MORE_OF_LINE">ZERO-OR-MORE-OF-LINE</a></div>
<div class="def-comment"></div>
</div>
<div class="def-item def-symbol">
<div class="def-type">Symbol:</div>
<div class="def-name"><a name="VERB"></a>VERB</div>
Expand Down
1 change: 1 addition & 0 deletions doxy/sys_symbols.html
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@
<tr><td><a name="are_you"></a>are_you</td><td><a href="ref_sys_structures.html#SEMTREX">SEMTREX</a></td><td></td></tr>
<tr><td><a name="i_am"></a>i_am</td><td><a href="ref_sys_structures.html#RECEPTOR_IDENTITY">RECEPTOR-IDENTITY</a></td><td></td></tr>
<tr><td><a name="LINE"></a>LINE</td><td><a href="ref_sys_structures.html#CSTRING">CSTRING</a></td><td> text lines from unix streams</td></tr>
<tr><td><a name="LINES"></a>LINES</td><td><a href="ref_sys_structures.html#ZERO_OR_MORE_OF_LINE">ZERO-OR-MORE-OF-LINE</a></td><td></td></tr>
<tr><td><a name="VERB"></a>VERB</td><td><a href="ref_sys_structures.html#CSTRING">CSTRING</a></td><td></td></tr>
<tr><td><a name="COMMAND_PARAMETER"></a>COMMAND_PARAMETER</td><td><a href="ref_sys_structures.html#CSTRING">CSTRING</a></td><td></td></tr>
<tr><td><a name="SHELL_COMMAND"></a>SHELL_COMMAND</td><td><a href="ref_sys_structures.html#COMMAND">COMMAND</a></td><td></td></tr>
Expand Down
10 changes: 8 additions & 2 deletions spec/process_spec.h
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,9 @@ void testProcessStream() {
_t_new_stream(n,TEST_STREAM_SYMBOL,st);
_t_new_str(n,TEST_STR_SYMBOL,"fish\n");
_t_new_str(n,LINE,"cow");
T *lns = _t_newr(n,LINES);
_t_new_str(lns,LINE,"thing1");
_t_new_str(lns,LINE,"thing2");
_t_newi(n,TEST_INT_SYMBOL,314);

run_tree = __p_build_run_tree(n,0);
Expand All @@ -665,10 +668,13 @@ void testProcessStream() {

spec_is_equal(_p_reduceq(q),noReductionErr);

spec_is_str_equal(buffer,"line1\nabc\nfish\ncow\n(TEST_INT_SYMBOL:314)\n");
char *expected_result = "line1\nabc\nfish\ncow\nthing1\nthing2\n(TEST_INT_SYMBOL:314)";

spec_is_str_equal(buffer,expected_result);

_st_free(st);


// test writing to a readonly stream
stream = fmemopen(buffer, strlen (buffer), "r");
st = _st_new_unix_stream(stream,0);
Expand All @@ -686,7 +692,7 @@ void testProcessStream() {
spec_is_equal(e->context->err,unixErrnoReductionErr);

// buffer should remain unchanged
spec_is_str_equal(buffer,"line1\nabc\nfish\ncow\n(TEST_INT_SYMBOL:314)\n");
spec_is_str_equal(buffer,expected_result);

while ((fgetc (stream)) != EOF);
n = _t_new_root(STREAM_ALIVE);
Expand Down
27 changes: 24 additions & 3 deletions spec/stream_spec.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,24 @@ void testCallback(Stream *st) {
st->callback = 0;
}

void testStream() {

void testStreamCreate() {
// test basic stream creation
Stream *s = _st_new_unix_stream(stdout,0);
spec_is_equal(s->type,UnixStream);
spec_is_equal(s->flags,StreamCloseOnFree);
spec_is_ptr_equal(s->data.unix_stream,stdout);
s->flags &= ~StreamCloseOnFree; // don't close the stdin on free...
_st_free(s);
}

void testStreamRead() {
// debug_enable(D_STREAM);
// test reading from a stream
FILE *input;

char data[] = "line1\nline2\n";
input = fmemopen(data, strlen(data), "r");
s = _st_new_unix_stream(input,1);
Stream *s = _st_new_unix_stream(input,1);
spec_is_true(s->flags&StreamReader);
spec_is_true(s->flags&StreamWaiting);

Expand Down Expand Up @@ -65,3 +66,23 @@ void testStream() {
}
_st_free(s);
}

void testStreamWrite() {
char buffer[500] = "x";
FILE *stream;
stream = fmemopen(buffer, 500, "r+");
Stream *st = _st_new_unix_stream(stream,1);

spec_is_equal(_st_write(st,"fishy",6),6);

char *expected_result = "fishy";
spec_is_str_equal(buffer,expected_result);

_st_free(st);
}

void testStream() {
testStreamCreate();
testStreamRead();
testStreamWrite();
}
30 changes: 30 additions & 0 deletions spec/tree_spec.h
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,35 @@ void testTreeTemplate() {
//! [testTreeTemplate]
}

void testTreeStreamWrite() {
char buffer[500] = "x";
FILE *stream;
stream = fmemopen(buffer, 500, "r+");
Stream *st = _st_new_unix_stream(stream,1);
T *t = _t_new_str(0,TEST_STR_SYMBOL,"fish\n");
_t_write(G_sem,t,st);
_t_free(t);

t = _t_new_str(0,LINE,"cow");
_t_write(G_sem,t,st);
_t_free(t);

t = _t_new_root(LINES);
_t_new_str(t,LINE,"thing1");
_t_new_str(t,LINE,"thing2");
_t_write(G_sem,t,st);
_t_free(t);

t = _t_newi(0,TEST_INT_SYMBOL,314);
_t_write(G_sem,t,st);
_t_free(t);

char *expected_result = "fish\ncow\nthing1\nthing2\n(TEST_INT_SYMBOL:314)";
spec_is_str_equal(buffer,expected_result);

_st_free(st);
}

void testTree() {
testCreateTreeNodes();
testTreeNewReceptor();
Expand Down Expand Up @@ -675,4 +704,5 @@ void testTree() {
testProcessHTML();
testTreeBuild();
testTreeTemplate();
testTreeStreamWrite();
}
1 change: 1 addition & 0 deletions src/base_defs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ Protocol: RECOGNIZE,recog_def;

##### defs for shell receptor
Symbol: LINE,CSTRING; text lines from unix streams
Symbol: LINES,[*LINE];
Symbol: VERB,CSTRING;
Symbol: COMMAND_PARAMETER,CSTRING;
Structure: COMMAND,(VERB,*COMMAND_PARAMETER);
Expand Down
4 changes: 4 additions & 0 deletions src/base_defs.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,8 @@ SemanticID i_am={0,0,0};
SemanticID fill_i_am={0,0,0};
SemanticID RECOGNIZE={0,0,0};
SemanticID LINE={0,0,0};
SemanticID ZERO_OR_MORE_OF_LINE={0,0,0};
SemanticID LINES={0,0,0};
SemanticID VERB={0,0,0};
SemanticID COMMAND_PARAMETER={0,0,0};
SemanticID COMMAND={0,0,0};
Expand Down Expand Up @@ -960,6 +962,8 @@ void base_defs(SemTable *sem) {
sData(DEV_COMPOSITORY_CONTEXT,recog_def,STX_OP,PROTOCOL_DEFINITION,STX_OP,PROTOCOL_SEMANTICS,STX_CP,STX_OP,INCLUSION,STX_OP,PNAME,REQUESTING,STX_CP,STX_OP,CONNECTION,STX_OP,WHICH_ROLE,STX_OP,ROLE,REQUESTER,STX_CP,STX_OP,ROLE,RECOGNIZER,STX_CP,STX_CP,STX_CP,STX_OP,CONNECTION,STX_OP,WHICH_ROLE,STX_OP,ROLE,RESPONDER,STX_CP,STX_OP,ROLE,RECOGNIZEE,STX_CP,STX_CP,STX_CP,STX_OP,CONNECTION,STX_OP,WHICH_GOAL,STX_OP,GOAL,REQUEST_HANDLER,STX_CP,STX_OP,GOAL,RECOGNITION,STX_CP,STX_CP,STX_CP,STX_OP,RESOLUTION,STX_OP,WHICH_SYMBOL,STX_OP,USAGE,REQUEST_DATA,STX_CP,STX_OP,ACTUAL_SYMBOL,are_you,STX_CP,STX_CP,STX_CP,STX_OP,RESOLUTION,STX_OP,WHICH_SYMBOL,STX_OP,USAGE,RESPONSE_DATA,STX_CP,STX_OP,ACTUAL_SYMBOL,i_am,STX_CP,STX_CP,STX_CP,STX_OP,RESOLUTION,STX_OP,WHICH_SYMBOL,STX_OP,USAGE,CHANNEL,STX_CP,STX_OP,ACTUAL_SYMBOL,DEFAULT_ASPECT,STX_CP,STX_CP,STX_CP,STX_OP,RESOLUTION,STX_OP,WHICH_PROCESS,STX_OP,GOAL,RESPONSE_HANDLER,STX_CP,STX_OP,ACTUAL_PROCESS,fill_i_am,STX_CP,STX_CP,STX_CP,STX_CP,STX_CP);
sProt(DEV_COMPOSITORY_CONTEXT,RECOGNIZE,recog_def);
sY(DEV_COMPOSITORY_CONTEXT,LINE,CSTRING);
sTs(DEV_COMPOSITORY_CONTEXT,ZERO_OR_MORE_OF_LINE,sT_STAR(sT_SYM(LINE)));
sY(DEV_COMPOSITORY_CONTEXT,LINES,ZERO_OR_MORE_OF_LINE);
sY(DEV_COMPOSITORY_CONTEXT,VERB,CSTRING);
sY(DEV_COMPOSITORY_CONTEXT,COMMAND_PARAMETER,CSTRING);
sTs(DEV_COMPOSITORY_CONTEXT,COMMAND,sT_SEQ(2,sT_SYM(VERB),sT_STAR(sT_SYM(COMMAND_PARAMETER))));
Expand Down
4 changes: 4 additions & 0 deletions src/base_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,7 @@ enum DEV_COMPOSITORYSymbolIDs {
are_you_ID,
i_am_ID,
LINE_ID,
LINES_ID,
VERB_ID,
COMMAND_PARAMETER_ID,
SHELL_COMMAND_ID,
Expand Down Expand Up @@ -867,6 +868,7 @@ SemanticID RECOGNITION;
SemanticID are_you;
SemanticID i_am;
SemanticID LINE;
SemanticID LINES;
SemanticID VERB;
SemanticID COMMAND_PARAMETER;
SemanticID SHELL_COMMAND;
Expand All @@ -890,9 +892,11 @@ SemanticID MESSAGE;
// DEV_COMPOSITORY:Structure
enum DEV_COMPOSITORYStructureIDs {
NULL_DEV_COMPOSITORY_STRUCTURE_ID,
ZERO_OR_MORE_OF_LINE_ID,
COMMAND_ID,
NUM_DEV_COMPOSITORY_STRUCTURES
};
SemanticID ZERO_OR_MORE_OF_LINE;
SemanticID COMMAND;

/**********************************************************************************/
Expand Down
33 changes: 2 additions & 31 deletions src/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -535,42 +535,13 @@ Error __p_reduce_sys_proc(R *context,Symbol s,T *code,Q *q) {
// get the stream param
T *s = _t_detach_by_idx(code,1);
Stream *st = _t_surface(s);
if (st->type != UnixStream) raise_error("unknown stream type:%d\n",st->type);
FILE *stream = st->data.unix_stream;
_t_free(s);
// get the data to write as string
while(s = _t_detach_by_idx(code,1)) {
/// @todo check the structure type to make sure it's compatible as a string (i.e. it's null terminated)
/// @todo other integrity checks, i.e. length etc?
char *str;
Symbol sym = _t_symbol(s);
Structure struc = _sem_get_symbol_structure(q->r->sem,sym);
int add_nl = 1;
if (semeq(struc,CSTRING)) {
str = _t_surface(s);
debug(D_STREAM,"found a cstring to write: %s\n",str);
if (!semeq(sym,LINE)) add_nl = 0;
}
else {
str = _t2s(q->r->sem,s);
}
//str = t2s(s);
int err = fputs(str,stream);
if (err < 0) {
debug(D_STREAM,"unix error %d on attempting to write: %s\n",errno,str);
_t_free(s);
return unixErrnoReductionErr;
}
else {
debug(D_STREAM,"just wrote: %s\n",str);
}
int err = _t_write(q->r->sem,s,st);
_t_free(s);
if (add_nl) {
err = fputs("\n",stream);
if (err < 0) return unixErrnoReductionErr;
}
if (err == 0) return unixErrnoReductionErr;
}
fflush(stream);
/// @todo what should this really return?
x = __t_news(0,REDUCTION_ERROR_SYMBOL,NULL_SYMBOL,1);
}
Expand Down
16 changes: 16 additions & 0 deletions src/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,19 @@ void _st_free(Stream *stream) {
}
free(stream);
}

/**
* write to a stream
*
*/
int _st_write(Stream *st,char *buf,size_t len) {
if (st->type != UnixStream) raise_error("unknown stream type:%d\n",st->type);
FILE *stream = st->data.unix_stream;

int err = fwrite(buf,sizeof(char),len,stream);
debug(D_STREAM,"write of '%s' results in %d\n",buf,err);
if (err > 0) {
fflush(stream);
}
return err;
}
2 changes: 2 additions & 0 deletions src/stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,7 @@ void _st_free(Stream *);
#define _st_data(st) (st)->buf
#define _st_data_size(st) (st)->data_size

int _st_write(Stream *stream,char *buf,size_t len);

#endif
/** @}*/
45 changes: 45 additions & 0 deletions src/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -1738,4 +1738,49 @@ char * _t2json(SemTable *sem,T *t,int level,char *buf) {
return result;
}

// assumes that t is a CSTRING structured tree
int __t_writeln(T *t,Stream *stream) {
int err;
char *str = _t_surface(t);
err = _st_write(stream,str,strlen(str));
if (err > 0) {
err = _st_write(stream,"\n",1);
}
return err;
}

/**
* write a tree out to a stream
*
* @param[in] sem current semantic contexts
* @param[in] t the tree to write out
* @param[in] stream the stream to write to
* @returns unix error code, or number of bytes written
*/
int _t_write(SemTable *sem,T *t,Stream *stream) {
int err;
Symbol sym = _t_symbol(t);
if (semeq(sym,LINE)) {
err = __t_writeln(t,stream);
}
else if (semeq(sym,LINES)) {
DO_KIDS(t,
err = __t_writeln(_t_child(t,i),stream);
if (err == 0) return 0;
);
}
else {
char *str;
Structure struc = _sem_get_symbol_structure(sem,sym);
if (semeq(struc,CSTRING)) {
str = _t_surface(t);
}
else {
str = _t2s(sem,t);
}
err = _st_write(stream,str,strlen(str));
}
return err;
}

/** @}*/
2 changes: 2 additions & 0 deletions src/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ char * _t2json(SemTable *sem,T *t,int level,char *buf);

/***************** Misc... */

int _t_write(SemTable *sem,T *t,Stream *stream);

#define DO_KIDS(t,x) {int i,_c=_t_children(t);for(i=1;i<=_c;i++){x;}}

#define root_check(c) if (c->structure.parent != 0) {raise_error("can't add a node that isn't a root!");}
Expand Down

0 comments on commit 2c650a1

Please sign in to comment.