summaryrefslogtreecommitdiff
path: root/src/bin/eolian/common_funcs.c
blob: 13d5ffc5c7431bde1cdbf7faad18927b41e92971 (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
#include "common_funcs.h"
#include "Eolian.h"

int _eolian_gen_log_dom = -1;

Eolian_Class *current_class;

static void
_class_name_concatenate(const Eolian_Class *class, char *buffer)
{
   Eina_Iterator *itr = eolian_class_namespaces_get(class);
   const char *name;
   buffer[0] = '\0';
   EINA_ITERATOR_FOREACH(itr, name)
     {
        sprintf(buffer, "%s_", name);
        buffer += (strlen(name) + 1);
     }
   sprintf(buffer, "%s", eolian_class_name_get(class));
   eina_iterator_free(itr);
}

void
_class_env_create(const Eolian_Class *class, const char *over_classname, _eolian_class_vars *env)
{
   if (!env) return;

   const char *eo_prefix = NULL;
   char *p;

   if (!class)
      strncpy(env->full_classname, over_classname, PATH_MAX - 1);
   else
      _class_name_concatenate(class, env->full_classname);

   /* class/CLASS*/
   p = strncpy(env->upper_classname, env->full_classname, PATH_MAX - 1);
   eina_str_toupper(&p);
   p = strncpy(env->lower_classname, env->full_classname, PATH_MAX - 1);
   eina_str_tolower(&p);

   /* eo_prefix */
   if (class) eo_prefix = eolian_class_eo_prefix_get(class);
   if (!eo_prefix) eo_prefix = env->full_classname;
   p = strncpy(env->upper_eo_prefix, eo_prefix, PATH_MAX - 1);
   eina_str_toupper(&p);
   p = strncpy(env->lower_eo_prefix, eo_prefix, PATH_MAX - 1);
   eina_str_tolower(&p);

   /* classtype */
   if (class) switch (eolian_class_type_get(class))
     {
        case EOLIAN_CLASS_REGULAR:
        case EOLIAN_CLASS_ABSTRACT:
           strcpy(env->lower_classtype, "class");
           strcpy(env->upper_classtype, "CLASS");
           break;
        case EOLIAN_CLASS_MIXIN:
           strcpy(env->lower_classtype, "mixin");
           strcpy(env->upper_classtype, "MIXIN");
           break;
        case EOLIAN_CLASS_INTERFACE:
           strcpy(env->lower_classtype, "interface");
           strcpy(env->upper_classtype, "INTERFACE");
           break;
        default:
           break;
     }
}

void
_class_func_env_create(const Eolian_Class *class, const char *funcname, Eolian_Function_Type ftype, _eolian_class_func_vars *env)
{
   char *p;
   const char *ret;
   const char *suffix = "";
   const char *legacy = NULL;
   const Eolian_Function *funcid = eolian_class_function_get_by_name(class, funcname, ftype);
   if (ftype == EOLIAN_PROP_GET)
     {
        suffix = "_get";
        legacy = eolian_function_legacy_get(funcid, ftype);
     }
   if (ftype == EOLIAN_PROP_SET)
     {
        suffix = "_set";
        legacy = eolian_function_legacy_get(funcid, ftype);
     }
   if (!legacy) legacy = eolian_function_legacy_get(funcid, EOLIAN_METHOD);

   _eolian_class_vars tmp_env;
   _class_env_create(class, NULL, &tmp_env);

   p = strncpy(env->upper_func, funcname, PATH_MAX - 1);
   eina_str_toupper(&p);

   ret = eolian_function_full_c_name_get(funcid, tmp_env.upper_eo_prefix);
   sprintf(p = env->upper_eo_func, "%s%s", ret, suffix);
   eina_str_toupper(&p);
   eina_stringshare_del(ret);

   ret = eolian_function_full_c_name_get(funcid, tmp_env.lower_eo_prefix);
   sprintf(p = env->lower_eo_func, "%s%s", ret, suffix);
   eina_str_tolower(&p);
   eina_stringshare_del(ret);

   env->legacy_func[0] = '\0';
   if (legacy && !strcmp(legacy, "null")) goto end;
   if (legacy)
     {
        sprintf(p = env->legacy_func, "%s", legacy);
        goto end;
     }

   legacy = eolian_class_legacy_prefix_get(class);
   if (legacy && !strcmp(legacy, "null")) goto end;

   sprintf(env->legacy_func, "%s_%s%s", legacy?legacy:tmp_env.lower_classname, funcname, suffix);

end:
   return;
}

void
_template_fill(Eina_Strbuf *buf, const char *templ, const Eolian_Class *class, const char *classname, const char *funcname, Eina_Bool reset)
{
   _eolian_class_vars tmp_env;
   _eolian_class_func_vars tmp_func_env;
   _class_env_create(class, classname, &tmp_env);
   if (funcname)
      _class_func_env_create(class, funcname, EOLIAN_UNRESOLVED, &tmp_func_env);
   if (buf)
     {
        if (reset) eina_strbuf_reset(buf);
        if (templ) eina_strbuf_append(buf, templ);
        if (funcname)
          {
             eina_strbuf_replace_all(buf, "@#func", funcname);
             eina_strbuf_replace_all(buf, "@#FUNC", tmp_func_env.upper_func);
          }
        eina_strbuf_replace_all(buf, "@#classtype", tmp_env.lower_classtype);
        eina_strbuf_replace_all(buf, "@#CLASSTYPE", tmp_env.upper_classtype);
        eina_strbuf_replace_all(buf, "@#Class", tmp_env.full_classname);
        eina_strbuf_replace_all(buf, "@#class", tmp_env.lower_classname);
        eina_strbuf_replace_all(buf, "@#CLASS", tmp_env.upper_classname);
     }
}

char*
_nextline(char *str, unsigned int lines)
{
   if (!str) return NULL;

   char *ret = str;
   while (lines--)
     {
        ret= strchr(ret, '\n');
        if (ret) ret++;
        else return NULL;
     }
   return ret;
}

char*
_startline(char *str, char *pos)
{
   if (!str || !pos) return NULL;

   char *ret =  pos;
   while ((ret > str) && (*(ret-1)!='\n')) ret--;

   return ret;
}

char*
_source_desc_get(const char *str)
{
   Eina_Strbuf *part = eina_strbuf_new();
   if (str)
     {
        const char *p = strchr(str, '\n');
        size_t offs = (p) ? (size_t)(p - str) : strlen(str);
        eina_strbuf_append_n(part, str, offs);
        eina_strbuf_replace_all(part, "\\", "\\\\");
        eina_strbuf_replace_all(part, "\"", "\\\"");
     }
   char *ret = eina_strbuf_string_steal(part);
   eina_strbuf_free(part);
   return ret;
}