ccexample.cc File Reference

#include <stdio.h>
#include <iostream>
#include "pi-source.h"
#include "pi-file.h"
#include "pi-todo.h"
#include "pi-memo.h"
#include "pi-datebook.h"
#include "pi-address.h"

Include dependency graph for ccexample.cc:

Go to the source code of this file.

Namespaces

namespace  std

Defines

#define bool   int
#define false   0
#define true   1

Functions

void printCategoryNames (appInfo_t &ai)
void memos (pi_file *pf)
void todos (pi_file *pf)
char * freqToStr (const int i)
void prettyPrintRepeat (appointment_t *appt)
void datebook (pi_file *pf)
void addresses (pi_file *pf)
int main (int argc, char **argv)

Variables

static char * days []
static char * months []


Define Documentation

#define bool   int

Definition at line 13 of file ccexample.cc.

#define false   0

Definition at line 14 of file ccexample.cc.

Referenced by org::gnu::pilotlink::DatebookRecord::resetVars().

#define true   1

Definition at line 15 of file ccexample.cc.


Function Documentation

void addresses ( pi_file pf  ) 

Definition at line 379 of file ccexample.cc.

References buf, dlpRecAttrArchived, dlpRecAttrDeleted, entnum, nentries, packed, packedBuf, pi_file_get_app_info(), pi_file_get_entries(), pi_file_read_record(), printCategoryNames(), and size.

Referenced by main().

00380 {
00381 
00382         void *app_info;
00383         int app_info_size;
00384 
00385         if (pi_file_get_app_info(pf, &app_info, &app_info_size) < 0) {
00386                 cerr << "Unable to get app info" << endl;
00387                 return;
00388         }
00389 
00390         addressAppInfo_t aai(app_info);
00391 
00392         printCategoryNames(aai);
00393 
00394         // packed is now a pointer to an area of memory that is
00395         // ADDRESS_APP_INFO_SIZE bytes long.  You are responsible for release this
00396         // memory via delete
00397         unsigned char* packed = (unsigned char*)aai.pack();
00398         delete[] packed;
00399 
00400         int nentries;
00401 
00402         pi_file_get_entries(pf, &nentries);
00403 
00404         unsigned char *buf, packedBuf[0xffff];
00405         int size, attrs, cat;
00406         recordid_t uid;
00407         address_t address;
00408         char *phonePtr;
00409 
00410         for (int entnum = 0; entnum < nentries; entnum++) {
00411                 if (pi_file_read_record(pf, entnum, (void **) &buf, &size,
00412                                         &attrs, &cat, &uid) < 0) {
00413                         cout << "Error reading record number " << entnum <<
00414                             endl;
00415                         return;
00416                 }
00417 
00418                 /* Skip deleted records */
00419                 if ((attrs & dlpRecAttrDeleted)
00420                     || (attrs & dlpRecAttrArchived))
00421                         continue;
00422 
00423                 address.unpack(buf);
00424 
00425                 cout << "Category: " << aai.category(cat) << endl;
00426                 phonePtr = address.entry(address_t::lastName);
00427                 if (phonePtr)
00428                         cout << "Last Name: " << phonePtr << endl;
00429                 for (cat = address_t::phone1; cat <= address_t::phone5;
00430                      cat++)
00431                         if ((phonePtr =
00432                              address.entry((address_t::labelTypes_t) cat)))
00433                                 cout << "Phone:  " << phonePtr << endl;
00434 
00435                 // Just like the memo app, you can pack it like this...
00436                 unsigned char* packed = (unsigned char*)address.pack(&size);
00437                 delete[] packed;
00438 
00439                 // ... or like this
00440                 size = sizeof(packedBuf);
00441                 if (address.pack(packedBuf, &size) == NULL)
00442                         cerr << "Record number " << (entnum +
00443                                                      1) << " too big for "
00444                             << "the buffer you passed in." << endl;
00445         }
00446 }

Here is the call graph for this function:

void datebook ( pi_file pf  ) 

Definition at line 284 of file ccexample.cc.

References buf, days, dlpRecAttrArchived, dlpRecAttrDeleted, entnum, nentries, packed, pi_file_get_app_info(), pi_file_get_entries(), pi_file_read_record(), prettyPrintRepeat(), and size.

Referenced by main().

