fixed string length checks, removed unnecessary loop, changed some other code for clarity

This commit is contained in:
Alan Yaniger 2021-08-01 16:50:04 +03:00
parent fd28fd498d
commit 523407a9c1

View file

@ -617,11 +617,14 @@ static int yang_patch_get_xval(
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
for (int j = 0; j < veclen; j++) { cxobj *xn_tmp = NULL;
cxobj *xn = vec[j]; if (veclen > 0) {
tmp_val = xml_body(xn); xn_tmp = vec[0]; //veclen should always be 1
}
if (xn_tmp != NULL) {
tmp_val = xml_body(xn_tmp);
cbuf_append_str(val, tmp_val);
} }
cbuf_append_str(val, tmp_val);
return 0; return 0;
} }
@ -673,7 +676,7 @@ static cbuf* yang_patch_xml2json_modified_cbuf(cxobj* x_simple_patch)
char c = json_simple_patch_tmp[l]; char c = json_simple_patch_tmp[l];
if (c == '{') { if (c == '{') {
brace_count++; brace_count++;
if (brace_count == 2) { if (brace_count == 2) { // We've reached the second brace, insert a '[' before it
cbuf_append(json_simple_patch,(int)'['); cbuf_append(json_simple_patch,(int)'[');
} }
} }
@ -682,7 +685,7 @@ static cbuf* yang_patch_xml2json_modified_cbuf(cxobj* x_simple_patch)
cbuf* json_simple_patch_2 = NULL; cbuf* json_simple_patch_2 = NULL;
// Insert a ']' before the last '}' to get the JSON to match what api_data_post() expects // Insert a ']' before the last '}' to get the JSON to match what api_data_post() expects
for (int l = cbuf_len(json_simple_patch); l>= 0; l--) { for (int l = cbuf_len(json_simple_patch) - 1; l >= 0; l--) {
char c = cbuf_get(json_simple_patch)[l]; char c = cbuf_get(json_simple_patch)[l];
if (c == '}') { if (c == '}') {
// Truncate and add a string, as there is not a function to insert a char into a cbuf // Truncate and add a string, as there is not a function to insert a char into a cbuf
@ -712,15 +715,15 @@ static cbuf* yang_patch_strip_after_last_slash(cbuf* val)
cbuf* val_tmp = cbuf_new(); cbuf* val_tmp = cbuf_new();
cbuf_append_str(val_tmp, cbuf_get(val)); cbuf_append_str(val_tmp, cbuf_get(val));
int idx = cbuf_len(val_tmp); int idx = cbuf_len(val_tmp);
for (int l = cbuf_len(val_tmp); l>= 0; l--) { for (int l = cbuf_len(val_tmp) - 1; l>= 0; l--) {
if (cbuf_get(val_tmp)[l] == '/') { if (cbuf_get(val_tmp)[l] == '/') {
idx = l; idx = l;
break; break;
} }
} }
cbuf* val_tmp_2 = cbuf_trunc(val_tmp, idx + 1); if (idx == cbuf_len(val_tmp)) // Didn't find a slash in the loop above
if (val_tmp_2 == NULL)
return NULL; return NULL;
cbuf* val_tmp_2 = cbuf_trunc(val_tmp, idx + 1);
if (cbuf_append_str(cb, cbuf_get(val_tmp_2)) < 0) if (cbuf_append_str(cb, cbuf_get(val_tmp_2)) < 0)
return NULL; return NULL;
cbuf_free(val_tmp); cbuf_free(val_tmp);