youtube: make util function from format printing thousand separated numbers - f… | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit a9e2584bc6c2b314c4f1b6d6a5f4715161d64bc5 | |
parent 634c895fbf8796d04bea2c0fef6615110c9ef951 | |
Author: Hiltjo Posthuma <[email protected]> | |
Date: Tue, 7 Mar 2023 19:32:47 +0100 | |
youtube: make util function from format printing thousand separated numbers | |
In other programs print views this way too. | |
Diffstat: | |
M util.c | 26 ++++++++++++++++++++++++++ | |
M util.h | 1 + | |
M youtube/cgi.c | 7 ++++--- | |
M youtube/cli.c | 5 +++-- | |
M youtube/gopher.c | 30 ++---------------------------- | |
5 files changed, 36 insertions(+), 33 deletions(-) | |
--- | |
diff --git a/util.c b/util.c | |
@@ -221,3 +221,29 @@ durationstr(long secs, char *buf, size_t bufsiz) | |
return r; | |
} | |
+ | |
+/* print views with thousand separators */ | |
+void | |
+printnumsep(const char *s) | |
+{ | |
+ const char *p; | |
+ int ndigits = 0; | |
+ | |
+ /* first count all digits */ | |
+ for (p = s; *p; p++) | |
+ if (*p >= '0' && *p <= '9') | |
+ ndigits++; | |
+ | |
+ for (p = s; *p; p++) { | |
+ if (!(*p >= '0' && *p <= '9')) | |
+ continue; | |
+ | |
+ putchar(*p); | |
+ ndigits--; | |
+ | |
+ /* show separator on every 3 digits and when there are | |
+ digits remaining */ | |
+ if ((ndigits % 3) == 0 && ndigits > 0) | |
+ putchar(','); | |
+ } | |
+} | |
diff --git a/util.h b/util.h | |
@@ -14,6 +14,7 @@ int friendlytime(time_t now, time_t t); | |
char *getparam(const char *query, const char *s); | |
void gophertext(FILE *fp, const char *s, size_t len); | |
int hexdigit(int c); | |
+void printnumsep(const char *s); | |
int uriencode(const char *s, char *buf, size_t bufsiz); | |
int utf8pad(char *buf, size_t bufsiz, const char *s, size_t len, int pad); | |
void xmlencode(const char *s); | |
diff --git a/youtube/cgi.c b/youtube/cgi.c | |
@@ -281,9 +281,9 @@ render_search(struct search_response *r) | |
OUT("</span><br/>\n"); | |
} | |
OUT(" <span class=\"stats\">"); | |
- OUT(v->viewcount); | |
+ printnumsep(v->viewcount); | |
OUT( | |
- "</span><br/>\n" | |
+ " views</span><br/>\n" | |
" </td>\n" | |
" <td align=\"right\" class=\"a-r\">\n" | |
" <span class=\"duration\">"); | |
@@ -366,7 +366,8 @@ render_video(struct video_response *r) | |
} | |
OUT("<tr><td><b>Views:</b></td><td>"); | |
- printf("%ld", r->viewcount); | |
+ snprintf(buf, sizeof(buf), "%ld", r->viewcount); | |
+ printnumsep(buf); | |
OUT("</td></tr>\n"); | |
if (r->publishdate[0]) { | |
diff --git a/youtube/cli.c b/youtube/cli.c | |
@@ -149,7 +149,7 @@ render_search(struct search_response *r) | |
} | |
if (v->viewcount[0]) { | |
OUT("Views: "); | |
- OUTESCAPE(v->viewcount); | |
+ printnumsep(v->viewcount); | |
OUT("\n"); | |
} | |
OUT("\n"); | |
@@ -182,7 +182,8 @@ render_video(struct video_response *r) | |
} | |
OUT("Views: "); | |
- printf("%ld", r->viewcount); | |
+ snprintf(buf, sizeof(buf), "%ld", r->viewcount); | |
+ printnumsep(buf); | |
OUT("\n"); | |
if (r->publishdate[0]) { | |
diff --git a/youtube/gopher.c b/youtube/gopher.c | |
@@ -42,32 +42,6 @@ info(const char *s) | |
line('i', s, ""); | |
} | |
-/* print views with thousand separators */ | |
-static void | |
-printviews(const char *s) | |
-{ | |
- const char *p; | |
- int ndigits = 0; | |
- | |
- /* first count all digits */ | |
- for (p = s; *p; p++) | |
- if (*p >= '0' && *p <= '9') | |
- ndigits++; | |
- | |
- for (p = s; *p; p++) { | |
- if (!(*p >= '0' && *p <= '9')) | |
- continue; | |
- | |
- putchar(*p); | |
- ndigits--; | |
- | |
- /* show separator on every 3 digits and when there are | |
- digits remaining */ | |
- if ((ndigits % 3) == 0 && ndigits > 0) | |
- putchar(','); | |
- } | |
-} | |
- | |
void | |
header(void) | |
{ | |
@@ -177,7 +151,7 @@ render_search(struct search_response *r) | |
} | |
if (v->viewcount[0]) { | |
OUT("iViews: "); | |
- printviews(v->viewcount); | |
+ printnumsep(v->viewcount); | |
printf("\t%s\t%s\t%s\r\n", "", host, port); | |
} | |
} | |
@@ -238,7 +212,7 @@ render_video(struct video_response *r) | |
OUT("iViews: "); | |
snprintf(buf, sizeof(buf), "%ld", r->viewcount); | |
- printviews(buf); | |
+ printnumsep(buf); | |
printf("\t%s\t%s\t%s\r\n", "", host, port); | |
if (r->publishdate[0]) { |