1 /* Dia -- an diagram creation/manipulation program
2 * Copyright (C) 1998 Alexander Larsson
3 *
4 * umlparameter.c : refactored from uml.c, class.c to final use StdProps
5 * PROP_TYPE_DARRAY, a list where each element is a set
6 * of properies described by the same StdPropDesc
7 * Copyright (C) 2005 Hans Breuer
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <string.h>
29
30 #include "uml.h"
31 #include "properties.h"
32
33 static PropEnumData _uml_parameter_kinds[] = {
34 { N_("Undefined"), UML_UNDEF_KIND} ,
35 { N_("In"), UML_IN },
36 { N_("Out"), UML_OUT },
37 { N_("In & Out"), UML_INOUT },
38 { NULL, 0 }
39 };
40
41 static PropDescription umlparameter_props[] = {
42 { "name", PROP_TYPE_STRING, PROP_FLAG_VISIBLE | PROP_FLAG_OPTIONAL,
43 N_("Name"), NULL, NULL },
44 { "type", PROP_TYPE_STRING, PROP_FLAG_VISIBLE | PROP_FLAG_OPTIONAL,
45 N_("Type"), NULL, NULL },
46 { "value", PROP_TYPE_STRING, PROP_FLAG_VISIBLE | PROP_FLAG_OPTIONAL,
47 N_("Value"), NULL, NULL },
48 { "comment", PROP_TYPE_STRING, PROP_FLAG_VISIBLE | PROP_FLAG_OPTIONAL,
49 N_("Comment"), NULL, NULL },
50 { "kind", PROP_TYPE_ENUM, PROP_FLAG_VISIBLE | PROP_FLAG_OPTIONAL,
51 N_("Kind"), NULL, _uml_parameter_kinds },
52
53 PROP_DESC_END
54 };
55
56 static PropOffset umlparameter_offsets[] = {
57 { "name", PROP_TYPE_STRING, offsetof(UMLParameter, name) },
58 { "type", PROP_TYPE_STRING, offsetof(UMLParameter, type) },
59 { "value", PROP_TYPE_STRING, offsetof(UMLParameter, value) },
60 { "comment", PROP_TYPE_STRING, offsetof(UMLParameter, comment) },
61 { "kind", PROP_TYPE_ENUM, offsetof(UMLParameter, kind) },
62 { NULL, 0, 0 },
63 };
64
65 PropDescDArrayExtra umlparameter_extra = {
66 { umlparameter_props, umlparameter_offsets, "umlparameter" },
67 (NewRecordFunc)uml_parameter_new,
68 (FreeRecordFunc)uml_parameter_destroy
69 };
70
71 UMLParameter *
uml_parameter_new(void)72 uml_parameter_new(void)
73 {
74 UMLParameter *param;
75
76 param = g_new0(UMLParameter, 1);
77 param->name = g_strdup("");
78 param->type = g_strdup("");
79 param->comment = g_strdup("");
80 param->value = NULL;
81 param->kind = UML_UNDEF_KIND;
82
83 return param;
84 }
85
86 void
uml_parameter_destroy(UMLParameter * param)87 uml_parameter_destroy(UMLParameter *param)
88 {
89 g_free(param->name);
90 g_free(param->type);
91 if (param->value != NULL)
92 g_free(param->value);
93 g_free(param->comment);
94
95 g_free(param);
96 }
97
98 char *
uml_get_parameter_string(UMLParameter * param)99 uml_get_parameter_string (UMLParameter *param)
100 {
101 int len;
102 char *str;
103
104 /* Calculate length: */
105 len = strlen (param->name) + 1 + strlen (param->type);
106
107 if (param->value != NULL) {
108 len += 1 + strlen (param->value) ;
109 }
110
111 switch(param->kind)
112 {
113 case UML_UNDEF_KIND:
114 break;
115 case UML_IN:
116 len += 3;
117 break;
118 case UML_OUT:
119 len += 4;
120 break;
121 case UML_INOUT:
122 len += 6;
123 break;
124 }
125
126 /* Generate string: */
127 str = g_malloc (sizeof (char) * (len + 1));
128
129 strcpy(str, "");
130
131 switch(param->kind)
132 {
133 case UML_UNDEF_KIND:
134 break;
135 case UML_IN:
136 strcat (str, "in ");
137 break;
138 case UML_OUT:
139 strcat (str, "out ");
140 break;
141 case UML_INOUT:
142 strcat (str, "inout ");
143 break;
144 }
145
146
147 strcat (str, param->name);
148 strcat (str, ":");
149 strcat (str, param->type);
150 if (param->value != NULL) {
151 strcat (str, "=");
152 strcat (str, param->value);
153 }
154
155 g_assert (strlen (str) == len);
156
157 return str;
158 }
159
160