00001
00002
00013 #include "eformat/write/node.h"
00014 #include <cstring>
00015
00016 void eformat::write::set (node_t& i, const uint32_t* b, size_t l, node_t* n)
00017 {
00018 i.base = const_cast<uint32_t*>(b);
00019 i.size_word = l;
00020 i.next = n;
00021 }
00022
00023 void eformat::write::set (node_t& i, const struct iovec& v, node_t* n)
00024 {
00025 i.base = reinterpret_cast<uint32_t*>(v.iov_base);
00026 i.size_word = v.iov_len/4;
00027 i.next = n;
00028 }
00029
00030 void eformat::write::cat (node_t* n, const struct iovec* v, uint32_t count)
00031 {
00032 for (size_t i=0; i<(count-1); ++i) set(n[i], v[i], &n[i+1]);
00033 set(n[count-1], v[count-1], 0);
00034 }
00035
00036 void eformat::write::cat (node_t* n, uint32_t count)
00037 {
00038 for (size_t i=0; i<(count-1); ++i) n[i].next = &n[i+1];
00039 n[count-1].next = 0;
00040 }
00041
00042 uint32_t eformat::write::count (const node_t& list)
00043 {
00044 const eformat::write::node_t* current = &list;
00045 uint32_t retval = 0;
00046 while (current) {
00047 ++retval;
00048 current = current->next;
00049 }
00050 return retval;
00051 }
00052
00053 uint32_t eformat::write::count_words (const node_t& list)
00054 {
00055 const eformat::write::node_t* current = &list;
00056 uint32_t retval = 0;
00057 while (current) {
00058 retval += current->size_word;
00059 current = current->next;
00060 }
00061 return retval;
00062 }
00063
00064 uint32_t eformat::write::copy (const node_t& list, uint32_t* dest,
00065 size_t max)
00066 {
00067 const eformat::write::node_t* current = &list;
00068 uint32_t cpos = 0;
00069 while (current) {
00070 if (cpos + current->size_word > max) return 0;
00071 if (current->size_word > 0 && current->base) {
00072 memcpy(&dest[cpos], current->base, sizeof(uint32_t)*current->size_word);
00073 cpos += current->size_word;
00074 }
00075 current = current->next;
00076 }
00077 return cpos;
00078 }
00079
00080 uint32_t eformat::write::shallow_copy (const node_t& list, struct iovec* dest,
00081 uint32_t max)
00082 {
00083 const eformat::write::node_t* current = &list;
00084 uint32_t cpos = 0;
00085 while (current) {
00086 if (cpos > max) return 0;
00087 if (current->size_word > 0 && current->base) {
00088 dest[cpos].iov_base =
00089 reinterpret_cast<void*>(const_cast<uint32_t*>(current->base));
00090 dest[cpos].iov_len = sizeof(uint32_t)*current->size_word;
00091 ++cpos;
00092 }
00093 current = current->next;
00094 }
00095 return cpos;
00096 }
00097