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 "ErrandRsc.h"
#include "Errand.h"

/***********************************************************************
 *
 * FUNCTION:GetFromDatabase
 *
 * DESCRIPTION:Find out if anything is stored for the day and return
 *any data that is stored.
 *
 * PARAMETERS:recordP - pointer to the record struct that has the 
 *selected day in it
 * 
 * RETURNED:The index of the record if there was a record stored 
 *for that day, -1 if not.  If there is data, the 
 *record is returned in the recordP pointer.
 *
 ***********************************************************************/
UInt16 GetFromDatabase(ErrandDBPtr recordP) {
  UInt16 index = -1;
  MemHandle recH;
  ErrandDBPtr recP;
  UInt16 i, numRecs = DmNumRecords(gDB);
  
  for (i = 0; i < numRecs; i++) {
    recH = DmQueryRecord(gDB, i);
    if (recH) {
      recP = (ErrandDBPtr)MemHandleLock(recH);
      if (recP->day == recordP->day) {
	StrCopy(recordP->errands, recP->errands);
	index = i;
      }
      MemHandleUnlock(recH);
      if (index != -1)
	return (index);
    }
  }
  return (index);
}
 
/***********************************************************************
 *
 * FUNCTION:AddToDatabase
 *
 *DESCRIPTION:Takes the data in struct form and adds them as a record
 *in the database.
 *
 *PARAMETERS:record - struct containing the record information.
 *
 * RETURNED:0 is there was no problem adding the record, and the
 *error code if there was a problem.
 *
 ***********************************************************************/
Err AddToDatabase(ErrandDBPtr record) {
  Err error = errNone;
  UInt16 recIndex = dmMaxRecordIndex;
  MemHandle recH;
  MemPtr recP;
  UInt16 index;
  
  recP = MemPtrNew(sizeof(ErrandDB));
  ((ErrandDBPtr)recP)->day = record->day;
  index = GetFromDatabase((ErrandDBPtr)recP);
  MemPtrFree(recP);
  
  if (index == -1) {
    recH = DmNewRecord(gDB, &recIndex, sizeof(Weekday)+StrLen(record->errands)+1);
    if (recH) {
      recP = MemHandleLock(recH);
      DmWrite(recP, 0, &(record->day), sizeof(Weekday));
      DmWrite(recP, sizeof(Weekday), record->errands, StrLen(record->errands)+1);
      error = DmReleaseRecord(gDB, recIndex, true);
      MemHandleUnlock(recH);
    }
  }
  else {
    UInt16 newSize = sizeof(record) + StrLen(record->errands) + 1;
    recH = DmResizeRecord(gDB, index, newSize);
    if (recH) {
      recP = MemHandleLock(recH);
      DmWrite(recP, sizeof(Weekday), record->errands, StrLen(record->errands)+1);
      DmReleaseRecord(gDB, index, true);
      MemHandleUnlock(recH);
    }
  }
  
  if (error == errNone)
    return 0;
  else
    return DmGetLastErr();
}

/***********************************************************************
 *
 * FUNCTION:    CreateFormDoTriggerCommand
 *
 * DESCRIPTION: This routine performs the specified action required by
 *a selection in the popup trigger.
 *
 * PARAMETERS:  day - the selection made in the trigger.
 *
 * RETURNED:    true if the event has been handled and should not be 
 *              passed to a higher level handler.
 *
 ***********************************************************************/
Boolean CreateFormDoTriggerCommand(Int16 day) {
  Boolean handled = false, foundData;
  ErrandDBPtr recP;
  FieldType *fldP = GetObjectPtr(CreateOtherField);
   
  recP = MemPtrNew(sizeof(ErrandDB));
  if (day >= 0) {
    recP->day = day;
    FldDelete(fldP, 0, FldGetMaxChars(fldP));
    foundData = GetFromDatabase(recP);
    if (foundData)   
      FldInsert(fldP, recP->errands, StrLen(recP->errands));
  }
  MemPtrFree(recP);
   
  return handled;
}

