Przeglądaj źródła

LocalizedText more easy to use..

We test the presence of locale/text by looking inside the strings.
So they are not pointers but "static" members of the struct.
Julius Pfrommer 10 lat temu
rodzic
commit
7232ead5f1

+ 2 - 2
src/ua_application.c

@@ -16,8 +16,8 @@ UA_Node* create_node_ns0(UA_Int32 class, UA_Int32 nodeClass, UA_Int32 const id,
 	n->nodeId.namespace = 0;
 	n->nodeId.identifier.numeric = id;
 	UA_String_copycstring(qn,&(n->browseName.name));
-	UA_String_copycstring(dn,n->displayName.text);
-	UA_String_copycstring(desc,n->description.text);
+	UA_String_copycstring(dn,&n->displayName.text);
+	UA_String_copycstring(desc,&n->description.text);
 	n->nodeClass = nodeClass;
 	return n;
 }

+ 11 - 25
src/ua_types.c

@@ -529,33 +529,27 @@ UA_TYPE_DELETE_DEFAULT(UA_LocalizedText)
 UA_Int32 UA_LocalizedText_deleteMembers(UA_LocalizedText *p) {
 	if(p == UA_NULL) return UA_SUCCESS;
 	UA_Int32 retval = UA_SUCCESS;
-	retval |= UA_String_delete(p->locale);
-	p->locale = UA_NULL;
-	retval |= UA_String_delete(p->text);
-	p->text = UA_NULL;
+	retval |= UA_String_deleteMembers(&p->locale);
+	retval |= UA_String_deleteMembers(&p->text);
 	return retval;
 }
 
 UA_Int32 UA_LocalizedText_init(UA_LocalizedText *p) {
 	if(p == UA_NULL) return UA_ERROR;
-	p->locale = UA_NULL;
-	p->text = UA_NULL;
+	UA_String_init(&p->locale);
+	UA_String_init(&p->text);
 	return UA_SUCCESS;
 }
 
 UA_TYPE_NEW_DEFAULT(UA_LocalizedText)
 UA_Int32 UA_LocalizedText_copycstring(char const *src, UA_LocalizedText *dst) {
 	if(dst == UA_NULL) return UA_ERROR;
-	UA_Int32 retval = UA_SUCCESS;
+	UA_LocalizedText_init(dst);
 
-	retval |= UA_LocalizedText_init(dst);
-	retval |= UA_String_new(&dst->locale);
-	if(retval != UA_SUCCESS) return retval;
-	retval |= UA_String_copycstring("EN", dst->locale); // TODO: Are language codes upper case?
-	if(retval != UA_SUCCESS) return retval;
-	retval |= UA_String_new(&dst->text);
+	UA_Int32 retval = UA_SUCCESS;
+	retval |= UA_String_copycstring("EN", &dst->locale); // TODO: Are language codes upper case?
 	if(retval != UA_SUCCESS) return retval;
-	retval |= UA_String_copycstring(src, dst->text);
+	retval |= UA_String_copycstring(src, &dst->text);
 	return retval;
 }
 
@@ -564,17 +558,9 @@ UA_Int32 UA_LocalizedText_copy(UA_LocalizedText const *src, UA_LocalizedText *ds
 	UA_Int32 retval = UA_SUCCESS;
 
 	retval |= UA_LocalizedText_init(dst);
-	if(src->locale != UA_NULL) {
-		retval |= UA_String_new(&dst->locale);
-		if(retval != UA_SUCCESS) return retval;
-		retval |= UA_String_copy(src->locale, dst->locale);
-		if(retval != UA_SUCCESS) return retval;
-	}
-	if(src->text != UA_NULL) {
-		retval |= UA_String_new(&dst->text);
-		if(retval != UA_SUCCESS) return retval;
-		retval |= UA_String_copy(src->text, dst->text);
-	}
+	retval |= UA_String_copy(&src->locale, &dst->locale);
+	if(retval != UA_SUCCESS) return retval;
+	retval |= UA_String_copy(&src->text, &dst->text);
 	return retval;
 }
 

+ 2 - 2
src/ua_types.h

