From b5eaa633efab06cca2a098d5abe5a5cb3d5d6498 Mon Sep 17 00:00:00 2001 From: kevin Date: Wed, 6 Dec 2023 15:22:41 -0500 Subject: [PATCH] cwText,cwPianoScore : Added textCopy() --- cwPianoScore.cpp | 17 +++++++++++------ cwScoreParse.cpp | 2 +- cwText.cpp | 24 ++++++++++++++++++++++++ cwText.h | 7 +++++++ 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/cwPianoScore.cpp b/cwPianoScore.cpp index f2d05fb..70174d4 100644 --- a/cwPianoScore.cpp +++ b/cwPianoScore.cpp @@ -167,7 +167,7 @@ namespace cw goto errLabel; } - strncpy(e->sci_pitch,sci_pitch,sizeof(e->sci_pitch)); + strncpy(e->sci_pitch,sci_pitch,sizeof(e->sci_pitch)-1); if( p->end == nullptr ) { @@ -570,16 +570,21 @@ namespace cw goto errLabel; } - + + textCopy( e->sci_pitch,sizeof(e->sci_pitch),sci_pitch); + textCopy( e->dmark,sizeof(e->dmark),dmark); + textCopy( e->grace_mark, sizeof(e->grace_mark),grace_mark); + /* if( sci_pitch != nullptr ) - strncpy(e->sci_pitch,sci_pitch,sizeof(e->sci_pitch)); + strncpy(e->sci_pitch,sci_pitch,sizeof(e->sci_pitch)-1); if( dmark != nullptr ) - strncpy(e->dmark,dmark,sizeof(e->dmark)); + strncpy(e->dmark,dmark,sizeof(e->dmark)-1); if( grace_mark != nullptr ) - strncpy(e->grace_mark,grace_mark,sizeof(e->grace_mark)); - + strncpy(e->grace_mark,grace_mark,sizeof(e->grace_mark)-1); + */ + // assign the UID e->uid = i; diff --git a/cwScoreParse.cpp b/cwScoreParse.cpp index 0ea2c51..bedfa8d 100644 --- a/cwScoreParse.cpp +++ b/cwScoreParse.cpp @@ -263,7 +263,7 @@ namespace cw rc_t _parse_note_on_row( score_parse_t* p, csv::handle_t csvH, event_t* e ) { - rc_t rc,rc0,rc1,rc2; + rc_t rc,rc0=kOkRC,rc1=kOkRC,rc2=kOkRC; const char* sciPitch = nullptr; const char* dmark = nullptr; const char* graceLabel = nullptr; diff --git a/cwText.cpp b/cwText.cpp index f8d0007..6e12f90 100644 --- a/cwText.cpp +++ b/cwText.cpp @@ -54,6 +54,30 @@ namespace cw unsigned cw::textLength( const char* s ) { return s == nullptr ? 0 : strlen(s); } +const char* cw::textCopy( char* dst, unsigned dstN, const char* src, unsigned srcN ) +{ + if( dst == nullptr || dstN == 0 ) + return nullptr; + + if( srcN == 0 ) + srcN = textLength(src); + + if( src == nullptr || srcN==0 || dstN==1 ) + { + dst[0] = 0; + } + else + { + + assert( dstN >= 2 ); + unsigned n = std::min( dstN-1, srcN ); + memcpy(dst,src,n); + dst[n] = 0; + } + return dst; +} + + void textToLower( char* s ) { if( s != nullptr ) diff --git a/cwText.h b/cwText.h index aff115f..7af14dc 100644 --- a/cwText.h +++ b/cwText.h @@ -7,6 +7,13 @@ namespace cw // Return 0 if s is null. unsigned textLength( const char* s ); + // If dst is non-null then dst is always 0-terminated. + // If src will be truncated if srcN > dstN-1. + // If dst is null then null is returned + // if src is null then dst[0] = 0. + // if srcN is 0 then textLength(src) is used for srcN + const char* textCopy( char* dst, unsigned dstN, const char* src, unsigned srcN=0 ); + void textToLower( char* s ); void textToUpper( char* s );