SourceForge.net Logo
/******************************************************************************
 *
 * Copyright (c) 2001 Metrowerks, Inc., a Motorola Company.
 * All rights reserved.
 *
 * File: Create.c
 *
 *****************************************************************************/
#define DO_NOT_ALLOW_ACCESS_TO_INTERNALS_OF_STRUCTS
 
#include <PalmOS.h>
#include "PennDateDev1.h"
#include "PennDateDev1Rsc.h"
#include "Datebook.h"
#include "DateDB.h"
//#include "MathLib.h"



typedef struct {
  ApptDateTimeType when;
  ApptDBRecordFlagsflags;// A flag set for each  datum present
  charfirstField;
  UInt8reserved;
} ApptPackedDBRecordType;

typedef ApptPackedDBRecordType * ApptPackedDBRecordPtr;






/***********************************************************************
 *
 * FUNCTION:    Form1FormDoButtonCommand
 *
 * DESCRIPTION: This routine performs the specified action required by
 *a button press.
 *
 * PARAMETERS:  command - the ID of the button that was pressed.
 *
 * RETURNED:    true if the event has been handled and should not be 
 *              passed to a higher level handler.
 *
 ***********************************************************************/
Boolean Form1FormDoButtonCommand(UInt16 command) {
  Boolean handled = false;

  //Char *fldTextP;
  FieldType *fldP = GetObjectPtr(Form1Field1Field);
  Char *s;
  Char *s2;

  //UInt16 slength;
  
  Char timeStr [timeStringLength];
  TimeFormatType timeFormat;
  DmOpenRefApptDB;
  Err err;

  /*
    UInt16 slength;
    short x0,y0;
    UInt16 i;
    double mt;
  */
  DateTimeType dateTime,dateTime2;
  
  switch (command) {
    
  case Form1Button1Button:
    WinSetTextColor(SysRandom(0)%256);
    WinSetForeColor(SysRandom(0)%256);
    WinSetBackColor(SysRandom(0)%256);
    
    // Reading in current date and time
    TimSecondsToDateTime (TimGetSeconds (), &dateTime);
    dateTime2.year = dateTime.year - firstYear;
    dateTime2.month = dateTime.month;
    dateTime2.day = dateTime.day;
    
    timeFormat = (TimeFormatType)PrefGetPreference(prefTimeFormat);
    TimeToAscii (dateTime.hour, 
		 dateTime.minute, timeFormat, timeStr);
    
    
    WinDrawChars(timeStr, StrLen(timeStr), SysRandom(0)%160, SysRandom(0)%160);



    handled = true;
    break;
    
    
  case Form1Button2Button:
    FrmGotoForm (MainForm);
    handled = true;
    break;
    
    
    
  case Form1Button3Button:
    err = DateGetDatabase (&ApptDB, dmModeReadWrite);
    handled = true;
    
    break;
    
    
  case Form1Button4Button:
    
    s=FldGetTextPtr(fldP);
    if (!s)
      s = "";
    //StrCopy(s,"done");  
    //FldSetText(fldP,s2,0,12);
    StrCopy(s2,"   ");
    StrCopy(s2,s);       
    SetFieldText( Form1Field1Field,"done");

    WinDrawChars(s2,StrLen(s2),0, 20);
    //FldDelete(fldP,0,StrLen(s));
    handled = true;
    break;
    
    
    /*
      case Form1HelloWorldButton:
      
          s=FldGetTextPtr(fldP);
	      if (!s)
	      s = "";

	      slength=StrLen(s);
	          y0=SysRandom(0) % 110;
		      x0=SysRandom(0) % 100 + slength;
		          WinSetTextColor(SysRandom(0)%256);
			      WinSetForeColor(SysRandom(0)%256);
			          WinSetBackColor(SysRandom(0)%256);
				  WinDrawChars(s, slength, x0, y0);
				  DateGetDatabase ();

				  
				  
				  handled = true;
				  break;
				  
				  
				  
				  case Form1DrawButton:
				  
				      s=FldGetTextPtr(fldP);
				          if (!s)
					  s = "";

					  slength=StrLen(s);
					      y0=SysRandom(0) % 110;
					          x0=SysRandom(0) % 100 + slength;
						      WinSetTextColor(SysRandom(0)%256);
						          WinSetForeColor(SysRandom(0)%256);
							      WinSetBackColor(SysRandom(0)%256);
							      WinDrawChars(s, slength, x0, y0);
							      
							      WinDrawLine(50,50,x0,y0);
							      
							      for(i=0; i< 160; i++){
							      
							      WinSetForeColor(200);
							      mt=cos(-3.141+ 0.07*i)*20+70;
							      WinDrawPixel ( i%160,((short)mt) %160);
							      
							      
							      
							      WinSetForeColor(120);
							      mt=cos(-3.141+ 0.07*i)*20+50;
							      WinDrawPixel ( i%160,((short)mt) %160);
							      
							      WinSetForeColor(170);
							      mt=sin(-3.141+ 0.07*i)*20+50;
							      WinDrawPixel ( i%160,((short)mt) %160);
							      }
							      
							      
							      
							      
							      handled = true;
							      break;
							      
							      
							      
							      
							      case Form1Button2Button:
							      FrmGotoForm (Form2Form);
							      handled = true;
							      break;
							      
							      
							      case Form1DoneButton:
							      FrmGotoForm (MainForm);
							      handled = true;
							      break;
    */
  default:
    break;
  }

  return (handled);
}




