summaryrefslogtreecommitdiffhomepage
path: root/Stars45/FormatUtil.cpp
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-04-01 21:23:39 +0200
committerAki <please@ignore.pl>2022-04-01 21:23:39 +0200
commit3c487c5cd69c53d6fea948643c0a76df03516605 (patch)
tree72730c7b8b26a5ef8fc9a987ec4c16129efd5aac /Stars45/FormatUtil.cpp
parent8f353abd0bfe18baddd8a8250ab7c4f2d1c83a6e (diff)
downloadstarshatter-3c487c5cd69c53d6fea948643c0a76df03516605.zip
starshatter-3c487c5cd69c53d6fea948643c0a76df03516605.tar.gz
starshatter-3c487c5cd69c53d6fea948643c0a76df03516605.tar.bz2
Moved Stars45 to StarsEx
Diffstat (limited to 'Stars45/FormatUtil.cpp')
-rw-r--r--Stars45/FormatUtil.cpp335
1 files changed, 0 insertions, 335 deletions
diff --git a/Stars45/FormatUtil.cpp b/Stars45/FormatUtil.cpp
deleted file mode 100644
index fd3ce66..0000000
--- a/Stars45/FormatUtil.cpp
+++ /dev/null
@@ -1,335 +0,0 @@
-/* Starshatter: The Open Source Project
- Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors
- Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
- Copyright (c) 1997-2006, Destroyer Studios LLC.
-
- AUTHOR: John DiCamillo
-
-
- OVERVIEW
- ========
-*/
-
-#include "FormatUtil.h"
-
-// +--------------------------------------------------------------------+
-
-void FormatNumber(char* txt, double n)
-{
- double a = fabs(n);
-
- if (a < 1e3)
- sprintf(txt, "%d", (int) (n));
-
- else if (a < 1e6)
- sprintf(txt, "%.1f K", (n/1e3));
-
- else if (a < 1e9)
- sprintf(txt, "%.1f M", (n/1e6));
-
- else if (a < 1e12)
- sprintf(txt, "%.1f G", (n/1e9));
-
- else if (a < 1e15)
- sprintf(txt, "%.1f T", (n/1e12));
-
- else
- sprintf(txt, "%.1e", n);
-}
-
-// +--------------------------------------------------------------------+
-
-void FormatNumberExp(char* txt, double n)
-{
- double a = fabs(n);
-
- if (a < 100e3)
- sprintf(txt, "%d", (int) (n));
-
- else
- sprintf(txt, "%.1e", n);
-}
-
-// +--------------------------------------------------------------------+
-
-const int MINUTE = 60;
-const int HOUR = 60 * MINUTE;
-const int DAY = 24 * HOUR;
-
-void FormatTime(char* txt, double time)
-{
- int t = (int) time;
-
- int h = (t / HOUR);
- int m = ((t - h*HOUR) / MINUTE);
- int s = (t - h*HOUR - m*MINUTE);
-
- if (h > 0)
- sprintf(txt, "%02d:%02d:%02d", h,m,s);
- else
- sprintf(txt, "%02d:%02d", m,s);
-}
-
-// +--------------------------------------------------------------------+
-
-void FormatTimeOfDay(char* txt, double time)
-{
- int t = (int) time;
-
- if (t >= DAY) {
- int d = t / DAY;
- t -= d * DAY;
- }
-
- int h = (t / HOUR);
- int m = ((t - h*HOUR) / MINUTE);
- int s = (t - h*HOUR - m*MINUTE);
-
- sprintf(txt, "%02d:%02d:%02d", h,m,s);
-}
-
-// +--------------------------------------------------------------------+
-
-void FormatDayTime(char* txt, double time, bool short_format)
-{
- int t = (int) time;
- int d = 1, h = 0, m = 0, s = 0;
-
- if (t >= DAY) {
- d = t / DAY;
- t -= d * DAY;
- d++;
- }
-
- if (t >= HOUR) {
- h = t / HOUR;
- t -= h * HOUR;
- }
-
- if (t >= MINUTE) {
- m = t / MINUTE;
- t -= m * MINUTE;
- }
-
- s = t;
-
- if (short_format)
- sprintf(txt, "%02d/%02d:%02d:%02d", d, h, m, s);
- else
- sprintf(txt, "Day %02d %02d:%02d:%02d", d, h, m, s);
-}
-
-// +--------------------------------------------------------------------+
-
-void FormatDay(char* txt, double time)
-{
- int t = (int) time;
- int d = 1, h = 0, m = 0, s = 0;
-
- if (t >= DAY) {
- d = t / DAY;
- t -= d * DAY;
- d++;
- }
-
- sprintf(txt, "Day %02d", d);
-}
-
-// +--------------------------------------------------------------------+
-
-void FormatPoint(char* txt, const Point& p)
-{
- char x[16];
- char y[16];
- char z[16];
-
- FormatNumber(x, p.x);
- FormatNumber(y, p.y);
- FormatNumber(z, p.z);
-
- sprintf(txt, "(%s, %s, %s)", x, y, z);
-}
-
-// +--------------------------------------------------------------------+
-
-Text FormatTimeString(int utc)
-{
- static const char* month[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
- "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
- static const char* meridian[2] = { "AM", "PM" };
-
- if (utc < 1)
- utc = (int) time(0);
-
- time_t aclock = utc; // Get time in seconds
- struct tm *t = localtime(&aclock); // Convert time to struct tm form
-
- char buffer[256];
- sprintf(buffer, "%d %s %d, %2d:%02d:%02d %s",
- t->tm_mday, month[t->tm_mon], 1900 + t->tm_year,
- t->tm_hour > 12 ? t->tm_hour-12 : t->tm_hour,
- t->tm_min, t->tm_sec, meridian[t->tm_hour > 12]);
-
- return buffer;
-}
-
-// +--------------------------------------------------------------------+
-
-static char safe_str[2048];
-
-const char* SafeString(const char* s)
-{
- ZeroMemory(safe_str, sizeof(safe_str));
-
- if (s && *s) {
- int len = strlen(s);
- int n = 0;
-
- for (int i = 0; i < len; i++) {
- char c = s[i];
-
- if (c == '\n') {
- safe_str[n++] = '\\';
- safe_str[n++] = 'n';
- }
-
- else if (c == '\t') {
- safe_str[n++] = '\\';
- safe_str[n++] = 't';
- }
-
- else if (c == '"') {
- safe_str[n++] = '\'';
- }
-
- else if (c == '\\' && i < len-1) {
- safe_str[n++] = s[i++];
- safe_str[n++] = s[i++];
- }
-
- else if (c < 32 || c > 126) {
- // non printing characters
- }
-
- else {
- safe_str[n++] = c;
- }
-
- if (n > 2040)
- break;
- }
- }
-
- return safe_str;
-}
-
-// +--------------------------------------------------------------------+
-
-const char* SafeQuotes(const char* msg)
-{
- int dst = 0;
-
- if (msg) {
- while (*msg && dst < 254) {
- if (*msg == '"') {
- safe_str[dst++] = '\'';
- msg++;
- }
- else if (isspace(*msg)) {
- safe_str[dst++] = ' ';
- msg++;
- }
- else {
- safe_str[dst++] = *msg++;
- }
- }
- }
-
- safe_str[dst] = 0;
- return safe_str;
-}
-
-// +--------------------------------------------------------------------+
-
-Text FormatTextReplace(const char* msg, const char* tgt, const char* val)
-{
- if (!msg || !tgt || !val)
- return "";
-
- if (!strchr(msg, *tgt))
- return msg;
-
- Text result;
- char* buffer = new char[strlen(msg) + 1];
- const char* p = msg;
- char* q = buffer;
- int tgtlen = strlen(tgt);
-
- while (*p) {
- if (!strncmp(p, tgt, tgtlen)) {
- p += tgtlen;
- *q = 0;
- q = buffer;
-
- result += buffer;
- result += val;
- }
-
- else {
- *q++ = *p++;
- }
- }
-
- if (q != buffer) {
- *q = 0;
- result += buffer;
- }
-
- delete [] buffer;
- return result;
-}
-
-// +--------------------------------------------------------------------+
-
-Text FormatTextEscape(const char* msg)
-{
- if (!msg)
- return "";
-
- if (!strchr(msg, '\\'))
- return msg;
-
- Text result;
- char* buffer = new char[strlen(msg) + 1];
- const char* p = msg;
- char* q = buffer;
-
- while (*p) {
- if (*p == '\\') {
- p++;
-
- if (*p == 'n') {
- *q++ = '\n';
- p++;
- }
-
- else if (*p == 't') {
- *q++ = '\t';
- p++;
- }
-
- else {
- *q++ = *p++;
- }
- }
-
- else {
- *q++ = *p++;
- }
- }
-
- *q = 0;
- result = buffer;
- delete [] buffer;
- return result;
-}