File: testblocks.cpp

package info (click to toggle)
sword 1.7.3+dfsg-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 12,584 kB
  • ctags: 5,515
  • sloc: cpp: 41,736; sh: 11,924; ansic: 6,447; objc: 1,362; makefile: 1,239; cs: 897; java: 821; perl: 784; pascal: 681; tcl: 350; xml: 180; python: 79
file content (119 lines) | stat: -rw-r--r-- 3,035 bytes parent folder | download
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
119
/******************************************************************************
 *
 *  testblocks.cpp -	
 *
 * $Id: testblocks.cpp 2833 2013-06-29 06:40:28Z chrislit $
 *
 * Copyright 2001-2013 CrossWire Bible Society (http://www.crosswire.org)
 *	CrossWire Bible Society
 *	P. O. Box 2528
 *	Tempe, AZ  85280-2528
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation version 2.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 */

#include <entriesblk.h>
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
#ifndef NO_SWORD_NAMESPACE
using namespace sword;
#endif

void addEntry(EntriesBlock *eb) {
	string input;
	string body;
	char line[1024], *c;
	std::cout << "\nEnter new Entry's text. '.' on an empty line to finish:\n";
	do {
		std::cout << "> ";
		c = fgets(line, 1000, stdin);
		if (c == NULL) {
			std::cerr << "ERROR: fgets failed during addEntry\n";
		} else {
			input = line;
			if (input.compare("."))
				body.append(input);
		}
	}
	while (input.compare("."));
	std::cout << "Adding new entry.  Index is: " << eb->addEntry(body.c_str()) << "\n\n";
}


void printEntry(EntriesBlock *eb, int index) {
	if (index < eb->getCount()) {
		std::cout << "Contents of entry [" << index << "]:\n";
		std::cout << eb->getEntry(index) << "\n";
	}
	else std::cout << "Invalid entry number\n\n";
}


void printSize(EntriesBlock *eb) {
	unsigned long size;
	eb->getRawData(&size);
	std::cout << "Size of raw data: " << size << "\n\n";
}


void removeEntry(EntriesBlock *eb, int index) {
	if (index < eb->getCount()) {
		std::cout << "Removing entry [" << index << "]\n";
		eb->removeEntry(index);
	}
	else std::cout << "Invalid entry number\n\n";
}


int main(int argc, char **argv) {

	EntriesBlock *eb = new EntriesBlock();
	string input;
	char line[1024], *c;

	std::cout << "Initial entry count should be 0: " << eb->getCount() << "\n";

	do {
		std::cout << "[" << eb->getCount() << "] > ";
		c = fgets(line, 1000, stdin);
		if (c == NULL) {
			std::cerr << "ERROR: fgets failed in main\n";
		} else {
			input = line;
			if (input.length() > 0) {
				switch (input[0]) {
					case 'a': addEntry(eb); break;
					case 'p':	printEntry(eb, atoi(input.c_str()+1)); break;
					case 'r':	removeEntry(eb, atoi(input.c_str()+1)); break;
					case 's': printSize(eb); break;
					case 'q': break;
					case '?':
					default:
						std::cout << "\n a - add a new entry\n";
						std::cout << " p <entry_index> - print entry\n";
						std::cout << " r <entry_index> - remove entry\n";
						std::cout << " s - print size of raw data\n";
						std::cout << " q - quit\n\n";
						break;
				}
			}
		}
	}
	while (input.compare("q"));

	delete eb;

	return 0;
}