comparison src/c/urweb.c @ 436:024478c34f4d

time type
author Adam Chlipala <adamc@hcoop.net>
date Thu, 30 Oct 2008 14:36:48 -0400
parents 3ca00463de20
children 1c27f03d9bd2
comparison
equal deleted inserted replaced
435:f7b25375c0cf 436:024478c34f4d
1 #define _XOPEN_SOURCE
2
1 #include <stdlib.h> 3 #include <stdlib.h>
2 #include <stdio.h> 4 #include <stdio.h>
3 #include <string.h> 5 #include <string.h>
4 #include <ctype.h> 6 #include <ctype.h>
5 #include <setjmp.h> 7 #include <setjmp.h>
254 void uw_memstats(uw_context ctx) { 256 void uw_memstats(uw_context ctx) {
255 printf("Page: %d/%d\n", ctx->page_front - ctx->page, ctx->page_back - ctx->page); 257 printf("Page: %d/%d\n", ctx->page_front - ctx->page, ctx->page_back - ctx->page);
256 printf("Heap: %d/%d\n", ctx->heap_front - ctx->heap, ctx->heap_back - ctx->heap); 258 printf("Heap: %d/%d\n", ctx->heap_front - ctx->heap, ctx->heap_back - ctx->heap);
257 } 259 }
258 260
259 int uw_really_send(int sock, const void *buf, ssize_t len) { 261 int uw_really_send(int sock, const void *buf, size_t len) {
260 while (len > 0) { 262 while (len > 0) {
261 ssize_t n = send(sock, buf, len, 0); 263 size_t n = send(sock, buf, len, 0);
262 264
263 if (n < 0) 265 if (n < 0)
264 return n; 266 return n;
265 267
266 buf += n; 268 buf += n;
723 } 725 }
724 726
725 return uw_unit_v; 727 return uw_unit_v;
726 } 728 }
727 729
730 #define TIME_FMT "%x %X"
731
732 uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) {
733 size_t len;
734 char *r;
735 struct tm stm;
736
737 if (localtime_r(&t, &stm)) {
738 uw_check_heap(ctx, TIMES_MAX);
739 r = ctx->heap_front;
740 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
741 ctx->heap_front += len+1;
742 return r;
743 } else
744 return "<i>Invalid time</i>";
745 }
746
747 uw_unit uw_Basis_htmlifyTime_w(uw_context ctx, uw_Basis_time t) {
748 size_t len;
749 char *r;
750 struct tm stm;
751
752 if (localtime_r(&t, &stm)) {
753 uw_check(ctx, TIMES_MAX);
754 r = ctx->page_front;
755 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
756 ctx->page_front += len;
757 } else {
758 uw_check(ctx, 20);
759 strcpy(ctx->page_front, "<i>Invalid time</i>");
760 ctx->page_front += 19;
761 }
762
763 return uw_unit_v;
764 }
765
728 uw_Basis_string uw_Basis_strcat(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) { 766 uw_Basis_string uw_Basis_strcat(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) {
729 int len = strlen(s1) + strlen(s2) + 1; 767 int len = strlen(s1) + strlen(s2) + 1;
730 char *s; 768 char *s;
731 769
732 uw_check_heap(ctx, len); 770 uw_check_heap(ctx, len);
858 return "False"; 896 return "False";
859 else 897 else
860 return "True"; 898 return "True";
861 } 899 }
862 900
901 uw_Basis_string uw_Basis_timeToString(uw_context ctx, uw_Basis_time t) {
902 size_t len;
903 char *r;
904 struct tm stm;
905
906 if (localtime_r(&t, &stm)) {
907 uw_check_heap(ctx, TIMES_MAX);
908 r = ctx->heap_front;
909 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
910 ctx->heap_front += len+1;
911 return r;
912 } else
913 return "<Invalid time>";
914 }
863 915
864 uw_Basis_int *uw_Basis_stringToInt(uw_context ctx, uw_Basis_string s) { 916 uw_Basis_int *uw_Basis_stringToInt(uw_context ctx, uw_Basis_string s) {
865 char *endptr; 917 char *endptr;
866 uw_Basis_int n = strtoll(s, &endptr, 10); 918 uw_Basis_int n = strtoll(s, &endptr, 10);
867 919
895 return &false; 947 return &false;
896 else 948 else
897 return NULL; 949 return NULL;
898 } 950 }
899 951
952 uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) {
953 char *end = strchr(s, 0);
954 struct tm stm;
955
956 if (strptime(s, TIME_FMT, &stm) == end) {
957 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
958 *r = mktime(&stm);
959 return r;
960 }
961 else
962 return NULL;
963 }
964
900 uw_Basis_int uw_Basis_stringToInt_error(uw_context ctx, uw_Basis_string s) { 965 uw_Basis_int uw_Basis_stringToInt_error(uw_context ctx, uw_Basis_string s) {
901 char *endptr; 966 char *endptr;
902 uw_Basis_int n = strtoll(s, &endptr, 10); 967 uw_Basis_int n = strtoll(s, &endptr, 10);
903 968
904 if (*s != '\0' && *endptr == '\0') 969 if (*s != '\0' && *endptr == '\0')
923 else if (!strcasecmp(s, "F") || !strcasecmp (s, "False")) 988 else if (!strcasecmp(s, "F") || !strcasecmp (s, "False"))
924 return uw_Basis_False; 989 return uw_Basis_False;
925 else 990 else
926 uw_error(ctx, FATAL, "Can't parse bool: %s", s); 991 uw_error(ctx, FATAL, "Can't parse bool: %s", s);
927 } 992 }
993
994 uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) {
995 char *end = strchr(s, 0);
996 struct tm stm = {};
997
998 if (strptime(s, TIME_FMT, &stm) == end)
999 return mktime(&stm);
1000 else
1001 uw_error(ctx, FATAL, "Can't parse time: %s", s);
1002 }