home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / happydays-src-1.37.tar.gz / happydays-src-1.37.tar / happydays-1.37 / util.c < prev    next >
C/C++ Source or Header  |  2000-05-20  |  6KB  |  215 lines

  1. /*
  2. HappyDays - A Birthdate displayer for the PalmPilot
  3. Copyright (C) 1999-2000 JaeMok Jeong
  4.  
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18. */
  19.  
  20. #include <PalmOS.h>
  21. #include "util.h"
  22. #include "calendar.h"
  23.  
  24. //
  25. // GetObjectPointer - return an object pointer given a form and objID
  26. //
  27. void * GetObjectPointer(FormPtr frm, UInt16 objID)
  28. {
  29.      return FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, objID));
  30. }
  31.  
  32.  
  33. //
  34. //  SetFieldTextFromHandle - set txtH into field object text
  35. //
  36. FieldPtr SetFieldTextFromHandle(UInt16 fieldID, MemHandle txtH)
  37. {
  38.    MemHandle   oldTxtH;
  39.    FormPtr     frm = FrmGetActiveForm();
  40.    FieldPtr    fldP;
  41.  
  42.    // get the field and the field's current text handle.
  43.    fldP     = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, fieldID));
  44.    ErrNonFatalDisplayIf(!fldP, "missing field");
  45.    oldTxtH  = FldGetTextHandle(fldP);
  46.    
  47.    // set the field's text to the new text.
  48.    FldSetTextHandle(fldP, txtH);
  49.  
  50.    // free the handle AFTER we call FldSetTextHandle().
  51.    if (oldTxtH) 
  52.       MemHandleFree((MemHandle)oldTxtH);
  53.    
  54.    return fldP;
  55. }
  56.  
  57. // Allocates new handle and copies incoming string
  58. FieldPtr SetFieldTextFromStr(UInt16 fieldID, Char * strP)
  59. {
  60.    MemHandle    txtH;
  61.    
  62.    // get some space in which to stash the string.
  63.    txtH  = MemHandleNew(StrLen(strP) + 1);
  64.    if (!txtH)
  65.       return NULL;
  66.  
  67.    // copy the string to the locked handle.
  68.    StrCopy((Char *)MemHandleLock(txtH), strP);
  69.  
  70.    // unlock the string handle.
  71.    MemHandleUnlock(txtH);
  72.    
  73.    // set the field to the handle
  74.    return SetFieldTextFromHandle(fieldID, txtH);
  75. }
  76.  
  77. FieldPtr ClearFieldText(UInt16 fieldID)
  78. {
  79.     return SetFieldTextFromHandle(fieldID, NULL);
  80. }
  81.  
  82. Boolean FindNearLunar(DateType *dt, DateType base, Boolean leapyes)
  83. {
  84.     int lunyear, lunmonth, lunday;
  85.     DateTimeType rtVal;
  86.     Int16 i;
  87.  
  88.     lunmonth = dt->month; lunday = dt->day;
  89.  
  90.     for (i = -1; i < 5; i++) {
  91.         lunyear = base.year + 1904 + i;
  92.  
  93.         if  (lun2sol(lunyear, lunmonth, lunday, leapyes, &rtVal)) continue;
  94.         
  95.         dt->year = rtVal.year - 1904;
  96.         dt->month = rtVal.month; dt->day = rtVal.day;
  97.  
  98.         if (DateCompare(base, *dt) <= 0) return true;
  99.     }
  100.     return false;
  101. }
  102.  
  103. // if year is -1, ignore year field
  104. //
  105. Char* DateToAsciiLong(UInt8 months, UInt8 days, Int16 year,
  106.                             DateFormatType dateFormat, Char * pstring)
  107. {
  108.     if (year >= 0) {
  109.         switch (dateFormat) {
  110.         case dfDMYWithSlashes:
  111.             StrPrintF(pstring, "%d/%d/%d", days, months, year);
  112.             break;
  113.         case dfDMYWithDots:
  114.             StrPrintF(pstring, "%d.%d.%d", days, months, year);
  115.             break;
  116.         case dfDMYWithDashes:
  117.             StrPrintF(pstring, "%d-%d-%d", days, months, year);
  118.             break;
  119.         case dfYMDWithSlashes:
  120.             StrPrintF(pstring, "%d/%d/%d", year, months, days);
  121.             break;
  122.         case dfYMDWithDots:
  123.             StrPrintF(pstring, "%d.%d.%d", year, months, days);
  124.             break;
  125.         case dfYMDWithDashes:
  126.             StrPrintF(pstring, "%d-%d-%d", year, months, days);
  127.             break;
  128.         case dfMDYWithSlashes:
  129.         default:
  130.             StrPrintF(pstring, "%d/%d/%d", months, days, year);
  131.             break;
  132.         }
  133.     }
  134.     else {      // no year
  135.         switch (dateFormat) {
  136.         case dfDMYWithSlashes:
  137.             StrPrintF(pstring, "%d/%d", days, months);
  138.             break;
  139.         case dfDMYWithDots:
  140.             StrPrintF(pstring, "%d.%d.", days, months);
  141.             break;
  142.         case dfDMYWithDashes:
  143.             StrPrintF(pstring, "%d-%d", days, months);
  144.             break;
  145.         case dfYMDWithSlashes:
  146.             StrPrintF(pstring, "%d/%d", months, days);
  147.             break;
  148.         case dfYMDWithDots:
  149.             StrPrintF(pstring, "%d.%d", months, days);
  150.             break;
  151.         case dfYMDWithDashes:
  152.             StrPrintF(pstring, "%d-%d", months, days);
  153.             break;
  154.         case dfMDYWithSlashes:
  155.         default:
  156.             StrPrintF(pstring, "%d/%d", months, days);
  157.             break;
  158.         }
  159.     }
  160.     return pstring;
  161. }
  162.  
  163. /************************************************************
  164.  *
  165.  *  FUNCTION: AppInfoGetPtr
  166.  *  DESCRIPTION: Return a locked pointer to the AppInfo or NULL
  167.  *  PARAMETERS: dbP - open database pointer
  168.  *  RETURNS: locked ptr to the AppInfo or NULL
  169.  *
  170.  *************************************************************/
  171. MemPtr AppInfoGetPtr(DmOpenRef dbP)
  172. {
  173.     UInt16       cardNo;
  174.     LocalID    dbID;
  175.     LocalID    appInfoID;
  176.    
  177.     if (DmOpenDatabaseInfo(dbP, &dbID, NULL, NULL, &cardNo, NULL))
  178.         return NULL;
  179.     if (DmDatabaseInfo(cardNo, dbID, NULL, NULL, NULL, NULL, NULL, NULL,
  180.                        NULL, &appInfoID, NULL, NULL, NULL))
  181.         return NULL;
  182.  
  183.     if (appInfoID == NULL)
  184.         return NULL;
  185.     else
  186.         return MemLocalIDToLockedPtr(appInfoID, cardNo);
  187. }   
  188.  
  189.  
  190. // Elf hash
  191. //
  192. // A very good hash function. A return value should be
  193. // taken modulo m, where m is the prime number of buckets
  194. //
  195. UInt32 Hash(Char* name1, Char* name2)
  196. {
  197.     UInt32 h = 0, g;
  198.  
  199.     while (*name1) {
  200.         h = ( h << 4) + *name1++;
  201.         g = h & 0xF0000000L;
  202.         if (g) h ^= g >> 24;
  203.         h &= ~g;
  204.     }
  205.     while (*name2) {
  206.         h = ( h << 4) + *name2++;
  207.         g = h & 0xF0000000L;
  208.         if (g) h ^= g >> 24;
  209.         h &= ~g;
  210.     }
  211.  
  212.     // more large number will be added.
  213.     return h & 65213L;
  214. }
  215.