39 #define LOCAL_PRINTF printf 45 const char DSEP =
'\\';
48 const char DSEP =
'/';
49 const char DSEP_OTHER =
'\\';
55 const char *AttributeTree::m_file_path_str =
"_ATTRIBUTE_TREE_FILE_PATH_";
56 const char *AttributeTree::m_file_name_str =
"_ATTRIBUTE_TREE_FILE_NAME_";
65 in.getline(buffer, 1998);
67 int line_length=strlen(buffer);
68 if (line_length > 0 && buffer[line_length-1]==
'\r')
70 buffer[line_length-1]=0;
80 if (isRelativePath(filename))
82 return normalizePath(currentDir() + DSEP + filename);
86 return normalizePath(filename);
98 return (filename.length() < 2 || filename[1] !=
':');
100 return filename[0] !=
DSEP;
106 if (_filename.empty())
110 string filename(_filename);
115 string::size_type slash_before_dot;
116 string::size_type dot_start = 0;
118 dot_start = filename.find(DSEP_OTHER, 0);
119 while (dot_start != string::npos)
121 filename[dot_start] =
DSEP;
123 dot_start = filename.find(DSEP_OTHER, dot_start);
130 while (!filename.find(
string(
".") + DSEP))
134 string temp_filename(filename, 2, string::npos);
135 temp_filename.swap(filename);
137 #ifdef _SYSTEM_WIN32_ 139 if (filename.find(
string(
":.") + DSEP) == 1)
141 string temp_filename;
142 temp_filename.append(filename, 0, 2);
144 temp_filename.append(filename, dot_start, filename.length() - dot_start);
145 temp_filename.swap(filename);
152 dot_start = filename.find(
string(1, DSEP) +
"." + DSEP, 0);
153 while (dot_start != string::npos)
157 string temp_filename(filename, 0, dot_start);
158 temp_filename.append(filename, dot_start + 2, filename.length() - dot_start - 2);
159 temp_filename.swap(filename);
161 dot_start = filename.find(
string(1, DSEP) +
"." + DSEP, dot_start);
166 dot_start = filename.find(
string(1, DSEP) +
".." + DSEP, 0);
167 while (dot_start != string::npos)
173 slash_before_dot = filename.rfind(DSEP, dot_start - 1);
174 if (slash_before_dot != string::npos)
177 if (filename[slash_before_dot+1] != DSEP && filename[slash_before_dot+1] !=
'.' &&
178 (slash_before_dot >= 1) ? filename[slash_before_dot-1] !=
':' : 1)
181 string temp_filename(filename, 0, slash_before_dot);
182 temp_filename.append(filename, dot_start, filename.length() - dot_start);
183 temp_filename.swap(filename);
190 dot_start = slash_before_dot;
192 else if (dot_start > 0)
194 string temp_filename;
196 #ifdef _SYSTEM_WIN32_ 198 if (filename[1] =
':')
200 temp_filename.append(filename, 0, 2);
205 temp_filename.append(filename, dot_start, filename.length() - dot_start);
206 temp_filename.swap(filename);
214 dot_start = filename.find(
string(1, DSEP) +
".." + DSEP, dot_start);
218 while (!filename.find(
string(
".") + DSEP))
222 string temp_filename(filename, 2, string::npos);
223 temp_filename.swap(filename);
225 #ifdef _SYSTEM_WIN32_ 229 if ((filename.find(
string(
":.") + DSEP) == 1))
231 string temp_filename;
232 temp_filename.append(filename, 0, 2);
234 temp_filename.append(filename, dot_start, filename.length() - dot_start);
235 temp_filename.swap(filename);
245 if (_filename.empty())
249 string filename(_filename);
253 for (
unsigned i = 0;i < filename.length();++i)
255 if (filename[i] == DSEP_OTHER)
266 const char* get_env = getenv(var_name.c_str());
273 return string(get_env);
279 if (_filename.empty())
283 string filename(_filename);
287 string::size_type dot_start = filename.find(
"${");
288 while (dot_start != string::npos)
291 string::size_type dot_end = filename.find(
"}", dot_start);
292 if (dot_end == string::npos)
294 printf(
"tFilePath::replaceEnvironment(%s)>> Failure on matching closing bracket " 295 "'}' in substring '%s'\n", (
const char*) _filename.c_str(),
296 (
const char*)
string(filename, dot_start, string::npos).c_str());
299 string var_name(filename, dot_start, dot_end - dot_start);
300 string temp_filename(filename, 0, dot_start - 2);
301 temp_filename += getEnvironment(var_name);
302 temp_filename += string(filename, dot_end + 1, string::npos);
303 filename.swap(temp_filename);
305 dot_start = filename.find(
"${");
313 #ifdef _IC_BUILDER_DEPRECATED_STYLE_ 336 return absoluteName();
360 return absolutePath(filename);
368 return isRelativePath(filename);
374 bool FilePath::IsRelativePath()
const 376 return isRelativePath();
384 return normalizePath(filename);
394 return exchangeSeparators(filename);
402 return getEnvironment(var_name);
410 return replaceEnvironment(filename);
417 void FilePath::init(
const char* filename)
420 #ifdef _SYSTEM_WIN32_ 422 char* buffer =
new char[1000];
423 if (_getcwd(buffer, 1000) != NULL)
446 m_pwd = normalizePath(m_pwd);
448 m_file = normalizePath(absolutePath(exchangeSeparators(
string(filename))));
451 string::size_type last_directory_separator = m_file.rfind(DSEP);
452 if (last_directory_separator < m_file.length())
454 m_file_path_name_split = last_directory_separator + 1;
459 m_file_path_name_split = 0;
462 m_file_name_extension_split = m_file.rfind(
'.');
488 assert(parent != NULL
489 &&
"SubTreeList::copy() called with NULL parent! Allocated attribute tree would be lost!");
654 #ifdef _IC_BUILDER_DEPRECATED_STYLE_ 835 first_entry->
unlink(
this);
877 if (m_sub_tree != NULL)
908 if (param_description)
912 char *subdescription;
913 split(description, subdescription);
928 if (!description || !*description)
948 return subtree->
setAttribute(subdescription, attribute);
955 return new_subtree->
setAttribute(subdescription, attribute);
964 while (at_path_parent && at_path == NULL)
967 at_path_parent = at_path_parent->
parentTree();
977 (*subtree) = at_path;
988 if (param_description)
995 int len = strlen(description);
996 if (len >= file_path_str_len
997 && !strncmp(description + (len - file_path_str_len),
m_file_path_str, file_path_str_len))
1001 else if (len >= file_name_str_len
1002 && !strncmp(description + (len - file_name_str_len),
m_file_name_str, file_name_str_len))
1010 char *description_part = description;
1012 for (; at && description_part;)
1015 char *next_description = description_part;
1017 description_part = strchr(description_part,
'.');
1018 if (description_part)
1020 *description_part = 0;
1049 ret =
const_cast<char*
>(default_attribute);
1061 attribute =
const_cast<char*
>(default_attribute);
1068 int base_len = strlen(base_description);
1069 char *description = (
char*)malloc(base_len + 6);
1070 assert(description != NULL);
1071 strcpy(description, base_description);
1078 while (base_len>=j-1 &&
1079 sscanf(description+base_len-j-1,
"%i", &i)==1)
1089 sprintf(description + base_len - j,
"%i", i);
1094 sprintf(description + base_len - j,
"%i", i);
1147 char *the_upper_description =
strdup(upper_description ? upper_description :
"");
1149 assert(the_upper_description != NULL);
1150 assert(t_description != NULL);
1155 out << the_upper_description << comment_str <<
'{' <<
endl;
1157 out << the_upper_description <<
'}' << comment_str <<
endl;
1159 free(the_upper_description);
1160 free(t_description);
1168 out << the_upper_description << t_description <<
'{' <<
endl;
1176 char *tab = (
char*)malloc(strlen(the_upper_description) + 2);
1177 assert(tab != NULL);
1178 strcat(strcpy(tab, the_upper_description),
" ");
1183 out << the_upper_description <<
'}' << t_description <<
endl;
1187 size_t tud_len = strlen(the_upper_description);
1188 size_t len = strlen(t_description) + tud_len + 1;
1189 char *description =
static_cast<char*
>(malloc(len + 1));
1190 assert(description != NULL);
1191 memset(description, 0, len + 1);
1193 if ((tud_len > 0) && (the_upper_description[tud_len-1] ==
' '))
1195 strcat(strcpy(description, the_upper_description), t_description);
1199 strcat(strcat(strcpy(description, the_upper_description),
"."), t_description);
1214 free(the_upper_description);
1215 free(t_description);
1224 ofstream out(filename);
1240 bool load_comments,
bool preserve_order)
1242 if (filename == NULL || strcmp(filename,
"") == 0)
1244 printf(
"tAttributeTree >> Trying to load an empty configuration file.\n");
1262 printf(
"tAttributeTree >> Could not open file '%s'\n", (
const char*)at_file.
absoluteName().c_str());
1266 error =
get(in, process_include, load_comments, &at_file);
1269 printf(
"Error in line %i while reading AttributeTree %s\n", error,
1305 while (isspace(*line))
1312 attribute = strchr(line,
':');
1323 if (!strcmp(line, include_str))
1325 if (process_include)
1327 string include_filename(line + INCLUDE_OFFSET);
1331 string absolute_include_filename(file_path ? file_path->
path() :
getFilePath());
1332 absolute_include_filename += include_filename;
1335 if (at->
load(include_filename.c_str(),
false, process_include, load_comments) !=
eOK)
1337 printf(
"error loading include file %s\n", (
const char*)include_filename.c_str());
1346 else if (!strstr(line, comment_str) || load_comments)
1355 attribute = strchr(line,
'{');
1361 if (!strcmp(line, comment_str))
1364 bool comment_end =
false;
1374 char *line_end = buffer + strlen(buffer);
1376 while (isspace(*line))
1380 while (line_end >= buffer && isspace(*line_end))
1384 *(line_end + 1) = 0;
1385 comment_end = (strstr(line, comment_end_str) != NULL);
1387 if (load_comments && !comment_end)
1392 while (!comment_end);
1401 attribute = strchr(line,
'}');
1421 if (!in.eof() && line[0])
1438 subdescription = strchr(description,
'.');
1441 *subdescription = 0;
1471 return !strcmp(content, attribute);
1477 return content != NULL;
1523 if (!additional_separator)
1525 additional_separator =
"";
1529 int old_len = strlen(dest);
1530 int additional_len = strlen(additional_separator);
1531 int whole_len = old_len + additional_len + strlen(src);
1532 char *new_attr =
static_cast<char*
>(malloc(whole_len + 1));
1533 assert(new_attr != NULL);
1534 strcpy(new_attr, dest);
1535 strcpy(new_attr + old_len, additional_separator);
1536 strcpy(new_attr + old_len + additional_len, src);
1586 return strchr(
comment(),
'\n') != NULL;
1595 #ifdef _IC_BUILDER_DEPRECATED_STYLE_ 1600 void AttributeTree::Unlink()
1608 void AttributeTree::UnlinkSub()
1616 AttributeTree *AttributeTree::SetAttribute(
const char *description,
1626 AttributeTree *AttributeTree::SetAttribute(
const char *description,
1627 const char *subdescription,
1628 const char *attribute)
1630 return setAttribute(description, subdescription, attribute);
1636 char *AttributeTree::GetAttribute()
1644 char *AttributeTree::GetAttribute(
const char *description,
1645 const char *default_attribute,
1648 return getAttribute(description, default_attribute, subtree);
1654 char *AttributeTree::GetOrSetDefault(
const char *description,
1655 const char *default_attribute)
1663 int AttributeTree::Save(
const char *filename,
int change_style_depth,
1664 bool unmark_changes)
1666 return save(filename, change_style_depth, unmark_changes);
1672 int AttributeTree::Load(
const char *filename,
bool unmark_changes,
1673 bool process_include,
bool load_comments,
1674 bool preserve_order)
1676 return load(filename, unmark_changes, process_include, load_comments, preserve_order);
1683 bool load_comments,
const FilePath *file_path)
1685 return get(in, process_include, load_comments, file_path);
1691 char *AttributeTree::NewSubNodeDescription(
const char *base_description)
1707 void AttributeTree::PrintSubTree(std::ostream &out,
1708 int change_style_depth,
1709 const char *upper_description)
1711 printSubTree(out, change_style_depth, upper_description);
1717 const char *AttributeTree::Description()
const 1725 const char *AttributeTree::Attribute()
const 1733 void AttributeTree::SetDescription(
const char *description)
1741 void AttributeTree::SetAttribute(
const char *attribute)
1749 void AttributeTree::SetComment(
const char *
comment)
1757 AttributeTree *AttributeTree::SubTree(
const char *description)
1765 AttributeTree *AttributeTree::GetSubTree(
const char *description)
1815 AttributeTree *AttributeTree::Search(
const char *description,
const char *attribute)
1817 return search(description, attribute);
1824 bool AttributeTree::IsAttribute(
const char *description,
const char *attribute)
1833 bool AttributeTree::Changed()
1841 void AttributeTree::UnmarkChanges()
1849 int AttributeTree::Contains()
1873 const char *AttributeTree::Comment()
1881 bool AttributeTree::HasComment()
1889 bool AttributeTree::IsComment()
1897 bool AttributeTree::HasMultilineComment()
1905 bool AttributeTree::IsMultilineComment()
1921 void AttributeTree::AppendAttribute(
const char *attribute,
const char *separator)
1929 void AttributeTree::AppendDescription(
const char *description,
const char *separator)
1937 void AttributeTree::RevertOrder()
1946 const char *AttributeTree::GetFilePath()
1955 const char *AttributeTree::FileName()
AttributeTree * search(const char *description, const char *attribute)
AttributeTree * next(AttributeTree *prev)
char * newSubNodeDescription(const char *base_description="")
AttributeTree * search(const char *description, const char *attribute)
bool isAttribute(const char *description, const char *attribute=0)
AttributeTree * commentAttributeTree()
Contains global functions for string manipulation, encapsulated into the icl_core::os namespace...
bool hasMultilineComment()
static icl_core::String replaceEnvironment(const icl_core::String &filename)
char * strdup(const char *s)
void appendDescription(const char *description, const char *separator="")
const char * getDescription() const
int load(const char *filename, bool unmark_changes=true, bool process_include=true, bool load_comments=false, bool preserve_order=false)
void copy(AttributeTree *parent)
int save(const char *filename, int change_style_depth=3, bool unmark_changes=true)
void printSubTree(std::ostream &out, int change_style_depth=3, const char *upper_description=0)
AttributeTree(const char *description=0, AttributeTree *parent=0)
SubTreeList(AttributeTree *sub_tree=0, SubTreeList *next=0)
static const char * m_file_name_str
char * m_this_description
AttributeTree * addNewSubTree()
AttributeTree * nextSubTree(AttributeTree *subtree)
void newSubTreeList(AttributeTree *new_tree, AttributeTree *after)
static icl_core::String normalizePath(const icl_core::String &filename)
AttributeTree * addSubTree(AttributeTree *subtree)
void readNextLineInBuffer(istream &in)
void unlink(AttributeTree *obsolete_tree)
ThreadStream & endl(ThreadStream &stream)
AttributeTree * addNextTree(AttributeTree *nexttree)
char * getSpecialAttribute(const char *description, AttributeTree **subtree=NULL)
bool isRelativePath() const
void setDescription(const char *description)
bool isMultilineComment()
char * getOrSetDefault(const char *description, const char *default_attribute)
AttributeTree * m_sub_tree
AttributeTree * parentTree()
AttributeTree * getSubTree(const char *description)
char * strdup(const char *s)
static const char * m_file_path_str
void printSubTree(std::ostream &out, int change_style_depth, char *upper_description)
void appendString(char *&dest, const char *src, const char *separator)
void * memset(void *dest, int c, size_t count)
icl_core::String absoluteName() const
const char * attribute() const
AttributeTree * subTree(const char *description)
icl_core::String path() const
void appendAttribute(const char *attribute, const char *separator="")
SubTreeList * m_subtree_list
AttributeTree * setAttribute(const char *description, const char *attribute)
AttributeTree * firstSubTree()
icl_core::String name() const
bool Get(const icl_core::String &key, typename icl_core::ConvertToRef< T >::ToRef value) ICL_CORE_GCC_DEPRECATE_STYLE
void split(char *&description, char *&sub_description)
AttributeTree * subTree(const char *description)
Reads a configuration file in the old MCA attribute tree format.
static icl_core::String exchangeSeparators(const icl_core::String &filename)
int get(std::istream &in, bool process_include=true, bool load_comments=false, const FilePath *file_path=NULL)
SubTreeList * revertOrder(SubTreeList *new_next=NULL)
void setComment(const char *comment)
friend class AttributeTree
const char * getFilePath()
const char * comment_end_str