DataReelDataReelC++ Transition Guide

Examples › Database Examples

database_infohog

C++17 or laterDataReel 6.4.3Portable C++ (tested on Linux)

Legacy: examples_database/infohog/testprog.cpp · Modern: examples_modern/database/infohog/modern.cpp

Modern equivalent of the InfoHog contact database demo. InfoHog and POD stay (the record and file format); the code around them moves to C++17:

Legacy calls in this program

LegacyModern C++StandardNotes
strcpy(), strncpy(), memmove() into char arraysstd::string; std::array<char, N> + string_view::copy()C++11 / C++17
gxDatabasekeep gxDatabase: a stack object, errors as exceptions, static_assert(std::is_trivially_copyable_v<Record>)C++17The standard library has no database file; the file format is DataReel's.
gxBtree, DatabaseKeyB key classeskeep gxBtree; key classes with std::array storage and correct copies (rule of three); std::set for in-memory indexesC++11 / C++17DatabaseKeyB's copy keeps the other object's db_key pointer: define the copies.
POD, InfoHogkeep them; std::unique_ptr + release() for PODIndexFile ownership; std::optional membersC++17

From the transition tables.

Side by side

DataReel 6.4 (legacy)

examples_database/infohog/testprog.cpp 160 lines
#include "gxdlcode.h"

#include <cstring>
#include <iostream>
#include <string>
#include "infohog.h"

// Fixed-length key. InfoHog keys are stored in the index as raw
// bytes, so they must not hold pointers (no std::string).
struct ContactName
{
  ContactName() { memset(name, 0, sizeof(name)); }
  ContactName(const char *s) {
    memset(name, 0, sizeof(name));
    strncpy(name, s, sizeof(name) - 1);
  }
  int operator==(const ContactName &o) const { return strcmp(name, o.name) == 0; }
  int operator<(const ContactName &o) const { return strcmp(name, o.name) < 0; }
  int operator>(const ContactName &o) const { return strcmp(name, o.name) > 0; }

  char name[64];
};

typedef InfoHog<ContactName> Contact;
typedef InfoHogKey<ContactName> ContactKey;

// Member numbers. Member 0 is always the key.
enum { NAME = 0, EMAIL = 1, PHONE = 2, NOTES = 3, NUM_MEMBERS = 4 };

static void set_text(Contact &c, unsigned member, const std::string &s)
{
  c.SetMember(s.c_str(), (unsigned)s.size() + 1, member); // Keep the null
}

static std::string get_text(Contact &c, unsigned member)
{
  const char *p = (const char *)c.GetMember(member);
  unsigned len = c.GetMemberLen(member);
  if(!p || len == 0) return std::string();
  return std::string(p, strnlen(p, len));
}

static int open_pod(POD &pod, const char *data_file, const char *index_file)
{
  ContactKey key_type;
  gxDatabaseError err = pod.Open(data_file, index_file, key_type,
				 InfoHogNodeOrder, gxDBASE_READWRITE,
				 InfoHogUseIndexFile, InfoHogStaticArea,
				 InfoHogNumTrees);
  if(err != gxDBASE_NO_ERROR) {
    std::cerr << "Cannot open the database: " 
	      << gxDatabaseExceptionMessage(err) << "\n";
    return -1;
  }
  return 0;
}

static int add_contact(POD &pod, const char *name, const char *email,
		       const char *phone, const char *notes)
{
  ContactName key(name);
  Contact c(&pod, key, NUM_MEMBERS);
  set_text(c, EMAIL, email);
  set_text(c, PHONE, phone);
  set_text(c, NOTES, notes);
  if(c.FindObject()) { // Keys are unique
    std::cout << "Not added, " << name << " is already in the database\n";
    return -1;
  }
  if(!c.WriteObject(0)) {
    std::cout << "Could not add " << name << "\n";
    return -1;
  }
  std::cout << "Added " << name << " at address " 
	    << (long long)c.ObjectAddress() << "\n";
  return 0;
}

static int find_contact(POD &pod, const char *name, Contact &c)
{
  ContactName key(name);
  Contact probe(&pod, key, NUM_MEMBERS);
  if(!probe.FindObject()) return -1;
  if(!c.ReadObject(probe.ObjectAddress())) return -1;
  return 0;
}

static void list_contacts(POD &pod)
{
  ContactKey key, compare_key;
  gxBtree *btx = pod.Index();
  std::cout << "Contacts in key order:\n";
  if(!btx->FindFirst(key)) {
    std::cout << "  (none)\n";
    return;
  }
  do {
    Contact c(&pod, NUM_MEMBERS);
    if(c.ReadObject(key.ObjectID())) {
      std::cout << "  " << key.ObjectName().name << " <" 
		<< get_text(c, EMAIL) << "> " << get_text(c, PHONE) << "\n";
    }
  } while(btx->FindNext(key, compare_key));
}

