aboutsummaryrefslogtreecommitdiff
path: root/fbwarn/src/parseBVG.c
blob: ead9c83f018b0ba03f19e6f60fa33b0f65e5bc34 (plain) (blame)
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
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "BVGTypes.h"
#include "raylib.h"
#include "extString.h"

void BVGDrawRectangle(BVGRectangle *rectangle) {
  if (rectangle->fill) {
    DrawRectangleRec(rectangle->rayrectangle, rectangle->color);
  } else {
    DrawRectangleLinesEx(rectangle->rayrectangle, rectangle->lineThickness, rectangle->color);
  }
  return;
}

// reads a given file
FILE *readFile(char *path) {
  FILE *fp = fopen (path, "r");
  if (fp == NULL) {
    perror ("Failed to open file!");
    exit(1);
  }
  return fp;
}

/*
  Converts a multiline function
  call into a single line call
*/
char *multiToSingle(char *lines) {
  // allocating the size of lines is safe since characters arent added
  char *result = malloc (strlen(lines)+1);
  char *line;
  while ((line = strsep(&lines, "\n")) != NULL)
    sprintf(result, "%s%s", result, line);

  free(line);
  return result; 
}

/*
  Converts a given char pointer to a corresponding boolean value
  expects the char to only contain true/false without any whitespace or similiar
  case is ignored
  If no match is found, true is returned by default
*/
bool parseBoolValue(char *value) {
  char *valueLower = strdup(strlwr(value));
  printf("Matching bool %s --- %s", valueLower, value);
  if (strcmp(valueLower, "false") == 0) {
    return false;
  } else {
    return true;
  }
}

BVGRectangle *BVGParseRectangle(char *argv[7]) {
  BVGRectangle *result = malloc(sizeof(BVGRectangle));
  Rectangle *rectangle = malloc(sizeof(Rectangle));
  argv[4][strlen(argv[4])-1] = '\0';
  argv[4] = argv[4]+strlen("color=#'");
  int x, y, width, height, r, g, b, a;
  float thickness = 1.0;
  Color *clr = malloc(sizeof(Color));
  sscanf(argv[0]+strlen("x="), "%d", &x);
  sscanf(argv[1]+strlen("y="), "%d", &y);
  sscanf(argv[2]+strlen("width="), "%d", &width);
  sscanf(argv[3]+strlen("height="), "%d", &height);
  if (strlen(argv[4]) == 6) {
    sscanf(argv[4], "%02x%02x%02x", &r, &g, &b);
    clr->r=r; clr->g=g; clr->b=b; clr->a=255;
  } else {
    sscanf(argv[4], "%02x%02x%02x%02x", &r, &g, &b, &a);
    clr->r=r; clr->g=g; clr->b=b; clr->a=a;
  }
  sscanf(argv[6]+strlen("thickness="), "%fd", &thickness);
  printf("X: %d, Y: %d\n", x, y);
  printf("Width: %d, Height: %d\n", width, height);
  printf("Color: %d, %d, %d\n", r, g, b);
  printf("Fill: %d, Thickness: %f\n", parseBoolValue(argv[5]+strlen("fill=")), thickness);

  rectangle->x=x; rectangle->y=y;
  rectangle->height=height; rectangle->width=width;
  result->rayrectangle=*rectangle;

  result->color=*clr;
  result->fill=parseBoolValue(argv[5]+strlen("fill="));
  result->lineThickness=thickness;
  
  return result;
}

void matchFunctionCall(char *call, void *ret) {
  printf("Matching %s\n", call);
  char *funcCall = strdup(call);
  char *funcName = strsep(&funcCall, "(");
  printf("Got function %s\n", funcName);
  if (strcmp(funcName, "rectangle ") == 0) {
      char **arg, *argv[7];
      call = call+strlen("rectangle (");
      for (arg = argv; (*arg = strsep(&call, ",")) != NULL;)
	if (**arg != '\0')
	  if (++arg >= &argv[7])
	    break;
      BVGRectangle *rect = malloc(sizeof(BVGRectangle)); 
      rect = BVGParseRectangle(argv);
      BVGDrawRectangle(rect);
  }
  return;
}