Move function strncpy_crlf() to src/common.c so the function can be tested in isolation.

This commit is contained in:
Erik de Castro Lopo 2010-08-22 20:36:37 +10:00
parent 1977dfa133
commit c67942ea42
4 changed files with 56 additions and 48 deletions

View File

@ -1,3 +1,9 @@
2010-08-22 Erik de Castro Lopo <erikd AT mega-nerd DOT com>
* src/broadcast.c src/common.(c|h)
Move function strncpy_crlf() to src/common.c so the function can be tested
in isolation.
2010-08-18 Erik de Castro Lopo <erikd AT mega-nerd DOT com>
* src/common.h

View File

@ -25,7 +25,10 @@
#include "common.h"
static void strncpy_crlf (char *dest, const char *src, size_t destmax, size_t srcmax) ;
#define CODE_HIST_OFFSET offsetof (SF_BROADCAST_INFO, coding_history)
#define DEBUG_CHS 0
static int gen_coding_history (char * added_history, int added_history_max, const SF_INFO * psfinfo) ;
static inline size_t
@ -34,7 +37,7 @@ bc_min_size (const SF_BROADCAST_INFO* info)
return 0 ;
return offsetof (SF_BROADCAST_INFO, coding_history) + info->coding_history_size ;
} /* broadcast_size */
} /* bc_min_size */
static inline size_t
@ -53,8 +56,6 @@ broadcast_var_alloc (size_t datasize)
} /* broadcast_var_alloc */
#define DEBUG_CHS 0
static size_t
var_coding_history_size (const PSF_BROADCAST_VAR *pvar)
{
@ -79,15 +80,14 @@ broadcast_var_set (SF_PRIVATE *psf, const SF_BROADCAST_INFO * info, size_t datas
return SF_FALSE ;
} ;
if (DEBUG_CHS) puts ("\n") ;
added_history_len = gen_coding_history (added_history, sizeof (added_history), &(psf->sf)) ;
if (psf->broadcast_var != NULL)
{ size_t coding_hist_offset = offsetof (SF_BROADCAST_INFO, coding_history) ;
if (psf->broadcast_var->binfo.coding_history_size + added_history_len < datasize - coding_hist_offset)
{ free (psf->broadcast_var) ;
psf->broadcast_var = NULL ;
} ;
if (psf->broadcast_var != NULL
&& psf->broadcast_var->binfo.coding_history_size + added_history_len < datasize - CODE_HIST_OFFSET)
{ free (psf->broadcast_var) ;
psf->broadcast_var = NULL ;
} ;
if (psf->broadcast_var == NULL)
@ -102,7 +102,7 @@ broadcast_var_set (SF_PRIVATE *psf, const SF_BROADCAST_INFO * info, size_t datas
memcpy (&(psf->broadcast_var->binfo), info, offsetof (SF_BROADCAST_INFO, coding_history)) ;
strncpy_crlf (psf->broadcast_var->binfo.coding_history, info->coding_history, bc_var_coding_hist_size (psf->broadcast_var), info->coding_history_size) ;
psf_strncpy_crlf (psf->broadcast_var->binfo.coding_history, info->coding_history, bc_var_coding_hist_size (psf->broadcast_var), info->coding_history_size) ;
len = strlen (psf->broadcast_var->binfo.coding_history) ;
if (DEBUG_CHS)
@ -143,43 +143,8 @@ broadcast_var_get (SF_PRIVATE *psf, SF_BROADCAST_INFO * data, size_t datasize)
} /* broadcast_var_get */
/*------------------------------------------------------------------------------
** Strncpy which converts all line endings to CR/LF.
*/
static void
strncpy_crlf (char *dest, const char *src, size_t destmax, size_t srcmax)
{ char * destend = dest + destmax - 1 ;
const char * srcend = src + srcmax ;
while (dest < destend && src < srcend)
{ if ((src [0] == '\r' && src [1] == '\n') || (src [0] == '\n' && src [1] == '\r'))
{ *dest++ = '\r' ;
*dest++ = '\n' ;
src += 2 ;
continue ;
} ;
if (src [0] == '\r')
{ *dest++ = '\r' ;
*dest++ = '\n' ;
src += 1 ;
continue ;
} ;
if (src [0] == '\n')
{ *dest++ = '\r' ;
*dest++ = '\n' ;
src += 1 ;
continue ;
} ;
*dest++ = *src++ ;
} ;
/* Make sure dest is terminated. */
*dest = 0 ;
} /* strncpy_crlf */
static int
gen_coding_history (char * added_history, int added_history_max, const SF_INFO * psfinfo)
{ char chnstr [16] ;

View File

@ -1,5 +1,5 @@
/*
** Copyright (C) 1999-2009 Erik de Castro Lopo <erikd@mega-nerd.com>
** Copyright (C) 1999-2010 Erik de Castro Lopo <erikd@mega-nerd.com>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
@ -1329,6 +1329,42 @@ append_snprintf (char * dest, size_t maxlen, const char * fmt, ...)
return ;
} /* append_snprintf */
void
psf_strncpy_crlf (char *dest, const char *src, size_t destmax, size_t srcmax)
{ /* Must be minus 2 so it can still expand a single trailing '\n' or '\r'. */
char * destend = dest + destmax - 2 ;
const char * srcend = src + srcmax ;
while (dest < destend && src < srcend)
{ if ((src [0] == '\r' && src [1] == '\n') || (src [0] == '\n' && src [1] == '\r'))
{ *dest++ = '\r' ;
*dest++ = '\n' ;
src += 2 ;
continue ;
} ;
if (src [0] == '\r')
{ *dest++ = '\r' ;
*dest++ = '\n' ;
src += 1 ;
continue ;
} ;
if (src [0] == '\n')
{ *dest++ = '\r' ;
*dest++ = '\n' ;
src += 1 ;
continue ;
} ;
*dest++ = *src++ ;
} ;
/* Make sure dest is terminated. */
*dest = 0 ;
} /* psf_strncpy_crlf */
/*==============================================================================
*/

View File

@ -663,6 +663,7 @@ void psf_log_SF_INFO (SF_PRIVATE *psf) ;
int32_t psf_rand_int32 (void) ;
void append_snprintf (char * dest, size_t maxlen, const char * fmt, ...) ;
void psf_strncpy_crlf (char *dest, const char *src, size_t destmax, size_t srcmax) ;
/* Functions used when writing file headers. */