Skip to content

Commit

Permalink
Print comma-separated results
Browse files Browse the repository at this point in the history
  • Loading branch information
eschan145 committed Jan 22, 2025
1 parent 7a90208 commit bf9d971
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ DK_API uint64_t dyknow_size(const std::string& directory) {
}

do {

if (data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
continue;
}
Expand Down Expand Up @@ -177,7 +176,9 @@ DK_API void validate() {
}

uint64_t size = dyknow_size();
std::cout << "DyKnow folder size: " << size << "\n";
std::cout << "DyKnow folder size: " << comma_separated(size)
<< " bytes (" << comma_separated(size / (1024 * 1024)
<< ") " << "MBs.\n";
}

bool exists(const char* path) {
Expand Down
1 change: 1 addition & 0 deletions src/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Compile with g++ -shared -o api.dll api.cpp -Ofast -fPIC -shared

#include "asteroids.h"
#include "settings.h"
#include "util.h"

#pragma comment(lib, "wininet.lib")

Expand Down
62 changes: 62 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
COPYRIGHT (C) 2025 ETHAN CHAN
ALL RIGHTS RESERVED. UNAUTHORIZED COPYING, MODIFICATION, DISTRIBUTION, OR USE
OF THIS SOFTWARE WITHOUT PRIOR PERMISSION IS STRICTLY PROHIBITED.
THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM,
OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
PROJECT NAME: DieKnow
FILENAME: src/util.h
DESCRIPTION: DieKnow miscellaneous utilities
AUTHOR: Ethan Chan
DATE: 2025-1-12
VERSION: 2.1
*/

#ifndef UTIL_H
#define UTIL_H

#include <iostream>
#include <string>
#include <type_traits>
#include <sstream>
#include <iomanip>


template <typename T>
std::string& comma_separated(const T& number) {
static_assert(std::is_arithmetic_v<T>, "Nonnumerical input!");

std::ostringstream oss;
oss << std::fixed << std::setprecision(0) << number;

std::string num_str = oss.str();
std::string formatted;

size_t start = 0;
if (num_str[0] == '-') {
formatted += '-';
start = 1;
}

size_t len = num_str.size();
size_t digits = len - start;
for (size_t i = start; i < len; ++i) {
formatted += num_str[i];
if ((digits - (i - start) - 1) % 3 == 0 && i < len - 1) {
// Single-apostrophe for char in C++
formatted += ',';
}
}

return formatted;
}

#endif // UTIL_H

0 comments on commit bf9d971

Please sign in to comment.