// Chirico added
static void SetFieldText( UInt16 fieldID, Char *textP )
{
  MemHandle newTextH, oldTextH;
  FieldPtr fieldP;
  
  fieldP = GetObjectPtr( fieldID );
  ErrNonFatalDisplayIf( !fieldP, "Invalid field identifier!" );
  
  newTextH = MemHandleNew( StrLen( textP ) + 1 );
  ErrNonFatalDisplayIf( !newTextH, "Out of memory!" );
  
  StrCopy( MemHandleLock( newTextH ), textP );
  
  MemHandleUnlock( newTextH );
  
  oldTextH = FldGetTextHandle( fieldP );
  
  FldSetTextHandle( fieldP, newTextH );
  FldDrawField( fieldP );
  
  if( oldTextH )
    MemHandleFree( oldTextH );
}



static void * GetObjectPtr(UInt16 objectID)
{
  FormType * frmP;

  frmP = FrmGetActiveForm();
  return FrmGetObjectPtr(frmP, FrmGetObjectIndex(frmP, objectID));
}



//static Err DateGetDatabase (DmOpenRef *dbPP, UInt16 mode)
/*
static Err DateGetDatabase ()
{

DmOpenRef *dbPP=0;

//UInt16 mode;
Char s[30];
MemHandle h;
UInt16 index = 0;
UInt16 NumRec;
ErrandDB r;

Err error = 0;
DmOpenRef dbP;
//UInt16 cardNo;
//LocalID dbID;


//*dbPP = 0;
   dbP = DmOpenDatabaseByTypeCreator(kDBType, kCreator, dmModeReadWrite);
   if (! dbP)
   {
   error = DmCreateDatabase(0, kDBName, kCreator, kDBType, false);
   
   if (error) return error;
   
   dbP = DmOpenDatabaseByTypeCreator(kDBType, kCreator, dmModeReadWrite);
   if (! dbP) return ~0;

   // Set the backup bit.  This is to aid syncs with non Palm software.
   //SetDBBackupBit(dbP);
   
   
   }
   
   
   //***********************
			    //  MemHandle h;
			    //UInt16 index = 0;
			    //ErrandDB r;  // this is our database record

			    h = DmNewRecord(dbP, &index, sizeof(r));

			    // fill the record with data
			    StrCopy(r.FirstName, "Jun");
			    StrCopy(r.LastName, "Zhou");
			    StrCopy(r.PhoneNum, "1 555 545 4434");

    
			    if (h) { // could fail due to insufficient memory!
			    MemPtr p = MemHandleLock(h);
			    Err err = DmWrite(p, 0, &r, sizeof(r));

			    MemPtrUnlock(p);
			    DmReleaseRecord(dbP, index, true);
    } 
    //****************************
				  
				  
				  StrCopy(s, "             ");
				  NumRec=DmNumRecords(dbP);
				  WinDrawChars(StrIToA(s,NumRec), StrLen(StrIToA(s,NumRec)), SysRandom(0) % 110, 
				  SysRandom(0) % 100 + 5);
				  
				  if (dbP)
				  DmCloseDatabase(dbP);

				  *dbPP = dbP;
				  return 0;
}

*/






