home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_01 / 1001026b < prev    next >
Text File  |  1991-11-17  |  6KB  |  296 lines

  1. //////////////////////////////////////////////////////
  2. // Pinclass.cpp
  3. //
  4. // Source code for Wrapping C++ around Pinnacle
  5. //
  6. // David Brumbaugh, 1991
  7. //////////////////////////////////////////////////////
  8. #include <iostream.h>
  9. #include <conio.h>
  10. #include "PINCLAS.H"
  11.  
  12. Pfm_List::Pfm_List(char *database, char *table_name,
  13. size_t mbs)
  14. {
  15.     db = DB_Open(database,"rw",0);
  16.     if (db == NULL)
  17.     {
  18.                 cerr << "Error opening database:"
  19.                 << database << "Error = "
  20.                 << DB_ErrorString() << "\n";
  21.         cerr << "Strike a Key"; getch();
  22.         return;
  23.     }
  24.     table = DB_Table(db,table_name);
  25.     if (DB_Errno != DB_OK)
  26.     {
  27.                 cerr << "Error opening table:"
  28.                 << table_name << "Error = "
  29.                 << DB_ErrorString() << "\n";
  30.                 cerr << "Strike a Key";
  31.                 getch();
  32.         DB_Close(db);
  33.         return;
  34.     }
  35.     DB_FirstRow(table);
  36.     DB_NextRow(table,DBNEXT);
  37.     is_at_top = true;
  38.     is_at_bottom = false;
  39.     needs_closed = true;
  40.     default_key = NULL;
  41.     default_dbsearch = NULL;
  42.     max_buffer_size = mbs;
  43.     buffer = new char[max_buffer_size];
  44.  
  45. }
  46.  
  47. Pfm_List::Pfm_List(DB &open_db, char *table_name,
  48. size_t mbs)
  49. {
  50.     db = open_db;
  51.     
  52.     table = DB_Table(db,table_name);
  53.     if (DB_Errno != DB_OK)
  54.     {
  55.         cerr << "Error opening table:"
  56.         << table_name << "Error = "
  57.         << DB_ErrorString() << "\n";
  58.         cerr << "Strike a Key"; getch();
  59.         return;
  60.     }
  61.     DB_FirstRow(table);
  62.     DB_NextRow(table,DBNEXT);
  63.     is_at_top = true;
  64.     is_at_bottom = false;
  65.     needs_closed = false;
  66.     default_key = NULL;
  67.     default_dbsearch = NULL;
  68.     max_buffer_size = mbs;
  69.     buffer = new char[max_buffer_size];
  70.  
  71. }
  72.  
  73. Pfm_List::Pfm_List(DB &open_db, DBTAB &db_table,
  74. size_t mbs)
  75. {
  76.     db = open_db;
  77.     table = db_table;
  78.     DB_FirstRow(table);
  79.     DB_NextRow(table,DBNEXT);
  80.     is_at_top = true;
  81.     is_at_bottom = false;
  82.     needs_closed = false;
  83.     default_key = NULL;
  84.     default_dbsearch = NULL;
  85.     max_buffer_size = mbs;
  86.     buffer = new char[max_buffer_size];
  87.  
  88.  
  89. }
  90.  
  91. Pfm_List::~Pfm_List()
  92. {
  93.     if (needs_closed)
  94.         DB_Close(db);
  95.     delete buffer;
  96. }
  97.  
  98. Boolean Pfm_List::find(void *key, char *relation)
  99. {
  100.     if (default_key == NULL)
  101.         return(false);
  102.     default_dbsearch = DB_SearchObject(db,
  103.     DB_GetType(default_key),key,relation);
  104.     DB_FirstRow(table);
  105.     return((Boolean) DB_FindNext(default_key,
  106.     default_dbsearch,DBNEXT));
  107.  
  108. }
  109.  
  110. Boolean Pfm_List::find(char *col, char *relation,
  111. void *key)
  112. {
  113.     default_key = DB_Column(table,col);
  114.     return(find(key,relation));
  115. }
  116.  
  117. Boolean Pfm_List::find(void *key)
  118. {
  119.     return(find(key,"=="));
  120. }
  121.  
  122. void Pfm_List::prev()
  123. {
  124.     if (DB_NextRow(table,DBPREVIOUS) != DB_OK)
  125.     {
  126.         is_at_top = false;
  127.     }
  128.     else
  129.     {
  130.         is_at_top = true;
  131.         if (total() > 1L)
  132.         {
  133.             is_at_bottom = false;
  134.         }
  135.     }
  136. }
  137.  
  138. void Pfm_List::next()
  139. {
  140.     if (DB_NextRow(table,DBNEXT) != DB_OK)
  141.     {
  142.         is_at_bottom = false;
  143.     }
  144.     else
  145.     {
  146.         is_at_bottom = true;
  147.         if (total() > 1L)
  148.         {
  149.             is_at_top = false;
  150.         }
  151.     }
  152. }
  153.  
  154. void Pfm_List::top()
  155. {
  156.     DB_FirstRow(table);
  157.     DB_NextRow(table,DBNEXT);
  158.     is_at_top = true;
  159.     if (total() > 1L)
  160.         is_at_bottom = false;
  161.  
  162. }
  163.  
  164. void Pfm_List::end()
  165. {
  166.     DB_ForAllRows(table);
  167.     DB_NextRow(table,DBPREVIOUS);
  168.     is_at_bottom = true;
  169.     if (total() > 1L)
  170.         is_at_top = false;
  171. }
  172.  
  173.  
  174. void Pfm_List::add()
  175. {
  176.     DB_AddRow(table);
  177. }
  178.  
  179.  
  180. void Pfm_List::replace(char *field,
  181. char *value)
  182. {
  183.     DBCOL col = DB_Column(table,field);
  184.     DB_PutString(col,(unsigned char *) value);
  185. }
  186.  
  187. void Pfm_List::replace(char *field,
  188. long value)
  189. {
  190.     DBCOL col = DB_Column(table,field);
  191.     DB_PutInteger(col, value);
  192. }
  193.  
  194. void Pfm_List::replace(char *field, double value)
  195. {
  196.     DBCOL col = DB_Column(table,field);
  197.     DB_PutReal(col, value);
  198. }
  199.  
  200. void Pfm_List::remove()
  201. {
  202.     DB_DeleteRow(table);
  203.     next();
  204. }
  205.  
  206. long Pfm_List::tell()
  207. {
  208.     DBROWID thisrow, checkrow;
  209.     long position = 0L;
  210.  
  211.     thisrow = DB_CurrentRow(table);
  212.     top();
  213.     do
  214.     {
  215.         checkrow = DB_CurrentRow(table);
  216.         if (checkrow != thisrow)
  217.         {
  218.             ++position;
  219.             DB_NextRow(table,DBNEXT);
  220.         }
  221.     } while(checkrow != thisrow);
  222.  
  223.     return(position);
  224. }
  225.  
  226. Boolean Pfm_List::findnext()
  227. {
  228.   if (default_key == NULL || default_dbsearch == NULL)
  229.       return(false);
  230.  
  231.   return((Boolean) DB_FindNext(default_key,
  232.   default_dbsearch,DBNEXT));
  233. }
  234.  
  235. Boolean Pfm_List::findprev()
  236. {
  237.   if (default_key == NULL || default_dbsearch == NULL)
  238.       return(false);
  239.  
  240.   return((Boolean) DB_FindNext(default_key,
  241.   default_dbsearch,DBPREVIOUS));
  242. }
  243.  
  244. char *Pfm_List::get(char *field, char *value)
  245. {
  246.     DBCOL col = DB_Column(table, field);
  247.     if (value != NULL)
  248.     {
  249.         strcpy(value,(char *)DB_GetString(col));
  250.     }
  251.     return(value);
  252. }
  253.  
  254. long Pfm_List::get(char *field, long &value)
  255. {
  256.     DBCOL col = DB_Column(table, field);
  257.     value = DB_GetInteger(col);
  258.     return(value);
  259. }
  260.  
  261. double Pfm_List::get(char *field, double &value)
  262. {
  263.     DBCOL col = DB_Column(table, field);
  264.     value = DB_GetReal(col);
  265.     return(value);
  266. }
  267.  
  268. void *Pfm_List::get(char *field, void *value)
  269. {
  270.  
  271.     DBCOL col = DB_Column(table,field);
  272.     unsigned long type = DB_GetType(col);
  273.  
  274.     if (type == Integer)
  275.     {
  276.         DBINTEGER *dbint = (DBINTEGER *) value;
  277.         get(field,*dbint);
  278.     }
  279.     else if (type == Real)
  280.     {
  281.         DBREAL *dbreal = (DBREAL*) value;
  282.         get(field,*dbreal);
  283.     }
  284.     else if (type == String)
  285.     {
  286.         get(field,(DBSTRING) value);
  287.     }
  288.     else if (type == NBytes)
  289.     {
  290.             memcpy(value,DB_GetNBytes(col,NULL),
  291.             DB_GetSize(col));
  292.     }
  293.     return(value);
  294. }
  295.  
  296.