int main(int argc, char **argv)
{
  const char *data_file = argc > 1 ? argv[1] : "contacts.ihd";
  const char *index_file = argc > 2 ? argv[2] : "contacts.ihx";

  POD pod;
  if(open_pod(pod, data_file, index_file) != 0) return 1;

  add_contact(pod, "Carol Smith", "carol@example.com", "555-0103", "Sales");
  add_contact(pod, "Alice Jones", "alice@example.com", "555-0101", "Engineering");
  add_contact(pod, "Bob Brown", "bob@example.com", "555-0102", "Support");
  add_contact(pod, "Alice Jones", "alice2@example.com", "555-0199", "Duplicate");
  list_contacts(pod);

  Contact c(&pod, NUM_MEMBERS);
  if(find_contact(pod, "Bob Brown", c) != 0) {
    std::cerr << "Bob Brown not found\n";
    return 1;
  }
  std::cout << "Found Bob Brown: notes = " << get_text(c, NOTES) << "\n";

  // Change one member in place. The record grows, so InfoHog moves it.
  std::string notes = "Support team lead since 2026";
  if(!c.ChangeMember(notes.c_str(), (unsigned)notes.size() + 1, NOTES)) {
    std::cerr << "ChangeMember failed\n";
    return 1;
  }
  std::cout << "Changed Bob Brown's notes\n";

  ContactName gone("Carol Smith");
  Contact del(&pod, gone, NUM_MEMBERS);
  if(del.FindObject() && del.DeleteObject()) {
    std::cout << "Deleted Carol Smith\n";
  }
  list_contacts(pod);
  pod.Close();

  // Reopen the files to show the changes were saved
  std::cout << "Reopening the database\n";
  POD pod2;
  if(open_pod(pod2, data_file, index_file) != 0) return 1;
  Contact again(&pod2, NUM_MEMBERS);
  if(find_contact(pod2, "Bob Brown", again) != 0) {
    std::cerr << "Bob Brown not found after reopening\n";
    return 1;
  }
  std::cout << "Bob Brown's notes: " << get_text(again, NOTES) << "\n";
  list_contacts(pod2);
  pod2.Close();
  return get_text(again, NOTES) == notes ? 0 : 1;
}
// ----------------------------------------------------------- // 
// ------------------------------- //
// --------- End of File --------- //
// ------------------------------- //

Modern C++ (C++17)

examples_modern/database/infohog/modern.cpp 133 lines
#include <cstring>
#include <filesystem>
#include <iostream>
#include <optional>
#include <string>
#include <string_view>
#include <type_traits>
#include <vector>

#include "infohog.h"

struct ContactName {                    // Raw bytes in the index
  char name[64];
  ContactName() { std::memset(name, 0, sizeof name); }
  ContactName(std::string_view s) : ContactName() {
    s.copy(name, std::min(s.size(), sizeof name - 1));
  }
  int operator==(const ContactName &o) const { return std::strcmp(name, o.name) == 0; }
  int operator<(const ContactName &o) const { return std::strcmp(name, o.name) < 0; }
  int operator>(const ContactName &o) const { return std::strcmp(name, o.name) > 0; }
};
static_assert(std::is_trivially_copyable_v<ContactName>);

using Contact = InfoHog<ContactName>;
using ContactKey = InfoHogKey<ContactName>;
enum Member : unsigned { NAME = 0, EMAIL = 1, PHONE = 2, NOTES = 3, NUM_MEMBERS = 4 };

void set_text(Contact &c, Member m, std::string_view s)
{
  std::string copy(s);                    // SetMember() needs the null
  c.SetMember(copy.c_str(), unsigned(copy.size() + 1), m);
}

std::optional<std::string> get_text(Contact &c, Member m)
{
  const char *p = reinterpret_cast<const char *>(c.GetMember(m)); // Bytes
  unsigned len = c.GetMemberLen(m);
  if(!p || len == 0) return std::nullopt;
  return std::string(p, strnlen(p, len));
}

bool open_pod(POD &pod, const std::filesystem::path &data, const std::filesystem::path &index)
{
  ContactKey key_type;
  gxDatabaseError err = pod.Open(data.string().c_str(), index.string().c_str(), key_type,
                                 InfoHogNodeOrder, gxDBASE_READWRITE,
                                 InfoHogUseIndexFile, InfoHogStaticArea, InfoHogNumTrees);
  if(err != gxDBASE_NO_ERROR)
    std::cout << "Cannot open the database: " << gxDatabaseExceptionMessage(err) << "\n";
  return err == gxDBASE_NO_ERROR;
}