00285 {
00286         void *app_info;
00287         int app_info_size;
00288 
00289         if (pi_file_get_app_info(pf, &app_info, &app_info_size) < 0) {
00290                 cerr << "Unable to get app info" << endl;
00291                 return;
00292         }
00293 
00294         appointmentAppInfo_t aai(app_info);
00295 
00296         // packed is now a pointer to an area of memory that is
00297         // APPOINTMENT_APP_INFO_SIZE bytes long.  You are responsible for
00298         // release this memory via delete
00299         unsigned char* packed = (unsigned char*)aai.pack();
00300         delete[] packed;
00301 
00302         int nentries;
00303 
00304         pi_file_get_entries(pf, &nentries);
00305 
00306         unsigned char *buf;
00307         int size, attrs, cat;
00308         recordid_t uid;
00309         tm *timePtr;
00310         appointment_t appt;
00311 
00312         for (int entnum = 0; entnum < nentries; entnum++) {
00313                 if (pi_file_read_record(pf, entnum, (void **) &buf, &size,
00314                                         &attrs, &cat, &uid) < 0) {
00315                         cout << "Error reading record number " << entnum <<
00316                             endl;
00317                         return;
00318                 }
00319 
00320                 /* Skip deleted records */
00321                 if ((attrs & dlpRecAttrDeleted)
00322                     || (attrs & dlpRecAttrArchived))
00323                         continue;
00324 
00325                 appt.unpack(buf);
00326 
00327                 if (appt.untimed() == false) {
00328                         cout << "Begin Time:  " << asctime(appt.
00329                                                            beginTime());
00330                         cout << "End Time:    " << asctime(appt.endTime());
00331                 } else
00332                         cout << "Untimed event" << endl;
00333 
00334                 if (appt.hasAlarm()) {
00335                         cout << "The alarm is set to go off " << appt.
00336                             advance() << " ";
00337 
00338                         switch (appt.advanceUnits()) {
00339                           case appointment_t::minutes:
00340                                   cout << "minutes";
00341                                   break;
00342                           case appointment_t::hours:
00343                                   cout << "hours";
00344                                   break;
00345                           case appointment_t::days:
00346                                   cout << "days";
00347                           default:
00348                                   cout << "(internal error)";
00349                         }
00350 
00351                         cout << " before the event" << endl;
00352                 } else
00353                         cout << "There is not an alarm set for this event"
00354                             << endl;
00355 
00356                 if (appt.repeatType() != appointment_t::none)
00357                         prettyPrintRepeat(&appt);
00358                 else
00359                         cout << "Event does not repeat" << endl;
00360 
00361                 if ((timePtr = appt.exceptions())) {
00362                         size = appt.numExceptions();
00363                         cout << "I seem to have " << size << " exceptions:"
00364                             << endl;
00365                         for (int i = 0; i < size; i++)
00366                                 cout << "\t" << asctime(&timePtr[i]);
00367                 }
00368 
00369                 cout << "Description: " << appt.description() << endl;
00370 
00371                 if (appt.note())
00372                         cout << "Note: " << appt.note() << endl;
00373 
00374                 cout << endl;
00375         }
00376 }

Here is the call graph for this function:

char* freqToStr ( const int  i  ) 

Definition at line 188 of file ccexample.cc.

References buf.

Referenced by prettyPrintRepeat().

00189 {
00190         static char buf[7];
00191 
00192         if (i == 1)
00193                 buf[0] = '\0';
00194         else if (i == 3 || i == 23)
00195                 (void) sprintf(buf, "%drd ", i);
00196         else if ((i > 3 && i < 21) || (i > 23 && i < 31))
00197                 (void) sprintf(buf, "%dth ", i);
00198         else if (i == 21 || i == 31)
00199                 (void) sprintf(buf, "%dst ", i);
00200         else if (i == 2 || i == 22)
00201                 (void) sprintf(buf, "%dnd ", i);
00202 
00203         return buf;
00204 }

int main ( int  argc,
char **  argv 
)

Definition at line 448 of file ccexample.cc.

References addresses(), datebook(), memos, pi_file_close(), pi_file_open(), and todos().

00449 {
00450         if (argc != 2) {
00451                 cerr << "Usage: " << *argv << " [.pdb file]" << endl;
00452                 return 1;
00453         }
00454 
00455         pi_file *pf;
00456 
00457         if ((pf = pi_file_open(*(argv + 1))) == NULL) {
00458                 perror("pi_file_open");
00459                 return 1;
00460         }
00461 
00462         char *slash = strrchr(*(argv + 1), '/');
00463 
00464         if (slash)
00465                 slash++;
00466         else
00467                 slash = *(argv + 1);
00468 
00469         if (!strcmp(slash, "ToDoDB.pdb"))
00470                 todos(pf);
00471         else if (!strcmp(slash, "DatebookDB.pdb"))
00472                 datebook(pf);
00473         else if (!strcmp(slash, "AddressDB.pdb"))
00474                 addresses(pf);
00475         else if (!strcmp(slash, "MemoDB.pdb"))
00476                 memos(pf);
00477         else
00478                 cerr << "Unknown database: " << slash << endl;
00479 
00480         pi_file_close(pf);
00481 
00482         return 0;
00483 }

Here is the call graph for this function:

void memos ( pi_file pf  ) 

Definition at line 52 of file ccexample.cc.

