//--------------------------------------------------------------------------------
// parsing
+char newline = '\n';
+char terminator = 0;
+
char tranche_begin_tag[] = "#tranche";
size_t tranche_begin_tag_len = 8;
-char tranche_end_tag[] = "#endtranche";
-size_t tranche_end_tag_len = 11;
+char tranche_end_tag[] = "#tranche-end";
+size_t tranche_end_tag_len = 12;
// given a line
// returns beginning of file name list
static bool parse_file_list(Da *file_names, char *pt0){
char *pt1;
while( *pt0 && isspace(*pt0) ) pt0++;
+ pt1 = pt0;
while( *pt0 ){
- pt1 = pt0;
- while( *pt1 && !isspace(pt1) ) pt1++;
+ while( *pt1 && !isspace(*pt1) ) pt1++;
char *file_name = strndup(pt0, pt1 - pt0);
- da_push(file_names, file_name);
+ da_push(file_names, &file_name);
+ while( *pt1 && isspace(*pt1) ) pt1++;
pt0 = pt1;
- while( *pt0 && isspace(*pt0) ) pt0++;
}
}
//--------------------------------------------------------------------------------
// da_map calls
-static void tranche_open_fd(void *fn, void *closure){
- char *file_name = (char *)fn;
- Da *fds = (Da *)closure;
- int fd = open(fn, O_CREAT | O_APPEND);
+static void tranche_open_fd(void *fnp, void *closure){
+ char *file_name = *(char **)fnp;
+ Da *fdap = (Da *)closure;
+ int fd = open(file_name, O_CREAT | O_TRUNC | O_WRONLY, 0666);
if(fd == -1){
fprintf(stderr, "Could not open file %s\n", file_name);
return;
}
- da_push(fds, &fd);
+ da_push(fdap, &fd);
return;
}
-static void tranche_open_fds(Da *fns, Da *fds){
- da_map(fns, tranche_open_fd, fds);
+static void tranche_open_fds(Da *fnap, Da *fdap){
+ da_map(fnap, tranche_open_fd, fdap);
}
-static void tranche_close_fd(void *fd, void *closure){
- close(*(int *)fd);
+static void tranche_close_fd(void *fdp, void *closure){
+ close(*(int *)fdp);
}
-static void tranche_close_fds(Da *fds){
- da_map(fds, tranche_close_fd, NULL);
+static void tranche_close_fds(Da *fdap){
+ da_map(fdap, tranche_close_fd, NULL);
+ da_rewind(fdap);
}
-static void tranche_puts(void *fd, void *string){
- write(*(int *)fd, string, strlen(string));
+static void tranche_puts(void *fdp, void *string){
+ write(*(int *)fdp, string, strlen(string));
}
-static void tranche_puts_all(Da *fds, char *string){
- da_map(fds, tranche_puts, string);
+static void tranche_puts_all(Da *fdap, char *string){
+ da_map(fdap, tranche_puts, string);
}
-//--------------------------------------------------------------------------------
-//
-int tranche_send(FILE *src, Da *arg_fds){
+
+//--------------------------------------------------------------------------------
+// we have a little problem if the user tries to tranche two things to the same file ..
+int tranche_send(FILE *src, Da *arg_fdap){
char *pt;
Da line;
- Da file_names;
- Da fds;
+ Da file_name_arr;
+ Da fda;
da_alloc(&line, sizeof(char));
- da_alloc(&file_names, sizeof(char *));
- da_alloc(&fds, sizeof(int));
-
- da_fgets(&line, src);
- while( !feof(src) && !is_tranche_end(line.base) ){
+ da_alloc(&file_name_arr, sizeof(char *));
+ da_alloc(&fda, sizeof(int));
+
+ while( !feof(src) ){
+ da_fgets(&line, src);
+ if( is_tranche_end(line.base) ) break;
pt = is_tranche_begin(line.base);
if(pt){ // then this line is the start of a nested tranche block
- parse_file_list(&file_names, pt);
- tranche_open_fds(&file_names, &fds);
- da_free_elements(&file_names);
- tranche_send(src, &fds);
- tranche_close_fds(&fds);
+ parse_file_list(&file_name_arr, pt);
+ tranche_open_fds(&file_name_arr, &fda);
+ da_free_elements(&file_name_arr);
+ tranche_send(src, &fda);
+ tranche_close_fds(&fda);
}else{
- tranche_puts_all(arg_fds, line.base);
- da_rewind(&line);
- da_fgets(&line, src);
+ da_pop(&line, NULL); // pop the terminating zero
+ da_push(&line, &newline);
+ da_push(&line, &terminator);
+ tranche_puts_all(arg_fdap, line.base);
}
+ da_rewind(&line);
}
-
+
da_free(&line);
- da_free(&file_names);
- da_free(&fds);
+ da_free(&file_name_arr);
+ da_free(&fda);
return 0;
}