return flag;
}
+void da_cat(Da *dap0, Da *dap1){
+ if(dap1->base == dap1->end) return;
+ size_t dap0_size = dap0->end - dap0->base;
+ size_t dap1_size = dap1->end - dap1->base; // size of the active portion
+ dap0->end += dap1_size;
+ while( dap0->end >= dap0->base + dap0->size ) da_expand(dap0);
+ memcpy(dap0->base + dap0_size, dap1->base, dap1_size);
+}
+
+// If dap0 has had a terminatating zero added, that must be popped off before
+// the call. Similarly if a terminating zero is desired, it should be pushed
+// after the call.
+void da_push_string(Da *dap0, char *string){
+ if(!*string) return;
+ size_t dap0_size = dap0->end - dap0->base;
+ size_t string_size = strlen(string);
+ dap0->end += string_size;
+ while( dap0->end >= dap0->base + dap0->size ) da_expand(dap0);
+ memcpy(dap0->base + dap0_size, string, string_size);
+}
+
+
char *da_index(Da *dap, size_t i){
size_t offset = i * dap->element_size;
char *pt = dap->base + offset;
// for the case of an array of strings
void da_strings_puts(Da *dap){
char *pt = dap->base;
- while( pt != dap->end ){
+ while( pt < dap->end ){
puts(*(char **)pt);
pt += dap->element_size;
}
}
+// would like to pass in the printf format to make a general print
+// but can't get *pt on the stack for the printf call .. hmmm
+void da_ints_print(Da *dap){
+ char *pt = dap->base;
+ while( pt < dap->end ){
+ printf("%u\n", *(int *)pt);
+ pt += dap->element_size;
+ }
+}
+
// Puts text from a line into buffer *dap. Does not push EOF or '\n' into the
// buffer. Returns the old_base so that external pointers can be rebased.
bool da_boundq(Da *dap);
void da_push(Da *dap, void *element);
bool da_pop(Da *dap, void *element);
+void da_cat(Da *dap_base, Da *dap_cat);
char *da_index(Da *dap, size_t i);
void da_map(Da *dap, void f(void *, void *), void *closure);
void da_free_elements(Da *dap);
void da_strings_puts(Da *dap);
+void da_ints_print(Da *dap);
char *da_fgets(Da *dap, FILE *fd);
#endif
// enumeration of tests
typedef bool (*test_fun)();
- test_fun tests[] = {test_da_0, test_da_1, test_da_2, test_da_3, NULL};
- char *test_names[] = {"test_da_0", "test_da_1", "test_da_2", "test_da_3", NULL};
+ test_fun tests[] = {test_da_0, test_da_1, test_da_2, test_da_3, test_da_4, NULL};
+ char *test_names[] = {"test_da_0", "test_da_1", "test_da_2", "test_da_3", "test_da_4", NULL};
// call tests
test_fun *tfp = tests;
return f0 && f1 && f2 && f3;
}
+// da_cat
+bool test_da_4(){
+
+ Da da0, da1;
+ da_alloc(&da0, sizeof(int));
+ da_alloc(&da1, sizeof(int));
+
+ int i = 5;
+ while(i < 8){
+ da_push(&da0, &i);
+ i++;
+ }
+ while(i < 11){
+ da_push(&da1, &i);
+ i++;
+ }
+
+ da_cat(&da0, &da1);
+
+ bool f[6];
+ int j;
+ int k = 0;
+ while(k < 6){
+ f[k] = da_pop(&da0, &j) && (j == 10 - k);
+ k++;
+ }
+
+ bool result = f[0];
+ k = 1;
+ while(result && k < 6){
+ result = f[k];
+ k++;
+ }
+
+ return result;
+}
+
bool test_da_1();
bool test_da_2();
bool test_da_3();
+bool test_da_4();
#endif
+++ /dev/null
-/*
-Scans a tranche file and outputs a dep line suitable for make.
-
-The dep file is opened for append. If the depfile is not present stdout is used.
-
-If the given source file name has a directory prefix, the targets in
-the dep line are given the same prefix.
-*/
-
-#include <stdio.h>
-#include <unistd.h>
-#include <da.h>
-#include "tranche.lib.h"
-
-#define ERR_ARGC 1
-#define ERR_SRC_OPEN 2
-#define ERR_DEP_OPEN 4
-
-
-int main(int argc, char **argv, char **envp){
- if(argc < 2 || argc > 3){
- fprintf(stderr, "usage: %s <source> [<dep-file>]\n",argv[0]);
- return ERR_ARGC;
- }
- char *src_file_name = argv[1];
- char *dep_file_name = argv[2];
-
- int dep_fd;
- FILE *src_file = fopen(src_file_name, "r");
- if(argc < 3)
- dep_fd = 1;
- else{
- dep_fd = open(file_name, O_WRONLY | O_APPEND | O_CREAT, 0666);
- }
- unsigned int err = 0;
- if(!src_file){
- fprintf(stderr,"could not open tranche source file %s\n", src_file_name);
- err+= ERR_SRC_OPEN;
- }
- if(dep_fd == -1){
- fprintf(stderr, "Could not open the dep file %s\n", dep_file_name);
- err+= ERR_DEP_OPEN;
- }
- if(err) return err;
-
- tranche_deps(src_file, dep_fd);
- fclose(file);
- close(dep_fd);
- return 0;
-}
--- /dev/null
+/*
+Scans a tranche file and outputs a make rule for makefile-deps, of the form:
+
+<target>... : <src>
+ tranche $@
+
+ file is opened for append. If the depfile is not present stdout is used.
+
+If the given source file name has a directory prefix, the targets in
+the dep line are given the same prefix.
+*/
+
+#include <stdio.h>
+#include <unistd.h>
+#include <da.h>
+#include "tranche.lib.h"
+
+#define ERR_ARGC 1
+#define ERR_SRC_OPEN 2
+#define ERR_DEP_OPEN 4
+
+
+int main(int argc, char **argv, char **envp){
+ if(argc < 2 || argc > 3){
+ fprintf(stderr, "usage: %s <source> [<dep-file>]\n",argv[0]);
+ return ERR_ARGC;
+ }
+ char *src_file_name = argv[1];
+ char *dep_file_name = argv[2];
+
+ int dep_fd;
+ FILE *src_file = fopen(src_file_name, "r");
+ if(argc < 3)
+ dep_fd = 1;
+ else{
+ dep_fd = open(file_name, O_WRONLY | O_APPEND | O_CREAT, 0666);
+ }
+ unsigned int err = 0;
+ if(!src_file){
+ fprintf(stderr,"could not open tranche source file %s\n", src_file_name);
+ err+= ERR_SRC_OPEN;
+ }
+ if(dep_fd == -1){
+ fprintf(stderr, "Could not open the dep file %s\n", dep_file_name);
+ err+= ERR_DEP_OPEN;
+ }
+ if(err) return err;
+
+ tranche_deps(src_file, dep_fd);
+ fclose(file);
+ close(dep_fd);
+ return 0;
+}
+++ /dev/null
-
-#tranche test11.dat test12.dat
-The little red hen said to Mick, no thank you not today sir.
-And then all the barnes animals shouted out in glee.
-No more misery!
-#tranche test13.dat
-apple banana pear
-kiwi
-#tranche-end
-cows
-and cats
-#tranche-end
-
-the between space
-
-#tranche test14.dat
-int float if while
-do
-function
-#tranche-end
-
-#tranche test15.dat
-#tranche-end
\ No newline at end of file
+++ /dev/null
-#!/bin/bash -x
-./tranche test1.dat >test1stdout.dat
-diff test11.dat test11.dat.expected
-diff test12.dat test12.dat.expected
-diff test13.dat test13.dat.expected
-diff test14.dat test14.dat.expected
-diff test15.dat test15.dat.expected
-diff test1stdout.dat test1stdout.dat.expected
-rm test11.dat test12.dat test13.dat test14.dat test15.dat test1stdout.dat
-
+++ /dev/null
-The little red hen said to Mick, no thank you not today sir.
-And then all the barnes animals shouted out in glee.
-No more misery!
-cows
-and cats
+++ /dev/null
-The little red hen said to Mick, no thank you not today sir.
-And then all the barnes animals shouted out in glee.
-No more misery!
-cows
-and cats
+++ /dev/null
-apple banana pear
-kiwi
+++ /dev/null
-int float if while
-do
-function
+++ /dev/null
-
-
-the between space
-
-
+++ /dev/null
-
-
-#include "test2.h"
-
-
-int f(int x){
- return x;
-}
-
-
+++ /dev/null
-#ifndef TEST2_H
-#define TEST2_H
-int f(int x);
-#endif
+++ /dev/null
-#!/bin/bash -x
-./tranche test2.trc.c >test2stdout.dat
-diff test2.c test2.c.expected
-diff test2.h test2.h.expected
-diff test2stdout.dat test2stdout.dat.expected
-rm test2.c test2.h
-
-
+++ /dev/null
-
-#tranche test2.c
-
-#tranche test2.h
-#ifndef TEST2_H
-#define TEST2_H
-#tranche-end
-
-#include "test2.h"
-
-#tranche test2.h
-int f(int x);
-#tranche-end
-
-int f(int x){
- return x;
-}
-
-#tranche test2.h
-#endif
-#tranche-end
-
-#tranche-end
+++ /dev/null
-test11.dat
-test12.dat
-test13.dat
-test14.dat
-test15.dat
+++ /dev/null
-#!/bin/bash -x
-./tranche-target test1.dat > test3.out
-diff test3.out test3.out.expected
-rm test3.out
-
-
+++ /dev/null
-test2.c
-test2.h
+++ /dev/null
-#!/bin/bash -x
-./tranche-target test2.trc.c > test4.out
-diff test4.out test4.out.expected
-rm test4.out
-
-
+++ /dev/null
-../exec/tranche
\ No newline at end of file
+++ /dev/null
-../exec/tranche-target
\ No newline at end of file
--- /dev/null
+
+#tranche test11.dat test12.dat
+The little red hen said to Mick, no thank you not today sir.
+And then all the barnes animals shouted out in glee.
+No more misery!
+#tranche test13.dat
+apple banana pear
+kiwi
+#tranche-end
+cows
+and cats
+#tranche-end
+
+the between space
+
+#tranche test14.dat
+int float if while
+do
+function
+#tranche-end
+
+#tranche test15.dat
+#tranche-end
\ No newline at end of file
--- /dev/null
+#!/bin/bash -x
+./tranche test1.dat >test1stdout.dat
+diff test11.dat test11.dat.expected
+diff test12.dat test12.dat.expected
+diff test13.dat test13.dat.expected
+diff test14.dat test14.dat.expected
+diff test15.dat test15.dat.expected
+diff test1stdout.dat test1stdout.dat.expected
+rm test11.dat test12.dat test13.dat test14.dat test15.dat test1stdout.dat
+
--- /dev/null
+The little red hen said to Mick, no thank you not today sir.
+And then all the barnes animals shouted out in glee.
+No more misery!
+cows
+and cats
--- /dev/null
+The little red hen said to Mick, no thank you not today sir.
+And then all the barnes animals shouted out in glee.
+No more misery!
+cows
+and cats
--- /dev/null
+apple banana pear
+kiwi
--- /dev/null
+int float if while
+do
+function
--- /dev/null
+
+
+the between space
+
+
--- /dev/null
+
+
+#include "test2.h"
+
+
+int f(int x){
+ return x;
+}
+
+
--- /dev/null
+#ifndef TEST2_H
+#define TEST2_H
+int f(int x);
+#endif
--- /dev/null
+#!/bin/bash -x
+./tranche test2.trc.c >test2stdout.dat
+diff test2.c test2.c.expected
+diff test2.h test2.h.expected
+diff test2stdout.dat test2stdout.dat.expected
+rm test2.c test2.h
+
+
--- /dev/null
+
+#tranche test2.c
+
+#tranche test2.h
+#ifndef TEST2_H
+#define TEST2_H
+#tranche-end
+
+#include "test2.h"
+
+#tranche test2.h
+int f(int x);
+#tranche-end
+
+int f(int x){
+ return x;
+}
+
+#tranche test2.h
+#endif
+#tranche-end
+
+#tranche-end
--- /dev/null
+test11.dat
+test12.dat
+test13.dat
+test14.dat
+test15.dat
--- /dev/null
+#!/bin/bash -x
+./tranche-target test1.dat > test3.out
+diff test3.out test3.out.expected
+rm test3.out
+
+
--- /dev/null
+test2.c
+test2.h
--- /dev/null
+#!/bin/bash -x
+./tranche-target test2.trc.c > test4.out
+diff test4.out test4.out.expected
+rm test4.out
+
+
--- /dev/null
+../../exec/tranche
\ No newline at end of file
--- /dev/null
+../../exec/tranche-target
\ No newline at end of file