/******************************************************************************
 *
 * 9998sketch/opts.h
 *
 * (c) Eugene K. Ressler, Jr. 2004, 2005
 *
 * A program related to the book
 *
 * Fundamentals of Virtual World Simulation
 * by Eugene K. Ressler, Jr.
 *
 * No warranty of correctness or capability of any kind is expressed or implied.
 *
 * The author grants free use of this code for any purpose as long any text,
 * executable program, library file, or source code distributed to others 
 * and employing any portion of this code clearly cites the book named above.
 *
 ******************************************************************************/

#ifndef __OPTS_H
#define __OPTS_H

#include <stdio.h>
#include "dynarray.h"
#include "error.h"

#define OPT_NONE				0x00

// type flags
#define OPT_INTERNAL		0x01
#define OPT_LINE				0x02
#define OPT_POLYGON			0x04

// default class flags
#define OPT_LINE_STYLE	0x08
#define OPT_FILL_STYLE	0x10
#define OPT_FILL_COLOR	0x20
#define OPT_DEFAULTS		(OPT_LINE_STYLE|OPT_FILL_STYLE|OPT_FILL_COLOR)

// key doesn't have type information; use val
#define OPT_TYPE_IN_VAL	0x40

// sufficient to emit value without key
#define OPT_EMIT_VAL		0x80

// some dynamic array types
typedef struct opt_t {
  char *key, *val;
} OPT;

typedef struct opt_list_t {
  DYNAMIC_ARRAY_FIELDS(OPT, elt, n_elts);
} OPT_LIST;

DECLARE_DYNAMIC_ARRAY_PROTOS(OPT_LIST, OPT, opt_list, elt, n_elts)

typedef struct opts_t {
  // DEBUG: could cache list data here for speed
  OPT_LIST list[1];
} OPTS;

void init_opts(OPTS *opts);
void setup_opts(OPTS *opts, char *opt_str, SRC_LINE line);
OPTS *new_opts(char *opts_str, SRC_LINE line);
void clear_opts(OPTS *opts);
char *opt_val(OPTS *opts, char *opt);
int bool_opt_p(OPTS *opts, char *opt, int default_p);
int opt_type(OPT *opt, int default_type, int lang);
void add_no_edges_default_opt(OPTS **opts_ptr, int lang);
void add_solid_white_default_opt(OPTS **opts_ptr, int lang);
// selective copy for splitting option lists by type
OPTS *copy_opts(OPTS *opts, int type_mask, int lang);
// selective copy for splitting out line options and modifying arrows
OPTS *copy_line_opts(OPTS *opts, int first_p, int last_p, int lang);
void emit_opts_raw(FILE *f, OPTS *opts, int lang);
void emit_opts(FILE *f, OPTS *opts, int lang);
void check_opts(OPTS *opts, 
								int allowed, char *allowed_msg,
								int lang, SRC_LINE line);

// slice src into dest using Perl/Python conventions
char *str_slice(char *dst, int dst_size, char *src, int beg, int end);
#define SLICE_TO_END  ((int)(~0u >> 1))

// find last occurance of aachar in set in src; return index or -1 if none
int str_last_occurance(char *src, char *set);

#endif
