home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Developer / StopWatch / Source / Session.m < prev    next >
Encoding:
Text File  |  1994-02-06  |  3.6 KB  |  196 lines

  1. /*
  2.  * For legal stuff see the file COPYRIGHT
  3.  */
  4. #import <sys/time.h>
  5. #import <stdio.h>
  6. #import <stdlib.h>
  7. #import "Session.h"
  8. #import "Preferences.h"
  9.  
  10. extern void freeAndCopy( char **ptr, const char *str );
  11. extern char *NXCopyStringBuffer(const char *buffer);
  12.  
  13. @implementation Session
  14.  
  15. static ColDesc LayoutOne[] = {
  16.   { 3.0,    "startDateString",    (SEL)0 },
  17.   { 60.0,    "startTimeString",    (SEL)0 },
  18.   { 100.0,    "durationString",    (SEL)0 },
  19.   { 134.0,    "description",        (SEL)0 },
  20.   { 0.0,    NULL,            (SEL)0 },
  21. };
  22.  
  23. /*
  24.  * If the user selected the "ShowEndTimes" preference,
  25.  * the spacing gets wider to accomodate the additional info.
  26.  */
  27. static ColDesc LayoutTwo[] = {
  28.   { 3.0,    "startDateString",    (SEL)0 },
  29.   { 60.0,    "sessionBounds",    (SEL)0 },
  30.   { 134.0,    "durationString",    (SEL)0 },
  31.   { 170.0,    "description",        (SEL)0 },
  32.   { 0.0,    NULL,            (SEL)0 },
  33. };
  34.  
  35. + initialize
  36. {
  37.   ColDesc *ptr;
  38.  
  39.   /* Initialize the selectors in the ColDesc table */ 
  40.   for ( ptr = LayoutOne; ptr->method; ptr++ )
  41.     ptr->selector = sel_getUid( ptr->method );
  42.  
  43.   for ( ptr = LayoutTwo; ptr->method; ptr++ )
  44.     ptr->selector = sel_getUid( ptr->method );
  45.  
  46.   return self;
  47. }
  48.  
  49. - init:(const char *)startDate
  50.        time:(const char *)startTime
  51.        duration:(int)elapsedMinutes
  52.        description:(const char *)desc
  53. {
  54.   [self setStartDateString:startDate];
  55.   [self setStartTimeString:startTime];
  56.   [self setDescription:desc];
  57.   minutes = elapsedMinutes;
  58.   return self;
  59. }
  60.  
  61. - (void) freeDesc
  62. {
  63.   if ( description ) {
  64.     free( description );
  65.     description = NULL;
  66.   }
  67. }
  68.  
  69. - free
  70. {
  71.   [self freeDesc];
  72.   return [super free];
  73. }
  74.  
  75. - (ColDesc *)colDesc
  76. {
  77.   return [[Preferences new] showEndTimes] ? LayoutTwo : LayoutOne;
  78. }
  79.  
  80. - setMinutes:(int)value
  81. {
  82.   minutes = value;
  83.   return self;
  84. }
  85.  
  86. /* Must we parse the string and convert to UNIX format? Why bother? */
  87. - setStartDateString:(const char *)str
  88. {
  89.   freeAndCopy( &startDateString, str );
  90.   return self;
  91. }
  92.  
  93. - setStartTimeString:(const char *)str
  94. {
  95.   freeAndCopy( &startTimeString, str );
  96.   return self;
  97. }
  98.  
  99. - setDescription:(const char *)desc
  100. {
  101.   [self freeDesc];
  102.   description = NXCopyStringBuffer(desc);
  103.   return self;
  104. }
  105.  
  106. - (const char *)startTimeString
  107. {
  108.   return startTimeString;
  109. }
  110.  
  111. - (const char *)sessionBounds
  112. {
  113.   static char buf[80];
  114.   int startHr, startMin, endHr, endMin;
  115.  
  116.   sscanf( startTimeString, "%d:%d", &startHr, &startMin );
  117.   endHr  = startHr  + minutes / 60;
  118.   endMin = startMin + minutes % 60;
  119.  
  120.   if ( endMin > 59 )
  121.     endHr++;
  122.   
  123.   endMin %= 60;
  124.   endHr  %= 24;
  125.  
  126.   sprintf( buf, "%02d:%02d-%02d:%02d", startHr, startMin, endHr, endMin );
  127.   return buf;
  128. }
  129.  
  130. - (const char *)startDateString
  131. {
  132.   return startDateString;
  133. }
  134.  
  135. - (const char *)description
  136. {
  137.   return description;
  138. }
  139.  
  140. - (int)minutes
  141. {
  142.   return minutes;
  143. }
  144.  
  145. - (float)hours
  146. {
  147.   return minutes/60.0;
  148. }
  149.  
  150. - (const char *)durationString
  151. {
  152.   static char buf[20];
  153.  
  154.   sprintf( buf, "%d:%02d", minutes / 60, minutes % 60 );
  155.   return buf;
  156. }
  157.  
  158. /*
  159.  * For use in comparisons, convert date such as 11/21/93 to a
  160.  * single integer 932111. THIS SHOULD BE CACHED! (TBD)
  161.  */
  162. - (int)dateValue
  163. {
  164.   int day, month, year;
  165.  
  166.   sscanf( startDateString, "%d/%d/%d", &month, &day, &year );
  167.   return ( year * 10000 + month * 100 + day );
  168. }
  169.  
  170. /*
  171.  * Similarly, convert 12:45 to the integer 1245.
  172.  */
  173. - (int)timeValue
  174. {
  175.   int minute, hour;
  176.  
  177.   sscanf( startTimeString, "%d:%d", &hour, &minute );
  178.   return ( hour * 100 + minute );
  179. }
  180.  
  181. - read:(NXTypedStream *) stream
  182. {
  183.   NXReadTypes( stream, "***i", &description, &startTimeString,
  184.           &startDateString, &minutes );
  185.   return self;
  186. }
  187.  
  188. - write:(NXTypedStream *) stream
  189. {
  190.   NXWriteTypes( stream, "***i", &description, &startTimeString,
  191.            &startDateString, &minutes );
  192.   return self;
  193. }
  194.  
  195. @end
  196.