/***********************************************************************
 *
 * FUNCTION:    Form1FormHandleEvent
 *
 * DESCRIPTION: This routine is the event handler for the 
 *              "Form1Form" of this application.
 *
 * PARAMETERS:  eventP  - a pointer to an EventType structure
 *
 * RETURNED:    handled - false if the event should still be handled by
 *the system, true if the system should ignore it.
 *
 ***********************************************************************/
Boolean Form1FormHandleEvent(EventPtr eventP) {
  Boolean handled = false;
  FormPtr frmP;

  switch (eventP->eType) {
  case frmOpenEvent:
    frmP = FrmGetActiveForm();
    FrmDrawForm(frmP);
    handled = true;
    break;

  case ctlSelectEvent:
    return Form1FormDoButtonCommand(eventP->data.ctlSelect.controlID);
        
  default:
    break;
    
  }
  return handled;
}



//******************** CHIRICO ************
/*
typedef struct {
TimeTypestartTime;// Time the appointment starts
TimeTypeendTime;// Time the appointment ends
DateTypedate;// date of appointment
} ApptDateTimeType;


typedef struct {
ApptDateTimeType *when;
AlarmInfoType *alarm;
RepeatInfoType *repeat;
ExceptionsListType *exceptions;
Char *description;
Char *note;
} ApptDBRecordType;





 */