struct NewContact { std::string_view name, email, phone, notes; };

bool add_contact(POD &pod, const NewContact &n)
{
  Contact c(&pod, ContactName(n.name), NUM_MEMBERS);
  set_text(c, EMAIL, n.email);
  set_text(c, PHONE, n.phone);
  set_text(c, NOTES, n.notes);
  if(c.FindObject()) {
    std::cout << "Not added, " << n.name << " is already in the database\n";
    return false;
  }
  if(!c.WriteObject(0)) { std::cout << "Could not add " << n.name << "\n"; return false; }
  std::cout << "Added " << n.name << "\n";
  return true;
}

bool find_contact(POD &pod, std::string_view name, Contact &c)
{
  Contact probe(&pod, ContactName(name), NUM_MEMBERS);
  return probe.FindObject() && c.ReadObject(probe.ObjectAddress());
}

void list_contacts(POD &pod)
{
  ContactKey key, compare_key;
  gxBtree *btx = pod.Index();
  std::cout << "Contacts in key order:\n";
  for(bool ok = btx->FindFirst(key); ok; ok = btx->FindNext(key, compare_key)) {
    Contact c(&pod, NUM_MEMBERS);
    if(c.ReadObject(key.ObjectID()))
      std::cout << "  " << key.ObjectName().name << " <"
                << get_text(c, EMAIL).value_or("?") << "> "
                << get_text(c, PHONE).value_or("?") << "\n";
  }
}

int main(int argc, char **argv)
{
  namespace fs = std::filesystem;
  const fs::path data = argc > 1 ? argv[1] : "modern_contacts.ihd";
  const fs::path index = argc > 2 ? argv[2] : "modern_contacts.ihx";
  const std::string notes = "Support team lead since 2026";
  std::optional<std::string> saved;
  {
    POD pod;
    if(!open_pod(pod, data, index)) return 1;
    const std::vector<NewContact> contacts = {
      {"Carol Smith", "carol@example.com", "555-0103", "Sales"},
      {"Alice Jones", "alice@example.com", "555-0101", "Engineering"},
      {"Bob Brown", "bob@example.com", "555-0102", "Support"},
      {"Alice Jones", "alice2@example.com", "555-0199", "Duplicate"}};
    for(const auto &c : contacts) add_contact(pod, c);
    list_contacts(pod);

    Contact c(&pod, NUM_MEMBERS);
    if(!find_contact(pod, "Bob Brown", c)) { std::cout << "Bob Brown not found\n"; return 1; }
    std::cout << "Found Bob Brown: notes = " << get_text(c, NOTES).value_or("") << "\n";
    if(!c.ChangeMember(notes.c_str(), unsigned(notes.size() + 1), NOTES)) return 1;

    Contact del(&pod, ContactName("Carol Smith"), NUM_MEMBERS);
    if(del.FindObject() && del.DeleteObject()) std::cout << "Deleted Carol Smith\n";
    list_contacts(pod);
  } // ~POD() closes the files

  std::cout << "Reopening the database\n";
  {
    POD pod;
    if(!open_pod(pod, data, index)) return 1;
    Contact again(&pod, NUM_MEMBERS);
    if(find_contact(pod, "Bob Brown", again)) saved = get_text(again, NOTES);
    std::cout << "Bob Brown's notes: " << saved.value_or("(missing)") << "\n";
    list_contacts(pod);
  }
  if(argc <= 1) { fs::remove(data); fs::remove(index); }
  return saved == notes ? 0 : 1;
}
// ----------------------------------------------------------- // 
// ------------------------------- //
// --------- End of File --------- //
// ------------------------------- //

Build and run

cmake -S examples_modern -B build-modern -DCMAKE_CXX_STANDARD=17
cmake --build build-modern --target database_infohog
./build-modern/database_infohog

Output of the modern program

From run_modern_tests.sh on RHEL 10, GCC 14, C++20.

Added Carol Smith
Added Alice Jones
Added Bob Brown
Not added, Alice Jones is already in the database
Contacts in key order:
  Alice Jones <alice@example.com> 555-0101
  Bob Brown <bob@example.com> 555-0102
  Carol Smith <carol@example.com> 555-0103
Found Bob Brown: notes = Support
Deleted Carol Smith
Contacts in key order:
  Alice Jones <alice@example.com> 555-0101
  Bob Brown <bob@example.com> 555-0102
Reopening the database
Bob Brown's notes: Support team lead since 2026
Contacts in key order:
  Alice Jones <alice@example.com> 555-0101
  Bob Brown <bob@example.com> 555-0102