File: options.c

package info (click to toggle)
nut-nutrition 15.5-1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 5,224 kB
  • ctags: 520
  • sloc: ansic: 7,230; sh: 187; makefile: 56
file content (810 lines) | stat: -rw-r--r-- 30,244 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
/* options.c */

/*
    NUT nutrition software 
    Copyright (C) 1996-2010 by Jim Jozwiak.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "options.h"
#include "util.h"
#include "db.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

struct opt options;

static float DefaultTarget = 50, DefaultEPADHA = 2.2;

void initialize_options()
{
int c;
options.delopt = -1;
options.defanal = 0;
options.defanalanal = 0;
options.defanalrec = 0;
options.screen = 0;
options.custom = 0;
options.pcprotein = 0;
options.pccarb= 0;
options.pcsatfat = 0;
options.protcalpergm = 4;
options.carbcalpergm = 4;
options.fatcalpergm = 9.2307692;
options.fattyacidfactor = 0.956;
options.next_recipe = 99000;
for (c=0; c < NUTRIENT_COUNT; c++)
 {
 options.locknuts[c] = 0;
 options.abnuts[c] = 0;
 }
if (mealdb_mealsperday > 0 && mealdb_mealsperday < 20) options.mealsperday = mealdb_mealsperday;
else options.mealsperday = 3;
options.grams = 0;
options.autocal = 0;
options.temp_meal_root = &meal_root;
}

void personal_cal()
{
float buf = 0;
int choice;
options.abnuts[FAMS] = 0;
header("NUT:  Set Personal Calorie Level");
if (options.autocal == 0 ) printf("\n  Calorie level is set to %0.0f.\n\n",DV[ENERC_KCAL]);
else printf("\n  Calorie level is set to %0.0f.  Option is #%d.\n\n",DV[ENERC_KCAL],options.autocal + 2);
printf("\n  1.  I will type a new calorie level.\n");
printf("\n  2.  I want to return to the program default of 2000.\n");
printf("\n  3.  I want NUT to automatically use the average calories from all my meals.\n");
printf("\n  4.  When I make a daily \"Weight Log Regression\" entry, make a small calorie\n");
printf("      adjustment to promote loss of fat mass without loss of lean mass.\n");
printf("\n  5.  When I make a daily \"Weight Log Regression\" entry, make a small calorie\n");
printf("      adjustment to promote gain of lean mass without gain of fat mass.\n");
spacer(15);
printf("\nEnter your choice (just <enter> to retain current setting):  ");
choice = get_int();
switch (choice)
 {
 case 1 :
  header("NUT:  Set Personal Calorie Level");
  spacer(0);
  printf("\nCalorie level is set to %0.0f.  Type new level (just <enter> for default):  ",DV[ENERC_KCAL]);
  get_cals(&buf);
  if (buf <= 0) options.locknuts[ENERC_KCAL] = 0;
  else
   {
   options.locknuts[ENERC_KCAL] = 1;
   options.abnuts[ENERC_KCAL] = buf;
   }
  if (!options.locknuts[FAT]) options.abnuts[FAT] = 0;
  options.abnuts[FAMS] = 0;
  options.autocal = 0;
  break;
 case 3 :
  options.locknuts[ENERC_KCAL] = 0;
  options.abnuts[ENERC_KCAL] = 0;
  options.locknuts[FAT] = 0;
  options.abnuts[FAT] = 0;
  options.locknuts[FAMS] = 0;
  options.abnuts[FAMS] = 0;
  options.autocal = 1;
  break;
 case 4 :
  options.locknuts[ENERC_KCAL] = 1;
  options.locknuts[FAT] = 0;
  options.abnuts[FAT] = 0;
  options.locknuts[FAMS] = 0;
  options.abnuts[FAMS] = 0;
  options.autocal = 2;
  break;
 case 5 :
  options.locknuts[ENERC_KCAL] = 1;
  options.locknuts[FAT] = 0;
  options.abnuts[FAT] = 0;
  options.locknuts[FAMS] = 0;
  options.abnuts[FAMS] = 0;
  options.autocal = 3;
  break;
 case 2 :
  options.locknuts[ENERC_KCAL] = 0;
  options.abnuts[ENERC_KCAL] = 0;
  if (!options.locknuts[FAT]) options.abnuts[FAT] = 0;
  options.abnuts[FAMS] = 0;
  options.autocal = 0;
  break;
 default :
  buf = options.abnuts[ENERC_KCAL];
  break;
  }
auto_cal(&buf,0);
header("NUT:  Set Personal Calorie Level");
new_nut_levels();
}
   
void efa_options()
{
char buf[128];
int menu_choice;
float tempfloat;

while (1)
 {
 header("NUT:  Essential Fatty Acid Options");
 printf("\n\n");
 if (options.locknuts[FAPU] != 1) printf("           1.  Accept current reference values as absolute amounts.\n\n");
 else printf("           1.  Allow reference values to change with analysis resets.\n\n");
 if (options.locknuts[FAPU] != 1) printf("           2.  Change target for Omega-6/3 Balance (currently %2.0f/%2.0f).\n\n",options.n6hufa == 0 ? DefaultTarget : options.n6hufa,options.n6hufa == 0 ? 100 - DefaultTarget : 100 - options.n6hufa);
 if (options.locknuts[FAPU] != 1) printf("           3.  Change EPA+DHA maximum grams (currently %3.1f grams).\n\n",options.epadhamax == 0 ? DefaultEPADHA : options.epadhamax);
 if (options.locknuts[FAPU] != 1) printf("\n\n\n      If you change options #2 or #3, you will have to reset the analysis\n");
 if (options.locknuts[FAPU] != 1) printf("      screen with a \"c\", \"m\", or \"o\" to have the new settings take effect.\n");
 if (options.locknuts[FAPU] != 1) spacer(13);
 else spacer(4);
 printf("\nEnter your choice (just <enter> to quit):  ");
 menu_choice = get_char();
 switch (menu_choice)
  {

  case '1' : efa_method();
             write_OPTIONS();
             break;

  case '2' : 
  header("NUT:  Essential Fatty Acid Options");
  printf("\n\n");
  if (options.locknuts[FAPU] != 1) printf("           1.  Accept current reference values as absolute amounts.\n\n");
  else printf("           1.  Allow reference values to change with analysis resets.\n\n");
  if (options.locknuts[FAPU] != 1) printf("           2.  Change target for Omega-6/3 Balance (currently %2.0f/%2.0f).\n\n",options.n6hufa == 0 ? DefaultTarget : options.n6hufa,options.n6hufa == 0 ? 100 - DefaultTarget : 100 - options.n6hufa);
  if (options.locknuts[FAPU] != 1) printf("           3.  Change EPA+DHA maximum grams (currently %3.1f grams).\n\n",options.epadhamax == 0 ? DefaultEPADHA : options.epadhamax);
  if (options.locknuts[FAPU] != 1) spacer(8);
  else spacer(4);
  printf("\nEnter new Omega-6 percentage (15-90, 90 = off):  ");
  get_string(buf,127);
  tempfloat = atof(buf);
  if (tempfloat < 15 || tempfloat > 90) break;
  else options.n6hufa = tempfloat;
  write_OPTIONS();
  break;                 

  case '3' : 
  header("NUT:  Essential Fatty Acid Options");
  printf("\n\n");
  if (options.locknuts[FAPU] != 1) printf("           1.  Accept current reference values as absolute amounts.\n\n");
  else printf("           1.  Allow reference values to change with analysis resets.\n\n");
  if (options.locknuts[FAPU] != 1) printf("           2.  Change target for Omega-6/3 Balance (currently %2.0f/%2.0f).\n\n",options.n6hufa == 0 ? DefaultTarget : options.n6hufa,options.n6hufa == 0 ? 100 - DefaultTarget : 100 - options.n6hufa);
  if (options.locknuts[FAPU] != 1) printf("           3.  Change EPA+DHA maximum grams (currently %3.1f grams).\n\n",options.epadhamax == 0 ? DefaultEPADHA : options.epadhamax);
  if (options.locknuts[FAPU] != 1) spacer(8);
  else spacer(4);
  printf("\nEnter new EPA+DHA maximum grams:  ");
  get_string(buf,127);
  tempfloat = atof(buf);
  if (tempfloat <= 0) break;
  else options.epadhamax = tempfloat;
  write_OPTIONS();
  break; 

  default  : return;
  }
 }
}

void efa_method()
{
if (options.locknuts[FAPU] == 1)
 {
 if (!options.locknuts[FAT]) options.abnuts[FAT] = 0;
 options.abnuts[FAMS] = 0;
 options.abnuts[FAPU] = 0; options.locknuts[FAPU] = 0;
 options.abnuts[OMEGA6] = 0; options.locknuts[OMEGA6] = 0;
 options.abnuts[LA] = 0; options.locknuts[LA] = 0;
 options.abnuts[AA] = 0; options.locknuts[AA] = 0;
 options.abnuts[OMEGA3] = 0; options.locknuts[OMEGA3] = 0;
 options.abnuts[ALA] = 0; options.locknuts[ALA] = 0;
 options.abnuts[EPA] = 0; options.locknuts[EPA] = 0;
 options.abnuts[DHA] = 0; options.locknuts[DHA] = 0;
 return;
 }
 
options.abnuts[FAPU] = DV[FAPU]; options.locknuts[FAPU] = 1;
options.abnuts[OMEGA6] = DV[OMEGA6]; options.locknuts[OMEGA6] = 1;
options.abnuts[LA] = DV[LA]; options.locknuts[LA] = 1;
options.abnuts[AA] = DV[AA]; options.locknuts[AA] = 1;
options.abnuts[OMEGA3] = DV[OMEGA3]; options.locknuts[OMEGA3] = 1;
options.abnuts[ALA] = DV[ALA]; options.locknuts[ALA] = 1;
options.abnuts[EPA] = DV[EPA]; options.locknuts[EPA] = 1;
options.abnuts[DHA] = DV[DHA]; options.locknuts[DHA] = 1;
auto_cal(&DV[ENERC_KCAL],0);
}

void protein_percent()
{
char buf[128];
header("NUT:  Protein Percentage");
spacer(0);
printf("\nEnter percentage of protein (just <enter> for default):  ");
get_string(buf,127);
options.pcprotein = atof(buf);
if ( options.pcprotein <= 0 || (options.pcprotein + options.pccarb) > 90 )
 {
 options.pcprotein = 0;
 if (options.abnuts[PROCNT] == 0) options.locknuts[PROCNT] = 0;
 }
if ( options.pcprotein > 0 )
 {
 options.abnuts[PROCNT] = 0;
 options.locknuts[PROCNT] = 1;
 }
if (!options.locknuts[FAT]) options.abnuts[FAT] = 0;
options.abnuts[FAMS] = 0;
options.locknuts[FAMS] = 0;
auto_cal(&DV[ENERC_KCAL],0);
header("NUT:  Protein Percentage");
new_nut_levels();
}

void carb_percent()
{
char buf[128];
header("NUT:  Carbohydrate Percentage");
spacer(0);
printf("\nEnter percentage of carbohydrate (just <enter> for default):  ");
get_string(buf,127);
options.pccarb = atof(buf);
options.locknuts[CHOCDF] = 1;
if ( options.pccarb <= 0 || (options.pcprotein + options.pccarb) > 90 )
 {
 options.pccarb = 0;
 options.locknuts[CHOCDF] = 0;
 }
if ( options.pccarb > 0 ) options.abnuts[CHOCDF] = 0;
if (!options.locknuts[FAT]) options.abnuts[FAT] = 0;
options.abnuts[FAMS] = 0;
options.locknuts[FAMS] = 0;
auto_cal(&DV[ENERC_KCAL],0);
header("NUT:  Carbohydrate Percentage");
new_nut_levels();
}

void satfat_percent()
{
char buf[128];
header("NUT:  Saturated Fat Percentage");
spacer(0);
printf("\nEnter percentage of saturated fat (just <enter> for default):  ");
get_string(buf,127);
options.pcsatfat = atof(buf);
options.locknuts[FASAT] = 1;
if ( options.pcsatfat <= 0  || options.pcsatfat > 90 )
 {
 options.pcsatfat = 0;
 options.locknuts[FASAT] = 0;
 options.abnuts[FASAT] = 0;
 }
if (!options.locknuts[FAT]) options.abnuts[FAT] = 0;
options.abnuts[FAMS] = 0;
options.locknuts[FAMS] = 0;
auto_cal(&DV[ENERC_KCAL],0);
header("NUT:  Saturated Fat Percentage");
new_nut_levels();
}

void protein_absolute()
{
char buf[128];
header("NUT:  Protein Absolute Amount");
spacer(0);
printf("\nEnter grams of protein (just <enter> for default):  ");
get_string(buf,127);
options.abnuts[PROCNT] = atof(buf);
if ( options.abnuts[PROCNT] <= 0 )
 {
 options.abnuts[PROCNT] = 0;
 if (options.pcprotein == 0) options.locknuts[PROCNT] = 0;
 }
if ( options.abnuts[PROCNT] > 0 )
 {
 options.pcprotein = 0;
 options.locknuts[PROCNT] = 1;
 }
if (!options.locknuts[FAT]) options.abnuts[FAT] = 0;
options.abnuts[FAMS] = 0;
options.locknuts[FAMS] = 0;
auto_cal(&DV[ENERC_KCAL],0);
header("NUT:  Protein Absolute Amount");
new_nut_levels();
}

void carb_absolute()
{
char buf[128];
header("NUT:  Carbohydrate Absolute Amount");
spacer(1);
printf("\nEnter grams of carbohydrate, or a negative value to set grams of non-fiber carb");
printf("\nindependent of fiber (just <enter> for default):  ");
get_string(buf,127);
options.abnuts[CHOCDF] = atof(buf);
options.locknuts[CHOCDF] = 0;
if ( options.abnuts[CHOCDF] != 0 )
 {
 options.pccarb = 0;
 options.locknuts[CHOCDF] = 1;
 }
if (!options.locknuts[FAT]) options.abnuts[FAT] = 0;
options.abnuts[FAMS] = 0;
options.locknuts[FAMS] = 0;
auto_cal(&DV[ENERC_KCAL],0);
header("NUT:  Carbohydrate Absolute Amount");
new_nut_levels();
}

void fiber_absolute()
{
char buf[128];
header("NUT:  Fiber Absolute Amount");
spacer(0);
printf("\nEnter grams of fiber (just <enter> for default):  ");
get_string(buf,127);
options.abnuts[FIBTG] = atof(buf);
options.locknuts[FIBTG] = 1;
if ( options.abnuts[FIBTG] < 1 || options.abnuts[FIBTG] > 999 )
 {
 options.abnuts[FIBTG] = 0;
 options.locknuts[FIBTG] = 0;
 }
auto_cal(&DV[ENERC_KCAL],0);
header("NUT:  Fiber Absolute Amount");
new_nut_levels();
}

void screen(void)
{
options.screen++;
options.screen %= MaxScreen;
write_OPTIONS();
}

void screen_previous(void)
{
    if (--options.screen < 0) {
        options.screen = MaxScreen - 1;
    }
    write_OPTIONS();
}

void get_cals(float *cals)
{
char buff[128];
fgets(buff,128,stdin);
*cals = (float) atof(buff);
}

void auto_cal(float *bufptr, int analflag)
{
float buf = *bufptr, temp = 0;
int i;
if (options.autocal == 1) buf = average_cals();
if (analflag)
 {
 if (food_work.nutrient[PROT_KCAL] > 0 && food_work.nutrient[PROCNT] > 0) options.protcalpergm = food_work.nutrient[PROT_KCAL] / food_work.nutrient[PROCNT];
 if (food_work.nutrient[CHO_KCAL] > 0 && food_work.nutrient[CHOCDF] > 0) options.carbcalpergm = food_work.nutrient[CHO_KCAL] / food_work.nutrient[CHOCDF];
 if (food_work.nutrient[FAT_KCAL] > 0 && food_work.nutrient[FAT] > 0) options.fatcalpergm = food_work.nutrient[FAT_KCAL] / food_work.nutrient[FAT];
 if (food_work.nutrient[FAT] > 0 && food_work.nutrient[FASAT] + food_work.nutrient[FAMS] + food_work.nutrient[FAPU] > 0) options.fattyacidfactor = (food_work.nutrient[FASAT] + food_work.nutrient[FAMS] + food_work.nutrient[FAPU]) / food_work.nutrient[FAT];
 }
for (i = 1 ; i <= *ScreenMap[0] ; i++) DV[ScreenMap[0][i]] = DVBase[ScreenMap[0][i]];
if (buf <= 0) 
 {
 buf = 2000;
 if (!options.locknuts[ENERC_KCAL]) options.abnuts[ENERC_KCAL] = DVBase[ENERC_KCAL];
 if (options.locknuts[ENERC_KCAL]) buf = options.abnuts[ENERC_KCAL];
 }
else if (buf > 0 && buf <= 100) buf = 100;
else if (buf > 9999) buf = 9999;
buf /= DVBase[ENERC_KCAL];
DV[ENERC_KCAL] = buf * DVBase[ENERC_KCAL];
if (buf != 1)
 {
 DV[PROCNT] = buf * DVBase[PROCNT] * 4 / options.protcalpergm;
 DV[FAT] = buf * DVBase[FAT] * 9.2307692 / options.fatcalpergm;
 DV[CHOCDF] = buf * DVBase[CHOCDF] * 4 / options.carbcalpergm;
 }
if (buf == 1)
 {
 DV[PROCNT] = DVBase[PROCNT];
 DV[FAT] = DVBase[FAT];
 DV[CHOCDF] = DVBase[CHOCDF];
 options.protcalpergm = 4;
 options.carbcalpergm = 4;
 options.fatcalpergm = 9.2307692;
 }
DV[FIBTG] = buf * DVBase[FIBTG];
DV[FASAT] = buf * DVBase[FASAT];
DV[FAPU] = buf * DVBase[FAPU];
DV[FAMS] = options.fattyacidfactor * DV[FAT] - DV[FASAT] - DV[FAPU];
DV[OMEGA6] = buf * DVBase[OMEGA6];
DV[LA] = buf * DVBase[LA];
DV[AA] = buf * DVBase[AA];
DV[OMEGA3] = buf * DVBase[OMEGA3];
DV[ALA] = buf * DVBase[ALA];
DV[EPA] = buf * DVBase[EPA];
DV[DHA] = buf * DVBase[DHA];
if (options.locknuts[ENERC_KCAL]) DV[ENERC_KCAL] = options.abnuts[ENERC_KCAL];
if (!options.locknuts[ENERC_KCAL]) options.abnuts[ENERC_KCAL] = DV[ENERC_KCAL];
if (options.abnuts[FIBTG] > 0) DV[FIBTG] = options.abnuts[FIBTG];
if (options.pcprotein > 0) DV[PROCNT] = options.pcprotein / 100 * DV[ENERC_KCAL] / options.protcalpergm;
if (options.abnuts[PROCNT] > 0) DV[PROCNT] = options.abnuts[PROCNT];
if (options.pccarb > 0) DV[CHOCDF] = options.pccarb / 100 * DV[ENERC_KCAL] / options.carbcalpergm;
if (options.abnuts[CHOCDF] > 0) DV[CHOCDF] = options.abnuts[CHOCDF];
if (options.abnuts[CHOCDF] < 0) DV[CHOCDF] = DV[FIBTG] + -1 * options.abnuts[CHOCDF];
if (DV[PROCNT] != DVBase[PROCNT] || DV[CHOCDF] != DVBase[CHOCDF])
 {
 temp = 1 - (DV[PROCNT] * options.protcalpergm + DV[CHOCDF] * options.carbcalpergm) / DV[ENERC_KCAL];
 DV[FAT] = temp * DV[ENERC_KCAL] / options.fatcalpergm;
 }
DV[FAMS] = options.fattyacidfactor * DV[FAT] - DV[FASAT] - DV[FAPU];
if (analflag && options.abnuts[FAPU] == 0) efa_dynamics();
if (options.abnuts[FAPU] > 0)
 {
 DV[FAMS] = DV[FAMS] - options.abnuts[FAPU] + DV[FAPU];
 DV[FAPU] = options.abnuts[FAPU];
 if (options.abnuts[OMEGA6] > 0) DV[OMEGA6] = options.abnuts[OMEGA6];
 if (options.abnuts[LA] > 0) DV[LA] = options.abnuts[LA];
 if (options.abnuts[AA] > 0) DV[AA] = options.abnuts[AA];
 if (options.abnuts[OMEGA3] > 0) DV[OMEGA3] = options.abnuts[OMEGA3];
 if (options.abnuts[ALA] > 0) DV[ALA] = options.abnuts[ALA];
 if (options.abnuts[EPA] > 0) DV[EPA] = options.abnuts[EPA];
 if (options.abnuts[DHA] > 0) DV[DHA] = options.abnuts[DHA];
 }
if (options.abnuts[FAPU] == 0)
 {
 if (options.abnuts[OMEGA6] != 0) DV[OMEGA6] = options.abnuts[OMEGA6];
 if (options.abnuts[LA] != 0) DV[LA] = options.abnuts[LA];
 if (options.abnuts[AA] != 0) DV[AA] = options.abnuts[AA];
 if (options.abnuts[OMEGA3] != 0) DV[OMEGA3] = options.abnuts[OMEGA3];
 if (options.abnuts[ALA] != 0) DV[ALA] = options.abnuts[ALA];
 if (options.abnuts[EPA] != 0) DV[EPA] = options.abnuts[EPA];
 if (options.abnuts[DHA] != 0) DV[DHA] = options.abnuts[DHA];
 temp = DV[OMEGA6] + DV[OMEGA3] - DV[FAPU];
 if (analflag) temp += food_work.nutrient[FAPU] - food_work.nutrient[OMEGA6] - food_work.nutrient[OMEGA3];
 DV[FAPU] += temp;
 DV[FAMS] -= temp;
 }
if (options.pcsatfat > 0)
 {
 temp = DV[FASAT] + DV[FAMS];
 DV[FASAT] = DVBase[FASAT] * buf * options.pcsatfat * .1;
 DV[FAMS] = temp - DV[FASAT];
 }
if (options.abnuts[CHOCDF] != 0 && options.abnuts[PROCNT] > 0 && options.abnuts[FAT] > 0) DV[FAT] = options.abnuts[FAT];
if (options.abnuts[CHOCDF] != 0 && options.abnuts[PROCNT] > 0 && options.abnuts[FAMS] > 0) DV[FAMS] = options.abnuts[FAMS];
if (options.abnuts[CHOLE] > 0) DV[CHOLE] = options.abnuts[CHOLE]; 
if (options.abnuts[VITA_IU] > 0) DV[VITA_IU] = options.abnuts[VITA_IU]; 
if (options.abnuts[THIA] > 0) DV[THIA] = options.abnuts[THIA]; 
if (options.abnuts[RIBF] > 0) DV[RIBF] = options.abnuts[RIBF]; 
if (options.abnuts[NIA] > 0) DV[NIA] = options.abnuts[NIA]; 
if (options.abnuts[PANTAC] > 0) DV[PANTAC] = options.abnuts[PANTAC]; 
if (options.abnuts[VITB6A] > 0) DV[VITB6A] = options.abnuts[VITB6A]; 
if (options.abnuts[FOL] > 0) DV[FOL] = options.abnuts[FOL]; 
if (options.abnuts[VITB12] > 0) DV[VITB12] = options.abnuts[VITB12]; 
if (options.abnuts[VITC] > 0) DV[VITC] = options.abnuts[VITC]; 
if (options.abnuts[VITD] > 0) DV[VITD] = options.abnuts[VITD]; 
if (options.abnuts[VITE] > 0) DV[VITE] = options.abnuts[VITE]; 
if (options.abnuts[VITK] > 0) DV[VITK] = options.abnuts[VITK]; 
if (options.abnuts[CA] > 0) DV[CA] = options.abnuts[CA]; 
if (options.abnuts[CU] > 0) DV[CU] = options.abnuts[CU]; 
if (options.abnuts[FE] > 0) DV[FE] = options.abnuts[FE]; 
if (options.abnuts[MG] > 0) DV[MG] = options.abnuts[MG]; 
if (options.abnuts[MN] > 0) DV[MN] = options.abnuts[MN]; 
if (options.abnuts[P] > 0) DV[P] = options.abnuts[P]; 
if (options.abnuts[K] > 0) DV[K] = options.abnuts[K]; 
if (options.abnuts[SE] > 0) DV[SE] = options.abnuts[SE]; 
if (options.abnuts[NA] > 0) DV[NA] = options.abnuts[NA]; 
if (options.abnuts[ZN] > 0) DV[ZN] = options.abnuts[ZN]; 
if (! options.abnuts[CHO_NONFIB]) DV[CHO_NONFIB] = DV[CHOCDF] - DV[FIBTG] < 0 ? 0 : DV[CHOCDF] - DV[FIBTG];
else DV[CHO_NONFIB] = options.abnuts[CHO_NONFIB];
if ( options.pcsatfat > 0 || options.pcprotein > 0 || options.pccarb > 0 || options.abnuts[PROCNT] > 0 || options.abnuts[CHOCDF] != 0 || options.abnuts[FIBTG] > 0 || options.abnuts[VITE]) options.custom = 1;
else options.custom = 0;
write_OPTIONS();
}

void auto_del()
{
char buf[128];
int junk;
header("NUT:  Automatic Deletion of Meals");
spacer(0);
printf("\nDo you want to have meals deleted automatically from database?  (y/n):  ");
junk = get_char();
if (junk != 'Y' && junk != 'y' && junk != 'N' && junk != 'n') return;
if (junk != 'Y' && junk != 'y')
 {
 header("NUT:  Automatic Deletion of Meals");
 spacer(0);
 options.delopt = -1;
 write_OPTIONS();
 printf("\nMeals will not be deleted automatically.  Press <enter> to continue...");
 junk = get_int();
 return;
 }
junk = 0;
header("NUT:  Automatic Deletion of Meals");
spacer(0);
printf("\nHow many meals should be kept in database?  "); 
while (junk < 1)
 {
 get_string(buf,127);
 junk = atoi(buf);
 if (junk > 0)
  {
  options.delopt = junk;
  write_OPTIONS();
  delete_meals(options.delopt);
  write_meal_db();
  header("NUT:  Automatic Deletion of Meals");
  spacer(0);
  if (junk == 1) printf("\n%d meal will be kept in database.  Press <enter> to continue...",junk); 
  else printf("\n%d meals will be kept in database.  Press <enter> to continue...",junk); 
  junk = get_int();
  return;
  }
 header("NUT:  Automatic Deletion of Meals");
 spacer(0);
 printf("\nMust keep at least one meal in database.  How many?  ");
 }
}

void new_nut_levels()
{
int junk;
printf("\n      New Nutrient Levels:    %-15s %6.1f %-s\n",Nutrient[ENERC_KCAL],DV[ENERC_KCAL],Unit[ENERC_KCAL]);
printf("                              %-15s %6.1f %-s\n",Nutrient[FAT],DV[FAT],Unit[FAT]);
printf("                              %-15s %6.1f %-s\n",Nutrient[FASAT],DV[FASAT],Unit[FASAT]);
printf("                              %-15s %6.1f %-s\n",Nutrient[FAMS],DV[FAMS],Unit[FAMS]);
printf("                              %-15s %6.1f %-s\n",Nutrient[FAPU],DV[FAPU],Unit[FAPU]);
printf("                              %-15s %6.1f %-s\n",Nutrient[OMEGA6],DV[OMEGA6],Unit[OMEGA6]);
printf("                              %-15s %6.1f %-s\n",Nutrient[LA],DV[LA],Unit[LA]);
printf("                              %-15s %6.1f %-s\n",Nutrient[AA],DV[AA],Unit[AA]);
printf("                              %-15s %6.1f %-s\n",Nutrient[OMEGA3],DV[OMEGA3],Unit[OMEGA3]);
printf("                              %-15s %6.1f %-s\n",Nutrient[ALA],DV[ALA],Unit[ALA]);
printf("                              %-15s %6.1f %-s\n",Nutrient[EPA],DV[EPA],Unit[EPA]);
printf("                              %-15s %6.1f %-s\n",Nutrient[DHA],DV[DHA],Unit[DHA]);
printf("                              %-15s %6.1f %-s\n",Nutrient[CHOCDF],DV[CHOCDF],Unit[CHOCDF]);
printf("                              %-15s %6.1f %-s\n",Nutrient[FIBTG],DV[FIBTG],Unit[FIBTG]);
printf("                              %-15s %6.1f %-s\n",Nutrient[CHO_NONFIB],DV[CHO_NONFIB],Unit[CHO_NONFIB]);
printf("                              %-15s %6.1f %-s\n",Nutrient[PROCNT],DV[PROCNT],Unit[PROCNT]);
spacer(17);
printf("\nPress <enter> to continue...");
junk = get_int();
}

void restore_defaults(int removelock)
{
int i;
if ( removelock ) for ( i = 0; i < NUTRIENT_COUNT ; i++ ) options.locknuts[i] = 0;
if ( !options.locknuts[PROCNT] ) options.pcprotein = 0;
if ( !options.locknuts[CHOCDF] ) options.pccarb = 0;
if ( !options.locknuts[FASAT] ) options.pcsatfat = 0;
if ( removelock )
 {
 options.autocal = 0;
 options.n6hufa = 0;
 options.epadhamax = 0;
 }
for ( i = 0; i < NUTRIENT_COUNT ; i++ ) if ( !options.locknuts[i] ) options.abnuts[i] = 0;
if ( !options.locknuts[ENERC_KCAL] ) auto_cal(&DVBase[ENERC_KCAL],removelock ? 0 : 1);
else auto_cal(&options.abnuts[ENERC_KCAL],removelock ? 0 : 1);
}

void efa_dynamics()
{
int i;
float temp, temp1, temp_la = 0, temp_aa = 0, hufapct, p3, h3, h6, p6, o;
float kludgefactor = .9;
float target = DefaultTarget + kludgefactor, epadhalimit, extra_ala;

if (options.n6hufa != 0)
 {
 if (options.n6hufa < 15) options.n6hufa = 15;
 if (options.n6hufa > 90) options.n6hufa = 90;
 }
  
if (options.epadhamax <= 0) epadhalimit = DefaultEPADHA;
else epadhalimit = options.epadhamax;

options.abnuts[ALA] = food_work.nutrient[ALA];
if (options.abnuts[ALA] < DV[ALA]) options.abnuts[ALA] = DV[ALA];
extra_ala = options.abnuts[ALA] - food_work.nutrient[ALA];

if ( (DefaultTarget == 90 && options.n6hufa == 0) || options.n6hufa == 90 || options.n6hufa == 15)
 {
 options.abnuts[OMEGA6] = food_work.nutrient[OMEGA6];
 if (options.abnuts[OMEGA6] <= 0) options.abnuts[OMEGA6] = .00001;
 options.abnuts[LA] = food_work.nutrient[LA];
 if (options.abnuts[LA] <= 0) options.abnuts[LA] = .00001;
 options.abnuts[AA] = food_work.nutrient[AA];
 if (options.abnuts[AA] <= 0) options.abnuts[AA] = .00001;
 options.abnuts[OMEGA3] = food_work.nutrient[OMEGA3] + extra_ala;
 if (options.abnuts[OMEGA3] <= 0) options.abnuts[OMEGA3] = .00001;
 options.abnuts[EPA] = food_work.nutrient[EPA];
 if (options.abnuts[EPA] <= 0) options.abnuts[EPA] = .00001;
 options.abnuts[DHA] = food_work.nutrient[DHA];
 if (options.abnuts[DHA] <= 0) options.abnuts[DHA] = .00001;
 if (options.abnuts[EPA] + options.abnuts[DHA] < DefaultEPADHA && (food_work.nutrient[EPA] > .01 || food_work.nutrient[DHA] > .01 || food_work.nutrient[AA] > .01))
  {
  temp1 = epadhalimit - (options.abnuts[EPA] + options.abnuts[DHA]);
  temp = epadhalimit / (options.abnuts[EPA] + options.abnuts[DHA]);
  options.abnuts[EPA] *= temp;
  options.abnuts[DHA] *= temp;
  options.abnuts[OMEGA3] += temp1;
  }
 return;
 }

if (options.n6hufa != 0) target = options.n6hufa + kludgefactor;

options.abnuts[AA] = food_work.nutrient[AA];
options.abnuts[LA] = food_work.nutrient[LA];
extra_ala = options.abnuts[ALA] - food_work.nutrient[ALA];
p3 = 902 * (food_work.nutrient[SHORT3] + extra_ala) / food_work.nutrient[ENERC_KCAL];
p6 = 902 * food_work.nutrient[SHORT6] / food_work.nutrient[ENERC_KCAL];
h6 = 902 * food_work.nutrient[LONG6] / food_work.nutrient[ENERC_KCAL];
o  = 902 * food_work.nutrient[FAT] * options.fattyacidfactor / food_work.nutrient[ENERC_KCAL] - food_work.nutrient[SHORT6] - food_work.nutrient[LONG6] - food_work.nutrient[SHORT3] - food_work.nutrient[LONG3];

if (food_work.nutrient[EPA] > .01 || food_work.nutrient[DHA] > .01 || food_work.nutrient[AA] > .01) for (i = 1; i < 200000; i++)
 {
 temp = i * .001;
 h3 = 902 * temp / food_work.nutrient[ENERC_KCAL];
 hufapct = n6hufa(p3,p6,h3,h6,o,food_work.nutrient[ENERC_KCAL]);

 if (hufapct > target) continue;

 options.abnuts[OMEGA6] = food_work.nutrient[SHORT6] + food_work.nutrient[LONG6];
 options.abnuts[OMEGA3] = food_work.nutrient[SHORT3] + extra_ala + temp;
 temp -= food_work.nutrient[LONG3] - food_work.nutrient[EPA] - food_work.nutrient[DHA];
 if (food_work.nutrient[EPA] > 0 || food_work.nutrient[DHA] > 0)
  {
  temp = temp / (food_work.nutrient[EPA] + food_work.nutrient[DHA]);
  options.abnuts[EPA] = food_work.nutrient[EPA] * temp;
  options.abnuts[DHA] = food_work.nutrient[DHA] * temp;
  }
 if (food_work.nutrient[EPA] <= 0 && food_work.nutrient[DHA] <= 0)
  {
  options.abnuts[EPA] = temp / 2;
  options.abnuts[DHA] = temp / 2;
  }
 if (options.abnuts[EPA] + options.abnuts[DHA] > epadhalimit)
  {
  temp = options.abnuts[EPA] + options.abnuts[DHA] - epadhalimit;
  temp1 = options.abnuts[EPA] / (options.abnuts[EPA] + options.abnuts[DHA]);
  options.abnuts[EPA] -= temp * temp1;
  options.abnuts[DHA] -= temp * (1 - temp1);
  options.abnuts[OMEGA3] -= temp;
  h3 -= 902 * temp / food_work.nutrient[ENERC_KCAL];
  for (i = 1; i < 200000; i++)
   {
   temp_la = p6 * i * .00001;
   temp_aa = h6 * i * .00001;
   hufapct = n6hufa(p3,p6-temp_la,h3,h6-temp_aa,o,food_work.nutrient[ENERC_KCAL]);
   if (hufapct < target) break;
   }
  options.abnuts[LA] -= temp_la / 902 * food_work.nutrient[ENERC_KCAL];
  temp_aa = 902 / food_work.nutrient[ENERC_KCAL] * food_work.nutrient[AA] * (1 - (options.abnuts[LA] / food_work.nutrient[LA]));
  options.abnuts[AA] -= temp_aa / 902 * food_work.nutrient[ENERC_KCAL];
  options.abnuts[OMEGA6] -= (temp_la + temp_aa) / 902 * food_work.nutrient[ENERC_KCAL];
  }
 if (options.abnuts[OMEGA6] <= 0) options.abnuts[OMEGA6] = .01;
 if (options.abnuts[LA] <= 0) options.abnuts[LA] = .01;
 if (options.abnuts[AA] <= 0) options.abnuts[AA] = .01;
 if (options.abnuts[OMEGA3] <= 0) options.abnuts[OMEGA3] = .01;
 if (options.abnuts[EPA] <= 0) options.abnuts[EPA] = .01;
 if (options.abnuts[DHA] <= 0) options.abnuts[DHA] = .01;
 return;
 }
else
 {
 h3 = 902 * food_work.nutrient[LONG3] / food_work.nutrient[ENERC_KCAL];
 for (i = 1; i < 200000; i++)
  {
  temp = i * .001;

  p3 = 902 * temp / food_work.nutrient[ENERC_KCAL];
  o  = 902 * (food_work.nutrient[FASAT] + food_work.nutrient[FAMS]) / food_work.nutrient[ENERC_KCAL];
  hufapct = n6hufa(p3,p6,h3,h6,o,food_work.nutrient[ENERC_KCAL]);

  if (hufapct > target) continue;
  options.abnuts[OMEGA6] = food_work.nutrient[SHORT6] + food_work.nutrient[LONG6];
  options.abnuts[OMEGA3] = food_work.nutrient[LONG3] + temp;
  temp -= food_work.nutrient[SHORT3] - food_work.nutrient[ALA];
  options.abnuts[ALA] = temp;
  options.abnuts[EPA] = .01;
  options.abnuts[DHA] = .01;
  if (options.abnuts[OMEGA6] <= 0) options.abnuts[OMEGA6] = .01;
  if (options.abnuts[LA] <= 0) options.abnuts[LA] = .01;
  if (options.abnuts[AA] <= 0) options.abnuts[AA] = .01;
  if (options.abnuts[OMEGA3] <= 0) options.abnuts[OMEGA3] = .01;
  if (options.abnuts[ALA] <= 0) options.abnuts[ALA] = .01;
  if (options.abnuts[EPA] <= 0) options.abnuts[EPA] = .01;
  if (options.abnuts[DHA] <= 0) options.abnuts[DHA] = .01;
  return;
  }
 }
}

void weight_log()
{
char newentry[128];
int n, nfat, up, justmadenewentry = 0;
float weightslope, fatslope;
float weight, bf, stardate;
time_t t;

while (1)
 {
 header("NUT:  Weight Log Regression");
 read_WLOG(&n,&nfat,&up,&weightslope,&fatslope);
 if (justmadenewentry && options.autocal > 1 && nfat > 1)
  {
  if (up) options.abnuts[ENERC_KCAL] += 20;
  else options.abnuts[ENERC_KCAL] -= 20;
  auto_cal(&options.abnuts[ENERC_KCAL],0);
  }
 justmadenewentry = 0;
 printf("\n\n");
 printf("                           data points:   %6d\n",n);
 printf("\n\n");
 printf("  Based on the trend\n");
 printf("  of data points so far...\n\n\n");
 if (n < 2) printf("         Predicted daily weight change:        ?\n\n");
 else printf("         Predicted daily weight change:   %+10.3f\n\n",weightslope);
 if (nfat < 2)
  {
  printf("      Predicted daily lean mass change:        ?\n\n");
  printf("       Predicted daily fat mass change:        ?\n\n");
  }
 else
  {
  printf("      Predicted daily lean mass change:   %+10.3f\n\n",weightslope-fatslope);
  printf("       Predicted daily fat mass change:   %+10.3f\n\n",fatslope);
  }
 spacer(14);
 if (n > 0) printf("Type daily weight and body fat %% (! to clear log, <enter> to quit):  ");
 else printf("Type daily weight and body fat %% (<enter> to quit):  ");
 get_string(newentry,127);
 if (strlen(newentry) == 0 || newentry[0] == ' ') return;
 if (strchr(newentry,'!') != NULL)
  {
  weight = 0;
  if (n > 0) write_WLOG(&weight,&bf,&stardate);
  }
 else
  {
  time(&t);
  if (strchr(newentry,' ') == NULL || newentry[strlen(newentry)-1] == ' ')
   {
   weight   = atof(strtok(newentry," "));
   bf = 0;
   }
  else
   {
   weight   = atof(strtok(newentry," "));
   bf       = atof(strtok(NULL," "));
   justmadenewentry = 1;
   }
  stardate = ((float) t / 86400) - 14486;
  if (weight > 0) write_WLOG(&weight,&bf,&stardate);
  }
 }
}