[7746] | 1 | /*
|
---|
| 2 | * \file Options.c
|
---|
| 3 | * \brief: implementation of the Options class, derived from DataSet class
|
---|
| 4 | */
|
---|
| 5 |
|
---|
[12706] | 6 | /*Headers: {{{*/
|
---|
[7746] | 7 | #ifdef HAVE_CONFIG_H
|
---|
[9320] | 8 | #include <config.h>
|
---|
[7746] | 9 | #else
|
---|
| 10 | #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
|
---|
| 11 | #endif
|
---|
| 12 |
|
---|
| 13 | #include <vector>
|
---|
| 14 | #include <algorithm>
|
---|
[12330] | 15 | #include <cstring>
|
---|
[7746] | 16 |
|
---|
| 17 | #include "./DataSet.h"
|
---|
| 18 | #include "../shared/shared.h"
|
---|
[9761] | 19 | #include "../io/io.h"
|
---|
[7746] | 20 | #include "../include/include.h"
|
---|
[13395] | 21 | #include "../classes/classes.h"
|
---|
[7746] | 22 | #include "../shared/shared.h"
|
---|
| 23 | #include "../EnumDefinitions/EnumDefinitions.h"
|
---|
| 24 | #include "../io/io.h"
|
---|
| 25 | /*}}}*/
|
---|
| 26 |
|
---|
| 27 | /*Object constructors and destructor*/
|
---|
[12706] | 28 | /*FUNCTION Options::Options(){{{*/
|
---|
[7746] | 29 | Options::Options(){
|
---|
| 30 | return;
|
---|
| 31 | }
|
---|
| 32 | /*}}}*/
|
---|
[12706] | 33 | /*FUNCTION Options::~Options(){{{*/
|
---|
[7746] | 34 | Options::~Options(){
|
---|
| 35 | return;
|
---|
| 36 | }
|
---|
| 37 | /*}}}*/
|
---|
| 38 |
|
---|
| 39 | /*Object management*/
|
---|
[12706] | 40 | /*FUNCTION Options::AddOption{{{*/
|
---|
[7746] | 41 | int Options::AddOption(Option* in_option){
|
---|
| 42 |
|
---|
[13395] | 43 | char* name=NULL;
|
---|
[13975] | 44 |
|
---|
[7746] | 45 | vector<Object*>::iterator object;
|
---|
| 46 | Option* option=NULL;
|
---|
| 47 |
|
---|
| 48 | /*In debugging mode, check that the option is not a NULL pointer*/
|
---|
| 49 | _assert_(in_option);
|
---|
| 50 |
|
---|
| 51 | /*Also, check the option name*/
|
---|
[13395] | 52 | name=in_option->Name();
|
---|
[7746] | 53 |
|
---|
[13395] | 54 | if(!name) _error_("input option has an empty name");
|
---|
| 55 | if(strchr(name,'.')) _error_("Option \"" << name << "\" has a protected character \".\"");
|
---|
| 56 | if(strchr(name,'[')) _error_("Option \"" << name << "\" has a protected character \"[\"");
|
---|
| 57 | if(strchr(name,']')) _error_("Option \"" << name << "\" has a protected character \"]\"");
|
---|
| 58 |
|
---|
[7746] | 59 | /*Finally, check that no option of the same name already exists in the dataset*/
|
---|
| 60 | for(object=objects.begin();object<objects.end();object++){
|
---|
| 61 |
|
---|
[13975] | 62 | option=dynamic_cast<Option*>(*object);
|
---|
[13395] | 63 | if (!strcmp(option->Name(),name)){
|
---|
| 64 | _error_("Options \"" << name << "\" found multiple times");
|
---|
[7746] | 65 | break;
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | /*OK, all checks went well, add option to dataset*/
|
---|
| 70 | this->AddObject(in_option);
|
---|
| 71 |
|
---|
| 72 | return 1;
|
---|
| 73 | }
|
---|
| 74 | /*}}}*/
|
---|
[12706] | 75 | /*FUNCTION Options::GetOption{{{*/
|
---|
[7746] | 76 | Option* Options::GetOption(const char* name){
|
---|
| 77 |
|
---|
| 78 | vector<Object*>::iterator object;
|
---|
| 79 | Option* option=NULL;
|
---|
| 80 |
|
---|
| 81 | /*Go through options and find option: */
|
---|
| 82 | for ( object=objects.begin() ; object < objects.end(); object++ ){
|
---|
| 83 |
|
---|
[13975] | 84 | option=dynamic_cast<Option*>(*object);
|
---|
[13395] | 85 | if (!strncmp(name,option->Name(),strlen(option->Name()))){
|
---|
[7746] | 86 |
|
---|
| 87 | /*OK, now do we have a complete name? If not, it is a cell or a structure, we need to go further*/
|
---|
[13395] | 88 | if(!strcmp(name,option->Name())){
|
---|
[7746] | 89 | return option;
|
---|
| 90 | }
|
---|
| 91 | else{
|
---|
| 92 | /*If the object is a Cell, recursive call to its options*/
|
---|
[9883] | 93 | if(option->ObjectEnum()==OptionCellEnum){
|
---|
[13395] | 94 | GenericOption<Options*>* celloption=(GenericOption<Options*>*)option;
|
---|
| 95 | return celloption->value->GetOption(name);
|
---|
[7746] | 96 | }
|
---|
| 97 | /*If the object is a Struct loop over its size and recursive call*/
|
---|
[9883] | 98 | else if(option->ObjectEnum()==OptionStructEnum){
|
---|
[13395] | 99 | for(int i=0;i<option->NumEl();i++){
|
---|
| 100 | GenericOption<Options**>* structoption=(GenericOption<Options**>*)option;
|
---|
| 101 | _assert_(structoption->value[i]);
|
---|
| 102 | return structoption->value[i]->GetOption(name);
|
---|
[7746] | 103 | }
|
---|
| 104 | }
|
---|
| 105 | /*Else: not supported*/
|
---|
| 106 | else{
|
---|
[13395] | 107 | _error_("Cannot recover field \"" << name << "\" for an option of type " << EnumToStringx(option->ObjectEnum()));
|
---|
[7746] | 108 | }
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | /*Option not found return NULL pointer*/
|
---|
| 114 | return NULL;
|
---|
| 115 | }
|
---|
| 116 | /*}}}*/
|
---|