References buf, dlpRecAttrArchived, dlpRecAttrDeleted, entnum, nentries, packed, packedBuf, pi_file_get_app_info(), pi_file_get_entries(), pi_file_read_record(), and size.

00053 {
00054         void *app_info;
00055         size_t app_info_size;
00056 
00057         if (pi_file_get_app_info(pf, &app_info, &app_info_size) < 0) {
00058                 cerr << "Unable to get app info" << endl;
00059                 return;
00060         }
00061         // Create mai as an unpacked structure with the memo app info
00062         memoAppInfo_t mai(app_info);
00063 
00064         // packed is now a pointer to an area of memory that is MEMO_APP_INFO_SIZE
00065         // bytes long.  You are responsible for release of this memory via delete
00066         unsigned char* packed = (unsigned char*)mai.pack();
00067         delete[]packed;
00068 
00069         int nentries;
00070 
00071         pi_file_get_entries(pf, &nentries);
00072 
00073         unsigned char *buf, packedBuf[0xffff];
00074         int size, attrs, cat;
00075         recordid_t uid;
00076 
00077         memo_t memo;
00078 
00079         for (int entnum = 0; entnum < nentries; entnum++) {
00080                 if (pi_file_read_record(pf, entnum, (void **) &buf, &size,
00081                                         &attrs, &cat, &uid) < 0) {
00082                         cout << "Error reading record number " << entnum <<
00083                             endl;
00084                         return;
00085                 }
00086 
00087                 /* Skip deleted records */
00088                 if ((attrs & dlpRecAttrDeleted)
00089                     || (attrs & dlpRecAttrArchived))
00090                         continue;
00091 
00092                 memo.unpack(buf);
00093 
00094                 cout << "Memo number " << (entnum + 1) << endl;
00095                 cout << memo.text() << endl << endl;
00096 
00097                 // Option 1 for getting a packed memo. Just give an int to be
00098                 // filled in with the size of the memo. You must free the space
00099                 // returned via delete
00100                 unsigned char* packed = (unsigned char*)memo.pack(&size);
00101                 delete[] packed;
00102 
00103                 // Option 2 for getting a packed memo.  Give a buffer, and an int
00104                 // telling how big it is.  If the buffer is too small to hold the
00105                 // packed data, NULL is returned.  Otherwise, the buffer is filled
00106                 // in with the packed data, a pointer to it is returned, and the
00107                 // integer passed in is reset to be the size of the packed data
00108                 if (memo.pack(packedBuf, &size) == NULL)
00109                         cerr << "Record number " << (entnum +
00110                                                      1) << " too big for "
00111                             << "the buffer you passed in." << endl;
00112         }
00113 }

Here is the call graph for this function:

void prettyPrintRepeat ( appointment_t *  appt  ) 

Definition at line 207 of file ccexample.cc.

References days, freqToStr(), and months.

Referenced by datebook().

00208 {
00209         tm *timePtr;
00210 
00211         if ((timePtr = appt->repeatEnd()))
00212                 cout << "This event repeats until " << asctime(timePtr);
00213         else
00214                 cout << "This event repeats forever" << endl;
00215 
00216         int freq = appt->repeatFreq();
00217         int on = appt->repeatOn();
00218 
00219         bool found = false;
00220 
00221         switch (appt->repeatType()) {
00222           case appointment_t::daily:
00223                   cout << "It repeats every " << freqToStr(freq) << "day";
00224                   break;
00225           case appointment_t::weekly:
00226           {
00227                   cout << "It repeats every " << freqToStr(freq) <<
00228                       "week on ";
00229 
00230                   for (int i = 0; i < 7; i++) {
00231                           if (on & (1 << i)) {
00232                                   if (found)
00233                                           cout << " and ";
00234                                   else
00235                                           found = true;
00236                                   cout << days[i];
00237                           }
00238                   }
00239                   cout << endl;
00240                   break;
00241           }
00242           case appointment_t::monthlyByDay:
00243                   cout << "It repeats on the ";
00244 
00245                   switch (on / 7) {
00246                     case 0:
00247                             cout << "first ";
00248                             break;
00249                     case 1:
00250                             cout << "second ";
00251                             break;
00252                     case 2:
00253                             cout << "third ";
00254                             break;
00255                     case 3:
00256                             cout << "fourth ";
00257                             break;
00258                     default:
00259                             cout << "last ";
00260                   }
00261 
00262                   cout << days[on % 7] << " of every month";
00263                   break;
00264           case appointment_t::monthlyByDate:
00265                   cout << "It repeats every " << freqToStr(freq);
00266                   timePtr = appt->beginTime();
00267                   cout << "month on the " << freqToStr(timePtr->
00268                                                        tm_mday) << endl;
00269                   cout << endl;
00270                   break;
00271           case appointment_t::yearly:
00272                   cout << "It repeats every " << freqToStr(freq) <<
00273                       "year on ";
00274                   timePtr = appt->beginTime();
00275                   cout << months[timePtr->tm_mon] << " " << timePtr->
00276                       tm_mday;
00277                   cout << endl;
00278                   break;
00279           default:
00280                   cerr << "Internal error" << endl;
00281         }
00282 }

