+GQuark new_definition_path(struct definition_scope *parent_scope,
+ GQuark field_name, const char *root_name)
+{
+ GQuark path;
+ GString *str;
+ gchar *c_str;
+ int need_dot = 0;
+
+ str = g_string_new("");
+ if (root_name) {
+ g_string_append(str, root_name);
+ need_dot = 1;
+ } else if (parent_scope) {
+ int i;
+
+ for (i = 0; i < parent_scope->scope_path->len; i++) {
+ GQuark q = g_array_index(parent_scope->scope_path,
+ GQuark, i);
+ if (!q)
+ continue;
+ if (need_dot)
+ g_string_append(str, ".");
+ g_string_append(str, g_quark_to_string(q));
+ need_dot = 1;
+ }
+ }
+ if (field_name) {
+ if (need_dot)
+ g_string_append(str, ".");
+ g_string_append(str, g_quark_to_string(field_name));
+ }
+ c_str = g_string_free(str, FALSE);
+ if (c_str[0] == '\0')
+ return 0;
+ path = g_quark_from_string(c_str);
+ printf_debug("new definition path: %s\n", c_str);
+ g_free(c_str);
+ return path;
+}
+
+struct definition_scope *
+ new_definition_scope(struct definition_scope *parent_scope,
+ GQuark field_name, const char *root_name)
+{
+ struct definition_scope *scope;
+
+ if (root_name) {
+ scope = _new_definition_scope(parent_scope, 0);
+ append_scope_path(root_name, scope->scope_path);
+ } else {
+ int scope_path_len = 1;
+
+ assert(parent_scope);
+ scope_path_len += parent_scope->scope_path->len;
+ scope = _new_definition_scope(parent_scope, scope_path_len);
+ memcpy(scope->scope_path->data, parent_scope->scope_path->data,
+ sizeof(GQuark) * (scope_path_len - 1));
+ g_array_index(scope->scope_path, GQuark, scope_path_len - 1) =
+ field_name;
+ }
+ if (babeltrace_debug) {
+ int i, need_dot = 0;
+
+ printf_debug("new definition scope: ");
+ for (i = 0; i < scope->scope_path->len; need_dot = 1, i++)
+ printf("%s%s", need_dot ? "." : "",
+ g_quark_to_string(g_array_index(scope->scope_path, GQuark, i)));
+ printf("\n");
+ }
+ return scope;
+}
+
+/*
+ * in: path (dot separated), out: q (GArray of GQuark)
+ */
+void append_scope_path(const char *path, GArray *q)
+{
+ const char *ptrbegin, *ptrend = path;
+ GQuark quark;
+
+ for (;;) {
+ char *str;
+ size_t len;
+
+ ptrbegin = ptrend;
+ ptrend = strchr(ptrbegin, '.');
+ if (!ptrend)
+ break;
+ len = ptrend - ptrbegin;
+ /* Don't accept two consecutive dots */
+ assert(len != 0);
+ str = g_new(char, len + 1); /* include \0 */
+ memcpy(str, ptrbegin, len);
+ str[len] = '\0';
+ quark = g_quark_from_string(str);
+ g_array_append_val(q, quark);
+ g_free(str);
+ ptrend++; /* skip current dot */
+ }
+ /* last. Check for trailing dot (and discard). */
+ if (ptrbegin[0] != '\0') {
+ quark = g_quark_from_string(ptrbegin);
+ g_array_append_val(q, quark);
+ }
+}
+