Fork me on GitHub

source: svn/trunk/Utilities/frog/Utils.h@ 583

Last change on this file since 583 was 583, checked in by cp3-support, 13 years ago

FROG 3.0

File size: 2.5 KB
RevLine 
[583]1// FROG_Element_Base.h: Contains everything related to an element to be written in a file :
2// type, size, data
3// And a vector of daughters
4//////////////////////////////////////////////////////////////////////
5
6#ifndef _FROG_UTILS_H__
7#define _FROG_UTILS_H__
8
9#define _CRT_SECURE_NO_WARNINGS 1
10
11#include <stdio.h>
12#include <stdarg.h>
13#include <string.h>
14#include <vector>
15#include <map>
16#include <algorithm>
17#include <iostream>
18#include <math.h>
19
20
21
22struct ByteBuffer{
23 unsigned char* buffer;
24 unsigned int capacity_;
25 unsigned int position_;
26
27 ByteBuffer(){
28 buffer = NULL;
29 capacity_ = 0;
30 position_ = 0;
31 }
32
33 ByteBuffer(int size){
34 buffer = new unsigned char[size];
35 capacity_ = size;
36 position_ = 0;
37 }
38
39 ~ByteBuffer(){
40 //printf("Destructor called\n");
41 }
42
43 void clear(){if(buffer!=NULL)delete [] buffer;}
44
45 unsigned int remaining(){return capacity_-position_;}
46 unsigned int capacity(){return capacity_;}
47 unsigned int position(){return position_;}
48 void position(unsigned int pos){position_ = pos;}
49 void flip(){position_ = 0;}
50
51 unsigned char get(unsigned int pos){return buffer[pos];}
52 unsigned char get(){unsigned char data=get(position_); position_++; return data;}
53
54 void put(unsigned int pos, unsigned char data){buffer[pos] = data;}
55 void put(unsigned char data){put(position_, data); position_++;}
56
57 void putByte(unsigned int pos, unsigned char data){put(pos, data);}
58 void putByte(unsigned char data){put(data);}
59
60 void putUShort(unsigned int pos, unsigned short data){memcpy(buffer+pos, (void*)&data, sizeof(data));}
61 void putUShort(unsigned short data){putUShort(position_, data); position_+=sizeof(data);}
62
63 void putInt(unsigned int pos, int data){memcpy(buffer+pos, (void*)&data, sizeof(data));}
64 void putInt(int data){putInt(position_, data); position_+=sizeof(data);}
65
66 void putUInt(unsigned int pos, unsigned int data){memcpy(buffer+pos, (void*)&data, sizeof(data));}
67 void putUInt(unsigned int data){putUInt(position_, data); position_+=sizeof(data);}
68
69 void putULong(unsigned int pos, unsigned long long data){memcpy(buffer+pos, (void*)&data, sizeof(data));}
70 void putULong(unsigned long long data){putULong(position_, data); position_+=sizeof(data);}
71
72 void putFloat(unsigned int pos, float data){memcpy(buffer+pos, (void*)&data, sizeof(data));}
73 void putFloat(float data){putFloat(position_, data); position_+=sizeof(data);}
74
75 void put(ByteBuffer& obj){
76 for(unsigned int i=obj.position();i<obj.capacity() && i-obj.position()+position_<capacity_;i++){
77 put(obj.get());
78 }
79 }
80};
81
82#endif
83
Note: See TracBrowser for help on using the repository browser.