Here is the call graph for this function:

void printCategoryNames ( appInfo_t &  ai  ) 

Definition at line 31 of file ccexample.cc.

Referenced by addresses().

00032 {
00033         char *ptr;
00034 
00035         for (short int i = 0; i < 16; i++) {
00036                 // This is sort of dangerous.  You are getting a pointer back to
00037                 // the real data.  That means if you modify what ptr points to, you
00038                 // are changing the value in the class itself.  Don't do that!
00039                 // I can't find a way to keep this from happening.  Even if I do
00040                 // something anal like return a const char *const all you
00041                 // have to do is cast it to char * and then it's modifiable.
00042                 ptr = ai.category(i);
00043 
00044                 // The first character will be non-null for an existing category
00045                 if (*ptr)
00046                         cout << "Category " << (i +
00047                                                 1) << " is " << ptr <<
00048                             endl;
00049         }
00050 }

void todos ( pi_file pf  ) 

Definition at line 116 of file ccexample.cc.

References buf, dlpRecAttrArchived, dlpRecAttrDeleted, entnum, nentries, packed, packedBuf, pi_file_get_app_info(), pi_file_get_entries(), pi_file_read_record(), and size.

Referenced by main().

00117 {
00118         void *app_info;
00119         int app_info_size;
00120 
00121         if (pi_file_get_app_info(pf, &app_info, &app_info_size) < 0) {
00122                 cerr << "Unable to get app info" << endl;
00123                 return;
00124         }
00125 
00126         todoAppInfo_t tai(app_info);
00127 
00128         // packed is now a pointer to an area of memory that is TODO_APP_INFO_SIZE
00129         // bytes long.  You are responsible for release this memory via delete
00130         unsigned char* packed = (unsigned char*)tai.pack();
00131         delete[] packed;
00132 
00133         int nentries;
00134 
00135         pi_file_get_entries(pf, &nentries);
00136 
00137         unsigned char *buf, packedBuf[0xffff];
00138         int size, attrs, cat;
00139         recordid_t uid;
00140         tm *due;
00141         todo_t todo;
00142 
00143         for (int entnum = 0; entnum < nentries; entnum++) {
00144                 if (pi_file_read_record(pf, entnum, (void **) &buf, &size,
00145                                         &attrs, &cat, &uid) < 0) {
00146                         cout << "Error reading record number " << entnum <<
00147                             endl;
00148                         return;
00149                 }
00150 
00151                 /* Skip deleted records */
00152                 if ((attrs & dlpRecAttrDeleted)
00153                     || (attrs & dlpRecAttrArchived))
00154                         continue;
00155 
00156                 todo.unpack(buf);
00157 
00158                 cout << "Category: " << tai.category(cat) << endl;
00159                 if (todo.description())
00160                         cout << "Description: " << todo.
00161                             description() << endl;
00162                 cout << "Priority: " << todo.priority() << endl;
00163                 cout << "Completed: " << (todo.
00164                                           complete()? "Yes" : "No") <<
00165                     endl;
00166                 if ((due = todo.due()))
00167                         cout << "Due: " << asctime(due);
00168                 else
00169                         cout << "Due: No Date" << endl;
00170                 if (todo.note())
00171                         cout << "Note: " << todo.note() << endl;
00172                 cout << endl;
00173 
00174                 // Just like the memo app, you can pack it like this...
00175                 unsigned char* packed = (unsigned char*)todo.pack(&size);
00176                 delete[] packed;
00177 
00178                 // ... or like this
00179                 size = sizeof(packedBuf);
00180                 if (todo.pack(packedBuf, &size) == NULL)
00181                         cerr << "Record number " << (entnum +
00182                                                      1) << " too big for "
00183                             << "the buffer you passed in." << endl;
00184         }
00185 }

Here is the call graph for this function:


Variable Documentation

char* days[] [static]

Initial value:

 {
        "Sunday", "Monday", "Tuesday", "Wednesday",
        "Thursday", "Friday", "Saturday"
}

Definition at line 17 of file ccexample.cc.

Referenced by datebook(), org::gnu::pilotlink::DatebookRecord::getBuffer(), and prettyPrintRepeat().

char* months[] [static]

Initial value:

 {
        "January", "February", "March", "April", "May", "June", "July",
        "August", "September", "October", "November", "December"
}

Definition at line 22 of file ccexample.cc.

Referenced by prettyPrintRepeat().


© 1996-2007 by pilot-link.org. All rights reserved.