NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
output_databuffer.cpp
Go to the documentation of this file.
1 /*
2  * nvbio
3  * Copyright (c) 2011-2014, NVIDIA CORPORATION. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the NVIDIA CORPORATION nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
29 #include <nvbio/basic/numbers.h>
30 
31 #include <stdio.h>
32 #include <stdarg.h>
33 
34 namespace nvbio {
35 namespace io {
36 
38  : pos(0)
39 {
40  buffer.resize( BUFFER_SIZE + BUFFER_EXTRA );
41  NVBIO_CUDA_ASSERT(buffer.size());
42 }
43 
45 {}
46 
47 void DataBuffer::append_data(const void *data, int size)
48 {
50 
51  memcpy(&buffer[0] + pos, data, size);
52  pos += size;
53 }
54 
56 {
57  append_data(&val, sizeof(val));
58 }
59 
61 {
62  append_data(&val, sizeof(val));
63 }
64 
66 {
67  append_data(&val, sizeof(val));
68 }
69 
71 {
72  append_data(&val, sizeof(val));
73 }
74 
75 void DataBuffer::append_formatted_string(const char *fmt, ...)
76 {
77  int bytes_left = BUFFER_SIZE + BUFFER_EXTRA - pos;
78  int bytes_written;
79  va_list args;
80 
81  va_start(args, fmt);
82  bytes_written = vsnprintf(&buffer[pos], bytes_left, fmt, args);
83 
84  // if we hit the very end, the string was likely truncated
85  NVBIO_CUDA_ASSERT(bytes_written < bytes_left);
86 
87  pos += bytes_written;
88 }
89 
90 void DataBuffer::append_string(const char *str)
91 {
92  append_data(str, strlen(str));
93 }
94 
96 {
97  return pos;
98 }
99 
101 {
103 
104  pos += n;
105 }
106 
107 void DataBuffer::poke_data(int offset, const void *data, int size)
108 {
109  NVBIO_CUDA_ASSERT(offset + size < BUFFER_SIZE + BUFFER_EXTRA);
110 
111  memcpy(&buffer[0] + offset, data, size);
112 }
113 
114 void DataBuffer::poke_int32(int offset, int32 val)
115 {
116  poke_data(offset, &val, sizeof(val));
117 }
118 
119 void DataBuffer::poke_uint16(int offset, uint16 val)
120 {
121  poke_data(offset, &val, sizeof(val));
122 }
123 
125 {
126  if (pos > BUFFER_SIZE)
127  {
128  return true;
129  } else {
130  return false;
131  }
132 }
133 
135 {
136  pos = 0;
137 }
138 
140 {
141  return &buffer[0];
142 }
143 
145 {
146  return &buffer[pos];
147 }
148 
150 {
151  return BUFFER_SIZE + BUFFER_EXTRA - pos;
152 }
153 
154 } // namespace io
155 } // namespace nvbio
156