void test_replace (void) { char str1[] = "\x01\x01\x01x\x01y\x01z\x01xy\x01yz\x01"; char *str2 = (char *)NULL; cbuffer_t *buf1 = (cbuffer_t *)NULL; cbuffer_t *buf2 = (cbuffer_t *)NULL; buf1 = cbuf_new (); cbuf_import (buf1, str1, strlen (str1)); buf2 = cbuf_replace (buf1, "\x01", ", ", 1, 2); if (buf2 != (cbuffer_t *)NULL) { str2 = (char *)xmalloc (buf2->sz + 1); if (str2 != (char *)NULL) { memset (str2, (int)NULL, buf2->sz + 1); memcpy (str2, buf2->data, buf2->sz); printf ("test_replace(): str1 = %s\n", str1); printf ("test_replace(): str2 = %s\n", str2); xfree (str2); } } cbuf_delete (buf1); cbuf_delete (buf2); }
void test_cut (void) { char str1[] = "\x01\x01\x01x234x\x01\x01y\x01\x01xy123\x01\x01"; char *str2 = (char *)NULL; char *str3 = (char *)NULL; cbuffer_t *buf1 = (cbuffer_t *)NULL; cbuffer_t *buf2 = (cbuffer_t *)NULL; buf1 = cbuf_new(); cbuf_import (buf1, str1, strlen (str1)); buf2 = cbuf_cut (buf1, 2, 14); printf ("test_cut(): str1 = %s\n", str1); str2 = (char *)xmalloc (buf1->sz + 1); memset (str2, (int)NULL, buf1->sz + 1); memcpy (str2, buf1->data, buf1->sz); str3 = (char *)xmalloc (buf2->sz + 1); memset (str3, (int)NULL, buf2->sz + 1); memcpy (str3, buf2->data, buf2->sz); printf ("test_cut(): str2 = %s\ntest_cut(): str3 = %s\n", str2, str3); xfree (str2); xfree (str3); cbuf_delete (buf1); cbuf_delete (buf2); }
void test_split (void) { char str0[] = "\x01"; char str1[] = "\x01\x01x\x01y\x01z\x01xy\x01yz"; char str2[] = "buffer one\n\x01\x02\nend buffer\n"; char str3[] = "buffer two\n\x01\x02\nend buffer\n"; deque_t *split = (deque_t *)NULL; cbuffer_t *buf1 = (cbuffer_t *)NULL; cbuffer_t *buf2 = (cbuffer_t *)NULL; cbuffer_t *buf3 = (cbuffer_t *)NULL; buf1 = cbuf_new (); buf2 = cbuf_new (); buf3 = cbuf_new (); cbuf_import (buf1, str1, strlen (str1)); cbuf_import (buf2, str2, strlen (str2)); cbuf_import (buf3, str3, strlen (str3)); split = cbuf_split (buf1, (void *)str0, 1); printf ("test_split(): len = %d\n", deque_length (split)); deque_push (split, buf2); printf ("test_split(): len = %d\n", deque_length (split)); deque_push (split, buf3); printf ("test_split(): len = %d\n", deque_length (split)); cbuf_delete (buf1); deque_delete (split, cbuf_delete_callback); }
void run_main_b() { cbuf *cb1 ; cb1 = cbuf_init() ; cbuf_update(cb1, 60, 1.291) ; cbuf_update(cb1, 63, 1.287) ; cbuf_update(cb1, 63, 1.231) ; cbuf_update(cb1, 69, 1.229) ; cbuf_update(cb1, 72, 1.247) ; cbuf_update(cb1, 361, 1.291) ; cbuf_update(cb1, 411, 1.291) ; cbuf_update(cb1, 412, 1.281) ; cbuf_update(cb1, 413, 1.292) ; cbuf_update(cb1, 414, 1.284) ; cbuf_update(cb1, 414, 1.290) ; cbuf_update(cb1, 511, 1.241) ; cbuf_update(cb1, 512, 1.251) ; cbuf_update(cb1, 513, 1.232) ; cbuf_update(cb1, 514, 1.202) ; cbuf_update(cb1, 517, 1.119) ; cbuf_update(cb1, 551, 1.080) ; cbuf_update(cb1, 552, 1.081) ; cbuf_update(cb1, 553, 1.079) ; cbuf_update(cb1, 554, 1.088) ; cbuf_update(cb1, 561, 1.072) ; cbuf_update(cb1, 562, 1.113) ; cbuf_update(cb1, 563, 1.091) ; cbuf_update(cb1, 564, 1.092) ; cbuf_update(cb1, 571, 1.089) ; cbuf_update(cb1, 572, 1.073) ; cbuf_update(cb1, 573, 1.061) ; cbuf_update(cb1, 574, 1.111) ; cbuf_update(cb1, 581, 1.119) ; cbuf_update(cb1, 582, 1.123) ; cbuf_update(cb1, 583, 1.151) ; cbuf_update(cb1, 584, 1.153) ; cbuf_dump(cb1) ; double avg ; avg = cbuf_average(cb1) ; printf("Average rate = %f\n", avg) ; quote *q_start, *q_end ; q_start = cbuf_start(cb1) ; q_end = cbuf_end(cb1) ; printf("Start: time = %d, rate = %f\n", q_start->time, q_start->rate) ; printf("End: time = %d, rate = %f\n", q_end->time, q_end->rate) ; cbuf_stats(cb1) ; cbuf_delete(cb1) ; }
void test_tail_head (void) { char str1[] = "123456"; char *str2 = (char *)NULL; char *str3 = (char *)NULL; cbuffer_t *buf1; cbuffer_t *buf2; cbuffer_t *buf3; buf1 = (cbuffer_t *)NULL; buf2 = (cbuffer_t *)NULL; buf3 = (cbuffer_t *)NULL; printf ("test_tail_head(): str1 = %s\n", (char *)str1); buf1 = cbuf_new(); cbuf_import (buf1, str1, strlen (str1)); buf2 = cbuf_head (buf1, 3); str2 = (char *)malloc (buf2->sz + 1); if (str2 != (char *)NULL) { memset (str2, (int)NULL, buf2->sz + 1); memcpy (str2, buf2->data, buf2->sz); printf ("test_tail_head(): str2 = %s\n", str2); free (str2); } buf3 = cbuf_tail (buf1, 3); str3 = (char *)malloc (buf3->sz + 1); if (str3 != (char *)NULL) { memset (str3, (int)NULL, buf3->sz + 1); memcpy (str3, buf3->data, buf3->sz); printf ("test_tail_head(): str3 = %s\n", str3); free (str3); } cbuf_delete (buf2); cbuf_delete (buf3); buf2 = cbuf_head_cut (buf1, 3); buf3 = cbuf_tail_cut (buf2, 3); cbuf_delete (buf2); cbuf_delete (buf3); cbuf_delete (buf1); }
cbuf* cbuf_takeover(cbuf* b1, cbuf* b2) { talloc_reparent(b2, b1, b2->buf); b1->buf = b2->buf; b1->pos = b2->pos; b1->size = b2->size; cbuf_delete(b2); return b1; }
void test_import (void) { char str1[] = "buffer one\n\x01\x02\nend buffer\n"; char str2[] = "buffer two\n\x01\x02\nend buffer\n"; char str3[] = "x" "\x01" "y" "\x01" "z" "\x01" "xy" "\x01" "yz"; cbuffer_t *buf1 = (cbuffer_t *)NULL; cbuffer_t *buf2 = (cbuffer_t *)NULL; cbuffer_t *buf3 = (cbuffer_t *)NULL; buf1 = cbuf_new (); buf2 = cbuf_new (); buf3 = cbuf_new (); cbuf_import (buf1, str1, strlen (str1)); cbuf_import (buf2, str2, strlen (str2)); cbuf_import (buf3, str3, strlen (str3)); cbuf_delete (buf1); cbuf_delete (buf2); cbuf_delete (buf3); }
void test_search (void) { char str1[] = "\x01\x01x234x\x01\x01y\x01\x01xy123\x01\x01"; deque_t *lst = (deque_t *)NULL; cbuffer_t *buf1 = (cbuffer_t *)NULL; buf1 = cbuf_new(); cbuf_import (buf1, str1, strlen (str1)); lst = cbuf_search (buf1, "\x01", 1); printf ("test_search(): len = %d\n", deque_length (lst)); deque_delete_nocb (lst); cbuf_delete (buf1); }
cbuf* cbuf_copy(const cbuf* b) { cbuf* s = talloc(talloc_parent(b), cbuf); if (s == NULL) { return NULL; } s->buf = (char *)talloc_memdup(s, b->buf, b->size); /* only up to pos? */ /* XXX shallow did not work, because realloc */ /* fails with multiple references */ /* s->buf = talloc_reference(s, b->buf); */ if (s->buf == NULL) { cbuf_delete(s); return NULL; } s->size = b->size; s->pos = b->pos; return s; }
void test_new (void) { cbuffer_t *buf = cbuf_new (); cbuf_delete (buf); }
void run_main_c() { cbuf *eur_usd, *eur_jpy, *usd_jpy ; FILE *ifile ; char fname[33], pair[10] ; unsigned int hour, min, isec, time ; double sec, rate ; int r ; eur_usd = cbuf_init() ; eur_jpy = cbuf_init() ; usd_jpy = cbuf_init() ; // Ask user for filename printf("File name (32 chars max): ") ; scanf("%32s", fname) ; // Try to open file ifile = fopen (fname, "r") ; if (ifile == NULL) { fprintf(stderr, "Could not open file: %s\n", fname) ; exit(1) ; } // keep reading file until no more ticks // while(1) { r = fscanf(ifile, "%7s %d:%d:%lf %lf", pair, &hour, &min, &sec, &rate) ; isec = sec ; // fprintf(stderr, "matched %d items\n", r) ; // fprintf(stderr, "%s, %02d:%02d:%02d %f\n", pair, hour, min, isec, rate) ; time = isec + 60 * min + 3600 * hour ; if (r <= 0) break ; // EOF? if (strcmp(pair, "EUR-USD") == 0) { cbuf_update(eur_usd, time, rate) ; } else if (strcmp(pair, "EUR-JPY") == 0) { cbuf_update(eur_jpy, time, rate) ; } else if (strcmp(pair, "USD-JPY") == 0) { cbuf_update(usd_jpy, time, rate) ; } else { fprintf(stderr, "Not a recognized currency pair!\n") ; } } printf("\n\n*** Data for EUR-USD ***\n") ; print_data(eur_usd) ; cbuf_stats(eur_usd) ; printf("\n\n*** Data for EUR-JPY ***\n") ; print_data(eur_jpy) ; cbuf_stats(eur_jpy) ; printf("\n\n*** Data for USD-JPY ***\n") ; print_data(usd_jpy) ; cbuf_stats(usd_jpy) ; // Be nice, clean up cbuf_delete(eur_usd) ; cbuf_delete(eur_jpy) ; cbuf_delete(usd_jpy) ; fclose(ifile) ; }
int main (void) { const char input_string[] = "Hello Base Encoding/Decoding World!!!"; const char long_string[] = "\t0. This is a very long message\n" "\t1. This is a very long message\n" "\t2. This is a very long message\n" "\t3. This is a very long message\n" "\t4. This is a very long message\n" "\t5. This is a very long message\n" "\t6. This is a very long message\n" "\t7. This is a very long message\n" "\t8. This is a very long message\n" "\t9. This is a very long message\n" "\t0. This is a very long message\n" "\t1. This is a very long message\n" "\t2. This is a very long message\n" "\t3. This is a very long message\n" "\t4. This is a very long message\n" "\t5. This is a very long message\n" "\t6. This is a very long message\n" "\t7. This is a very long message\n" "\t8. This is a very long message\n" "\t9. This is a very\nlong message\n"; char *cache = (char *)NULL; cbuffer_t *in_single = (cbuffer_t *)NULL; cbuffer_t *in_encode = (cbuffer_t *)NULL; cbuffer_t *out_decode = (cbuffer_t *)NULL; printf ("base 64 chunk size: in=%d, out=%d\n", base_encode_chunk_sz (6), base_decode_chunk_sz (6)); printf ("base 32 chunk size: in=%d, out=%d\n", base_encode_chunk_sz (5), base_decode_chunk_sz (5)); printf ("base 16 chunk size: in=%d, out=%d\n", base_encode_chunk_sz (4), base_decode_chunk_sz (4)); printf ("base 64 buffer size: in=%d, out=%d\n", base_encode_buffer_sz (6), base_decode_buffer_sz (6)); printf ("base 32 buffer size: in=%d, out=%d\n", base_encode_buffer_sz (5), base_decode_buffer_sz (5)); printf ("base 16 buffer size: in=%d, out=%d\n", base_encode_buffer_sz (4), base_decode_buffer_sz (4)); printf ("==================================================\n"); in_single = cbuf_create ((size_t)(strlen (input_string) + 1)); if (in_single != (cbuffer_t *)NULL) { printf ("input: %s (len:%d; %p)\n", input_string, strlen (input_string), (void *)in_single); cbuf_clean (in_single); cbuf_import (in_single, input_string, strlen (input_string)); // base16 encoding/decoding in_encode = caf_base16_encode (in_single); if (in_encode != (cbuffer_t *)NULL) { // display encoding cache = (char *)xmalloc (in_encode->sz + 1); memset (cache, 0, in_encode->sz + 1); memcpy (cache, in_encode->data, in_encode->sz); printf ("base16 encode: %s\n", cache); xfree (cache); // decoding and display decoding out_decode = caf_base16_decode (in_encode); if (out_decode != (cbuffer_t *)NULL) { cache = (char *)xmalloc (out_decode->iosz + 1); memset (cache, 0, out_decode->sz + 1); memcpy (cache, out_decode->data, out_decode->sz); printf ("base16 decode: %s (len:%d; blen:%d)\n", cache, strlen (cache), (int)out_decode->iosz); cbuf_delete (out_decode); xfree (cache); } cbuf_delete (in_encode); } printf ("==================================================\n"); // base32 encoding/decoding in_encode = caf_base32_encode (in_single); if (in_encode != (cbuffer_t *)NULL) { // display encoding cache = (char *)xmalloc (in_encode->sz + 1); memset (cache, 0, in_encode->sz + 1); memcpy (cache, in_encode->data, in_encode->sz); printf ("base32 encode: %s\n", cache); xfree (cache); // decoding and display decoding out_decode = caf_base32_decode (in_encode); if (out_decode != (cbuffer_t *)NULL) { cache = (char *)xmalloc (out_decode->iosz + 1); memset (cache, 0, out_decode->sz + 1); memcpy (cache, out_decode->data, out_decode->sz); printf ("base32 decode: %s (len:%d; blen:%d)\n", cache, strlen (cache), (int)out_decode->iosz); cbuf_delete (out_decode); xfree (cache); } cbuf_delete (in_encode); } printf ("==================================================\n"); // base64 encoding/decoding in_encode = caf_base64_encode (in_single); if (in_encode != (cbuffer_t *)NULL) { // display encoding cache = (char *)xmalloc (in_encode->sz + 1); memset (cache, 0, in_encode->sz + 1); memcpy (cache, in_encode->data, in_encode->sz); printf ("base64 encode: %s\n", cache); xfree (cache); // decoding and display decoding out_decode = caf_base64_decode (in_encode); if (out_decode != (cbuffer_t *)NULL) { cache = (char *)xmalloc (out_decode->iosz + 1); memset (cache, 0, out_decode->sz + 1); memcpy (cache, out_decode->data, out_decode->sz); printf ("base64 decode: %s (len:%d; blen:%d)\n", cache, strlen (cache), (int)out_decode->iosz); cbuf_delete (out_decode); xfree (cache); } cbuf_delete (in_encode); } printf ("==================================================\n"); cbuf_delete (in_single); } in_single = cbuf_create ((size_t)(strlen (long_string) + 1)); if (in_single != (cbuffer_t *)NULL) { printf ("input:\n%s (len:%d; %p)\n", long_string, strlen (long_string), (void *)in_single); cbuf_clean (in_single); cbuf_import (in_single, long_string, strlen (long_string)); // base16 encoding/decoding in_encode = caf_base16_encode (in_single); if (in_encode != (cbuffer_t *)NULL) { // display encoding cache = (char *)xmalloc (in_encode->sz + 1); memset (cache, 0, in_encode->sz + 1); memcpy (cache, in_encode->data, in_encode->sz); printf ("base16 encode: %s\n", cache); xfree (cache); // decoding and display decoding out_decode = caf_base16_decode (in_encode); if (out_decode != (cbuffer_t *)NULL) { cache = (char *)xmalloc (out_decode->iosz + 1); memset (cache, 0, out_decode->sz + 1); memcpy (cache, out_decode->data, out_decode->sz); printf ("base16 decode: %s (len:%d; blen:%d)\n", cache, strlen (cache), (int)out_decode->iosz); cbuf_delete (out_decode); xfree (cache); } cbuf_delete (in_encode); } printf ("==================================================\n"); // base32 encoding/decoding in_encode = caf_base32_encode (in_single); if (in_encode != (cbuffer_t *)NULL) { // display encoding cache = (char *)xmalloc (in_encode->sz + 1); memset (cache, 0, in_encode->sz + 1); memcpy (cache, in_encode->data, in_encode->sz); printf ("base32 encode: %s\n", cache); xfree (cache); // decoding and display decoding out_decode = caf_base32_decode (in_encode); if (out_decode != (cbuffer_t *)NULL) { cache = (char *)xmalloc (out_decode->iosz + 1); memset (cache, 0, out_decode->sz + 1); memcpy (cache, out_decode->data, out_decode->sz); printf ("base32 decode: %s (len:%d; blen:%d)\n", cache, strlen (cache), (int)out_decode->iosz); cbuf_delete (out_decode); xfree (cache); } cbuf_delete (in_encode); } printf ("==================================================\n"); // base64 encoding/decoding in_encode = caf_base64_encode (in_single); if (in_encode != (cbuffer_t *)NULL) { // display encoding cache = (char *)xmalloc (in_encode->sz + 1); memset (cache, 0, in_encode->sz + 1); memcpy (cache, in_encode->data, in_encode->sz); printf ("base64 encode: %s\n", cache); xfree (cache); // decoding and display decoding out_decode = caf_base64_decode (in_encode); if (out_decode != (cbuffer_t *)NULL) { cache = (char *)xmalloc (out_decode->iosz + 1); memset (cache, 0, out_decode->sz + 1); memcpy (cache, out_decode->data, out_decode->sz); printf ("base64 decode: %s (len:%d; blen:%d)\n", cache, strlen (cache), (int)out_decode->iosz); cbuf_delete (out_decode); xfree (cache); } cbuf_delete (in_encode); } printf ("==================================================\n"); cbuf_delete (in_single); } return 0; }