summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTimo Savinen <tjsa@iki.fi>2005-11-15 18:53:17 +0000
committerHadrien Dorio <hadrien.dorio@gmail.com>2017-12-16 00:24:06 +0100
commitdcefa7a144fb35b23d1ca339d58f3b9e58e7faa3 (patch)
tree733c9942e36b0c7ecaf7597036806d7f1d2a536a /src
parent63ae206f2970076a9d09e90d7fbcb17ec1fa26e4 (diff)
downloadbinary-block-editor-dcefa7a144fb35b23d1ca339d58f3b9e58e7faa3.tar.gz
binary-block-editor-dcefa7a144fb35b23d1ca339d58f3b9e58e7faa3.zip
0.1.8
Diffstat (limited to 'src')
-rw-r--r--src/bbe.c25
-rw-r--r--src/bbe.h10
-rw-r--r--src/buffer.c44
-rw-r--r--src/execute.c83
4 files changed, 105 insertions, 57 deletions
diff --git a/src/bbe.c b/src/bbe.c
index 5ba73f3..e0e10da 100644
--- a/src/bbe.c
+++ b/src/bbe.c
@@ -20,7 +20,7 @@
*
*/
-/* $Id: bbe.c,v 1.40 2005/11/01 19:01:16 timo Exp $ */
+/* $Id: bbe.c,v 1.42 2005/11/15 13:59:15 timo Exp $ */
#include "bbe.h"
#ifdef HAVE_GETOPT_H
@@ -72,13 +72,13 @@ char *convert_strings[] = {
"",
};
/* commands to be executed at start of buffer */
-#define BLOCK_START_COMMANDS "DIJLFBN"
+#define BLOCK_START_COMMANDS "DIJLFBN>"
/* commands to be executed for each byte */
-#define BYTE_COMMANDS "acdirsywjpl&|^~"
+#define BYTE_COMMANDS "acdirsywjpl&|^~uf"
/* commands to be executed at end of buffer */
-#define BLOCK_END_COMMANDS "A"
+#define BLOCK_END_COMMANDS "A<"
/* format types for p command */
char *p_formats="DOHAB";
@@ -149,7 +149,7 @@ parse_long(char *long_int)
if(!isdigit(*scan)) panic("Error in number",long_int,NULL);
break;
case 'o':
- if(!isdigit(*scan) || *scan > '8') panic("Error in number",long_int,NULL);
+ if(!isdigit(*scan) || *scan >= '8') panic("Error in number",long_int,NULL);
break;
case 'x':
if(!isxdigit(*scan)) panic("Error in number",long_int,NULL);
@@ -226,7 +226,7 @@ parse_string(char *string,off_t *length)
min_len=3;
break;
case '0':
- while(isdigit(*p) && *p <= '8' && j < 4) num[j++] = *p++;
+ while(isdigit(*p) && *p < '8' && j < 4) num[j++] = *p++;
min_len=1;
break;
default:
@@ -301,7 +301,7 @@ parse_block(char *bs)
while(isxdigit(*p)) buf[i++] = *p++;
break;
case '0':
- while(isdigit(*p) && *p <= '8') buf[i++] = *p++;
+ while(isdigit(*p) && *p < '8') buf[i++] = *p++;
break;
default:
while(isdigit(*p)) buf[i++] = *p++;
@@ -348,7 +348,7 @@ parse_block(char *bs)
while(isxdigit(*p)) buf[i++] = *p++;
break;
case '0':
- while(isdigit(*p) && *p <= '8') buf[i++] = *p++;
+ while(isdigit(*p) && *p < '8') buf[i++] = *p++;
break;
default:
while(isdigit(*p)) buf[i++] = *p++;
@@ -466,6 +466,8 @@ parse_command(char *command_string)
new->s1 = parse_string(token[1],&new->s1_len);
break;
case 'w':
+ case '<':
+ case '>':
if(i != 2 || strlen(token[0]) > 1) panic("Error in command",command_string,NULL);
new->s1 = xstrdup(token[1]);
break;
@@ -568,6 +570,13 @@ parse_command(char *command_string)
case '~':
if(i != 1 || strlen(token[0]) > 1) panic("Error in command",command_string,NULL);
break;
+ case 'u':
+ case 'f':
+ if(i != 3 || strlen(token[0]) > 1) panic("Error in command",command_string,NULL);
+ new->offset = parse_long(token[1]);
+ new->s1 = parse_string(token[2],&new->s1_len);
+ if(new->s1_len != 1) panic("Error in command",command_string,NULL);
+ break;
default:
panic("Unknown command",command_string,NULL);
break;
diff --git a/src/bbe.h b/src/bbe.h
index cf00c6b..e0dc3e4 100644
--- a/src/bbe.h
+++ b/src/bbe.h
@@ -20,7 +20,7 @@
*
*/
-/* $Id: bbe.h,v 1.27 2005/10/27 16:37:14 timo Exp $ */
+/* $Id: bbe.h,v 1.28 2005/11/11 13:06:53 timo Exp $ */
#ifdef HAVE_CONFIG_H
#include "config.h"
@@ -146,8 +146,6 @@ struct output_buffer {
unsigned char *end;
unsigned char *write_pos; // current write psotion;
unsigned char *low_pos; // low water mark
- unsigned char *cycle_start; // at this position started the last command cycle
- // (written bytes n curren cycle are between cycle_start and write_pos
off_t block_offset; // block offset (start = 0) number of bytes written at position write_pos
};
@@ -202,9 +200,6 @@ init_commands(struct commands *c);
extern void
close_commands(struct commands *c);
-extern inline void
-set_cycle_start();
-
extern void
close_output_stream();
@@ -215,9 +210,6 @@ extern void
execute_program(struct commands *c);
extern void
-reverse_bytes(size_t count);
-
-extern void
write_string(char *string);
extern char *
diff --git a/src/buffer.c b/src/buffer.c
index 9566e08..c546d5c 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -20,7 +20,7 @@
*
*/
-/* $Id: buffer.c,v 1.33 2005/10/19 18:39:13 timo Exp $ */
+/* $Id: buffer.c,v 1.35 2005/11/14 10:40:47 timo Exp $ */
#include "bbe.h"
#include <stdlib.h>
@@ -465,20 +465,13 @@ write_string(char *string)
void
write_buffer(unsigned char *buf,off_t length)
{
- unsigned char *save_pos;
if(!length) return;
- if(length > (off_t) (OUTPUT_BUFFER_SIZE - (out_buffer.write_pos - out_buffer.buffer)))
+ if(out_buffer.write_pos + length >= out_buffer.end)
{
- save_pos = out_buffer.cycle_start < out_buffer.low_pos ? out_buffer.cycle_start : out_buffer.low_pos;
- if(save_pos == out_buffer.buffer)
- panic("Write buffer too small",NULL,NULL);
- write_output_stream(out_buffer.buffer,save_pos - out_buffer.buffer);
- write_w_command(out_buffer.buffer,save_pos - out_buffer.buffer);
- memmove(out_buffer.buffer,save_pos,out_buffer.write_pos - save_pos);
- out_buffer.write_pos = out_buffer.buffer + (out_buffer.write_pos - save_pos);
- out_buffer.cycle_start = out_buffer.buffer + (out_buffer.cycle_start - save_pos);
+ if(out_buffer.write_pos == out_buffer.buffer) panic("Out buffer too small, should not happen!",NULL,NULL);
+ flush_buffer();
}
memcpy(out_buffer.write_pos,buf,length);
out_buffer.write_pos += length;
@@ -497,39 +490,14 @@ put_byte(unsigned char byte)
inline void
write_next_byte()
{
- unsigned char *save_pos;
-
out_buffer.write_pos++;
out_buffer.block_offset++;
- if(out_buffer.write_pos > out_buffer.end)
+ if(out_buffer.write_pos >= out_buffer.end)
{
- save_pos = out_buffer.cycle_start < out_buffer.low_pos ? out_buffer.cycle_start : out_buffer.low_pos;
- if(save_pos == out_buffer.buffer)
- panic("Write buffer too small",NULL,NULL);
- write_output_stream(out_buffer.buffer,save_pos - out_buffer.buffer);
- write_w_command(out_buffer.buffer,save_pos - out_buffer.buffer);
- memmove(out_buffer.buffer,save_pos,out_buffer.write_pos - save_pos);
- out_buffer.write_pos = out_buffer.buffer + (out_buffer.write_pos - save_pos);
- out_buffer.cycle_start = out_buffer.buffer + (out_buffer.cycle_start - save_pos);
+ flush_buffer();
}
}
-/* reverse_bytes, reverse the write position (for replace etc.) */
-inline void
-reverse_bytes(size_t count)
-{
- out_buffer.write_pos -= count;
- out_buffer.block_offset -= count;
- if(out_buffer.write_pos < out_buffer.buffer) panic("Too many bytes reversed, should not happen",NULL,NULL);
-}
-
-/* set editing cycle start position */
-inline void
-set_cycle_start()
-{
- out_buffer.cycle_start = out_buffer.write_pos;
-}
-
/* write unwritten data from buffer to disk */
void
flush_buffer()
diff --git a/src/execute.c b/src/execute.c
index 53ac02b..bfc3b29 100644
--- a/src/execute.c
+++ b/src/execute.c
@@ -20,7 +20,7 @@
*
*/
-/* $Id: execute.c,v 1.33 2005/11/01 19:01:16 timo Exp $ */
+/* $Id: execute.c,v 1.37 2005/11/15 13:59:15 timo Exp $ */
#include "bbe.h"
#include <stdlib.h>
@@ -118,6 +118,7 @@ off_t_to_string(off_t number,char format)
+#define IO_BLOCK_SIZE (8 * 1024)
/* execute given commands */
void
@@ -127,8 +128,11 @@ execute_commands(struct command_list *c)
unsigned char a,b;
unsigned char *p;
char *str;
+ off_t read_count;
+ static unsigned char ioblock[IO_BLOCK_SIZE];
if(skip_this_block) return;
+
while(c != NULL)
{
switch(c->letter)
@@ -411,6 +415,27 @@ execute_commands(struct command_list *c)
case '~':
put_byte(~*out_buffer.write_pos);
break;
+ case '<':
+ case '>':
+ if (fseeko(c->fd,0,SEEK_SET)) panic("Cannot seek file",c->s1,strerror(errno));
+ do
+ {
+ read_count = fread(ioblock,1,IO_BLOCK_SIZE,c->fd);
+ write_buffer(ioblock,read_count);
+ } while(read_count);
+ break;
+ case 'u':
+ if(in_buffer.block_offset <= c->offset)
+ {
+ put_byte(c->s1[0]);
+ }
+ break;
+ case 'f':
+ if(in_buffer.block_offset >= c->offset)
+ {
+ put_byte(c->s1[0]);
+ }
+ break;
case 'w':
break;
}
@@ -563,6 +588,35 @@ init_commands(struct commands *commands)
}
c = c->next;
}
+
+ c = commands->block_start;
+
+ while(c != NULL)
+ {
+ switch(c->letter)
+ {
+ case '>':
+ c->fd = fopen(c->s1,"r");
+ if(c->fd == NULL) panic("Cannot open file for reading",c->s1,strerror(errno));
+ break;
+ }
+ c = c->next;
+ }
+
+ c = commands->block_end;
+
+ while(c != NULL)
+ {
+ switch(c->letter)
+ {
+ case '<':
+ c->fd = fopen(c->s1,"r");
+ if(c->fd == NULL) panic("Cannot open file for reading",c->s1,strerror(errno));
+ break;
+ }
+ c = c->next;
+ }
+
}
@@ -591,6 +645,32 @@ close_commands(struct commands *commands)
}
c = c->next;
}
+
+ c = commands->block_start;
+
+ while(c != NULL)
+ {
+ switch(c->letter)
+ {
+ case '>':
+ fclose(c->fd);
+ break;
+ }
+ c = c->next;
+ }
+
+ c = commands->block_end;
+
+ while(c != NULL)
+ {
+ switch(c->letter)
+ {
+ case '<':
+ fclose(c->fd);
+ break;
+ }
+ c = c->next;
+ }
}
/* reset the rpos counter for next block, in case block was shorter eg. delete count */
@@ -625,7 +705,6 @@ execute_program(struct commands *commands)
execute_commands(commands->block_start);
do
{
- set_cycle_start();
delete_this_byte = 0;
inserting = 0;
block_end = last_byte();
OpenPOWER on IntegriCloud