/***********************************************************************
 *
 * FUNCTION:    CreateFormDoMenuCommand
 *
 * DESCRIPTION: This routine performs the specified action required by
 *a menu item.
 *
 * 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 CreateFormDoMenuCommand(UInt16 command) {
  Boolean handled = false;
  FieldType* fldP = GetObjectPtr(CreateOtherField);
  UInt16 startPos = 0;
  UInt16 endPos = FldGetTextLength(fldP);
   
  MenuEraseStatus(NULL);
  switch (command) {
  case EditCut:
    FldCut(fldP);
    handled = true;
    break;
    
  case EditCopy:
    FldCopy(fldP);
    handled = true;
    break;
    
  case EditPaste:
    FldPaste(fldP);
    handled = true;
    break;
    
  case EditUndo:
    FldUndo(fldP);
    handled = true;
    break;
    
  case EditSelectAll:
    FldSetSelection(fldP, startPos, endPos);
    handled = true;
    break;
  }

  return (handled);
}

/***********************************************************************
 *
 * FUNCTION:    CreateFormDoButtonCommand
 *
 * 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 CreateFormDoButtonCommand(UInt16 command) {
  Boolean handled = false;
  Char *fldTextP, *listTextP, *newline = "\n";
  FieldType *fldP = GetObjectPtr(CreateOtherField);
  ListType *listP;
  Err error;
  Int16 day;
  ErrandDB record;
  Char *dayList[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", 
		      "Thursday", "Friday", "Saturday"};
  
  switch (command) {
  case CreateDoneButton:
    FrmGotoForm (MainForm);
    handled = true;
    break;
    
  case CreateAddtoListButton:
    listP = GetObjectPtr(CreateErrandsList);
    listTextP = LstGetSelectionText(listP, LstGetSelection(listP));
    if (listTextP != NULL) {  /* If NULL, nothing is selected. */
      FldGrabFocus(fldP);
      FldInsert(fldP, listTextP, StrLen(listTextP));
      FldInsert(fldP, newline, StrLen(newline));
    }
    else
      FrmAlert(ChooseErrandAlert);
    handled = true;
    break;
    
  case CreateSaveDayButton:
    fldTextP = FldGetTextPtr(fldP);
    day = LstGetSelection((ListType *)GetObjectPtr(CreateDateList));
    /* If either value is NULL, we need more information. */
    if ((day == noListSelection) || (fldTextP == NULL))
      FrmAlert(NeedMoreInfoAlert);
    else {
      record.day = day;
      StrCopy(record.errands, fldTextP);
      error = AddToDatabase(&record);
      if (!error)
	FrmCustomAlert(SavedAlert, dayList[record.day], NULL, NULL); 
    }
    handled = true;
    break;
    
  default:
    break;
  }

  return (handled);
}

/***********************************************************************
 *
 * FUNCTION:    CreateFormHandleEvent
 *
 * DESCRIPTION: This routine is the event handler for the 
 *              "CreateForm" 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 CreateFormHandleEvent(EventPtr eventP) {
  Boolean handled = false;
  FormPtr frmP;

  switch (eventP->eType) {
  case frmOpenEvent:
    frmP = FrmGetActiveForm();
    FrmDrawForm(frmP);
    /* Set the list and popup trigger so nothing is selected by 
     * default. Otherwise the 0th item in the list will be the one
     * that is selected. */
    LstSetSelection(GetObjectPtr(CreateErrandsList), -1);
    LstSetSelection(GetObjectPtr(CreateDateList), -1);
    break;

  case ctlSelectEvent:
    return CreateFormDoButtonCommand(eventP->data.ctlSelect.controlID);
       
  case menuEvent:
    return CreateFormDoMenuCommand(eventP->data.menu.itemID);
    
  case popSelectEvent:
    return CreateFormDoTriggerCommand(eventP->data.popSelect.selection);
    
  default:
    break;
    
  }
  return handled;
}



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