1. Andreas Tscharner
  2. lrc

Commits

Andreas Tscharner  committed d66947f

replace_extension: Make new extension const and fix a bug with a terminating null byte

  • Participants
  • Parent commits 4beb21f
  • Branches default

Comments (0)

Files changed (2)

File src/Utils.cxx

View file
 //      Utils.cxx
 //
-//      Copyright 2011 Andreas Tscharner <andy@vis.ethz.ch>
+//      Copyright 2011, 2014 Andreas Tscharner <andy@vis.ethz.ch>
 //
 //      This program is free software; you can redistribute it and/or modify
 //      it under the terms of the GNU Lesser General Public License as
 	return strrchr(p_filename, '.');
 };
 
-char *replace_extension(char *p_filename, char *p_newExt)
+char *replace_extension(char *p_filename, const char *p_newExt)
 {
-	char *retStr = NULL;
-	char *tmpStr = NULL;
+	char *retStr = nullptr;
+	char *tmpStr = nullptr;
 	char *oldExt;
 	size_t sLen = 0;
 	size_t tLen = 0;
 
 
-	DEBUG_PRINT(("Filename: %s; new extension: %s\n", p_filename, p_newExt))
-
 	oldExt = strrchr(p_filename, '.');
 	if (!oldExt) {
 		sLen = strlen(p_filename) + strlen(p_newExt) + 1;
 
 	sLen = strlen(p_filename) - strlen(oldExt) + strlen(p_newExt) + 1;
 	retStr = new char[sLen];
-	tLen = strlen(p_filename) - strlen(oldExt);
+
+	tLen = strlen(p_filename) - strlen(oldExt) + 1;
 	tmpStr = new char[tLen];
-	strncpy(tmpStr, p_filename, tLen);
+	strncpy(tmpStr, p_filename, tLen-1);
+	tmpStr[tLen-1] = '\0';
+
 	snprintf(retStr, sLen, "%s%s", tmpStr, p_newExt);
 	delete[] tmpStr;
 
-	DEBUG_PRINT(("Replaced old extension with new one: %s\n", retStr))
+	DEBUG_PRINT(("Replaced old extension (%s) with new one: %s\n", oldExt, retStr))
 	return retStr;
 };
 

File src/Utils.hxx

View file
 //      Utils.hxx
 //
-//      Copyright 2011 Andreas Tscharner <andy@vis.ethz.ch>
+//      Copyright 2011, 2014 Andreas Tscharner <andy@vis.ethz.ch>
 //
 //      This program is free software; you can redistribute it and/or modify
 //      it under the terms of the GNU Lesser General Public License as
  * used in the whole project
  *
  * \author Andreas Tscharner
- * \date 2011-09-11
+ * \date 2014-08-22
  */
 
 
  * \remarks The caller is responsible to free the allocated memory of the
  * returned string
  */
-char *replace_extension(char *, char *);
+char *replace_extension(char *, const char *);
 
 /*! \brief Delete list of data
  *