Skip to content

Commit 47278eb

Browse files
Introduce 'Loaded' stamps
1 parent e13d50f commit 47278eb

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ blob-stamper-clean:
4141
clean: blob-stamper-clean
4242
$(MAKE) -C blobstamper clean
4343
$(MAKE) -C t clean
44+
$(MAKE) -C examples clean
4445
$(MAKE) -C libtappp clean
4546
@echo Clean done!
4647

blobstamper/stamp.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,27 @@
2424

2525
#include "blob.h"
2626
#include "stamp.h"
27+
28+
29+
void
30+
StampBase::Load(Blob &blob)
31+
{
32+
33+
if (minSize() > blob.Size())
34+
{
35+
throw OutOfData();
36+
}
37+
38+
size_t res_size;
39+
if (isUnbounded())
40+
{
41+
res_size = blob.Size();
42+
} else
43+
{
44+
res_size = maxSize();
45+
if (res_size > blob.Size())
46+
res_size = blob.Size();
47+
}
48+
Blob *pb = new Blob(blob.ShiftBytes(res_size));
49+
bitten_blob = std::unique_ptr<Blob>(pb);
50+
}

blobstamper/stamp.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,21 @@
2222
#include <string>
2323
#include <list>
2424
#include <vector>
25+
#include <memory>
2526

2627
#include "helpers.h"
2728

2829

2930
class StampBase
3031
{
32+
protected:
33+
std::unique_ptr<Blob> bitten_blob;
3134
public:
3235
virtual int minSize() = 0;
3336
virtual int maxSize() = 0;
3437

38+
void Load(Blob &blob);
39+
3540
bool isFixedSize() {return minSize() == maxSize();}
3641
bool isVariated() {return ! isFixedSize() && ! isUnbounded();}
3742
bool isUnbounded() {return maxSize() == -1;}
@@ -42,20 +47,23 @@ class StampBaseStr: public virtual StampBase
4247
{
4348
public:
4449
virtual std::string ExtractStr(Blob &blob) = 0;
50+
std::string UnloadStr() {return ExtractStr(*bitten_blob);};
4551
};
4652

4753

4854
class StampBaseBin: public virtual StampBase
4955
{
5056
public:
5157
virtual std::vector<char> ExtractBin(Blob &blob) = 0;
58+
std::vector<char> UnloadBin() {return ExtractBin(*bitten_blob);};
5259
};
5360

5461

5562
template<class T> class StampBasePV: public StampBaseBin
5663
{
5764
public:
5865
virtual sized_ptr<T> ExtractPValue(Blob &blob) = 0;/* Shoud be defined by derived classes*/
66+
sized_ptr<T> UnloadPValue() {return ExtractPValue(*bitten_blob);};
5967
virtual std::vector<char> ExtractBin(Blob &blob) override;
6068
};
6169

@@ -70,9 +78,12 @@ StampBasePV<T>::ExtractBin(Blob &blob)
7078
}
7179

7280
template<class T> class StampBaseV: public StampBasePV<T>
81+
,public virtual StampBase //FIXME I do not understand why do we need it here, but wihtout it, it does not build
7382
{
7483
public:
7584
virtual T ExtractValue(Blob &blob) = 0;/* Shoud be defined by derived classes*/
85+
T UnloadValue() {return ExtractValue(*bitten_blob);};
86+
7687
virtual std::vector<char> ExtractBin(Blob &blob) override;
7788
virtual sized_ptr<T> ExtractPValue(Blob &blob) override;
7889
};

examples/example01a.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include<stdio.h>
2+
#include<string.h>
3+
4+
#include<string>
5+
#include<iostream>
6+
7+
#include<blobstamper/blobstamper.h>
8+
9+
int main()
10+
{
11+
char data[] = "abcde";
12+
Blob blob(data, strlen(data));
13+
StampArithm<short int> stamp;
14+
stamp.Load(blob);
15+
16+
std::string s = stamp.UnloadStr();
17+
18+
std::cout << "Stamp minSize: " << stamp.minSize() << "\n";
19+
std::cout << "Stamp maxSize: " << stamp.maxSize() << "\n";
20+
std::cout << "Extracted value: " << s <<"\n";
21+
std::cout << "Remaining blob: " << blob.asString() << "\n";
22+
}

examples/example02a.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include<stdio.h>
2+
#include<string.h>
3+
4+
#include<string>
5+
#include<iostream>
6+
7+
#include<blobstamper/blobstamper.h>
8+
9+
int main()
10+
{
11+
char data[] = "abcdefjhi";
12+
Blob blob(data, strlen(data));
13+
StampArithm<short int> stamp1;
14+
StampArithm<short int> stamp2;
15+
StampArithm<short int> stamp3;
16+
StampArithm<short int> stamp4;
17+
18+
stamp1.Load(blob);
19+
stamp2.Load(blob);
20+
stamp3.Load(blob);
21+
stamp4.Load(blob);
22+
23+
std::string s = stamp1.UnloadStr();
24+
std::vector<char> vec= stamp2.UnloadBin();
25+
short int i = stamp3.UnloadValue();
26+
27+
sized_ptr<short int> spi = stamp4.UnloadPValue();
28+
short int *pi = spi;
29+
30+
std::cout << "String value: '" << s <<"'\n";
31+
std::cout << "Bin value: '" << vec[0] <<"', '" << vec[1] <<"'\n";
32+
std::cout << "Value: " << i <<"\n";
33+
std::cout << "PValue: " << *pi <<"\n";
34+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy