[sword-devel] task

Bobby Nations sword-devel@crosswire.org
Sat, 08 Sep 2001 17:04:09 -0500


This is a multi-part message in MIME format.
--------------3B1B71C82BE5385DA2F821A3
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Troy,

I took a shot at it, but it may not be what you're expecting.  See, I just
rewrote it more or less completely.  Gone are the calloc's and memcopy's.
Instead, I've written it to use the STL vector container.  I can do it the
other way as well, but this is much cleaner to look at.

Is there a reason why you didn't use the STL in the first place?

Also, I modified the test program slightly since the original never called
the removeEntry function.

I'm now able to add and entry, print it out, and then delete it via the
testblocks program.  Just curious, but it sure would be helpful for us
newbies if there were a set of unit tests (something like CppUnit would
work nicely).

Anyway, I've attached the changed files for your perusal.

Cheers,

Bobby Nations


"Troy A. Griffitts" wrote:

> Ok, here's some things that I've been meaning to get around to that I
> think might be a good, isolated place for people to begin getting
> involved in.
>
> There is a new class I started and is almost complete, but still has a
> few bugs.  It will be used in compression of lexicons and dictionaries,
> but that isn't necessary knowledge to complete the class.
>
> The class can be found at sword/src/modules/common/entriesblk.cpp
>
> The test program for this class is at sword/tests/testblocks.cpp
>
> It's been a while since I looked at the code.  I just ran the test
> program.  The remove option doesn't seem to work.
>
> Anyone interested in completing this class, review the code and is you
> STILL what to complete this class :), please speak up!
>
>         Thanks,
>                 -Troy.

--------------3B1B71C82BE5385DA2F821A3
Content-Type: text/plain; charset=us-ascii;
 name="testblocks.cpp"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="testblocks.cpp"

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


void addEntry(EntriesBlock *eb) {
	string input;
	string body;
	char line[1024];
	cout << "\nEnter new Entry's text. '.' on an empty line to finish:\n";
	do {
		cout << "> ";
		gets(line);
		input = line;
		if (input.compare("."))
			body.append(input);
	}
	while (input.compare("."));
	cout << "Adding new entry.  Index is: " << eb->addEntry(body.c_str()) << "\n\n";
}


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


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


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

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

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

	do {
		cout << "[" << eb->getCount() << "] > ";
		gets(line);
		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 'q': break;
				case '?':
				default:
					cout << "\n a - add a new entry\n";
					cout << " p <entry_index> - print entry\n";
					cout << " r <entry_index> - remove entry\n";
					cout << " q - quit\n\n";
					break;
			}
		}
	}
	while (input.compare("q"));

	delete eb;

	return 0;
}

--------------3B1B71C82BE5385DA2F821A3
Content-Type: text/plain; charset=us-ascii;
 name="entriesblk.cpp"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="entriesblk.cpp"

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


//
// Constructors
//

//
// Destructor
//

//
// Accessors
//
int EntriesBlock::getCount() { return m_entries.size(); }
string EntriesBlock::getEntry( int index ) { return m_entries[ index ]; }


//
// Mutators
//
int EntriesBlock::addEntry( string str ) 
{
    m_entries.push_back( str );
    return m_entries.size();
}

void EntriesBlock::removeEntry( int index ) 
{ 
    // Shift everything back over the entrie to be deleted
    int last = m_entries.size() - 1; 
    for ( int i=index; i<last; i++ )
    {
        m_entries[ i ] = m_entries[ i+1 ];
    }
    // Remove the empty entry at the end
    m_entries.pop_back();
}



--------------3B1B71C82BE5385DA2F821A3
Content-Type: text/plain; charset=us-ascii;
 name="entriesblk.h"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="entriesblk.h"

#ifndef ENTRIESBLK_H
#define ENTRIESBLK_H

#include <vector>
#include <string>

class EntriesBlock 
{
    public:
        //
        // Constructors & Destructor
        //

        //
        // Accessors
        //
        int getCount();
        string getEntry( int index );

        //
        // Mutators
        //
        int addEntry( string entry );
        void removeEntry( int index );

    private:
        //
        // Member variables
        //
        vector< string > m_entries;
};


#endif

--------------3B1B71C82BE5385DA2F821A3--