You've already forked pgbackrest
							
							
				mirror of
				https://github.com/pgbackrest/pgbackrest.git
				synced 2025-10-30 23:37:45 +02:00 
			
		
		
		
	Add strSizeFormat() to String object.
Converts sizes in bytes to a more human-readable form, .e.g. 1KB, 1.1GB. Contributed by Cynthia Shang.
This commit is contained in:
		
				
					committed by
					
						 David Steele
						David Steele
					
				
			
			
				
	
			
			
			
						parent
						
							947fa6fc2b
						
					
				
				
					commit
					80a3e21521
				
			| @@ -107,6 +107,14 @@ | ||||
|                         <p>Improve error message when info files are missing/corrupt.</p> | ||||
|                     </release-item> | ||||
|  | ||||
|                     <release-item> | ||||
|                         <release-item-contributor-list> | ||||
|                             <release-item-contributor id="cynthia.shang"/> | ||||
|                         </release-item-contributor-list> | ||||
|  | ||||
|                         <p>Add <code>strSizeFormat()</code> to <code>String</code> object.</p> | ||||
|                     </release-item> | ||||
|  | ||||
|                     <release-item> | ||||
|                         <p>Rename <code>PGBACKREST</code>/<code>BACKREST</code> constants to <code>PROJECT</code>.</p> | ||||
|                     </release-item> | ||||
|   | ||||
| @@ -795,6 +795,46 @@ strToLog(const String *this) | ||||
|     return strNewFmt("{\"%s\"}", strPtr(this)); | ||||
| } | ||||
|  | ||||
| /*********************************************************************************************************************************** | ||||
| Format sizes (file, buffer, etc.) in human-readable form | ||||
| ***********************************************************************************************************************************/ | ||||
| String * | ||||
| strSizeFormat(const uint64_t size) | ||||
| { | ||||
|     FUNCTION_TEST_BEGIN(); | ||||
|         FUNCTION_TEST_PARAM(UINT64, size); | ||||
|  | ||||
|     FUNCTION_TEST_END(); | ||||
|  | ||||
|     String *result = NULL; | ||||
|  | ||||
|     if (size < 1024) | ||||
|         result = strNewFmt("%" PRIu64 "B", size); | ||||
|     else if (size < (1024 * 1024)) | ||||
|     { | ||||
|         if ((int)((double)size / 102.4) % 10 != 0) | ||||
|             result = strNewFmt("%.1fKB", ((double)size / 102.4) / 10); | ||||
|         else | ||||
|             result = strNewFmt("%dKB", (int)((double)size / 102.4) / 10); | ||||
|     } | ||||
|     else if (size < (1024 * 1024 * 1024)) | ||||
|     { | ||||
|         if ((int)((double)size / 1024 / 102.4) % 10 != 0) | ||||
|             result = strNewFmt("%.1fMB", ((double)size / 1024 / 102.4) / 10); | ||||
|         else | ||||
|             result = strNewFmt("%dMB", (int)((double)size / 1024 / 102.4) / 10); | ||||
|     } | ||||
|     else | ||||
|     { | ||||
|         if ((int)((double)size / 1024 / 1024 / 102.4) % 10 != 0) | ||||
|             result = strNewFmt("%.1fGB", ((double)size / 1024 / 1024 / 102.4) / 10); | ||||
|         else | ||||
|             result = strNewFmt("%dGB", (int)((double)size / 1024 / 1024 / 102.4) / 10); | ||||
|     } | ||||
|  | ||||
|     FUNCTION_TEST_RESULT(STRING, result); | ||||
| } | ||||
|  | ||||
| /*********************************************************************************************************************************** | ||||
| Free the string | ||||
| ***********************************************************************************************************************************/ | ||||
|   | ||||
| @@ -18,6 +18,8 @@ old context and then back. Below is a simplified example: | ||||
| #ifndef COMMON_TYPE_STRING_H | ||||
| #define COMMON_TYPE_STRING_H | ||||
|  | ||||
| #include <stdint.h> | ||||
|  | ||||
| /*********************************************************************************************************************************** | ||||
| String object | ||||
| ***********************************************************************************************************************************/ | ||||
| @@ -62,6 +64,7 @@ String *strSubN(const String *this, size_t start, size_t size); | ||||
| String *strTrim(String *this); | ||||
| int strChr(const String *this, char chr); | ||||
| String *strTrunc(String *this, int idx); | ||||
| String *strSizeFormat(const uint64_t fileSize); | ||||
|  | ||||
| void strFree(String *this); | ||||
|  | ||||
|   | ||||
| @@ -155,7 +155,7 @@ unit: | ||||
|  | ||||
|       # ---------------------------------------------------------------------------------------------------------------------------- | ||||
|       - name: type-string | ||||
|         total: 15 | ||||
|         total: 16 | ||||
|  | ||||
|         coverage: | ||||
|           common/type/string: full | ||||
|   | ||||
| @@ -210,5 +210,18 @@ testRun(void) | ||||
|         TEST_RESULT_STR(strPtr(strToLog(strNew("test"))), "{\"test\"}", "format string"); | ||||
|     } | ||||
|  | ||||
|     // ***************************************************************************************************************************** | ||||
|     if (testBegin("strSizeFormat()")) | ||||
|     { | ||||
|         TEST_RESULT_STR(strPtr(strSizeFormat(0)), "0B", "zero bytes"); | ||||
|         TEST_RESULT_STR(strPtr(strSizeFormat(1023)), "1023B", "1023 bytes"); | ||||
|         TEST_RESULT_STR(strPtr(strSizeFormat(1024)), "1KB", "1 KB"); | ||||
|         TEST_RESULT_STR(strPtr(strSizeFormat(2200)), "2.1KB", "2.1 KB"); | ||||
|         TEST_RESULT_STR(strPtr(strSizeFormat(1048576)), "1MB", "1 MB"); | ||||
|         TEST_RESULT_STR(strPtr(strSizeFormat(20162900)), "19.2MB", "19.2 MB"); | ||||
|         TEST_RESULT_STR(strPtr(strSizeFormat(1073741824)), "1GB", "1 GB"); | ||||
|         TEST_RESULT_STR(strPtr(strSizeFormat(UINT64_MAX)), "17179869184.0GB", "uint64 max"); | ||||
|     } | ||||
|  | ||||
|     FUNCTION_HARNESS_RESULT_VOID(); | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user