home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / LOG.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  8KB  |  219 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /************************************************************************
  4. * LOG.C - Creates a ASCII file with time and date stamps for logging    *
  5. *         hours.                                                        *
  6. *                                                                       *
  7. *   usage: LOG [IN][OUT][CALC]                                          *
  8. *               IN - Creates an opening entry from which a "time spent" *
  9. *                    is calculated.                                     *
  10. *              OUT - Creates a closing entry and calculates             *
  11. *                    "time spent" for that entry.                       *
  12. *             CALC - Calculates total overall "time spent" for the      *
  13. *                    entire log.                                        *
  14. *                                                                       *
  15. *   NOTES:  I used seconds to do all the calculations. The other        *
  16. *           time/date entries are for human readability. Some           *
  17. *           enhancements can be done to this program.                   *
  18. *           i.e. Wage/Pay calculation, closing the log after a CALC     *
  19. *           to insure log is not reused, tracking hours for individual  *
  20. *           people, tracking hours for individual projects, etc.        *
  21. *                                                                       *
  22. *  Public domain by Robert Sprawls.                                     *
  23. ************************************************************************/
  24.  
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <stddef.h>
  28. #include <string.h>
  29. #include <time.h>
  30.  
  31. /* Define time file constants   */
  32.  
  33. #define HOUR        3600        /* Number of seconds in an hour.        */
  34. #define MINS        60          /* Number of seconds in a minute        */
  35. #define IN_ENTRY    40          /* Size of an IN entry                  */
  36. #define SEC_OFF     4           /* Offset of seconds in an IN entry     */
  37. #define HOUR_OFF    64          /* Offset of seconds in an "time spent" */
  38.                                 /* calculated entry.                    */
  39.  
  40. /* Define values returned to DOS */
  41.  
  42. #define OK          0   /* Executed normally, nothing will be echoed */
  43. #define OPENLOG     1   /* Attempted to log in while open entry in log */
  44. #define CLOSEDLOG   2   /* Attempted to log out while closed entry in log */
  45. #define FILE_ERROR  3   /* File access error. Just in case. */
  46. #define SEEK_ERROR  4   /* File positioning error */
  47. #define NOPARMS     5   /* No parameters supplied to program */
  48. #define INVALID     6   /* Invalid parameters */
  49.  
  50. void    usage( void );
  51. long    get_in_entry( FILE * );
  52. void    fastforw( FILE * );     /* Opposite of rewind();        */
  53. void    quit( int );
  54.  
  55. char    strbuf[ IN_ENTRY + 1 ];
  56. FILE    *wrklog;
  57.  
  58. int main( int argc, char *argv[] )
  59. {
  60.     char    outline[ IN_ENTRY * 2 + 1 ];
  61.     long    in_entry_time = 0, total_seconds = 0;
  62.     time_t  current_time;
  63.     div_t   hdiv, mdiv;
  64.  
  65.     if( argc < 2 )
  66.     {
  67.         usage();
  68.         quit( NOPARMS );
  69.     }
  70.  
  71.     /* Open log. Can be any directory.  */
  72.     if(( wrklog = fopen( "WORK.LOG", "a+" )) == NULL )
  73.         quit( FILE_ERROR );
  74.  
  75.     strupr( argv[ 1 ] );
  76.  
  77.     time( ¤t_time );
  78.  
  79.     /* Create an opening IN entry.  */
  80.     if( strcmp( "IN", argv[ 1 ] ) == 0 )
  81.     {
  82.         /* Make sure there isn't a open entry already.  */
  83.         if( get_in_entry( wrklog ) )
  84.             quit( OPENLOG );
  85.  
  86.         /* Stamp it.    */
  87.         fprintf( wrklog, "%3s %ld %s", argv[ 1 ], current_time,
  88.             ctime( ¤t_time ));
  89.     }
  90.     /* Create a closing OUT entry.  */
  91.     else if( strcmp( "OUT", argv[ 1 ] ) == 0 )
  92.     {
  93.         /* Make sure there is a previous IN entry.  */
  94.         if( ( in_entry_time = get_in_entry( wrklog )) == 0 )
  95.             quit( CLOSEDLOG );
  96.  
  97.         total_seconds = current_time - in_entry_time;
  98.         sprintf( outline, "%3s %ld %s", argv[ 1 ], current_time,
  99.             ctime( ¤t_time ));
  100.  
  101.         /* Cut off the newline character that's normally put on.    */
  102.         outline[ strlen( outline ) - 1 ] = '\0';
  103.         hdiv = div( total_seconds, HOUR );
  104.         mdiv = div( hdiv.rem, MINS );
  105.  
  106.         sprintf( strbuf, "     Time Spent: %02d:%02d:%02d/%ld\n\n",
  107.             hdiv.quot, mdiv.quot, mdiv.rem, total_seconds );
  108.         strcat( outline, strbuf );
  109.         fprintf( wrklog, outline );
  110.     }
  111.     /* Calculate the overall "time spent"   */
  112.     else if( strcmp( "CALC", argv[ 1 ] ) == 0 )
  113.     {
  114.         rewind( wrklog );
  115.         while( !feof( wrklog ) )
  116.         {
  117.             /* This is to eliminate garbage or residual entries.    */
  118.             outline[ 0 ] = '\0';
  119.  
  120.             fgets( outline, 80, wrklog );
  121.             if( strstr( outline, "OUT" ) != NULL )
  122.             {
  123.                 total_seconds += atol( &outline[ HOUR_OFF ] );
  124.             }
  125.  
  126.         }
  127.  
  128.         /* goto to end of file and stamp total hours    */
  129.         fastforw( wrklog );
  130.         if( total_seconds )
  131.         {
  132.             hdiv = div( total_seconds, HOUR );
  133.             mdiv = div( hdiv.rem, MINS );
  134.             fprintf( wrklog, "\t\t\t\t\t\t\t\t\t\t  "
  135.                 "Total Hours: %02d:%02d:%02d/%ld\n",
  136.                 hdiv.quot, mdiv.quot, mdiv.rem, total_seconds );
  137.         }
  138.     }
  139.     else
  140.     {
  141.         usage();
  142.         quit( INVALID );
  143.     }
  144.  
  145.     quit( OK );
  146.     return 0;
  147. }
  148.  
  149. void usage( void )
  150. {
  151.     printf( "\nusage: LOG [IN][OUT][CALC]\n");
  152. }
  153.  
  154. /****************************************************************
  155. * get_in_entry - gets a previous IN entry.                      *
  156. *                                                               *
  157. *  Params: FILES *fp - file pointer.                            *
  158. * Returns: The entry's seconds if successful, else 0            *
  159. *                                                               *
  160. * NOTES: If fseek fails for any reason, function does not       *
  161. *        return. Instead, quit is call with the error code.     *
  162. ****************************************************************/
  163.  
  164. long get_in_entry( FILE *fp )
  165. {
  166.  
  167.     if( fseek( fp, -IN_ENTRY, SEEK_END ) != 0 )
  168.         quit( SEEK_ERROR );
  169.  
  170.     fread( strbuf, 1, IN_ENTRY, fp );
  171.     fastforw( fp );
  172.  
  173.     if( strstr( strbuf, "IN" ) == NULL )
  174.         return( 0 );
  175.     else
  176.     {
  177.         return( atol( &strbuf[ SEC_OFF ]));
  178.     }
  179. }
  180.  
  181. /****************************************************************
  182. * quit() - Program exit function. Reports of any outstanding    *
  183. *          errors.                                              *
  184. *                                                               *
  185. *  Params:  errcode - Error code as defined in beginning.       *
  186. * Returns:  nothing.                                            *
  187. ****************************************************************/
  188.  
  189. void quit( int errcode )
  190. {
  191.     char *errmsg[] =
  192.     {
  193.         "",
  194.         "Log has an open entry.",
  195.         "No corresponding IN entry.",
  196.         "File open error.",
  197.         "Seek error",
  198.         "No parameters specified.",
  199.         "Invalid Parameters."
  200.     };
  201.  
  202.     printf( "\n%s\n", errmsg[ errcode ] );
  203.  
  204.     fclose( wrklog );
  205.     exit( errcode );
  206. }
  207.  
  208. /****************************************************************
  209. * fastforw() - Puts file pointer to end of file.                *
  210. *                                                               *
  211. *  Params: fp - File pointer.                                   *
  212. * Returns: nothing.                                             *
  213. ****************************************************************/
  214.  
  215. void fastforw( FILE *fp )
  216. {
  217.     fseek( fp, 0, SEEK_END );
  218. }
  219.