[PATCH 1/3] Return error message from ScreenSnapShot_SaveToFile()

[ Thread Index | Date Index | More lists.tuxfamily.org/hatari-devel Archives ]


Used for better error output control in next commit.
---
 src/debug/debugui.c           |  6 +++-
 src/includes/screenSnapShot.h |  2 +-
 src/screenSnapShot.c          | 53 ++++++++++++++++++++++++-----------
 3 files changed, 43 insertions(+), 18 deletions(-)

diff --git a/src/debug/debugui.c b/src/debug/debugui.c
index 6076ab07..5e746b34 100644
--- a/src/debug/debugui.c
+++ b/src/debug/debugui.c
@@ -387,7 +387,11 @@ static int DebugUI_SetOptions(int argc, char *argv[])
 static int DebugUI_Screenshot(int argc, char *argv[])
 {
 	if (argc == 2)
-		ScreenSnapShot_SaveToFile(argv[1]);
+	{
+		const char *errmsg = ScreenSnapShot_SaveToFile(argv[1]);
+		if (errmsg)
+			fprintf(stderr, "ERROR: %s!\n", errmsg);
+	}
 	else
 		return DebugUI_PrintCmdHelp(argv[0]);
 	return DEBUGGER_CMDDONE;
diff --git a/src/includes/screenSnapShot.h b/src/includes/screenSnapShot.h
index 5727759e..e29b11ea 100644
--- a/src/includes/screenSnapShot.h
+++ b/src/includes/screenSnapShot.h
@@ -14,6 +14,6 @@ extern int ScreenSnapShot_SavePNG_ToFile(SDL_Surface *surface, int destw,
 		int desth, FILE *fp, int png_compression_level, int png_filter,
 		int CropLeft , int CropRight , int CropTop , int CropBottom );
 extern void ScreenSnapShot_SaveScreen(void);
-extern void ScreenSnapShot_SaveToFile(const char *filename);
+extern const char* ScreenSnapShot_SaveToFile(const char *filename);
 
 #endif /* ifndef HATARI_SCREENSNAPSHOT_H */
diff --git a/src/screenSnapShot.c b/src/screenSnapShot.c
index 60023030..8b7bcfc2 100644
--- a/src/screenSnapShot.c
+++ b/src/screenSnapShot.c
@@ -257,33 +257,54 @@ void ScreenSnapShot_SaveScreen(void)
 }
 
 /**
- * Save screen shot to given file.
+ * Save screen shot to given file.  File name extension must be one
+ * of the supported image extensions, either .png or .bmp.
+ *
+ * Unless given file name path starts from root, it's prefixed with
+ * Hatari's screenshot directory path.
+ *
+ * Returns error message on failure, NULL on success.
  */
-void ScreenSnapShot_SaveToFile(const char *szFileName)
+const char* ScreenSnapShot_SaveToFile(const char *name)
 {
-	bool success = false;
+	const char *errmsg;
+	char *szFileName;
+	bool success;
 
-	if (!szFileName)
+	if (!name)
 	{
-		fprintf(stderr, "ERROR: no screen dump file name specified\n");
-		return;
+		return "no screen dump file name specified";
 	}
-#if HAVE_LIBPNG
-	if (File_DoesFileExtensionMatch(szFileName, ".png"))
+	if (name[0] != PATHSEP)
 	{
-		success = ScreenSnapShot_SavePNG(sdlscrn, szFileName) > 0;
+		const char *path = Paths_GetScreenShotDir();
+		szFileName = malloc(strlen(path) + 1 + strlen(name) + 1);
+		if (!szFileName)
+			return "screen dump file name alloc failed";
+		sprintf(szFileName, "%s%c%s", path, PATHSEP, name);
 	}
 	else
+		szFileName = strdup(name);
+
+	errmsg = NULL;
+	success = false;
+#if HAVE_LIBPNG
+	if (File_DoesFileExtensionMatch(name, ".png"))
+		success = ScreenSnapShot_SavePNG(sdlscrn, szFileName) > 0;
+	else
 #endif
-	if (File_DoesFileExtensionMatch(szFileName, ".bmp"))
-	{
+	if (File_DoesFileExtensionMatch(name, ".bmp"))
 		success = SDL_SaveBMP(sdlscrn, szFileName) == 0;
-	}
 	else
+		errmsg = "unrecognized screen dump file name extension";
+
+	if (success)
+		Log_Printf(LOG_INFO, "screen dump saved to: %s\n", szFileName);
+	else if (!errmsg)
 	{
-		fprintf(stderr, "ERROR: unknown screen dump file name extension: %s\n", szFileName);
-		return;
+		Log_Printf(LOG_WARN, "failed saving screen dump to: %s\n", szFileName);
+		errmsg = "screen dump saving failed";
 	}
-	fprintf(stderr, "Screen dump to '%s' %s\n", szFileName,
-		success ? "succeeded" : "failed");
+	free(szFileName);
+	return errmsg;
 }
-- 
2.20.1


--------------6BD8094804F7DEED823E40A9--



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/