Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / DtSearch / objdate.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*
24  *   COMPONENT_NAME: austext
25  *
26  *   FUNCTIONS: is_objdatestr
27  *              is_objdatetm
28  *              objdate2fzkstr
29  *              objdate2tm
30  *              objdate_in_range
31  *              tm2objdate
32  *
33  *   ORIGINS: 27
34  *
35  *
36  *   (C) COPYRIGHT International Business Machines Corp. 1994,1995
37  *   All Rights Reserved
38  *   Licensed Materials - Property of IBM
39  *   US Government Users Restricted Rights - Use, duplication or
40  *   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
41  */
42 /*************************** OBJDATE.C *************************
43  * $XConsortium: objdate.c /main/8 1996/11/21 19:51:44 drk $
44  * November 1994.
45  * Utilites for generic manipulation of austext objdates.
46  * Most of these functions were originally in msgutil.c
47  *
48  * $Log$
49  * Revision 2.5  1996/03/05  18:00:36  miker
50  * Replaced hardcoded strings with refs to NULLDATESTR.
51  *
52  * Revision 2.4  1996/02/13  16:44:40  miker
53  * Allow \n to terminate null date string in is_objdatestr.
54  *
55  * Revision 2.3  1995/10/25  16:38:10  miker
56  * Added prolog.
57  *
58  * Revision 2.2  1995/10/02  20:37:30  miker
59  * Cosmetic cleanup only.
60  *
61  * Revision 2.1  1995/09/22  21:25:35  miker
62  * Freeze DtSearch 0.1, AusText 2.1.8
63  *
64  * Revision 1.3  1995/09/05  18:24:09  miker
65  * Remove refs to usrblk so objdate can be used in offline programs.
66  * Name changes for DtSearch.
67  */
68
69 #include "SearchP.h"
70 #define X_INCLUDE_STRING_H
71 #define XOS_USE_NO_LOCKING
72 #include <X11/Xos_r.h>
73
74 #define PROGNAME        "OBJDATE"
75
76 /************************************************/
77 /*                                              */
78 /*                is_objdatestr                 */
79 /*                                              */
80 /************************************************/
81 /* Converts OBJDATESTR formatted string as found in .fzk files
82  * to DtSrObjdate long integer if string is valid.
83  * Returns TRUE if passed string is correctly formatted
84  * and conversion successful.  Returns FALSE and
85  * does not alter passed objdate if string is not valid.
86  * String format is: "yy/mm/dd~hh:mm[\n]" (see OBJDATESTR in SearchP.h).
87  * The slashes and tilde are mandatory, the final \n is optional.
88  * Each field maps to an objdate bitfield; bitfields map
89  * to struct tm fields (see fuzzy.h).
90  * Can be used merely to test for valid string format by
91  * passing NULL for objdate pointer.
92  */
93 int     is_objdatestr (char *string, DtSrObjdate *objdptr)
94 {
95     static char     parsebuf[24];
96     int             i;
97     char           *token;
98     DtSrObjdate     myobjdate = 0L;
99     _Xstrtokparams  strtok_buf;
100
101     /* Test for "null" objdate (which is valid) */
102     if (strncmp (string, NULLDATESTR, 9) == 0) {
103         if (string[9] == 0 || string[9] == '\n') {
104             if (objdptr)
105                 *objdptr = 0L;
106             return TRUE;
107         }
108     }
109
110     strncpy (parsebuf, string, sizeof (parsebuf));
111     parsebuf[sizeof (parsebuf) - 1] = '\0';
112
113     if ((token = _XStrtok(parsebuf, "/", strtok_buf)) == NULL)
114         return FALSE;
115     i = atoi (token);
116     if (i < 1 || i > 4095)      /* yy */
117         return FALSE;
118     else
119         myobjdate |= (i << 20);
120
121     if ((token = _XStrtok(NULL, "/", strtok_buf)) == NULL)
122         return FALSE;
123     i = atoi (token);
124     if (i < 1 || i > 12)        /* mm */
125         return FALSE;
126     else
127         myobjdate |= (--i << 16);
128
129     if ((token = _XStrtok(NULL, "~", strtok_buf)) == NULL)
130         return FALSE;
131     i = atoi (token);
132     if (i < 1 || i > 31)        /* dd */
133         return FALSE;
134     else
135         myobjdate |= (i << 11);
136
137     if ((token = _XStrtok(NULL, ":", strtok_buf)) == NULL)
138         return FALSE;
139     i = atoi (token);
140     if (i < 0 || i > 23)        /* hh */
141         return FALSE;
142     else
143         myobjdate |= (i << 6);
144
145     if ((token = _XStrtok(NULL, "\n", strtok_buf)) == NULL)
146         return FALSE;
147     i = atoi (token);
148     if (i < 0 || i > 59)        /* mm */
149         return FALSE;
150     else
151         myobjdate |= i;
152
153     if (objdptr)
154         *objdptr = myobjdate;
155     return TRUE;
156 }  /* is_objdatestr() */
157
158
159
160 /************************************************/
161 /*                                              */
162 /*                 is_objdatetm                 */
163 /*                                              */
164 /************************************************/
165 /* Returns TRUE if passed structure is correctly formatted
166  * for conversion to DtSrObjdate variable, else returns FALSE.
167  */
168 int     is_objdatetm (struct tm *objdatetm)
169 {
170     if (objdatetm->tm_year < 0)
171         return FALSE;
172     if (objdatetm->tm_year > 4095)
173         return FALSE;
174     if (objdatetm->tm_mon < 0)
175         return FALSE;
176     if (objdatetm->tm_mon > 11)
177         return FALSE;
178     if (objdatetm->tm_mday < 1)
179         return FALSE;
180     if (objdatetm->tm_mday > 31)
181         return FALSE;
182     if (objdatetm->tm_hour < 0)
183         return FALSE;
184     if (objdatetm->tm_hour > 23)
185         return FALSE;
186     if (objdatetm->tm_min < 0)
187         return FALSE;
188     if (objdatetm->tm_min > 59)
189         return FALSE;
190     return TRUE;
191 }  /* is_objdatetm() */
192
193
194 /************************************************/
195 /*                                              */
196 /*                  objdate2tm                  */
197 /*                                              */
198 /************************************************/
199 /* Converts DtSrObjdate formatted long int and returns
200  * pointer to static, standard unix tm structure.
201  * Bitfields map to struct tm fields (see fuzzy.h).
202  * The output tm structure is suitable for mktime(),
203  * but may only work for some formats of strftime().
204  */
205 struct tm      *objdate2tm (DtSrObjdate objdate)
206 {
207     static struct tm    mytm;
208     memset (&mytm, 0, sizeof (struct tm));
209     mytm.tm_year = objdate >> 20;
210     mytm.tm_mon = (objdate >> 16) & 0xf;
211     mytm.tm_mday = (objdate >> 11) & 0x1f;
212     mytm.tm_hour = (objdate >> 6) & 0x1f;
213     mytm.tm_min = objdate & 0x3f;
214     mytm.tm_isdst = -1;
215     return &mytm;
216 }  /* objdate2tm() */
217
218
219 /************************************************/
220 /*                                              */
221 /*                objdate2fzkstr                */
222 /*                                              */
223 /************************************************/
224 /* Converts DtSrObjdate formatted long int and returns pointer
225  * to static date string in .fzk format for debugging.
226  */
227 char           *objdate2fzkstr (DtSrObjdate objdate)
228 {
229     struct tm      *tmptr;
230     static char     strbuf[36];
231     if (objdate == 0L)
232         return NULLDATESTR;
233     tmptr = objdate2tm (objdate);
234     sprintf (strbuf, OBJDATESTR,
235         tmptr->tm_year, tmptr->tm_mon + 1,
236         tmptr->tm_mday, tmptr->tm_hour, tmptr->tm_min);
237     return strbuf;
238 }  /* objdate2fzkstr() */
239
240
241 /************************************************/
242 /*                                              */
243 /*                  tm2objdate                  */
244 /*                                              */
245 /************************************************/
246 /* Converts specific fields in a tm structure to
247  * an DtSrObjdate formatted long int and returns it.
248  * DtSrObjdate bitfields map to struct tm fields (see fuzzy.h).
249  * Does not validate tm fields (use is_objdate() to confirm).
250  */
251 DtSrObjdate     tm2objdate (struct tm *tmptr)
252 {
253     return ((tmptr->tm_year << 20) |
254         (tmptr->tm_mon << 16) |
255         (tmptr->tm_mday << 11) |
256         (tmptr->tm_hour << 6) |
257         tmptr->tm_min);
258 }  /* tm2objdate() */
259
260
261 /************************************************/
262 /*                                              */
263 /*              objdate_in_range                */
264 /*                                              */
265 /************************************************/
266 /* Returns TRUE if passed record objdate (presumably read from
267  * an austext record) is within the objdate ranges specified
268  * in usrblk.  Returns FALSE if record is in any way disqualified
269  * from inclusion on a hitlist because of date.
270  * Zero in any of the three dates is automatic "in range" qualification.
271  */
272 int     objdate_in_range (
273                 DtSrObjdate     recdate,
274                 DtSrObjdate     date1,
275                 DtSrObjdate     date2)
276 {
277     if (recdate == 0L)  /* Null dated record always qualifies */
278         return TRUE;
279     if (date1 != 0L && date1 > recdate)
280         return FALSE;
281     if (date2 != 0L && date2 < recdate)
282         return FALSE;
283     return TRUE;
284 }  /* objdate_in_range() */
285
286 /********************** OBJDATE.C *************************/