Indentation of utility.c

This commit is contained in:
Anshul Maheshwari 2015-05-18 19:10:48 +05:30
parent 88015d6d4b
commit d54a2d9486

View File

@ -3,7 +3,7 @@
#include "ccx_common_option.h" #include "ccx_common_option.h"
static char *text; static char *text;
static int text_size=0; static int text_size = 0;
int temp_debug = 0; // This is a convenience variable used to enable/disable debug on variable conditions. Find references to understand. int temp_debug = 0; // This is a convenience variable used to enable/disable debug on variable conditions. Find references to understand.
@ -17,31 +17,34 @@ void timestamp_to_srttime(uint64_t timestamp, char *buffer) {
} }
void timestamp_to_smptetttime(uint64_t timestamp, char *buffer) { void timestamp_to_smptetttime(uint64_t timestamp, char *buffer) {
uint64_t p = timestamp; uint64_t p = timestamp;
uint8_t h = (uint8_t) (p / 3600000); uint8_t h = (uint8_t) (p / 3600000);
uint8_t m = (uint8_t) (p / 60000 - 60 * h); uint8_t m = (uint8_t) (p / 60000 - 60 * h);
uint8_t s = (uint8_t) (p / 1000 - 3600 * h - 60 * m); uint8_t s = (uint8_t) (p / 1000 - 3600 * h - 60 * m);
uint16_t u = (uint16_t) (p - 3600000 * h - 60000 * m - 1000 * s); uint16_t u = (uint16_t) (p - 3600000 * h - 60000 * m - 1000 * s);
sprintf(buffer, "%02"PRIu8":%02"PRIu8":%02"PRIu8".%03"PRIu16, h, m, s, u); sprintf(buffer, "%02"PRIu8":%02"PRIu8":%02"PRIu8".%03"PRIu16, h, m, s, u);
} }
#define MIN3(a, b, c) ((a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c))) #define MIN3(a, b, c) ((a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c)))
int levenshtein_dist (const uint64_t *s1, const uint64_t *s2, unsigned s1len, unsigned s2len) { int levenshtein_dist (const uint64_t *s1, const uint64_t *s2, unsigned s1len, unsigned s2len)
unsigned int x, y, v, lastdiag, olddiag; {
unsigned int *column=(unsigned *) malloc ((s1len+1)*sizeof (unsigned int)); unsigned int x, y, v, lastdiag, olddiag;
for (y = 1; y <= s1len; y++) unsigned int *column = (unsigned *) malloc ((s1len+1)*sizeof (unsigned int));
column[y] = y; for (y = 1; y <= s1len; y++)
for (x = 1; x <= s2len; x++) { column[y] = y;
column[0] = x; for (x = 1; x <= s2len; x++)
for (y = 1, lastdiag = x-1; y <= s1len; y++) { {
olddiag = column[y]; column[0] = x;
column[y] = MIN3(column[y] + 1, column[y-1] + 1, lastdiag + (s1[y-1] == s2[x-1] ? 0 : 1)); for (y = 1, lastdiag = x-1; y <= s1len; y++)
lastdiag = olddiag; {
} olddiag = column[y];
} column[y] = MIN3(column[y] + 1, column[y-1] + 1, lastdiag + (s1[y-1] == s2[x-1] ? 0 : 1));
lastdiag = olddiag;
}
}
v=column[s1len]; v=column[s1len];
free (column); free (column);
return v; return v;
} }
@ -67,14 +70,14 @@ void millis_to_date (uint64_t timestamp, char *buffer, enum ccx_output_date_form
secs=(time_t) (timestamp/1000); secs=(time_t) (timestamp/1000);
millis=(time_t) (timestamp%1000); millis=(time_t) (timestamp%1000);
sprintf (buffer, "%lu%c%03u", (unsigned long) secs, sprintf (buffer, "%lu%c%03u", (unsigned long) secs,
millis_separator,(unsigned) millis); millis_separator, (unsigned) millis);
break; break;
case ODF_DATE: case ODF_DATE:
secs=(time_t) (timestamp/1000); secs=(time_t) (timestamp/1000);
millis=(unsigned int) (timestamp%1000); millis=(unsigned int) (timestamp%1000);
time_struct = gmtime(&secs); time_struct = gmtime(&secs);
strftime(c_temp, sizeof(c_temp), "%Y%m%d%H%M%S", time_struct); strftime(c_temp, sizeof(c_temp), "%Y%m%d%H%M%S", time_struct);
sprintf (buffer,"%s%c%03u", c_temp, millis_separator, millis); sprintf (buffer, "%s%c%03u", c_temp, millis_separator, millis);
break; break;
default: default:
@ -95,7 +98,7 @@ bool_t in_array(uint16_t *array, uint16_t length, uint16_t element) {
/* Alloc text space */ /* Alloc text space */
void resize_text() void resize_text()
{ {
text_size=(!text_size)?1024:text_size*2; text_size = (!text_size)?1024:text_size*2;
if (text) if (text)
free (text); free (text);
text=(char *) malloc (text_size); text=(char *) malloc (text_size);
@ -107,27 +110,27 @@ void resize_text()
/* Write formatted message to stderr and then exit. */ /* Write formatted message to stderr and then exit. */
void fatal(int exit_code, const char *fmt, ...) void fatal(int exit_code, const char *fmt, ...)
{ {
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
if (ccx_options.gui_mode_reports) if (ccx_options.gui_mode_reports)
fprintf(stderr,"###MESSAGE#"); fprintf(stderr,"###MESSAGE#");
else else
fprintf(stderr, "\rError: "); fprintf(stderr, "\rError: ");
vfprintf(stderr, fmt, args); vfprintf(stderr, fmt, args);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
va_end(args); va_end(args);
exit(exit_code); exit(exit_code);
} }
/* General output, replacement for printf so we can control globally where messages go. /* General output, replacement for printf so we can control globally where messages go.
mprint => Message print */ mprint => Message print */
void mprint (const char *fmt, ...) void mprint (const char *fmt, ...)
{ {
va_list args; va_list args;
if (!ccx_options.messages_target) if (!ccx_options.messages_target)
return; return;
activity_header(); // Brag about writing it :-) activity_header(); // Brag about writing it :-)
va_start(args, fmt); va_start(args, fmt);
if (ccx_options.messages_target==CCX_MESSAGES_STDOUT) if (ccx_options.messages_target==CCX_MESSAGES_STDOUT)
{ {
vfprintf(stdout, fmt, args); vfprintf(stdout, fmt, args);
@ -138,7 +141,7 @@ void mprint (const char *fmt, ...)
vfprintf(stderr, fmt, args); vfprintf(stderr, fmt, args);
fflush (stderr); fflush (stderr);
} }
va_end(args); va_end(args);
} }
/* Shorten some debug output code. */ /* Shorten some debug output code. */
@ -148,11 +151,11 @@ void dbg_print(LLONG mask, const char *fmt, ...)
LLONG t; LLONG t;
if (!ccx_options.messages_target) if (!ccx_options.messages_target)
return; return;
t=temp_debug ? (ccx_options.debug_mask_on_debug | ccx_options.debug_mask) : ccx_options.debug_mask; // Mask override? t = temp_debug ? (ccx_options.debug_mask_on_debug | ccx_options.debug_mask) : ccx_options.debug_mask; // Mask override?
if(mask & t) if(mask & t)
{ {
va_start(args, fmt); va_start(args, fmt);
if (ccx_options.messages_target==CCX_MESSAGES_STDOUT) if (ccx_options.messages_target==CCX_MESSAGES_STDOUT)
{ {
vfprintf(stdout, fmt, args); vfprintf(stdout, fmt, args);
@ -171,13 +174,13 @@ void dbg_print(LLONG mask, const char *fmt, ...)
/* Shorten some debug output code. */ /* Shorten some debug output code. */
void dvprint(const char *fmt, ...) void dvprint(const char *fmt, ...)
{ {
va_list args; va_list args;
if (!ccx_options.messages_target) if (!ccx_options.messages_target)
return; return;
if(! (ccx_options.debug_mask & CCX_DMT_VIDES )) if(! (ccx_options.debug_mask & CCX_DMT_VIDES ))
return; return;
va_start(args, fmt); va_start(args, fmt);
if (ccx_options.messages_target==CCX_MESSAGES_STDOUT) if (ccx_options.messages_target==CCX_MESSAGES_STDOUT)
{ {
vfprintf(stdout, fmt, args); vfprintf(stdout, fmt, args);
@ -188,44 +191,44 @@ void dvprint(const char *fmt, ...)
vfprintf(stderr, fmt, args); vfprintf(stderr, fmt, args);
fflush (stderr); fflush (stderr);
} }
va_end(args); va_end(args);
} }
void dump (LLONG mask, unsigned char *start, int l, unsigned long abs_start, unsigned clear_high_bit) void dump (LLONG mask, unsigned char *start, int l, unsigned long abs_start, unsigned clear_high_bit)
{ {
LLONG t=temp_debug ? (ccx_options.debug_mask_on_debug | ccx_options.debug_mask) : ccx_options.debug_mask; // Mask override? LLONG t=temp_debug ? (ccx_options.debug_mask_on_debug | ccx_options.debug_mask) : ccx_options.debug_mask; // Mask override?
if(! (mask & t)) if(! (mask & t))
return; return;
for (int x=0; x<l; x=x+16) for (int x=0; x<l; x=x+16)
{ {
mprint ("%08ld | ",x+abs_start); mprint ("%08ld | ",x+abs_start);
for (int j=0; j<16; j++) for (int j=0; j<16; j++)
{ {
if (x+j<l) if (x+j<l)
mprint ("%02X ",start[x+j]); mprint ("%02X ",start[x+j]);
else else
mprint (" "); mprint (" ");
} }
mprint (" | "); mprint (" | ");
for (int j=0; j<16; j++) for (int j=0; j<16; j++)
{ {
if (x+j<l && start[x+j]>=' ') if (x+j<l && start[x+j]>=' ')
mprint ("%c",start[x+j] & (clear_high_bit?0x7F:0xFF)); // 0x7F < remove high bit, convenient for visual CC inspection mprint ("%c", start[x+j] & (clear_high_bit ? 0x7F : 0xFF)); // 0x7F < remove high bit, convenient for visual CC inspection
else else
mprint (" "); mprint (" ");
} }
mprint ("\n"); mprint ("\n");
} }
} }
void init_boundary_time (struct ccx_boundary_time *bt) void init_boundary_time (struct ccx_boundary_time *bt)
{ {
bt->hh=0; bt->hh = 0;
bt->mm=0; bt->mm = 0;
bt->ss=0; bt->ss = 0;
bt->set=0; bt->set = 0;
bt->time_in_ms=0; bt->time_in_ms = 0;
} }
@ -241,19 +244,19 @@ void sleep_secs (int secs)
int hex2int (char high, char low) int hex2int (char high, char low)
{ {
unsigned char h,l; unsigned char h,l;
if (high>='0' && high<='9') if (high >= '0' && high <= '9')
h=high-'0'; h = high-'0';
else if (high>='a' && high<='f') else if (high >= 'a' && high <= 'f')
h=high-'a'+10; h = high-'a'+10;
else else
return -1; return -1;
if (low>='0' && low<='9') if (low >= '0' && low <= '9')
l=low-'0'; l = low - '0';
else if (low>='a' && low<='f') else if (low >= 'a' && low <= 'f')
l=low-'a'+10; l = low - 'a'+10;
else else
return -1; return -1;
return h*16+l; return h * 16+l;
} }
#ifndef _WIN32 #ifndef _WIN32