1 /** @file c_plus.c  Library initialization.
2 
3 @authors Copyright (c) 2017 Jaakko Keränen <jaakko.keranen@iki.fi>
4 
5 @par License
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice, this
11    list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright notice,
13    this list of conditions and the following disclaimer in the documentation
14    and/or other materials provided with the distribution.
15 
16 <small>THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</small>
26 */
27 
28 #include "the_Foundation/defs.h"
29 #include "the_Foundation/string.h"
30 #include "the_Foundation/time.h"
31 #include "the_Foundation/version.h"
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <time.h>
36 #include <locale.h>
37 
38 static iBool hasBeenInitialized_ = iFalse;
39 
40 void deinitForThread_Garbage_(void); /* garbage.c */
41 void deinit_DatagramThreads_(void);  /* datagram.c */
42 void deinit_Address_(void);          /* address.c */
43 void deinit_Threads_(void);          /* thread.c */
44 void init_DatagramThreads_(void);    /* datagram.c */
45 void init_Locale(void);              /* locale */
46 void init_Threads(void);             /* thread.c */
47 
init_Foundation(void)48 void init_Foundation(void) {
49     init_Threads();
50     init_Garbage();
51     iDebug("[the_Foundation] version: %i.%i.%i%s cstd:%li\n",
52            version_Foundation.major, version_Foundation.minor, version_Foundation.patch,
53            strlen(iFoundationLibraryGitTag) ? format_CStr("-%s", iFoundationLibraryGitTag) : "",
54            __STDC_VERSION__);
55     /* Locale. */ {
56         setLocale_Foundation();
57     }
58     hasBeenInitialized_ = iTrue;
59     atexit(deinit_Foundation); /* should be manually called, though */
60 }
61 
deinit_Foundation(void)62 void deinit_Foundation(void) {
63     if (isInitialized_Foundation()) {
64         hasBeenInitialized_ = iFalse;
65         deinit_DatagramThreads_();
66         deinit_Address_();
67         deinitForThread_Garbage_();
68         deinit_Threads_();
69     }
70 }
71 
isInitialized_Foundation(void)72 iBool isInitialized_Foundation(void) {
73     return hasBeenInitialized_;
74 }
75 
setLocale_Foundation(void)76 void setLocale_Foundation(void) {
77     init_Locale();
78 }
79 
printMessage_Foundation(FILE * output,const char * format,...)80 void printMessage_Foundation(FILE *output, const char *format, ...) {
81     va_list args;
82     va_start(args, format);
83     vfprintf(output, format, args);
84     va_end(args);
85 }
86