static Err DateGetDatabase (DmOpenRef *dbPP, UInt16 mode)
{
  Err error = 0;
  DmOpenRef dbP;
  //UInt16 cardNo;
  //LocalID dbID;
  Int16 NumRec;
  Char s[30];
  Char s2[30];
  //ApptDBRecordType r;
  DateTypeDate;
  DateTimeType dateTime;

  //MemHandle apptListH;
  //MemHandleApptsOnlyH;
  //UInt16NumApptsOnly;


  UInt16 counts[daysInWeek];
  UInt16 i;
  MemHandle apptLists[daysInWeek];
  ApptInfoPtr apptList;

  Char dateStr [dateStringLength];

  DateFormatType dateFormat;
  UInt32 uniqueIDP;
  MemHandle recordH;
  Boolean found = false;
  ApptPackedDBRecordPtr rP;
  Int16 kmin, probe;// all positions in the database.
  UInt16 index,index2;
  *dbPP = 0;
  dbP = DmOpenDatabaseByTypeCreator(datebookDBType, sysFileCDatebook, mode);
  if (! dbP)
    {
      error = DmCreateDatabase (0, datebookDBName, sysFileCDatebook,
				datebookDBType, false);
      if (error) return error;
      
      dbP = DmOpenDatabaseByTypeCreator(datebookDBType, sysFileCDatebook, mode);
      if (! dbP) return ~0;

      // Set the backup bit.  This is to aid syncs with non Palm software. May NEED TO LOOK AT chirico
      // NEED TO LOOK AT
      /*
	SetDBBackupBit(dbP);
	
	error = ApptAppInfoInit (dbP);
      if (error) 
      {
      DmOpenDatabaseInfo(dbP, &dbID, NULL, NULL, &cardNo, NULL);
      DmCloseDatabase(dbP);
      DmDeleteDatabase(cardNo, dbID);
         return error;
         }*/
    }


  StrCopy(s, "             ");
  NumRec=DmNumRecords(dbP);
  WinDrawChars(StrIToA(s,NumRec), StrLen(StrIToA(s,NumRec)), SysRandom(0) % 110, 
	       SysRandom(0) % 100 + 5);

  NumRec=DmNumRecords(dbP);
  kmin = probe = index=0;
  index2=0;

  //if(NumRec > 3) NumRec=4;//Take this one out
  //for(i=0;i < NumRec;i++ ){
  if(NumRec > 0){
    
    //MemHandle h;
    //  UInt16 index = 0;

    //VoidHand myRecord = DmQueryRecord(gDB, recordNumber);
    //StructType *s = MemHandleLock(myRecord);
    //DoSomethingReadOnly(s->field);
    //MemHandleUnlock(myRecord);





    // get the handle to the record and set busy bit
    //recordH = DmQueryRecord(dbP, i);
    recordH = DmQueryNextInCategory (dbP, &index, dmAllCategories);
    if (recordH) { // could fail due to insufficient memory!
      rP = (ApptPackedDBRecordPtr) MemHandleLock (recordH);

      // do something with the record's contents here
      
      TimSecondsToDateTime (TimGetSeconds (), &dateTime);
      Date.year = dateTime.year - firstYear; //Year( dDate) - 1904  2002-1904=98
      //Date.year = 2002;
      Date.month = dateTime.month;
      Date.day = dateTime.day;
      dateFormat = dfDMYWithDashes;
      DateToAscii(Date.month, Date.day, (Date.year+firstYear),  dateFormat, dateStr);
      WinDrawChars(dateStr, StrLen(dateStr), SysRandom(0)%160, SysRandom(0)%160);
      
      
      
      Date.year = 2002 - firstYear; //Year( dDate) - 1904  2002-1904=98
      //Date.year = 2002;
      Date.month = 8;
      Date.day = 5;
      
      
      dateFormat = (DateFormatType)PrefGetPreference(prefDateFormat);
      dateFormat = dfDMYWithDashes;
      DateToAscii(rP->when.date.month, rP->when.date.day, (rP->when.date.year+firstYear) ,  dateFormat, dateStr);
      //DateToDOWDMFormat (month, day, year, ShortDateFormat, label);
      //if(StrCompare(dateStr,"8/8/02")){
      //if(i+2 > NumRec){
      WinDrawChars(dateStr, StrLen(dateStr), SysRandom(0)%160, SysRandom(0)%160);
      //}
      MemHandleUnlock (recordH);
        
        
      if(ApptFindFirst (dbP, Date, &index))
        {
	  WinDrawChars("Found", StrLen("Found"), SysRandom(0)%160, SysRandom(0)%160);
	  ApptGetAppointments (dbP, Date, daysInWeek, apptLists, counts);
	  for(i=0; i < counts[0];i++)
	    {
	      
	      apptList=MemHandleLock (apptLists[0]); //must move with counts[0]
	      
	      /*
                apptList[0].startTime;
                apptList[0].endTime;
                apptList[0].recordNum;
	      */



        
	      TimeToAscii (apptList[i].startTime.hours, 
			   apptList[i].startTime.minutes, (TimeFormatType)PrefGetPreference(prefTimeFormat), dateStr);
	      
	      StrCopy(s,dateStr);
	      StrCat(s,":");
	      StrIToA(s2,apptList[i].recordNum);
	      StrCat(s,s2);

	      WinSetForeColor(240);
	      WinSetBackColor(130);
	      WinSetTextColor(6);
	      DmRecordInfo(dbP, apptList[i].recordNum, NULL, &uniqueIDP, NULL);
	      StrIToA(s2,uniqueIDP);
	      StrCat(s,":");
	      StrCat(s,s2);

	      WinDrawChars(s, StrLen(s), 0, SysRandom(0)%160);
	      
	      
	      //WinDrawChars(">0", StrLen(">0"), SysRandom(0)%160, SysRandom(0)%80);
	      

	      MemHandleUnlock(apptLists[0]); //must move with counts[0]

	    }

	  // Get all the appointments in the week.
	  //ApptGetAppointments (ApptDB, week->startDate, daysInWeek,apptLists, counts);                
	}
        else
	  WinDrawChars("NotFound", StrLen("NotFound"), SysRandom(0)%160, SysRandom(0)%160);
        
        
    }

    /*
      
      recordH = DmQueryNextInCategory (dbP, &index, dmAllCategories);
      
      if (recordH)
      {
      rP = (ApptPackedDBRecordPtr) MemHandleLock (recordH);
      //if (rP->flags.repeat)
      //result = 1;
      //else
      //result = DateCompare (date, r->when.date);
      Date=rP->when.date;
      dateFormat = (DateFormatType)PrefGetPreference(prefDateFormat);
      DateToAscii (Date.month, Date.day, Date.year,  dateFormat, dateStr);

      StrIToA(s,NumApptsOnly);
      WinDrawChars(dateStr, StrLen(s), SysRandom(0)%160, SysRandom(0)%160);
      
      MemHandleUnlock (recordH);
      }
    */

    /*
      //if (ApptsOnlyH) MemHandleFree (ApptsOnlyH);
      Date.month=7;
      Date.day=20;
      Date.year=2002;
      
      
      
      
      ApptGetAppointments (dbP, Date, 20, &ApptsOnlyH, &NumApptsOnly);
      
      
      dateFormat = (DateFormatType)PrefGetPreference(prefDateFormat);
      DateToAscii (Date.month, Date.day, Date.year,  dateFormat, dateStr);

      StrIToA(s,NumApptsOnly);
      
      WinDrawChars(s, StrLen(s), SysRandom(0)%160, SysRandom(0)%160);
      //WinDrawChars(dateStr, StrLen(s), SysRandom(0)%160, SysRandom(0)%160);
    */
    
  }

  
  if (dbP)
    DmCloseDatabase(dbP);
  
  //*dbPP = dbP;
  return 0;
}

