1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
// Copyright (c) 2002 David Muse
// See the file COPYING for more information
#include <rudiments/variablebuffer.h>
#include <stdio.h>
#include <string.h>
int main(int argc, const char **argv) {
// create a buffer
variablebuffer *vb=new variablebuffer(10,10);
// append some string sequences to the buffer and display the contents
// of the buffer byte by byte
vb->append((unsigned char *)"12345",5);
vb->append((unsigned char *)"12345",5);
vb->append((unsigned char *)"12345",5);
vb->append((unsigned char *)"12345",5);
vb->append((unsigned char *)"12345",5);
for (size_t i=0; i<vb->getSize(); i++) {
printf("%c",vb->getBuffer()[i]);
}
printf("\n");
// write 66666 to the buffer at position 0 and display it's contents
// byte by byte (the first 5 bytes should be overwritten)
vb->setPosition(0);
vb->write((unsigned char *)"66666",5);
for (size_t i=0; i<vb->getSize(); i++) {
printf("%c",vb->getBuffer()[i]);
}
printf("\n");
// write 66666 to the buffer at position 30 and display it's contents
// byte by byte, displaying nonprintable characters as .'s
// (there should be a gap in the buffer now containing random data)
vb->setPosition(30);
vb->write((unsigned char *)"66666",5);
for (size_t i=0; i<vb->getSize(); i++) {
if (vb->getBuffer()[i]>=' ' && vb->getBuffer()[i]<='~') {
printf("%c",vb->getBuffer()[i]);
} else {
printf(".");
}
}
printf("\n");
// set the current position to 50
vb->setPosition(50);
// Append 12345 to the buffer and display it's contents byte by byte,
// displaying nonprintable characters as .'s
// Since we used append() instead of write(), the data should not be
// written at position 50, but rather just at the current end of
// the buffer.
vb->append((unsigned char *)"12345",5);
for (size_t i=0; i<vb->getSize(); i++) {
if (vb->getBuffer()[i]>=' ' && vb->getBuffer()[i]<='~') {
printf("%c",vb->getBuffer()[i]);
} else {
printf(".");
}
}
printf("\n");
// Write 12345 to the buffer at the current position and display it's
// contents byte by byte, displaying nonprintable characters as .'s
// The current position should just be the end of the buffer, since
// we just appended. So calling write() here is equivalent to calling
// append.
vb->write((unsigned char *)"12345",5);
for (size_t i=0; i<vb->getSize(); i++) {
if (vb->getBuffer()[i]>=' ' && vb->getBuffer()[i]<='~') {
printf("%c",vb->getBuffer()[i]);
} else {
printf(".");
}
}
printf("\n");
// read 4 bytes from position 0 of the buffer and display them
unsigned char buffer[5];
buffer[4]=(unsigned char)NULL;
vb->setPosition(0);
unsigned long sizeread=vb->read(buffer,4);
printf("%ld: ",sizeread);
for (unsigned long i=0; i<sizeread; i++) {
printf("%c",buffer[i]);
}
printf("\n");
// read 4 bytes from position 5 of the buffer and display them
vb->setPosition(5);
sizeread=vb->read(buffer,4);
printf("%ld: ",sizeread);
for (unsigned long i=0; i<sizeread; i++) {
printf("%c",buffer[i]);
}
printf("\n");
// read 4 bytes from position 60 of the buffer and display them
// (since this is off of the end of the buffer, nothing should be
// displayed)
vb->setPosition(60);
sizeread=vb->read(buffer,4);
printf("%ld: ",sizeread);
for (unsigned long i=0; i<sizeread; i++) {
printf("%c",buffer[i]);
}
printf("\n");
delete vb;
}
|