summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/util/options.cpp
blob: 65aac4a888379ec38c00f16a3c5bb3a697357116 (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
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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
// license:BSD-3-Clause
// copyright-holders:Aaron Giles
/***************************************************************************

    options.cpp

    Core options code code

***************************************************************************/

#include "options.h"

#include "corestr.h"

#include <locale>
#include <string>

#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdlib.h>


const int core_options::MAX_UNADORNED_OPTIONS;

//**************************************************************************
//  GLOBAL VARIABLES
//**************************************************************************

const char *const core_options::s_option_unadorned[MAX_UNADORNED_OPTIONS] =
{
	"<UNADORNED0>",
	"<UNADORNED1>",
	"<UNADORNED2>",
	"<UNADORNED3>",
	"<UNADORNED4>",
	"<UNADORNED5>",
	"<UNADORNED6>",
	"<UNADORNED7>",
	"<UNADORNED8>",
	"<UNADORNED9>",
	"<UNADORNED10>",
	"<UNADORNED11>",
	"<UNADORNED12>",
	"<UNADORNED13>",
	"<UNADORNED14>",
	"<UNADORNED15>"
};


//**************************************************************************
//  UTILITY
//**************************************************************************

namespace
{
	void trim_spaces_and_quotes(std::string &data)
	{
		// trim any whitespace
		strtrimspace(data);

		// trim quotes
		if (data.find_first_of('"') == 0 && data.find_last_of('"') == data.length() - 1)
		{
			data.erase(0, 1);
			data.erase(data.length() - 1, 1);
		}
	}
};

//**************************************************************************
//  OPTIONS EXCEPTION CLASS
//**************************************************************************

//-------------------------------------------------
//  options_exception - constructor
//-------------------------------------------------

options_exception::options_exception(std::string &&message)
	: m_message(std::move(message))
{
}


//-------------------------------------------------
//  options_warning_exception - constructor
//-------------------------------------------------

options_warning_exception::options_warning_exception(std::string &&message)
	: options_exception(std::move(message))
{
}


//-------------------------------------------------
//  options_error_exception - constructor
//-------------------------------------------------

options_error_exception::options_error_exception(std::string &&message)
	: options_exception(std::move(message))
{
}


//**************************************************************************
//  CORE OPTIONS ENTRY BASE CLASS
//**************************************************************************

//-------------------------------------------------
//  entry - constructor
//-------------------------------------------------

core_options::entry::entry(std::vector<std::string> &&names, core_options::option_type type, const char *description)
	: m_names(std::move(names))
	, m_priority(OPTION_PRIORITY_DEFAULT)
	, m_type(type)
	, m_description(description)
{
	assert(m_names.empty() == (m_type == option_type::HEADER));
}

core_options::entry::entry(std::string &&name, core_options::option_type type, const char *description)
	: entry(std::vector<std::string>({ std::move(name) }), type, description)
{
}


//-------------------------------------------------
//  entry - destructor
//-------------------------------------------------

core_options::entry::~entry()
{
}


//-------------------------------------------------
//  entry::value
//-------------------------------------------------

const char *core_options::entry::value() const noexcept
{
	// returning 'nullptr' from here signifies a value entry that is essentially "write only"
	// and cannot be meaningfully persisted (e.g. - a command or the software name)
	return nullptr;
}


//-------------------------------------------------
//  entry::set_value
//-------------------------------------------------

void core_options::entry::set_value(std::string &&newvalue, int priority_value, bool always_override)
{
	// it is invalid to set the value on a header
	assert(type() != option_type::HEADER);

	// only set the value if we have priority
	if (always_override || priority_value >= priority())
	{
		internal_set_value(std::move(newvalue));
		m_priority = priority_value;

		// invoke the value changed handler, if appropriate
		if (m_value_changed_handler)
			m_value_changed_handler(value());
	}
}


//-------------------------------------------------
//  entry::set_default_value
//-------------------------------------------------

void core_options::entry::set_default_value(std::string &&newvalue)
{
	// set_default_value() is not necessarily supported for all entry types
	throw false;
}


//-------------------------------------------------
//  entry::validate
//-------------------------------------------------

void core_options::entry::validate(const std::string &data)
{
	std::istringstream str(data);
	str.imbue(std::locale::classic());

	switch (type())
	{
	case option_type::BOOLEAN:
		{
			// booleans must be 0 or 1
			int ival;
			if (!(str >> ival) || (0 > ival) || (1 < ival))
				throw options_warning_exception("Illegal boolean value for %s: \"%s\"; reverting to %s\n", name(), data, value());
		}
		break;

	case option_type::INTEGER:
		{
			// integers must be integral
			int ival;
			if (!(str >> ival))
				throw options_warning_exception("Illegal integer value for %s: \"%s\"; reverting to %s\n", name(), data, value());

			// range checking
			char const *const strmin(minimum());
			char const *const strmax(maximum());
			int imin(0), imax(0);
			if (strmin)
			{
				str.str(strmin);
				str >> imin;
			}
			if (strmax)
			{
				str.str(strmax);
				str >> imax;
			}
			if ((strmin && (ival < imin)) || (strmax && (ival > imax)))
			{
				if (!strmax)
					throw options_warning_exception("Out-of-range integer value for %s: \"%s\" (must be no less than %d); reverting to %s\n", name(), data, imin, value());
				else if (!strmin)
					throw options_warning_exception("Out-of-range integer value for %s: \"%s\" (must be no greater than %d); reverting to %s\n", name(), data, imax, value());
				else
					throw options_warning_exception("Out-of-range integer value for %s: \"%s\" (must be between %d and %d, inclusive); reverting to %s\n", name(), data, imin, imax, value());
			}
		}
		break;

	case option_type::FLOAT:
		{
			float fval;
			if (!(str >> fval))
				throw options_warning_exception("Illegal float value for %s: \"%s\"; reverting to %s\n", name(), data, value());

			// range checking
			char const *const strmin(minimum());
			char const *const strmax(maximum());
			float fmin(0), fmax(0);
			if (strmin)
			{
				str.str(strmin);
				str >> fmin;
			}
			if (strmax)
			{
				str.str(strmax);
				str >> fmax;
			}
			if ((strmin && (fval < fmin)) || (strmax && (fval > fmax)))
			{
				if (!strmax)
					throw options_warning_exception("Out-of-range float value for %s: \"%s\" (must be no less than %f); reverting to %s\n", name(), data, fmin, value());
				else if (!strmin)
					throw options_warning_exception("Out-of-range float value for %s: \"%s\" (must be no greater than %f); reverting to %s\n", name(), data, fmax, value());
				else
					throw options_warning_exception("Out-of-range float value for %s: \"%s\" (must be between %f and %f, inclusive); reverting to %s\n", name(), data, fmin, fmax, value());
			}
		}
		break;

	case OPTION_STRING:
		// strings can be anything
		break;

	case OPTION_INVALID:
	case OPTION_HEADER:
	default:
		// anything else is invalid
		throw options_error_exception("Attempted to set invalid option %s\n", name());
	}
}


//-------------------------------------------------
//  entry::minimum
//-------------------------------------------------

const char *core_options::entry::minimum() const noexcept
{
	return nullptr;
}


//-------------------------------------------------
//  entry::maximum
//-------------------------------------------------

const char *core_options::entry::maximum() const noexcept
{
	return nullptr;
}


//-------------------------------------------------
//  entry::has_range
//-------------------------------------------------

bool core_options::entry::has_range() const noexcept
{
	return minimum() && maximum();
}


//-------------------------------------------------
//  entry::default_value
//-------------------------------------------------

const std::string &core_options::entry::default_value() const noexcept
{
	// I don't really want this generally available, but MewUI seems to need it.  Please do not use.
	abort();
}


//**************************************************************************
//  CORE OPTIONS SIMPLE ENTRYCLASS
//**************************************************************************

//-------------------------------------------------
//  simple_entry - constructor
//-------------------------------------------------

core_options::simple_entry::simple_entry(std::vector<std::string> &&names, const char *description, core_options::option_type type, std::string &&defdata, std::string &&minimum, std::string &&maximum)
	: entry(std::move(names), type, description)
	, m_defdata(std::move(defdata))
	, m_minimum(std::move(minimum))
	, m_maximum(std::move(maximum))
{
	m_data = m_defdata;
}


//-------------------------------------------------
//  simple_entry - destructor
//-------------------------------------------------

core_options::simple_entry::~simple_entry()
{
}


//-------------------------------------------------
//  simple_entry::value
//-------------------------------------------------

const char *core_options::simple_entry::value() const noexcept
{
	switch (type())
	{
	case core_options::option_type::BOOLEAN:
	case core_options::option_type::INTEGER:
	case core_options::option_type::FLOAT:
	case core_options::option_type::STRING:
		return m_data.c_str();

	default:
		// this is an option type for which returning a value is
		// a meaningless operation (e.g. - core_options::option_type::COMMAND)
		return nullptr;
	}
}


//-------------------------------------------------
//  simple_entry::default_value
//-------------------------------------------------

const std::string &core_options::simple_entry::default_value() const noexcept
{
	// only MewUI seems to need this; please don't use
	return m_defdata;
}


//-------------------------------------------------
//  internal_set_value
//-------------------------------------------------

void core_options::simple_entry::internal_set_value(std::string &&newvalue)
{
	m_data = std::move(newvalue);
}


//-------------------------------------------------
//  set_default_value
//-------------------------------------------------

void core_options::simple_entry::set_default_value(std::string &&newvalue)
{
	m_data = m_defdata = std::move(newvalue);
}


//-------------------------------------------------
//  minimum
//-------------------------------------------------

const char *core_options::simple_entry::minimum() const noexcept
{
	return m_minimum.c_str();
}


//-------------------------------------------------
//  maximum
//-------------------------------------------------

const char *core_options::simple_entry::maximum() const noexcept
{
	return m_maximum.c_str();
}


//**************************************************************************
//  CORE OPTIONS
//**************************************************************************

//-------------------------------------------------
//  core_options - constructor
//-------------------------------------------------

core_options::core_options()
{
}


//-------------------------------------------------
//  ~core_options - destructor
//-------------------------------------------------

core_options::~core_options()
{
}


//-------------------------------------------------
//  add_entry - adds an entry
//-------------------------------------------------

void core_options::add_entry(entry::shared_ptr &&entry, const char *after_header)
{
	// update the entry map
	for (const std::string &name : entry->names())
	{
		// append the entry
		add_to_entry_map(std::string(name), entry);

		// for booleans, add the "-noXYZ" option as well
		if (entry->type() == option_type::BOOLEAN)
			add_to_entry_map(std::string("no") + name, entry);
	}

	// and add the entry to the vector
	m_entries.emplace_back(std::move(entry));
}


//-------------------------------------------------
//  add_to_entry_map - adds an entry to the entry
//  map
//-------------------------------------------------

void core_options::add_to_entry_map(std::string &&name, entry::shared_ptr &entry)
{
	// it is illegal to call this method for something that already exists
	assert(m_entrymap.find(name) == m_entrymap.end());

	// append the entry
	m_entrymap.emplace(std::make_pair(name, entry::weak_ptr(entry)));
}


//-------------------------------------------------
//  add_entry - adds an entry based on an
//  options_entry
//-------------------------------------------------

void core_options::add_entry(const options_entry &opt, bool override_existing)
{
	std::vector<std::string> names;
	std::string minimum, maximum;

	// copy in the name(s) as appropriate
	if (opt.name)
	{
		// first extract any range
		std::string namestr(opt.name);
		int lparen = namestr.find_first_of('(', 0);
		int dash = namestr.find_first_of('-', lparen + 1);
		int rparen = namestr.find_first_of(')', dash + 1);
		if (lparen != -1 && dash != -1 && rparen != -1)
		{
			strtrimspace(minimum.assign(namestr.substr(lparen + 1, dash - (lparen + 1))));
			strtrimspace(maximum.assign(namestr.substr(dash + 1, rparen - (dash + 1))));
			namestr.erase(lparen, rparen + 1 - lparen);
		}

		// then chop up any semicolon-separated names
		size_t semi;
		while ((semi = namestr.find_first_of(';')) != std::string::npos)
		{
			names.push_back(namestr.substr(0, semi));
			namestr.erase(0, semi + 1);
		}

		// finally add the last item
		names.push_back(std::move(namestr));
	}

	// we might be called with an existing entry
	entry::shared_ptr existing_entry;
	do
	{
		for (const std::string &name : names)
		{
			existing_entry = get_entry(name.c_str());
			if (existing_entry)
				break;
		}

		if (existing_entry)
		{
			if (override_existing)
				remove_entry(*existing_entry);
			else
				return;
		}
	} while (existing_entry);

	// set the default value
	std::string defdata = opt.defvalue ? opt.defvalue : "";

	// create and add the entry
	add_entry(
			std::move(names),
			opt.description,
			opt.type,
			std::move(defdata),
			std::move(minimum),
			std::move(maximum));
}


//-------------------------------------------------
//  add_entry
//-------------------------------------------------

void core_options::add_entry(std::vector<std::string> &&names, const char *description, option_type type, std::string &&default_value, std::string &&minimum, std::string &&maximum)
{
	// create the entry
	entry::shared_ptr new_entry = std::make_shared<simple_entry>(
			std::move(names),
			description,
			type,
			std::move(default_value),
			std::move(minimum),
			std::move(maximum));

	// and add it
	add_entry(std::move(new_entry));
}


//-------------------------------------------------
//  add_header
//-------------------------------------------------

void core_options::add_header(const char *description)
{
	add_entry(std::vector<std::string>(), description, option_type::HEADER);
}


//-------------------------------------------------
//  add_entries - add entries to the current
//  options sets
//-------------------------------------------------

void core_options::add_entries(const options_entry *entrylist, bool override_existing)
{
	// loop over entries until we hit a nullptr name
	for (int i = 0; entrylist[i].name || entrylist[i].type == option_type::HEADER; i++)
		add_entry(entrylist[i], override_existing);
}


//-------------------------------------------------
//  set_default_value - change the default value
//  of an option
//-------------------------------------------------

void core_options::set_default_value(const char *name, const char *defvalue)
{
	// update the data and default data
	get_entry(name)->set_default_value(defvalue);
}


//-------------------------------------------------
//  set_description - change the description
//  of an option
//-------------------------------------------------

void core_options::set_description(const char *name, const char *description)
{
	// update the data and default data
	get_entry(name)->set_description(description);
}


//-------------------------------------------------
//  parse_command_line - parse a series of
//  command line arguments
//-------------------------------------------------

void core_options::parse_command_line(const std::vector<std::string> &args, int priority, bool ignore_unknown_options)
{
	std::ostringstream error_stream;
	condition_type condition = condition_type::NONE;

	// reset the errors and the command
	m_command.clear();

	// we want to identify commands first
	for (size_t arg = 1; arg < args.size(); arg++)
	{
		if (!args[arg].empty() && args[arg][0] == '-')
		{
			auto curentry = get_entry(&args[arg][1]);
			if (curentry && curentry->type() == OPTION_COMMAND)
			{
				// can only have one command
				if (!m_command.empty())
					throw options_error_exception("Error: multiple commands specified -%s and %s\n", m_command, args[arg]);

				m_command = curentry->name();
			}
		}
	}

	// iterate through arguments
	int unadorned_index = 0;
	for (size_t arg = 1; arg < args.size(); arg++)
	{
		// determine the entry name to search for
		const char *curarg = args[arg].c_str();
		bool is_unadorned = (curarg[0] != '-');
		const char *optionname = is_unadorned ? core_options::unadorned(unadorned_index++) : &curarg[1];

		// special case - collect unadorned arguments after commands into a special place
		if (is_unadorned && !m_command.empty())
		{
			m_command_arguments.push_back(std::move(args[arg]));
			command_argument_processed();
			continue;
		}

		// find our entry; if not found, continue
		auto curentry = get_entry(optionname);
		if (!curentry)
		{
			if (!ignore_unknown_options)
				throw options_error_exception("Error: unknown option: -%s\n", optionname);
			continue;
		}

		// at this point, we've already processed commands
		if (curentry->type() == OPTION_COMMAND)
			continue;

		// get the data for this argument, special casing booleans
		std::string newdata;
		if (curentry->type() == option_type::BOOLEAN)
		{
			newdata = (strncmp(&curarg[1], "no", 2) == 0) ? "0" : "1";
		}
		else if (is_unadorned)
		{
			newdata = curarg;
		}
		else if (arg + 1 < args.size())
		{
			newdata = args[++arg];
		}
		else
		{
			throw options_error_exception("Error: option %s expected a parameter\n", curarg);
		}

		// set the new data
		do_set_value(*curentry, std::move(newdata), priority, error_stream, condition);
	}

	// did we have any errors that may need to be aggregated?
	throw_options_exception_if_appropriate(condition, error_stream);
}


//-------------------------------------------------
//  parse_ini_file - parse a series of entries in
//  an INI file
//-------------------------------------------------

void core_options::parse_ini_file(util::core_file &inifile, int priority, bool ignore_unknown_options, bool always_override)
{
	std::ostringstream error_stream;
	condition_type condition = condition_type::NONE;

	// loop over lines in the file
	char buffer[4096];
	while (inifile.gets(buffer, ARRAY_LENGTH(buffer)) != nullptr)
	{
		// find the extent of the name
		char *optionname;
		for (optionname = buffer; *optionname != 0; optionname++)
			if (!isspace((uint8_t)*optionname))
				break;

		// skip comments
		if (*optionname == 0 || *optionname == '#')
			continue;

		// scan forward to find the first space
		char *temp;
		for (temp = optionname; *temp != 0; temp++)
			if (isspace((uint8_t)*temp))
				break;

		// if we hit the end early, print a warning and continue
		if (*temp == 0)
		{
			condition = std::max(condition, condition_type::WARN);
			util::stream_format(error_stream, "Warning: invalid line in INI: %s", buffer);
			continue;
		}

		// NULL-terminate
		*temp++ = 0;
		char *optiondata = temp;

		// scan the data, stopping when we hit a comment
		bool inquotes = false;
		for (temp = optiondata; *temp != 0; temp++)
		{
			if (*temp == '"')
				inquotes = !inquotes;
			if (*temp == '#' && !inquotes)
				break;
		}
		*temp = 0;

		// find our entry
		entry::shared_ptr curentry = get_entry(optionname);
		if (!curentry)
		{
			if (!ignore_unknown_options)
			{
				condition = std::max(condition, condition_type::WARN);
				util::stream_format(error_stream, "Warning: unknown option in INI: %s\n", optionname);
			}
			continue;
		}

		// set the new data
		std::string data = optiondata;
		trim_spaces_and_quotes(data);
		do_set_value(*curentry, std::move(data), priority, error_stream, condition);
	}

	// did we have any errors that may need to be aggregated?
	throw_options_exception_if_appropriate(condition, error_stream);
}


//-------------------------------------------------
//  throw_options_exception_if_appropriate
//-------------------------------------------------

void core_options::throw_options_exception_if_appropriate(core_options::condition_type condition, std::ostringstream &error_stream)
{
	switch(condition)
	{
	case condition_type::NONE:
		// do nothing
		break;

	case condition_type::WARN:
		throw options_warning_exception(error_stream.str());

	case condition_type::ERR:
		throw options_error_exception(error_stream.str());

	default:
		// should not get here
		throw false;
	}
}


//-------------------------------------------------
//  copy_from
//-------------------------------------------------

void core_options::copy_from(const core_options &that)
{
	for (auto &dest_entry : m_entries)
	{
		if (dest_entry->names().size() > 0)
		{
			// identify the source entry
			const entry::shared_const_ptr source_entry = that.get_entry(dest_entry->name());
			if (source_entry)
			{
				const char *value = source_entry->value();
				if (value)
					dest_entry->set_value(value, source_entry->priority(), true);
			}
		}
	}
}


//-------------------------------------------------
//  output_ini - output the options in INI format,
//  only outputting entries that different from
//  the optional diff
//-------------------------------------------------

std::string core_options::output_ini(const core_options *diff) const
{
	// INI files are complete, so always start with a blank buffer
	std::ostringstream buffer;
	buffer.imbue(std::locale::classic());

	int num_valid_headers = 0;
	int unadorned_index = 0;
	const char *last_header = nullptr;
	std::string overridden_value;

	// loop over all items
	for (auto &curentry : m_entries)
	{
		if (curentry->type() == option_type::HEADER)
		{
			// header: record description
			last_header = curentry->description();
		}
		else
		{
			const std::string &name(curentry->name());
			const char *value(curentry->value());

			// check if it's unadorned
			bool is_unadorned = false;
			if (name == core_options::unadorned(unadorned_index))
			{
				unadorned_index++;
				is_unadorned = true;
			}

			// output entries for all non-command items (items with value)
			if (value)
			{
				// look up counterpart in diff, if diff is specified
				if (!diff || strcmp(value, diff->value(name.c_str())))
				{
					// output header, if we have one
					if (last_header)
					{
						if (num_valid_headers++)
							buffer << '\n';
						util::stream_format(buffer, "#\n# %s\n#\n", last_header);
						last_header = nullptr;
					}

					// and finally output the data, skip if unadorned
					if (!is_unadorned)
					{
						if (strchr(value, ' '))
							util::stream_format(buffer, "%-25s \"%s\"\n", name, value);
						else
							util::stream_format(buffer, "%-25s %s\n", name, value);
					}
				}
			}
		}
	}
	return buffer.str();
}


//-------------------------------------------------
//  output_help - output option help to a string
//-------------------------------------------------

std::string core_options::output_help() const
{
	// start empty
	std::ostringstream buffer;

	// loop over all items
	for (auto &curentry : m_entries)
	{
		// header: just print
		if (curentry->type() == option_type::HEADER)
			util::stream_format(buffer, "\n#\n# %s\n#\n", curentry->description());

		// otherwise, output entries for all non-deprecated items
		else if (curentry->description() != nullptr)
			util::stream_format(buffer, "-%-20s%s\n", curentry->name(), curentry->description());
	}
	return buffer.str();
}


//-------------------------------------------------
//  value - return the raw option value
//-------------------------------------------------

const char *core_options::value(const char *option) const noexcept
{
	auto const entry = get_entry(option);
	return entry ? entry->value() : nullptr;
}


//-------------------------------------------------
//  description - return description of option
//-------------------------------------------------

const char *core_options::description(const char *option) const noexcept
{
	auto const entry = get_entry(option);
	return entry ? entry->description() : nullptr;
}


//-------------------------------------------------
//  value - return the option value as an integer
//-------------------------------------------------

int core_options::int_value(const char *option) const
{
	char const *const data = value(option);
	if (!data)
		return 0;
	std::istringstream str(data);
	str.imbue(std::locale::classic());
	int ival;
	if (str >> ival)
		return ival;
	else
		return 0;
}


//-------------------------------------------------
//  value - return the option value as a float
//-------------------------------------------------

float core_options::float_value(const char *option) const
{
	char const *const data = value(option);
	if (!data)
		return 0.0f;
	std::istringstream str(data);
	str.imbue(std::locale::classic());
	float fval;
	if (str >> fval)
		return fval;
	else
		return 0.0f;
}


//**************************************************************************
//  LEGACY
//**************************************************************************

//-------------------------------------------------
//  set_value - set the raw option value
//-------------------------------------------------

void core_options::set_value(const std::string &name, const std::string &value, int priority)
{
	set_value(name, std::string(value), priority);
}

void core_options::set_value(const std::string &name, std::string &&value, int priority)
{
	get_entry(name)->set_value(std::move(value), priority);
}

void core_options::set_value(const std::string &name, int value, int priority)
{
	set_value(name, string_format("%d", value), priority);
}

void core_options::set_value(const std::string &name, float value, int priority)
{
	set_value(name, string_format("%f", value), priority);
}


//-------------------------------------------------
//  remove_entry - remove an entry from our list
//  and map
//-------------------------------------------------

void core_options::remove_entry(core_options::entry &delentry)
{
	// find this in m_entries
	auto iter = std::find_if(
			m_entries.begin(),
			m_entries.end(),
			[&delentry](const auto &x) { return &*x == &delentry; });
	assert(iter != m_entries.end());

	// erase each of the items out of the entry map
	for (const std::string &name : delentry.names())
		m_entrymap.erase(name);

	// finally erase it
	m_entries.erase(iter);
}


//-------------------------------------------------
//  do_set_value
//-------------------------------------------------

void core_options::do_set_value(entry &curentry, std::string &&data, int priority, std::ostream &error_stream, condition_type &condition)
{
	// this is called when parsing a command line or an INI - we want to catch the option_exception and write
	// any exception messages to the error stream
	try
	{
		curentry.set_value(std::move(data), priority);
	}
	catch (options_warning_exception &ex)
	{
		// we want to aggregate option exceptions
		error_stream << ex.message();
		condition = std::max(condition, condition_type::WARN);
	}
	catch (options_error_exception &ex)
	{
		// we want to aggregate option exceptions
		error_stream << ex.message();
		condition = std::max(condition, condition_type::ERR);
	}
}


//-------------------------------------------------
//  get_entry
//-------------------------------------------------

core_options::entry::shared_const_ptr core_options::get_entry(const std::string &name) const noexcept
{
	auto curentry = m_entrymap.find(name);
	return (curentry != m_entrymap.end()) ? curentry->second.lock() : nullptr;
}

core_options::entry::shared_ptr core_options::get_entry(const std::string &name) noexcept
{
	auto curentry = m_entrymap.find(name);
	return (curentry != m_entrymap.end()) ? curentry->second.lock() : nullptr;
}


//-------------------------------------------------
//  set_value_changed_handler
//-------------------------------------------------

void core_options::set_value_changed_handler(const std::string &name, std::function<void(const char *)> &&handler)
{
	get_entry(name)->set_value_changed_handler(std::move(handler));
}


//-------------------------------------------------
//  header_exists
//-------------------------------------------------

bool core_options::header_exists(const char *description) const noexcept
{
	auto iter = std::find_if(
			m_entries.begin(),
			m_entries.end(),
			[description](const auto &entry)
			{
				return entry->type() == option_type::HEADER
						&& entry->description()
						&& !strcmp(entry->description(), description);
			});

	return iter != m_entries.end();
}

//-------------------------------------------------
//  revert - revert options at or below a certain
//  priority back to their defaults
//-------------------------------------------------

void core_options::revert(int priority_hi, int priority_lo)
{
	for (entry::shared_ptr &curentry : m_entries)
		if (curentry->type() != option_type::HEADER)
			curentry->revert(priority_hi, priority_lo);
}

//-------------------------------------------------
//  revert - revert back to our default if we are
//  within the given priority range
//-------------------------------------------------

void core_options::simple_entry::revert(int priority_hi, int priority_lo)
{
	// if our priority is within the range, revert to the default
	if (priority() <= priority_hi && priority() >= priority_lo)
	{
		set_value(std::string(default_value()), priority(), true);
		set_priority(OPTION_PRIORITY_DEFAULT);
	}
}