summaryrefslogblamecommitdiffstats
path: root/src/eval.c
blob: aa9a236e619a8589a3ee01a83aadca10ca2f2e16 (plain) (tree)
1
2
3
4
5
6
7
8
9
10

                 







                                                                                    
                                   










                                                           
 



















































                                                                          


                   


                                       



















                                 



                      
                             
 


                       
                                                   
 
                  

                    
  
            

 
#include "eval.h"
#include "util.h"
#include <stdint.h>

extern int
sha1digest(uint8_t *digest, char *hexdigest, const uint8_t *data, size_t databytes);

void
create_hash(recipe * r)
{
  char data[FOOD_MAX_ARRAY] = "\0";
  
  for (int i = 0; i < r->in; i++) {
    strcat(data, r->i[i]->name);
    strcat(data, r->i[i]->qty);
  }
  if (r->sn) {
    for (int i = 0; i < r->sn; i++)
      strcat(data, r->s[i]->inst);
  }
  sha1digest(NULL, r->sha1, (uint8_t *)data, strlen(data));
}

/* TODO: Needs work to actually remove the extra characters only when they
   define a variable/time schedule */
char *
inst2txt(char * s)
{
  char txt[FOOD_MAX_ARRAY] = "\0";
  int l = 0;
  for (int i=0; i<strlen(s); i++) {
    l = strlen(txt);
    switch(s[i]) {
    case '$':
    case '[':
    case ']':
    case '{':
    case '}':
      continue;
    default:
      txt[l] = s[i];
      txt[l+1] = '\0';
    }    
  }
  return strdup(txt);
}

/* TODO: Still needs work for when the `[`s are not balanced */
char *
inst2duration(char * s)
{
  char duration[FOOD_MAX_ARRAY] = "\0";
  
  int flag = 0;
  int l = 0;

  for (int i=0; i<strlen(s); i++) {
    if (s[i] == ']')
      flag = flag - 1;
    if (flag > 0) {
      duration[l] = s[i];
      duration[l+1] = '\0';
      l = strlen(duration);
    }
    if (s[i] == '[')
      flag = flag + 1;
  }

  if (strlen(duration)) {
    return strdup(duration);
  }

  return NULL;
}

int
eval_step(step * s)
{
  s->txt = inst2txt(s->inst);
  s->duration = inst2duration(s->inst);
  //  s->result = strdup(s->inst);
  return 0;
}

int
resolve_steps(recipe * r)
{
  if (!r) return 1;

  int rc = 0;
  for (int i=0; i < r->sn; i++) {
    rc += eval_step(r->s[i]);
  }

  if (rc > 0) {
    return 1;
  }

  return 0;
}

recipe *
eval(recipe * r)
{
  if (!r) return NULL;
  recipe * _r = new_recipe();

  merge_items(_r, r);
  merge_steps(_r, r);
  copy_metadata(_r, r);
  copy_subrecipes(_r, r, 0 /* shallow copy off */);

  create_hash(_r);

  resolve_steps(_r);
  
  return _r;
}