while'd version of u8vector-append replaced back with tail recursion version
[mot-flash.git] / main.c
blob5ce17cd2e4c250a818b74914ce1affcd802a8e0d
1 #include <readline/readline.h>
2 #include <libguile.h>
4 #include "iface_type.h"
6 #define ASSERT_U_VECTOR(v, p, func) SCM_ASSERT(scm_is_uniform_vector(v), (v), (p), (func))
7 #define ASSERT_SIZE_T(v, p, func) SCM_ASSERT(scm_is_unsigned_integer(v, 0, SCM_I_SIZE_MAX), (v), (p), (func))
9 SCM make_libusb_iface();
12 SCM uniform_vector_move_x(SCM src, SCM s_start, SCM s_end, SCM dst, SCM d_start)
14 size_t ss_start, ss_end, sd_start;
15 scm_t_array_handle s_handle, d_handle;
16 size_t copy_len;
17 size_t s_esize;
18 size_t s_len, d_len;
19 ssize_t s_inc, d_inc;
20 const void *raw_src;
21 void *raw_dst;
23 ASSERT_U_VECTOR (src , SCM_ARG1, "uniform-vector-move!");
24 ASSERT_SIZE_T (s_start, SCM_ARG2, "uniform-vector-move!");
25 ASSERT_SIZE_T (s_end , SCM_ARG3, "uniform-vector-move!");
26 ASSERT_U_VECTOR (dst , SCM_ARG4, "uniform-vector-move!");
27 ASSERT_SIZE_T (d_start, SCM_ARG5, "uniform-vector-move!");
29 ss_start = scm_to_size_t(s_start);
30 ss_end = scm_to_size_t(s_end);
31 sd_start = scm_to_size_t(d_start);
33 raw_src = scm_uniform_vector_elements(src, &s_handle, &s_len, &s_inc);
34 if ((ss_end < ss_start) || (ss_end - ss_start > s_len)) {
35 scm_array_handle_release(&s_handle);
36 scm_out_of_range("uniform-vector-move!", s_end);
38 copy_len = (size_t)(ss_end - ss_start);
40 raw_dst = scm_uniform_vector_writable_elements(dst, &d_handle, &d_len, &d_inc);
41 if (copy_len + sd_start > d_len) {
42 scm_array_handle_release(&s_handle);
43 scm_array_handle_release(&d_handle);
44 scm_out_of_range("uniform-vector-move!", s_end);
47 s_esize = scm_array_handle_uniform_element_size(&s_handle);
48 if (s_esize != scm_array_handle_uniform_element_size(&d_handle)) {
49 scm_array_handle_release(&s_handle);
50 scm_array_handle_release(&d_handle);
51 scm_wrong_type_arg_msg("uniform-vector-move!", SCM_ARG4, dst, "source and dest with a same type");
54 if (s_inc == d_inc) {
55 if (d_inc == 1) {
56 raw_src+=ss_start*s_inc*s_esize;
57 raw_dst+=sd_start*d_inc*s_esize;
58 memcpy(raw_dst, raw_src, copy_len * s_esize);
59 goto out;
60 } else if (s_inc == -1 && s_esize == 1) {
61 raw_dst -= sd_start;
62 raw_src -= ss_start;
63 memcpy(raw_dst - copy_len + 1, raw_src - copy_len + 1, copy_len);
64 goto out;
67 s_inc*=s_esize;
68 d_inc*=s_esize;
69 raw_src+=ss_start*s_inc;
70 raw_dst+=sd_start*d_inc;
71 while (copy_len--) {
72 memcpy(raw_dst, raw_src, s_esize);
73 raw_src+=s_inc;
74 raw_dst+=d_inc;
77 out: scm_array_handle_release(&s_handle);
78 scm_array_handle_release(&d_handle);
79 return SCM_UNSPECIFIED;
82 void main_repl(void *closure, int argc, char *argv[])
84 init_flash_iface_type();
85 scm_c_define_gsubr("make-libusb-iface", 0, 0, 0, &make_libusb_iface);
86 scm_c_define_gsubr("uniform-vector-move!", 5, 0, 0, &uniform_vector_move_x);
87 scm_c_primitive_load("common.scm");
88 scm_shell(argc, argv);
89 return;
92 int main(int argc, char *argv[])
94 scm_boot_guile(argc, argv, main_repl, 0);
95 return 0;