/*

Boolean ApptFindFirst (DmOpenRef dbP, DateType date, UInt16* indexP)
{
Err err;
Int16 numOfRecords;
Int16 kmin, probe, i;// all positions in the database.
Int16 result = 0;// result of comparing two records
UInt16 index;
MemHandle recordH;
Boolean found = false;
ApptPackedDBRecordPtr r;



WinSetForeColor(240);
WinSetBackColor(100);
WinSetTextColor(6);
WinDrawChars("ApptFindFirst", StrLen("ApptFindFirst"), SysRandom(0)%160, SysRandom(0)%160);




kmin = probe = 0;
numOfRecords = DmNumRecords(dbP);


while (numOfRecords > 0)
{
  i = numOfRecords >> 1;
  probe = kmin + i;
  
  index = probe;
  recordH = DmQueryNextInCategory (dbP, &index, dmAllCategories);
  if (recordH)
    {
      r = (ApptPackedDBRecordPtr) MemHandleLock (recordH);
      if (r->flags.repeat)
	result = 1;
      else
	result = DateCompare (date, r->when.date);
      MemHandleUnlock (recordH);
    }

  // If no handle, assume the record is deleted, deleted records
  // are greater.
  else
    result = -1;
  

  // If the date passed is less than the probe's date, keep searching.
  if (result < 0)
    numOfRecords = i;

  // If the date passed is greater than the probe's date, keep searching.
  else if (result > 0)
    {
      kmin = probe + 1;
      numOfRecords = numOfRecords - i - 1;
    }

  // If the records are equal find the first record on the day.
  else
    {
      found = true;
      *indexP = index;
      while (true)
	{
	  err = DmSeekRecordInCategory (dbP, &index, 1, dmSeekBackward, 
					dmAllCategories);
	  if (err == dmErrSeekFailed) break;
	  
	  recordH = DmQueryRecord(dbP, index);
	  r = (ApptPackedDBRecordPtr) MemHandleLock (recordH);
	  if (r->flags.repeat)
	    result = 1;
	  else
	    result = DateCompare (date, r->when.date);
	  MemHandleUnlock (recordH);
	  if (result != 0) break;
	  *indexP = index;
	}

      break;
    }
}


// If that were no appointments on the specified day, return the 
// index of the next appointment (on a future day).
 if (! found)
   {
     if (result < 0) 
       *indexP = probe;
     else if (DmNumRecords(dbP) == 0)
       *indexP = 0;
     else
       *indexP = probe + 1;
   }

 return (found);
}


/***********************************************************************
 *
 * FUNCTION:    DateCompare
 *
 * DESCRIPTION: This routine compares two dates.
 *
 * PARAMETERS:  d1 - a date 
 *              d2 - a date 
 *
 * RETURNED:    if d1 > d2  returns a positive int
 *              if d1 < d2  returns a negative int
 *              if d1 = d2  returns zero
 *
 * NOTE: This routine treats the DateType structure like an unsigned int,
 *       it depends on the fact the the members of the structure are ordered
 *       year, month, day form high bit to low low bit.
 *
 * REVISION HISTORY:
 *NameDateDescription
 *-------------------
 *art6/12/95Initial Revision
 *
 ***********************************************************************/
