home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: OtherApp / OtherApp.zip / nut-os2.zip / src / db.c < prev    next >
Text File  |  1998-09-11  |  10KB  |  411 lines

  1. /* db.c */
  2.  
  3. /*
  4.     Nut nutrition software 
  5.     Copyright (C) 1998 Jim Jozwiak.
  6.  
  7.     Source code OS/2 adaptation by Michele Della Guardia
  8.     E-mail : mikedg@ghostbbs.ml.org
  9.     Team OS/2 Italy
  10.  
  11.     This program is free software; you can redistribute it and/or modify
  12.     it under the terms of the GNU General Public License as published by
  13.     the Free Software Foundation; either version 2 of the License, or
  14.     (at your option) any later version.
  15.  
  16.     This program is distributed in the hope that it will be useful,
  17.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.     GNU General Public License for more details.
  20.  
  21.     You should have received a copy of the GNU General Public License
  22.     along with this program; if not, write to the Free Software
  23.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25.  
  26. #include "db.h"
  27. #include "food.h"
  28. #include "options.h"
  29. #include "util.h"
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33.  
  34. #ifndef NUTDIR
  35. char nutdir1[]=".\.";
  36. #else
  37. char nutdir1[]=NUTDIR ;
  38. #endif
  39.  
  40. char rawfoodfile1[] = "abbrev.txt";
  41. char rawnutfoodfile1[] = "nutfood.txt";
  42. char foodfile1[] = "food.db";
  43. char mealfile1[] = "meal.db";
  44. char optionsfile1[] = "options.db";
  45.  
  46. char rawfoodfile[2000];
  47. char rawnutfoodfile[2000];
  48. char foodfile[2000];
  49. char mealfile[2000];
  50. char optionsfile[2000];
  51.  
  52. FILE *fp;
  53.  
  54. void read_raw_food_db() 
  55. {
  56. char buffer[700];
  57. char *bufptr;
  58. char *fieldptr;
  59. int count, foodcount = 0, junk;
  60. float ratio, weight2;
  61. if ((fp = fopen(rawfoodfile,"r")) == NULL)
  62.  {
  63.  printf("Can't open food file \"%s\" to create food database.\n",rawfoodfile);
  64.  printf("Press <enter> to continue...");
  65.  junk = get_int();
  66.  abort();
  67.  }
  68. printf("Reading food file and creating NUT database. Please wait...");
  69. while (fgets(buffer,700,fp) != NULL)
  70.  {
  71.  if ((new_food = malloc(sizeof(struct food))) == NULL)
  72.   {
  73.   printf("We are out of memory.  Bummer.\n");
  74.   abort();
  75.   }
  76.  bufptr = buffer ;
  77.  bufptr += 9;                  /* bypass food id number */
  78.  fieldptr = new_food->name;
  79.  while (*bufptr != '~')
  80.   {
  81.   *fieldptr++ = *bufptr++ ;
  82.   }
  83.  *fieldptr = '\0' ; bufptr +=2 ;
  84.  bufptr += strcspn(bufptr,"^"); bufptr++ ; /* bypass water value */ 
  85.  count = 0;
  86.  while (count < 5)
  87.   {
  88.   fieldptr = bufptr ;
  89.   while (*bufptr != '^' ) bufptr++ ;
  90.   *bufptr = '\0'; bufptr++ ;
  91.   new_food->nutrient[count] = (float) atof(fieldptr);
  92.   count++;
  93.   }
  94.  bufptr += strcspn(bufptr,"^"); bufptr++ ; /* bypass ash value */ 
  95.  while (count < NutrientCount)
  96.   {
  97.   fieldptr = bufptr ;
  98.   while (*bufptr != '^' ) bufptr++ ;
  99.   *bufptr = '\0'; bufptr++ ;
  100.   new_food->nutrient[count] = (float) atof(fieldptr);
  101.   count++;
  102.   }
  103.  fieldptr = bufptr;
  104.  while (*bufptr != '^' ) bufptr++ ;
  105.  *bufptr = '\0'; bufptr++ ;
  106.  new_food->grams = (float) atof(fieldptr);
  107.  if (new_food->grams == 0) new_food->grams = 100;
  108.  fieldptr = ++bufptr;
  109.  while (*bufptr != '~' ) bufptr++ ;
  110.  *bufptr = '\0'; bufptr += 2 ;
  111.  strncpy(new_food->serving,fieldptr,50); new_food->serving[50] = '\0';
  112.  fieldptr = bufptr;
  113.  while (*bufptr != '^' ) bufptr++ ;
  114.  *bufptr = '\0'; bufptr++ ;
  115. /* is second household weight smaller? */
  116.  weight2 = (float) atof(fieldptr);
  117.  if (weight2 < new_food->grams && weight2 > 0)
  118.   {
  119.   new_food->grams = weight2;
  120.   fieldptr = ++bufptr;
  121.   while (*bufptr != '~' ) bufptr++ ;
  122.   *bufptr = '\0'; bufptr += 2 ;
  123.   strncpy(new_food->serving,fieldptr,50); new_food->serving[50] = '\0';
  124.   }
  125.  else
  126.   {
  127.   fieldptr = ++bufptr;
  128.   while (*bufptr != '~' ) bufptr++ ;
  129.   *bufptr = '\0'; bufptr += 2 ;
  130.   }
  131.  fieldptr = ++bufptr;
  132.  new_food->refuse = (float) atof(fieldptr);
  133.  ratio = new_food->grams / 100;
  134.  for (count = 0 ; count < NutrientCount ; count++) new_food->nutrient[count] *= ratio;
  135.  order_new_food();
  136.  foodcount++;
  137.  }
  138. fclose(fp);
  139. FoodCount += foodcount;
  140. write_food_db();
  141. }
  142.  
  143. int read_food_db()
  144. {
  145. int block, foodcount = 0;
  146. struct food *food_ptr = &food_root;
  147. if ((fp = fopen(foodfile,"r")) == NULL) return 0;
  148. for ( ; ; )
  149.  {
  150.  if ((new_food = malloc(sizeof(struct food))) == NULL)
  151.   {
  152.   printf("Bad news.  We are out of memory...");
  153.   abort();
  154.   }
  155.  if ((block = fread(new_food,sizeof(struct food),1,fp)) != 0)
  156.   {
  157.   food_ptr->next = new_food;
  158.   food_ptr = new_food;
  159.   foodcount++;
  160.   }
  161.  else break;
  162.  }
  163. fclose(fp);
  164. FoodCount += foodcount;
  165. return 1;
  166. }
  167.  
  168. void read_meal_db()
  169. {
  170. int block;
  171. struct food *meal_ptr = &meal_root;
  172. if ((fp = fopen(mealfile,"r")) == NULL)
  173.  {
  174.  write_meal_db();
  175.  }
  176. for ( ; ; )
  177.  {
  178.  if ((new_meal = malloc(sizeof(struct food))) == NULL)
  179.   {
  180.   printf("Bad news.  We are out of memory...");
  181.   abort();
  182.   }
  183.  if ((block = fread(new_meal,sizeof(struct food),1,fp)) != 0)
  184.   {
  185.   meal_ptr->next = new_meal;
  186.   meal_ptr = new_meal;
  187.   }
  188.  else break;
  189.  }
  190. fclose(fp);
  191. }
  192.  
  193. void write_meal_db()
  194. {
  195. int block;
  196. struct food *meal_ptr = &meal_root;
  197. if ((fp = fopen(mealfile,"w")) == NULL)
  198.  {
  199.  printf("Can't open meal database %s to write.\n",mealfile);
  200.  abort();
  201.  }
  202. while (meal_ptr -> next != NULL)
  203.  {
  204.  meal_ptr = meal_ptr -> next;
  205.  if ((block = fwrite(meal_ptr,sizeof(struct food),1,fp)) != 1)
  206.   {
  207.   printf("Cannot write to the disk.  Aborting...\n");
  208.   abort();
  209.   }
  210.  }
  211. fclose(fp);
  212. }
  213.  
  214. void write_food_db()
  215. {
  216. int block;
  217. struct food *food_ptr = &food_root;
  218. if ((fp = fopen(foodfile,"w")) == NULL)
  219.  {
  220.  printf("Can't open food database %s to write.\n",foodfile);
  221.  abort();
  222.  }
  223. while (food_ptr -> next != NULL)
  224.  {
  225.  food_ptr = food_ptr -> next;
  226.  if ((block = fwrite(food_ptr,sizeof(struct food),1,fp)) != 1)
  227.   {
  228.   printf("Cannot write to the disk.  Aborting...\n");
  229.   abort();
  230.   }
  231.  }
  232. fclose(fp);
  233. }
  234.  
  235. void write_nut_raw_food_file()
  236. {
  237. char buffer[700], numbuf[11];
  238. int block, c;
  239. if ((fp = fopen(rawnutfoodfile,"a")) == NULL)
  240.  {
  241.  printf("Can't open nut food file %s to append.\n",rawnutfoodfile);
  242.  abort();
  243.  }
  244. strcpy(buffer,"~99999~^~\0");
  245. strcat(buffer,food_work.name);
  246. strcat(buffer,"~^^\0");
  247. for (c = 0 ; c < 5 ; c++)
  248.  {
  249.  sprintf(numbuf,"%5.3f^",100 * food_work.nutrient[c] / food_work.grams);
  250.  strcat(buffer,numbuf);
  251.  }
  252. strcat(buffer,"^\0");
  253. for (c = 5 ; c < NutrientCount ; c++)
  254.  {
  255.  sprintf(numbuf,"%5.3f^",100 * food_work.nutrient[c] / food_work.grams);
  256.  strcat(buffer,numbuf);
  257.  }
  258. sprintf(numbuf,"%5.2f^",food_work.grams);
  259. strcat(buffer,numbuf);
  260. strcat(buffer,"~\0");
  261. strcat(buffer,food_work.serving);
  262. strcat(buffer,"~^^~~^");
  263. sprintf(numbuf,"%2.0f^",food_work.refuse);
  264. strcat(buffer,numbuf);
  265. strcat(buffer,"\n\0");
  266. c = strlen(buffer);
  267. if ((block = fwrite(buffer,c,1,fp)) != 1)
  268.  {
  269.  printf("Cannot write to the disk.  Aborting...\n");
  270.  abort();
  271.  }
  272. fclose(fp);
  273. }
  274.  
  275. void read_nut_raw_food_file() 
  276. {
  277. char buffer[700];
  278. char *bufptr;
  279. char *fieldptr;
  280. int count, foodcount = 0;
  281. float ratio, weight2;
  282. if ((fp = fopen(rawnutfoodfile,"r")) == NULL) return;
  283. printf("Reading nut food file \"%s\" to add to food database \"%s\"...\n",rawnutfoodfile,foodfile);
  284. while (fgets(buffer,700,fp) != NULL)
  285.  {
  286.  if ((new_food = malloc(sizeof(struct food))) == NULL)
  287.   {
  288.   printf("We are out of memory.  Bummer.\n");
  289.   abort();
  290.   }
  291.  bufptr = buffer ;
  292.  bufptr += 9;                  /* bypass food id number */
  293.  fieldptr = new_food->name;
  294.  while (*bufptr != '~')
  295.   {
  296.   *fieldptr++ = *bufptr++ ;
  297.   }
  298.  *fieldptr = '\0' ; bufptr +=2 ;
  299.  bufptr += strcspn(bufptr,"^"); bufptr++ ; /* bypass water value */ 
  300.  count = 0;
  301.  while (count < 5)
  302.   {
  303.   fieldptr = bufptr ;
  304.   while (*bufptr != '^' ) bufptr++ ;
  305.   *bufptr = '\0'; bufptr++ ;
  306.   new_food->nutrient[count] = (float) atof(fieldptr);
  307.   count++;
  308.   }
  309.  bufptr += strcspn(bufptr,"^"); bufptr++ ; /* bypass ash value */ 
  310.  while (count < NutrientCount)
  311.   {
  312.   fieldptr = bufptr ;
  313.   while (*bufptr != '^' ) bufptr++ ;
  314.   *bufptr = '\0'; bufptr++ ;
  315.   new_food->nutrient[count] = (float) atof(fieldptr);
  316.   count++;
  317.   }
  318.  fieldptr = bufptr;
  319.  while (*bufptr != '^' ) bufptr++ ;
  320.  *bufptr = '\0'; bufptr++ ;
  321.  new_food->grams = (float) atof(fieldptr);
  322.  if (new_food->grams == 0) new_food->grams = 100;
  323.  fieldptr = ++bufptr;
  324.  while (*bufptr != '~' ) bufptr++ ;
  325.  *bufptr = '\0'; bufptr += 2 ;
  326.  strncpy(new_food->serving,fieldptr,50); new_food->serving[50] = '\0';
  327.  fieldptr = bufptr;
  328.  while (*bufptr != '^' ) bufptr++ ;
  329.  *bufptr = '\0'; bufptr++ ;
  330. /* is second household weight smaller? */
  331.  weight2 = (float) atof(fieldptr);
  332.  if (weight2 < new_food->grams && weight2 > 0)
  333.   {
  334.   new_food->grams = weight2;
  335.   fieldptr = ++bufptr;
  336.   while (*bufptr != '~' ) bufptr++ ;
  337.   *bufptr = '\0'; bufptr += 2 ;
  338.   strncpy(new_food->serving,fieldptr,50); new_food->serving[50] = '\0';
  339.   }
  340.  else
  341.   {
  342.   fieldptr = ++bufptr;
  343.   while (*bufptr != '~' ) bufptr++ ;
  344.   *bufptr = '\0'; bufptr += 2 ;
  345.   }
  346.  fieldptr = ++bufptr;
  347.  new_food->refuse = (float) atof(fieldptr);
  348.  ratio = new_food->grams / 100;
  349.  for (count = 0 ; count < NutrientCount ; count++) new_food->nutrient[count] *= ratio;
  350.  order_new_food();
  351.  foodcount++;
  352.  }
  353. fclose(fp);
  354. FoodCount += foodcount;
  355. write_food_db();
  356. }
  357.  
  358. void write_options_db()
  359. {
  360. int block;
  361. if ((fp = fopen(optionsfile,"w")) == NULL)
  362.  {
  363.  printf("Can't open options database \"%s\" to write.\n",optionsfile);
  364.  printf("Press <enter> to continue...");
  365.  block = get_int();
  366.  abort();
  367.  }
  368. if ((block = fwrite(&options,sizeof(struct opt),1,fp)) != 1)
  369.  {
  370.  printf("Cannot write to the disk.  Aborting...\n");
  371.  abort();
  372.  }
  373. fclose(fp);
  374. }
  375.  
  376. void read_options_db()
  377. {
  378. int block;
  379. float ratio;
  380. if ((fp = fopen(optionsfile,"r")) == NULL)
  381.  {
  382.  write_options_db();
  383.  return;
  384.  }
  385. if ((block = fread(&options,sizeof(struct opt),1,fp)) != 0)
  386.  {
  387.  fclose(fp);
  388.  ratio = options.calopt / RdiBase[0];
  389.  Rdi[0] *= ratio;
  390.  Rdi[2] *= ratio;
  391.  Rdi[3] *= ratio;
  392.  Rdi[4] *= ratio;
  393.  Rdi[25] *= ratio;
  394.  Rdi[26] *= ratio;
  395.  Rdi[27] *= ratio;  
  396.  Rdi[29] *= ratio;  
  397.  Rdi[30] *= ratio;  
  398.  delete_meals(options.delopt);
  399.  write_meal_db();
  400.  }
  401. }
  402.  
  403. void make_filenames(void)
  404. {
  405. sprintf(rawfoodfile,"%s/%s/%s",getenv("HOME"),nutdir1,rawfoodfile1);
  406. sprintf(rawnutfoodfile,"%s/%s/%s",getenv("HOME"),nutdir1,rawnutfoodfile1);
  407. sprintf(foodfile,"%s/%s/%s",getenv("HOME"),nutdir1,foodfile1);
  408. sprintf(mealfile,"%s/%s/%s",getenv("HOME"),nutdir1,mealfile1);
  409. sprintf(optionsfile,"%s/%s/%s",getenv("HOME"),nutdir1,optionsfile1);
  410. }                             
  411.