/******************************************************************************
*
* Copyright (c) 2001 Metrowerks, Inc., a Motorola Company.
* All rights reserved.
*
* File: Create.c
*
This is for Mike Chirico' notes -- it's a simple database access example
*****************************************************************************/
#define DO_NOT_ALLOW_ACCESS_TO_INTERNALS_OF_STRUCTS
#include <PalmOS.h>
#include "Form1.h"
#include "Form1Rsc.h"
//#include "MathLib.h"
/******************************************************************************
*
* Copyright (c) 2002 Chirico Inc, University of Pennsylvania
* All rights reserved.
*
*
*
*****************************************************************************/
// Alarm advance - the period of time before the appointment that the
// alarm should sound.
//
typedef enum alarmTypes {aauMinutes, aauHours, aauDays} AlarmUnitType;
typedef struct {
Int8 advance; // Alarm advance (-1 = no alarm)
AlarmUnitType advanceUnit; // minutes, hours, days
} AlarmInfoType;
typedef struct {
TimeType startTime; // Time the appointment starts
TimeType endTime; // Time the appointment ends
DateType date; // date of appointment
} ApptDateTimeType;
// The following enum is used to specify the frequency of
// repeating appointments.
//
enum repeatTypes {
repeatNone,
repeatDaily,
repeatWeekly,
repeatMonthlyByDay,
repeatMonthlyByDate,
repeatYearly
};
typedef enum repeatTypes RepeatType;
// This structure contains information about repeat appointments. The
// repeatOn member is only used by weelky and monthly-by-day repeating
// appointments. For weekly the byte is a bit field that contains the
// days of the week the appointments occurs on (bit: 0-sun, 1-mon,
// 2-tue, etc.). For monthly-by-day the byte contains the day the
// appointments occurs, (ex: the 3rd friday), the byte is of type
// DayOfMonthType.
//
typedef struct {
RepeatType repeatType; // daily, weekly, monthlyByDay, etc.
UInt8 reserved1;
DateType repeatEndDate; // minus one if forever
UInt8 repeatFrequency; // i.e. every 2 days if repeatType daily
UInt8 repeatOn; // monthlyByDay and repeatWeekly only
UInt8 repeatStartOfWeek;// repeatWeekly only
UInt8 reserved2;
} RepeatInfoType;
typedef RepeatInfoType * RepeatInfoPtr;
typedef struct {
UInt16 numExceptions;
DateType exception;
} ExceptionsListType;
typedef ExceptionsListType * ExceptionsListPtr;
typedef struct {
ApptDateTimeType * when;
AlarmInfoType * alarm;
RepeatInfoType * repeat;
ExceptionsListType * exceptions;
Char * description;
Char * note;
} ApptDBRecordType;
// This structure describes what information is present in an
// AppointmentPackedDBRecordType
typedef struct {
unsigned when :1; // set if when info changed (ApptChangeRecord)
unsigned alarm :1; // set if record contains alarm info
unsigned repeat :1; // set if record contains repeat info
unsigned note :1; // set if record contains a note
unsigned exceptions :1; // set if record contains exceptions list
unsigned description :1;
} ApptDBRecordFlags;
// The following structure doesn't really exist. The first field
// varies depending on the data present. However, it is convient
// (and less error prone) to use when accessing the other information.
typedef struct {
ApptDateTimeType when;
ApptDBRecordFlags flags; // A flag set for each datum present
char firstField;
UInt8 reserved;
} ApptPackedDBRecordType;
typedef ApptPackedDBRecordType * ApptPackedDBRecordPtr;
/******************************************************************************
* END*************************************************************************
* Copyright (c) 2002 Chirico Inc, University of Pennsylvania
* All rights reserved.
*
*
*
*****************************************************************************/
/***********************************************************************
*
* 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;
UInt16 slength;
short x0,y0;
UInt16 i;
double mt;
*/
switch (command) {
case Form1Button1Button:
WinSetTextColor(SysRandom(0)%256);
WinSetForeColor(SysRandom(0)%256);
WinSetBackColor(SysRandom(0)%256);
//WinDrawChars("test", StrLen("test"), SysRandom(0)%160, SysRandom(0)%160);
chiricoDatabase();
handled = true;
break;
case Form1Button2Button:
FrmGotoForm (MainForm);
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
/*
void* GetObjectPtr(UInt16 objectID)
{
FormPtr 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;
}
Err chiricoDatabase()
{
DmOpenRef dbP;
MemHandle recordH;
UInt16 recordNum;
Err error;
ApptPackedDBRecordPtr r;
TimeType startTime;
TimeType endTime;
DateType apptDate;
Char dateStr [dateStringLength];
/*
DateType date;
UInt16 days;
MemHandle apptLists[];
UInt16 counts[];
UInt16 startDate;
UInt16 endDate;
UInt16 index;
UInt16 recordNum;
UInt32 dateInDays;
DateType apptDate;
TimeType startTime;
TimeType endTime;
DateType tempDate;
DateType repeatDate;
Boolean repeats;
MemHandle recordH;
ApptInfoPtr apptList;
ApptDBRecordType apptRec;
ApptPackedDBRecordPtr r;
*/
dbP = DmOpenDatabaseByTypeCreator('DATA', sysFileCDatebook, dmModeReadWrite);
if (! dbP)
{
error = DmCreateDatabase (0, "DatebookDB", sysFileCDatebook,
'DATA', false);
if (error) return error;
dbP = DmOpenDatabaseByTypeCreator('DATA', sysFileCDatebook, dmModeReadWrite);
if (! dbP) return ~0;
}
recordNum = 0;
//dateInDays = DateToDays (date);
//while (true)
while(true)
{
recordH = DmQueryNextInCategory (dbP, &recordNum, dmAllCategories);
if (! recordH) break;
r = MemHandleLock (recordH);
startTime = r->when.startTime;
endTime = r->when.endTime;
apptDate = r->when.date;
MemHandleUnlock (recordH);
if(recordNum < 10){
DateToAscii (apptDate.month, apptDate.day, (apptDate.year+firstYear), dfDMYWithDashes, dateStr);
WinDrawChars(dateStr, StrLen(dateStr), SysRandom(0)%160, SysRandom(0)%160);
}
recordNum++;
}
DmCloseDatabase(dbP);
return 0;
}
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?
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.