+/* Format Field for output */
+int field_add_string(char *dest, int size, const char *format, const char *value ) {
+ char buffer[OS_SIZE_2048];
+ int len = 0;
+ int dest_sz = size - strnlen(dest, OS_SIZE_2048);
+
+ if(dest_sz <= 0 ) {
+ // Not enough room in the buffer
+ return -1;
+ }
+
+ if(value != NULL &&
+ (
+ ((value[0] != '(') && (value[1] != 'n') && (value[2] != 'o')) ||
+ ((value[0] != '(') && (value[1] != 'u') && (value[2] != 'n')) ||
+ ((value[0] != 'u') && (value[1] != 'n') && (value[4] != 'k'))
+ )
+ ) {
+ len = snprintf(buffer, sizeof(buffer) - dest_sz - 1, format, value);
+ strncat(dest, buffer, dest_sz);
+ }
+
+ return len;
+}
+
+/* Add a field, but truncate if too long */
+int field_add_truncated(char *dest, int size, const char *format, const char *value, int fmt_size ) {
+ char buffer[OS_SIZE_2048];
+
+ int available_sz = size - strnlen(dest, OS_SIZE_2048);
+ int total_sz = strlen(value) + strlen(format) - fmt_size;
+ int field_sz = available_sz - strlen(format) + fmt_size;
+
+ int len = 0;
+ char trailer[] = "...";
+ char *truncated;
+
+ if(available_sz <= 0 ) {
+ // Not enough room in the buffer
+ return -1;
+ }
+
+ if(value != NULL &&
+ (
+ ((value[0] != '(') && (value[1] != 'n') && (value[2] != 'o')) ||
+ ((value[0] != '(') && (value[1] != 'u') && (value[2] != 'n')) ||
+ ((value[0] != 'u') && (value[1] != 'n') && (value[4] != 'k'))
+ )
+ ) {
+
+ if( (truncated=malloc(field_sz + 1)) != NULL ) {
+ if( total_sz > available_sz ) {
+ // Truncate and add a trailer
+ os_substr(truncated, value, 0, field_sz - strlen(trailer));
+ strcat(truncated, trailer);
+ }
+ else {
+ strncpy(truncated,value,field_sz);
+ }
+
+ len = snprintf(buffer, available_sz, format, truncated);
+ strncat(dest, buffer, available_sz);
+ }
+ else {
+ // Memory Error
+ len = -3;
+ }
+ }
+ // Free the temporary pointer
+ free(truncated);
+
+ return len;
+}
+
+/* Handle integers in the second position */
+int field_add_int(char *dest, int size, const char *format, const int value ) {
+ char buffer[255];
+ int len = 0;
+ int dest_sz = size - strnlen(dest, OS_SIZE_2048);
+
+ if(dest_sz <= 0 ) {
+ // Not enough room in the buffer
+ return -1;
+ }
+
+ if( value > 0 ) {
+ len = snprintf(buffer, sizeof(buffer), format, value);
+ strncat(dest, buffer, dest_sz);
+ }
+
+ return len;
+}