mightymandel v16

GPU-based Mandelbrot set explorer

parse_mm.c
Go to the documentation of this file.
1 // mightymandel -- GPU-based Mandelbrot Set explorer
2 // Copyright (C) 2012,2013,2014,2015 Claude Heiland-Allen
3 // License GPL3+ http://www.gnu.org/licenses/gpl.html
4 
5 #include <assert.h>
6 #include <stdbool.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <mpfr.h>
11 
12 #include "parse.h"
13 #include "parse_mm.h"
14 
15 bool parse_mm(const char *source, int length, mpfr_t cx, mpfr_t cy, mpfr_t cz) {
16  (void) length;
17  assert(source);
18  char *source2 = strdup(source);
19  assert(source2); // strdup allocates memory which can fail
20  char *s = source2;
21  char *sx = 0;
22  char *sy = 0;
23  char *sz = 0;
24  while (s) {
25  char *line = parse_line(&s);
26  int len = strlen(line);
27  if (len > 2 && line[len - 2] == ' ') {
28  line[len - 2] = 0;
29  switch (line[len - 1]) {
30  case '+': sx = line; break;
31  case 'i': sy = line; break;
32  case '@': sz = line; break;
33  }
34  }
35  }
36  if (sx && sy && sz) {
37  mpfr_set_prec(cz, 53);
38  mpfr_set_str(cz, sz, 10, MPFR_RNDN);
39  if (! radius_is_valid(cz)) {
40  free(source2);
41  return false;
42  }
43  mpfr_prec_t p = precision_for_radius(cz);
44  mpfr_set_prec(cx, p);
45  mpfr_set_prec(cy, p);
46  mpfr_set_str(cx, sx, 10, MPFR_RNDN);
47  mpfr_set_str(cy, sy, 10, MPFR_RNDN);
48  free(source2);
49  return true;
50  } else {
51  free(source2);
52  return false;
53  }
54 }