
/**********************************************************//**
 **
 ** @file test/src/tutorial.cpp
 **
 ** Copyright (C) 2008  Xpace, LLC.  All rights reserved.
 **
 **************************************************************/

#include <strstream>

#include "util/bitstream.h"
#include "util/file.h"
#include "data/Store/tablecursor.h"
#include "index/indexlist.h"

void show_record
  (const Xpace::TableCursor& tc)
{
  for (Xpace::uint i(0); i < tc.getColumnCount(); ++i)
  {
    Xpace::String str(tc.getCellStr(i));
    wprintf(L"%s, ", str.toUtf16());
  }
  printf("\n");
}
   

int main
  (int argc,
   char** argv)
{
  char* path = (argc < 2) ? "." : argv[1];
  std::strstream config;
  config << "<nyse><store type=\"NYSE\" di=\"" << path << "\\quotes25.di\""
                                   << " dd=\"" << path << "\\quotes25.dd\" />"
                        << "<index_list ii=\"" << path << "\\quotes25.dr\"" 
                                   << " id=\"" << path << "\\quotes25.dx\" />" 
                                   << "</nyse>" << '\0';
 
  try
  {
    // open table database
    Xpace::TableCursor tc(Xpace::String(config.str()));
    
    // open index list
    Xpace::IndexList il(Xpace::String(config.str()));

    // zeroth index is stock symbol
    Xpace::Index idx(il.openIndex(0));

    // search the index
    Xpace::Index::Iter ii(idx.begin("DWTI"));

    // get a reference list for that term
    Xpace::RefListCursor rlc(ii.getRefList());

    // show each record in the reference list
    do
    {
      tc.moveToRow(*rlc);
      show_record(tc);
    }
    while (rlc.move(1));  
  }

  catch (Xpace::Exception& ex)
  {
    for (Xpace::Exception& e(ex); !!e; e = e.why())
      printf("Exception %s\n\n", e.what());
  }

  return 0;
}



