2006-12-05 David Lodge <dave@cirt.net>
[dia.git] / objects / UML / umlformalparameter.c
blob9e273e653993ecd632551aa314fff9614992c365
1 /* Dia -- an diagram creation/manipulation program
2 * Copyright (C) 1998 Alexander Larsson
4 * umlformalparameter.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
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.
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.
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.
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
28 #include <string.h>
30 #include "uml.h"
31 #include "properties.h"
33 static PropDescription umlformalparameter_props[] = {
34 { "name", PROP_TYPE_STRING, PROP_FLAG_VISIBLE | PROP_FLAG_OPTIONAL,
35 N_("Name"), NULL, NULL },
36 { "type", PROP_TYPE_STRING, PROP_FLAG_VISIBLE | PROP_FLAG_OPTIONAL,
37 N_("Type"), NULL, NULL },
39 PROP_DESC_END
42 static PropOffset umlformalparameter_offsets[] = {
43 { "name", PROP_TYPE_STRING, offsetof(UMLFormalParameter, name) },
44 { "type", PROP_TYPE_STRING, offsetof(UMLFormalParameter, type) },
45 { NULL, 0, 0 },
48 PropDescDArrayExtra umlformalparameter_extra = {
49 { umlformalparameter_props, umlformalparameter_offsets, "umlformalparameter" },
50 (NewRecordFunc)uml_formalparameter_new,
51 (FreeRecordFunc)uml_formalparameter_destroy
54 UMLFormalParameter *
55 uml_formalparameter_new(void)
57 UMLFormalParameter *param;
59 param = g_new0(UMLFormalParameter, 1);
60 param->name = g_strdup("");
61 param->type = NULL;
63 return param;
66 UMLFormalParameter *
67 uml_formalparameter_copy(UMLFormalParameter *param)
69 UMLFormalParameter *newparam;
71 newparam = g_new0(UMLFormalParameter, 1);
73 newparam->name = g_strdup(param->name);
74 if (param->type != NULL) {
75 newparam->type = g_strdup(param->type);
76 } else {
77 newparam->type = NULL;
80 return newparam;
83 void
84 uml_formalparameter_destroy(UMLFormalParameter *param)
86 g_free(param->name);
87 if (param->type != NULL)
88 g_free(param->type);
89 g_free(param);
92 void
93 uml_formalparameter_write(AttributeNode attr_node, UMLFormalParameter *param)
95 DataNode composite;
97 composite = data_add_composite(attr_node, "umlformalparameter");
99 data_add_string(composite_add_attribute(composite, "name"),
100 param->name);
101 data_add_string(composite_add_attribute(composite, "type"),
102 param->type);
105 char *
106 uml_get_formalparameter_string (UMLFormalParameter *parameter)
108 int len;
109 char *str;
111 /* Calculate length: */
112 len = strlen (parameter->name);
114 if (parameter->type != NULL) {
115 len += 1 + strlen (parameter->type);
118 /* Generate string: */
119 str = g_malloc (sizeof (char) * (len + 1));
120 strcpy (str, parameter->name);
121 if (parameter->type != NULL) {
122 strcat (str, ":");
123 strcat (str, parameter->type);
126 g_assert (strlen (str) == len);
128 return str;