ONPOSIX  2.0
 All Classes Namespaces Files Functions Variables Enumerator Friends Macros Pages
Buffer.cpp
Go to the documentation of this file.
1 /*
2  * Buffer.cpp
3  *
4  * Copyright (C) 2012 Evidence Srl - www.evidence.eu.com
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <stdexcept>
22 #include <cstring>
23 
24 #include "Buffer.hpp"
25 
26 namespace onposix {
27 
28 /**
29  * \brief Constructor. It checks size and allocates memory.
30  *
31  * @param size size of the buffer
32  * @exception invalid_argument in case of wrong size
33  */
34 Buffer::Buffer(unsigned long int size): size_(size)
35 {
36  if (size == 0)
37  throw std::invalid_argument("Buffer with size 0");
38  else
39  data_ = new char[size_];
40 }
41 
42 /**
43  * Destructor.
44  * It deallocates memory.
45  */
47 {
48  if (size_ != 0)
49  delete[] data_;
50 }
51 
52 /**
53  * \brief Method to access a specific byte of the buffer.
54  *
55  * It checks the argument and throws an exception in case of out of range.
56  * @param p position in the buffer
57  * @return the byte at the specified position
58  * @exception out_of_range in case the position is out of boundary
59  */
60 char& Buffer::operator[](unsigned long int p)
61 {
62  if (p > (size_ - 1))
63  throw std::out_of_range("Operation on buffer out of boundary");
64  else
65  return data_[p];
66 }
67 
68 
69 /**
70  * \brief Method to fill the buffer
71  *
72  * It takes the content from a specified address.
73  * @param src source of the content used to fill the buffer
74  * @param size number of bytes to be copied
75  * @return number of bytes copied
76  * @exception out_of_range in case the size is greater than the size of the buffer
77  * @exception invalid_argument in case the source points to NULL
78  */
79 unsigned long int Buffer::fill(const char* src, unsigned long int size)
80 {
81  if (size > size_)
82  throw std::out_of_range("Operation on buffer out of boundary");
83  else if (src == 0)
84  throw std::invalid_argument("Attempt to copy from NULL pointer");
85  else if (size == 0)
86  return 0;
87  std::memcpy (data_, src, size);
88  return size;
89 }
90 
91 /**
92  * \brief Method to fill the buffer with the content of another buffer
93  *
94  * The number of bytes copied is the minimum between the size of the buffer and
95  * the size provided as argument.
96  * @param b pointer to the buffer whose data must be used to fill this buffer
97  * @param size size of bytes to be copied
98  * @return number of bytes copied
99  * @see Buffer::fill(const char* src, unsigned long int size)
100  */
101 unsigned long int Buffer::fill(Buffer* b, unsigned long int size)
102 {
103  unsigned long int min;
104  if (size < b->getSize())
105  min = size;
106  else
107  min = b->getSize();
108  return fill (b->getBuffer(), min);
109 }
110 
111 
112 
113 /**
114  * \brief Method to compare two buffers' content
115  *
116  * It compares the content of this buffer with the content of another buffer
117  * @param b the buffer against whose content it must be compared
118  * @param size size of bytes to be compared
119  * @return true if the contents match, false otherwise
120  * @exception out_of_range in case the given size is greater than the size of one of the two
121  * buffers
122  */
123 bool Buffer::compare(Buffer* b, unsigned long int size)
124 {
125  if (size > size_ || size > b->getSize())
126  throw std::out_of_range("Operation on buffer out of boundary");
127  return !std::memcmp(data_, b->getBuffer(), size);
128 }
129 
130 /**
131  * \brief Method to compare the content of the buffer against a buffer
132  * in memory.
133  *
134  * It compares the content of this buffer with the content at a
135  * specified memory address
136  * @param s pointer to the memory address against whose content it must be compared
137  * @param size size of bytes to be compared
138  * @return true if the contents match, false otherwise
139  * @exception out_of_range in case the given size is greater than the buffer
140  * buffers
141  */
142 bool Buffer::compare(const char* s, unsigned long int size)
143 {
144  if (size > size_)
145  throw std::out_of_range("Operation on buffer out of boundary");
146  return !memcmp(data_, s, size);
147 }
148 
149 
150 
151 } /* onposix */