Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
STRUCT_TM.cpp
Go to the documentation of this file.
1// checking for struct tm in <time.h>
2
3
25#include <stddef.h> // for offsetof, size_t
26#include <stdio.h> // for printf
27#include <string.h> // for strlen
28#include <time.h> // for struct tm
29
30#include "config.h"
31
32#ifndef _RWSTD_NO_OFFSETOF
33# define OFF(T, m) offsetof (T, m)
34#else
35 // provide own equivalent when offsetof() macro doesn't work
36 // (e.g., when using the EDG eccp front end with an incompatible
37 // version of gcc)
38# define OFF(T, m) (((const char*)&((T*)0)->m) - ((const char*)(T*)0))
39#endif // _RWSTD_NO_OFFSETOF
40
41// prevent IBM xlC 5.0 errors caused by using /usr/include/string.h
42// which #defines these (and other) names to undeclared symbols
43#undef strlen
44
45int main ()
46{
47 typedef struct tm TimeT;
48
49 struct {
50 size_t offset;
51 const char *name;
52 const char *comment;
53 } tm_info[] = {
54
55#define ENTRY(member, comment) \
56 { OFF (struct tm, member), # member ";", " /* " comment " */" }
57
58 ENTRY (tm_sec, "seconds after the minute [O..61] "),
59 ENTRY (tm_min, "minutes after the hour [0..59] "),
60 ENTRY (tm_hour, "hours since midnight [0..23] "),
61 ENTRY (tm_mday, "day of the month [1..31] "),
62 ENTRY (tm_mon, "months since January [0..11] "),
63 ENTRY (tm_year, "years since 1900 "),
64 ENTRY (tm_wday, "days since Sunday [0..6] "),
65 ENTRY (tm_yday, "days since January 1 [0..365]"),
66 ENTRY (tm_isdst, "Daylight Saving Time "),
67 };
68
69 const size_t N = sizeof tm_info / sizeof *tm_info;
70
71 char def [1024] = "";
72
73 size_t curoff = 0; // current offset
74
75 // assume contiguous standard members
76 // optionally followed by non-standard ones
77 for (size_t i = 0; i != N; ++i) {
78 for (size_t j = 0; j != N; ++j) {
79 if (curoff == tm_info [j].offset) {
80 sprintf (def + strlen (def),
81 " int %-10s %s \\\n",
82 tm_info [j].name, tm_info [j].comment);
83 tm_info [j].offset = size_t (-1);
84 curoff += sizeof (int);
85 break;
86 }
87 }
88 if (!curoff) {
89 curoff = sizeof (struct tm) - N * sizeof (int);
90 }
91 }
92
93 printf ("#define _RWSTD_STRUCT_TM { "
94 "/* sizeof (struct tm) == %u */ \\\n%s",
95 sizeof (struct tm), def);
96
97
98 if (N * sizeof (int) != sizeof (struct tm)) {
99 printf (" char _C_fill [%u]; "
100 "/* fill to size */ \\\n",
101 sizeof (struct tm) - N * sizeof (int));
102 }
103
104 printf (" }\n");
105
106 return 0;
107}
#define N
Definition 17.names.cpp:157
#define i
Definition 17.names.cpp:126
#define j
Definition 17.names.cpp:127
#define offset
Definition 17.names.cpp:508
IMPORT_C SInt32 printf(const Char *,...)
#define ENTRY(member, comment)
int main()
Definition STRUCT_TM.cpp:45
const char * name
Definition codecvt1.cpp:72