@@ -17,7 +17,17 @@ static inline void on_error_encountered(file_mem *mem, int err, void *caller) {
1717 fprintf (stderr , "%s\n" , strerror (err ));
1818}
1919
20- static inline void on_eof_reached (file_mem * mem ) {
20+ static inline void op_postprocessor (file_mem * mem , void * caller ) {
21+ close (mem -> fd );
22+
23+ if (& read_into_mem != caller ) {
24+ return ;
25+ }
26+ free (mem -> buffer );
27+ mem -> buffer = NULL ;
28+ }
29+
30+ static inline void on_eof_reached (file_mem * mem , void * caller ) {
2131 // -------- Start the polling block --------
2232 uint8_t __predicate = mem -> bytes_processed == 0 ;
2333 if (__predicate ) {
@@ -27,19 +37,15 @@ static inline void on_eof_reached(file_mem *mem) {
2737 }
2838 // -------- End of the polling block --------
2939 fprintf (stdout , "Read process = %s\n" , mem -> buffer );
30- free (mem -> buffer );
31- mem -> buffer = NULL ;
3240 mem -> __continue_after_eof = 0 ;
3341
34- close (mem -> fd );
3542}
3643
37- static inline void on_eob_reached (file_mem * mem ) {
44+ static inline void on_eob_reached (file_mem * mem , void * caller ) {
3845 fprintf (stdout , "Write process = %s" , mem -> buffer );
39- close (mem -> fd );
4046}
4147
42- static inline void on_last_char_sampled (file_mem * mem , void * caller ) {
48+ static inline void on_last_byte_sampled (file_mem * mem , void * caller ) {
4349 mem -> n_bytes += mem -> n_bytes ;
4450 mem -> buffer = realloc (mem -> buffer , sizeof (char ) * (mem -> n_bytes ));
4551}
@@ -49,19 +55,22 @@ static inline void on_bytes_processed(file_mem *mem, ssize_t bytes,
4955}
5056
5157static inline void writing_process (pipe_serializer * serializer ) {
58+ fprintf (stdout , "%s\n" , "Started the writing process..." );
5259
5360 serializer -> write_end .buffer = "Hello From the Electrostatic-Sandbox SDK Pipe Serializer...\n" ;
5461 serializer -> write_end .n_bytes = strlen (serializer -> write_end .buffer ) + 1 ;
5562
5663 sleep (10 ); // simulate jobs by sleeping the process
5764
58- write_from_mem (& serializer -> write_end , & (write_op_processor ) {
59- .on_eob_reached = & on_eob_reached ,
60- .on_bytes_processed = & on_bytes_processed
65+ write_from_mem (& serializer -> write_end , & (op_processor ) {
66+ .on_trailing_char_sampled = & on_eob_reached ,
67+ .on_bytes_processed = & on_bytes_processed ,
68+ .op_postprocessor = & op_postprocessor
6169 }, NULL );
6270}
6371
6472static inline void reading_process (pipe_serializer * serializer ) {
73+ fprintf (stdout , "%s\n" , "Started the reading process..." );
6574
6675 serializer -> read_end .n_bytes = 2 ;
6776 serializer -> read_end .buffer = calloc (serializer -> read_end .n_bytes , sizeof (char ));
@@ -71,11 +80,12 @@ static inline void reading_process(pipe_serializer *serializer) {
7180 exit (errno );
7281 }
7382
74- status_code __status = read_into_mem (& serializer -> read_end , & (read_op_processor ) {
75- .on_eof_reached = & on_eof_reached ,
76- .on_last_char_sampled = & on_last_char_sampled ,
83+ status_code __status = read_into_mem (& serializer -> read_end , & (op_processor ) {
84+ .on_trailing_char_sampled = & on_eof_reached ,
85+ .on_last_byte_sampled = & on_last_byte_sampled ,
7786 .on_bytes_processed = & on_bytes_processed ,
78- .on_error_encountered = & on_error_encountered
87+ .on_error_encountered = & on_error_encountered ,
88+ .op_postprocessor = & op_postprocessor
7989 }, NULL );
8090
8191 if (PASS != __status ) {
@@ -88,21 +98,20 @@ static inline void serializer_init_preprocessor(pipe_serializer *serializer) {
8898 // preprocessing check the file and remove it if it exists
8999 // for the test and the showcase
90100
101+ serializer -> write_end .fd = open ("/home/pavl-g/Desktop/test.txt" , O_RDWR | O_CREAT );
102+ serializer -> read_end .fd = open ("/home/pavl-g/Desktop/test.txt" , O_RDONLY );
91103}
92104
93105static inline void serializer_init_postprocessor (pipe_serializer * serializer ) {
94106 // do the read/write testing here!
95- serializer -> write_end .fd = open ("/home/pavl-g/Desktop/test.txt" , O_RDWR | O_CREAT );
96- serializer -> read_end .fd = open ("/home/pavl-g/Desktop/test.txt" , O_RDONLY );
97-
98107 // start another process
99108 pid_t pid = fork ();
100109 if (pid > 0 ) { // parent process
101110 writing_process (serializer );
102111 } else if (pid == 0 ) { // subprocess
103112 reading_process (serializer );
104113 } else {
105-
114+ exit ( pid );
106115 }
107116}
108117
@@ -120,7 +129,8 @@ int main() {
120129 .fd = -1 ,
121130 .trailing = '\0' ,
122131 .buffer = NULL ,
123- .n_bytes = -1
132+ .n_bytes = -1 ,
133+ .__auto_update_attrs = 0
124134 }
125135 };
126136
@@ -134,7 +144,7 @@ int main() {
134144
135145 status_code __status_code = init_serializer (& serializer , & _processor , & __processor );
136146 if (PASS != __status_code ) {
137- return __status_code ;
147+ fprintf ( stderr , "%s\n" , strerror ( __status_code )) ;
138148 }
139149
140150 return 0 ;
0 commit comments