@@ -143,8 +143,8 @@ typedef struct UA_QualifiedName {
 
 /** @brief Human readable text with an optional locale identifier. */
 typedef struct UA_LocalizedText {
-	UA_String *locale;
-	UA_String *text;
+	UA_String locale;
+	UA_String text;
 } UA_LocalizedText;
 
 enum UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_enum {

+ 11 - 11
src/ua_types_encoding_binary.c

@@ -501,35 +501,35 @@ UA_Int32 UA_LocalizedText_calcSizeBinary(UA_LocalizedText const *p) {
 	UA_Int32 length = 1; // for encodingMask
 	if(p == UA_NULL)
 		return sizeof(UA_LocalizedText);
-	if(p->locale != UA_NULL)
-		length += UA_String_calcSizeBinary(p->locale);
-	if(p->text != UA_NULL)
-		length += UA_String_calcSizeBinary(p->text);
+	if(p->locale.data != UA_NULL)
+		length += UA_String_calcSizeBinary(&p->locale);
+	if(p->text.data != UA_NULL)
+		length += UA_String_calcSizeBinary(&p->text);
 	return length;
 }
 
 UA_TYPE_ENCODEBINARY(UA_LocalizedText,
 					 UA_Byte encodingMask = 0;
-					 if(src->locale != UA_NULL)
+					 if(src->locale.data != UA_NULL)
 						 encodingMask |= UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_LOCALE;
-					 if(src->text != UA_NULL)
+					 if(src->text.data != UA_NULL)
 						 encodingMask |= UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_TEXT;
                      retval |= UA_Byte_encodeBinary(&encodingMask, dst, offset);
                      if(encodingMask & UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_LOCALE)
-						 retval |= UA_String_encodeBinary(src->locale, dst, offset);
+						 retval |= UA_String_encodeBinary(&src->locale, dst, offset);
                      if(encodingMask & UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_TEXT)
-						 retval |= UA_String_encodeBinary(src->text, dst, offset); )
+						 retval |= UA_String_encodeBinary(&src->text, dst, offset); )
 
 UA_Int32 UA_LocalizedText_decodeBinary(UA_ByteString const *src, UA_UInt32 *offset, UA_LocalizedText *dst) {
 	UA_Int32 retval = UA_SUCCESS;
 	retval |= UA_LocalizedText_init(dst);
-	UA_Byte encodingMask;
+	UA_Byte encodingMask = 0;
 	CHECKED_DECODE(UA_Byte_decodeBinary(src, offset, &encodingMask),; );
 	if(encodingMask & UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_LOCALE)
-		CHECKED_DECODE(UA_String_new(&dst->locale); UA_String_decodeBinary(src, offset, dst->locale),
+		CHECKED_DECODE(UA_String_decodeBinary(src, offset, &dst->locale),
 					   UA_LocalizedText_deleteMembers(dst));
 	if(encodingMask & UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_TEXT)
-		CHECKED_DECODE(UA_String_new(&dst->text); UA_String_decodeBinary(src, offset, dst->text),
+		CHECKED_DECODE(UA_String_decodeBinary(src, offset, &dst->text),
 					   UA_LocalizedText_deleteMembers(dst));
 	return retval;
 }

+ 2 - 2
src/ua_types_encoding_xml.c

@@ -320,9 +320,9 @@ UA_Int32 UA_LocalizedText_decodeXmlFromStack(XML_Stack *s, XML_Attr *attr, UA_Lo
 		// set attributes
 		for(i = 0;attr[i];i += 2) {
 			if(0 == strncmp("Text", attr[i], strlen("Text"))) {
-				UA_String_copycstring(attr[i + 1], dst->text);
+				UA_String_copycstring(attr[i + 1], &dst->text);
 			} else if(0 == strncmp("Locale", attr[i], strlen("Locale"))) {
-				UA_String_copycstring(attr[i + 1], dst->locale);
+				UA_String_copycstring(attr[i + 1], &dst->locale);
 			} else
 				perror("Unknown attribute");
 		}

+ 6 - 5
src/ua_xml.c

@@ -672,9 +672,9 @@ void print_node(UA_Node const * node) {
 	if (node != UA_NULL) {
 		UA_NodeId_printf("node.nodeId=", &(node->nodeId));
 		printf("\t.browseName='%.*s'\n", node->browseName.name.length, node->browseName.name.data);
-		printf("\t.displayName='%.*s'\n", node->displayName.text->length, node->displayName.text->data);
-		printf("\t.description='%.*s%s'\n", node->description.text->length > 40 ? 40 : node->description.text->length,
-		       node->description.text->data, node->description.text->length > 40 ? "..." : "");
+		printf("\t.displayName='%.*s'\n", node->displayName.text.length, node->displayName.text.data);
+		printf("\t.description='%.*s%s'\n", node->description.text.length > 40 ? 40 : node->description.text.length,
+		       node->description.text.data, node->description.text.length > 40 ? "..." : "");
 		printf("\t.nodeClass=%d\n", node->nodeClass);
 		printf("\t.writeMask=%d\n", node->writeMask);
 		printf("\t.userWriteMask=%d\n", node->userWriteMask);
@@ -709,8 +709,9 @@ void print_node(UA_Node const * node) {
 				{
 					if(p->value.data != UA_NULL) {
 						UA_LocalizedText *ltp = (UA_LocalizedText *)currentData;
-						printf(",locale={%d,{%.*s}},text={%d,{%.*s}}", ltp->locale->length, ltp->locale->length,
-							   ltp->locale->data, ltp->text->length, ltp->text->length, ltp->text->data);
+						printf(",locale={%d,{%.*s}},text={%d,{%.*s}}", ltp->locale.length,
+							   ltp->locale.length, ltp->locale.data, ltp->text.length,
+							   ltp->text.length, ltp->text.data);
 					}
 				}
 				break;

+ 10 - 11
tests/check_builtin.c

@@ -425,13 +425,13 @@ START_TEST(UA_LocalizedText_calcSizeTextOnlyShallReturnEncodingSize) {
 	// given
 	UA_LocalizedText arg;
 	UA_LocalizedText_init(&arg);
-	UA_String_new(&arg.text);
-	arg.text->length  = 42;
+	arg.text = (UA_String) {8, (UA_Byte *)"12345678"};
 	// when
 	UA_UInt32 encodingSize = UA_LocalizedText_calcSizeBinary(&arg);
 	// then
-	ck_assert_int_eq(encodingSize, 1+4+42);
+	ck_assert_int_eq(encodingSize, 1+4+8);
 	// finally
+	UA_LocalizedText_init(&arg); // do not delete text
 	UA_LocalizedText_deleteMembers(&arg);
 }
 END_TEST
@@ -439,12 +439,12 @@ START_TEST(UA_LocalizedText_calcSizeLocaleOnlyShallReturnEncodingSize) {
 	// given
 	UA_LocalizedText arg;
 	UA_LocalizedText_init(&arg);
-	UA_String_new(&arg.locale);
-	arg.locale->length = 11;
+	arg.locale = (UA_String) {8, (UA_Byte *)"12345678"};
 	// when
 	UA_UInt32 encodingSize = UA_LocalizedText_calcSizeBinary(&arg);
 	// then
-	ck_assert_int_eq(encodingSize, 1+4+11);
+	ck_assert_int_eq(encodingSize, 1+4+8);
+	UA_LocalizedText_init(&arg); // do not delete locale
 	UA_LocalizedText_deleteMembers(&arg);
 }
 END_TEST
@@ -452,14 +452,13 @@ START_TEST(UA_LocalizedText_calcSizeTextAndLocaleShallReturnEncodingSize) {
 	// given
 	UA_LocalizedText arg;
 	UA_LocalizedText_init(&arg);
-	UA_String_new(&arg.text);
-	UA_String_new(&arg.locale);
-	arg.text->length   = 47;
-	arg.locale->length = 11;
+	arg.locale = (UA_String) {8, (UA_Byte *)"12345678"};
+	arg.text = (UA_String) {8, (UA_Byte *)"12345678"};
 	// when
 	UA_UInt32 encodingSize = UA_LocalizedText_calcSizeBinary(&arg);
 	// then
-	ck_assert_int_eq(encodingSize, 1+4+11+4+47);
+	ck_assert_int_eq(encodingSize, 1+4+8+4+8);
+	UA_LocalizedText_init(&arg); // do not delete locale and text
 	UA_LocalizedText_deleteMembers(&arg);
 }
 END_TEST