/*
static Int16 DateCompare (DateType d1, DateType d2)
{
UInt16 int1, int2;

int1 = DateToInt(d1);
int2 = DateToInt(d2);

WinSetForeColor(240);
WinSetBackColor(100);
WinSetTextColor(6);

WinDrawChars("DateCompare", StrLen("DateCompare"), 50, 50);

if (int1 > int2)
return (1);
else if (int1 < int2)
return (-1);
return 0;
}
*/



Tutorials

Linux System Admin Tips: There are over 200 Linux tips and tricks in this article. That is over 100 pages covering everything from NTP, setting up 2 IP address on one NIC, sharing directories among several users, putting running jobs in the background, find out who is doing what on your system by examining open sockets and the ps command, how to watch a file, how to prevent even root from deleting a file, tape commands, setting up cron jobs, using rsync, using screen conveniently with emacs, how to kill every process for a user, security tips and a lot more. These tip grow weekly. The above link will download the text version for easy grep searching. There is also an html version here.

Breaking Firewalls with OpenSSH and PuTTY: If the system administrator deliberately filters out all traffic except port 22 (ssh), to a single server, it is very likely that you can still gain access other computers behind the firewall. This article shows how remote Linux and Windows users can gain access to firewalled samba, mail, and http servers. In essence, it shows how openSSH and Putty can be used as a VPN solution for your home or workplace.

MySQL Tips and Tricks: Find out who is doing what in MySQL and how to kill the process, create binary log files, connect, create and select with Perl and Java, remove duplicates in a table with the index command, rollback and how to apply, merging several tables into one, updating foreign keys, monitor port 3306 with the tcpdump command, creating a C API, complex selects, and much more.

Create a Live Linux CD - BusyBox and OpenSSH Included: These steps will show you how to create a functioning Linux system, with the latest 2.6 kernel compiled from source, and how to integrate the BusyBox utilities including the installation of DHCP. Plus, how to compile in the OpenSSH package on this CD based system. On system boot-up a filesystem will be created and the contents from the CD will be uncompressed and completely loaded into RAM -- the CD could be removed at this point for boot-up on a second computer. The remaining functioning system will have full ssh capabilities. You can take over any PC assuming, of course, you have configured the kernel with the appropriate drivers and the PC can boot from a CD. This tutorial steps you through the whole processes.

SQLite Tutorial : This article explores the power and simplicity of sqlite3, first by starting with common commands and triggers, then the attach statement with the union operation is introduced in a way that allows multiple tables, in separate databases, to be combined as one virtual table, without the overhead of copying or moving data. Next, the simple sign function and the amazingly powerful trick of using this function in SQL select statements to solve complex queries with a single pass through the data is demonstrated, after making a brief mathematical case for how the sign function defines the absolute value and IF conditions.

The Lemon Parser Tutorial: This article explains how to build grammars and programs using the lemon parser, which is faster than yacc. And, unlike yacc, it is thread safe.

How to Compile the 2.6 kernel for Red Hat 9 and 8.0 and get Fedora Updates: This is a step by step tutorial on how to compile the 2.6 kernel from source.

Virtual Filesystem: Building A Linux Filesystem From An Ordinary File. You can take a disk file, format it as ext2, ext3, or reiser filesystem and then mount it, just like a physical drive. Yes, it then possible to read and write files to this newly mounted device. You can also copy the complete filesystem, since it is just a file, to another computer. If security is an issue, read on. This article will show you how to encrypt the filesystem, and mount it with ACL (Access Control Lists), which give you rights beyond the traditional read (r) write (w) and execute (x) for the 3 user groups file, owner and other.

Working With Time: What? There are 61 seconds in a minute? We can go back in time? We still tell time by the sun?



Chirico img Mike Chirico, a father of triplets (all girls) lives outside of Philadelphia, PA, USA. He has worked with Linux since 1996, has a Masters in Computer Science and Mathematics from Villanova University, and has worked in computer-related jobs from Wall Street to the University of Pennsylvania. His hero is Paul Erdos, a brilliant number theorist who was known for his open collaboration with others.


Mike's notes page is souptonuts. For open source consulting needs, please send an email to mchirico@gmail.com. All consulting work must include a donation to SourceForge.net.

SourceForge.net Logo


SourceForge.net Logo