home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: OtherApp / OtherApp.zip / nut-os2.zip / src / recmeal.c < prev    next >
C/C++ Source or Header  |  1998-09-10  |  4KB  |  144 lines

  1. /* recmeal.c */
  2.  
  3. /*
  4.     Nut nutrition software 
  5.     Copyright (C) 1998 Jim Jozwiak.
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 2 of the License, or
  10.     (at your option) any later version.
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21.  
  22. #include "recmeal.h"
  23. #include "food.h"
  24. #include "options.h"
  25. #include "util.h"
  26. #include "db.h"
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <time.h>
  31.  
  32. void record_meals()
  33. {
  34. struct food *food_ptr;
  35. char meal_date[7];
  36. char new_meal_date[7];
  37. char key[60];
  38. int meal, c, delfood, junk;
  39. float ratio = 0, one = 1;
  40. key[0] = '\0';
  41. today(meal_date);
  42. for ( ; ; )
  43.  {
  44.  header("NUT:  Record Meals");
  45.  printf("\n\n\n\n\n\nMeal Date:    %s\n\n",meal_date);
  46.  spacer(8);
  47.  printf("\nType meal date (yymmdd) or just press <enter> for today's date:  ");
  48.  get_string(new_meal_date,7);
  49.  if (strlen(new_meal_date) == 6) strcpy(meal_date,new_meal_date);
  50.  if (strlen(new_meal_date) == 6 || strlen(new_meal_date) == 0) break;
  51.  }
  52. header("NUT:  Record Meals");
  53. printf("\n\n\n\n\n\nMeal Date:    %s\n\n",meal_date);
  54. printf("Meal Number:\n");
  55. spacer(9);
  56. printf("\nType meal number (1, 2, or 3) or just <enter> to quit:  ");
  57. meal = get_int();
  58. if (meal < 1 || meal > 3) return;
  59. for ( ; ; )
  60.  {
  61.  for ( ; ; )
  62.   {
  63.   if ((ratio >= 0) && (meal_find(meal_date,meal) != 0))
  64.    {
  65.    header("NUT:  Record Meals");
  66.    if (! meal_show(meal_date,meal))
  67.     {
  68.     printf("\nPress <enter> to continue...");
  69.     junk = get_int();
  70.     }
  71.    else
  72.     {
  73.     printf("\nEnter number to delete or name of next food:  ");
  74.     get_string(key,60);
  75.     if (strcmp(key,"") == 0)
  76.      {
  77.      delete_meals(options.delopt); 
  78.      return;
  79.      }
  80.     }
  81.    }
  82.   delfood = atoi(key);
  83.   if (strcmp(key,"") == 0 || delfood == 0) break;
  84.   delete_meal_food(meal_date,meal,delfood);
  85.   write_meal_db();
  86.   strcpy(key,"");
  87.   }
  88.  key_put(key);
  89.  food_ptr = food_choice("NUT:  Record Meals");
  90.  if (food_ptr == (struct food *) -1) return;
  91.  if (food_ptr == (struct food *)  0) key_clean();
  92.  if (food_ptr == (struct food *)  0) continue;
  93.  header("NUT:  Record Meals");
  94.  food_show(food_ptr, &one);
  95.  printf("\nType # of servings {xxxg for grams, xxxo for oz, \"b\" for back}:  ");
  96.  get_qty(&ratio, &(food_ptr->grams));
  97.  if (ratio < 0)
  98.   {
  99.   key_take();
  100.   strcpy(key,key_take());
  101.   }
  102.  if (ratio > 0)
  103.   {
  104.   if ((new_meal = malloc(sizeof(struct food))) == NULL)
  105.    {
  106.    printf("We are out of memory.  Bummer.\n");
  107.    abort();
  108.    } 
  109.   memcpy(new_meal,food_ptr,sizeof(struct food));
  110.   strcpy(new_meal->meal_date,meal_date);
  111.   new_meal->meal = meal;
  112.   new_meal->grams *= ratio;
  113.   for (c = 0; c < NutrientCount; c++) new_meal->nutrient[c] *= ratio; 
  114.   order_new_meal();
  115.   write_meal_db();
  116.   }
  117.  }
  118. }
  119.  
  120. void today(char *whatever)
  121. {
  122. int c, month = 0;
  123. struct tm *p;
  124. time_t t;
  125. char timestamp[26];
  126. char meal_date[7];
  127. char *thismonth;
  128. char *Months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  129.                 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  130. time(&t);
  131. p = localtime(&t);
  132. strcpy(timestamp,asctime(p));
  133. meal_date[0] = timestamp[22];
  134. meal_date[1] = timestamp[23];
  135. thismonth = timestamp + 4;
  136. for (c = 0; c < 12 ; c++) if (strncmp(thismonth,Months[c],3) == 0) month = c+1;
  137. sprintf(meal_date+2,"%02d",month);
  138. meal_date[4] = timestamp[8];
  139. meal_date[5] = timestamp[9];
  140. meal_date[6] = '\0';
  141. if (meal_date[4] == ' ') meal_date[4] = '0';
  142. strncpy(whatever,meal_date,7);
  143. }
  144.