diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/strlib.c | 20 | ||||
-rw-r--r-- | src/common/strlib.h | 4 |
2 files changed, 24 insertions, 0 deletions
diff --git a/src/common/strlib.c b/src/common/strlib.c index c0a6238f9..d0b312f0a 100644 --- a/src/common/strlib.c +++ b/src/common/strlib.c @@ -316,6 +316,26 @@ size_t safestrnlen(const char* string, size_t maxlen) return ( string != NULL ) ? strnlen(string, maxlen) : 0; } +/// Returns the line of the target position in the string. +/// Lines start at 1. +int strline(const char* str, size_t pos) +{ + const char* target; + int line; + + if( str == NULL || pos == 0 ) + return 1; + + target = str+pos; + for( line = 1; ; ++line ) + { + str = strchr(str, '\n'); + if( str == NULL || target <= str ) + break;// found target line + ++str;// skip newline + } + return line; +} ///////////////////////////////////////////////////////////////////// // StringBuf - dynamic string diff --git a/src/common/strlib.h b/src/common/strlib.h index fd24b4b24..4df5a51d2 100644 --- a/src/common/strlib.h +++ b/src/common/strlib.h @@ -37,6 +37,10 @@ char* safestrncpy(char* dst, const char* src, size_t n); /// doesn't crash on null pointer size_t safestrnlen(const char* string, size_t maxlen); +/// Returns the line of the target position in the string. +/// Lines start at 1. +int strline(const char* str, size_t pos); + /// StringBuf - dynamic string struct StringBuf { |