objdate.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 libraries 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. #include "SearchP.h"
  69. #include <stdlib.h>
  70. #define X_INCLUDE_STRING_H
  71. #define XOS_USE_NO_LOCKING
  72. #include <X11/Xos_r.h>
  73. #define PROGNAME "OBJDATE"
  74. /************************************************/
  75. /* */
  76. /* is_objdatestr */
  77. /* */
  78. /************************************************/
  79. /* Converts OBJDATESTR formatted string as found in .fzk files
  80. * to DtSrObjdate long integer if string is valid.
  81. * Returns TRUE if passed string is correctly formatted
  82. * and conversion successful. Returns FALSE and
  83. * does not alter passed objdate if string is not valid.
  84. * String format is: "yy/mm/dd~hh:mm[\n]" (see OBJDATESTR in SearchP.h).
  85. * The slashes and tilde are mandatory, the final \n is optional.
  86. * Each field maps to an objdate bitfield; bitfields map
  87. * to struct tm fields (see fuzzy.h).
  88. * Can be used merely to test for valid string format by
  89. * passing NULL for objdate pointer.
  90. */
  91. int is_objdatestr (char *string, DtSrObjdate *objdptr)
  92. {
  93. static char parsebuf[24];
  94. int i;
  95. char *token;
  96. DtSrObjdate myobjdate = 0L;
  97. _Xstrtokparams strtok_buf;
  98. /* Test for "null" objdate (which is valid) */
  99. if (strncmp (string, NULLDATESTR, 9) == 0) {
  100. if (string[9] == 0 || string[9] == '\n') {
  101. if (objdptr)
  102. *objdptr = 0L;
  103. return TRUE;
  104. }
  105. }
  106. strncpy (parsebuf, string, sizeof (parsebuf));
  107. parsebuf[sizeof (parsebuf) - 1] = '\0';
  108. if ((token = _XStrtok(parsebuf, "/", strtok_buf)) == NULL)
  109. return FALSE;
  110. i = atoi (token);
  111. if (i < 1 || i > 4095) /* yy */
  112. return FALSE;
  113. else
  114. myobjdate |= (i << 20);
  115. if ((token = _XStrtok(NULL, "/", strtok_buf)) == NULL)
  116. return FALSE;
  117. i = atoi (token);
  118. if (i < 1 || i > 12) /* mm */
  119. return FALSE;
  120. else
  121. myobjdate |= (--i << 16);
  122. if ((token = _XStrtok(NULL, "~", strtok_buf)) == NULL)
  123. return FALSE;
  124. i = atoi (token);
  125. if (i < 1 || i > 31) /* dd */
  126. return FALSE;
  127. else
  128. myobjdate |= (i << 11);
  129. if ((token = _XStrtok(NULL, ":", strtok_buf)) == NULL)
  130. return FALSE;
  131. i = atoi (token);
  132. if (i < 0 || i > 23) /* hh */
  133. return FALSE;
  134. else
  135. myobjdate |= (i << 6);
  136. if ((token = _XStrtok(NULL, "\n", strtok_buf)) == NULL)
  137. return FALSE;
  138. i = atoi (token);
  139. if (i < 0 || i > 59) /* mm */
  140. return FALSE;
  141. else
  142. myobjdate |= i;
  143. if (objdptr)
  144. *objdptr = myobjdate;
  145. return TRUE;
  146. } /* is_objdatestr() */
  147. /************************************************/
  148. /* */
  149. /* is_objdatetm */
  150. /* */
  151. /************************************************/
  152. /* Returns TRUE if passed structure is correctly formatted
  153. * for conversion to DtSrObjdate variable, else returns FALSE.
  154. */
  155. int is_objdatetm (struct tm *objdatetm)
  156. {
  157. if (objdatetm->tm_year < 0)
  158. return FALSE;
  159. if (objdatetm->tm_year > 4095)
  160. return FALSE;
  161. if (objdatetm->tm_mon < 0)
  162. return FALSE;
  163. if (objdatetm->tm_mon > 11)
  164. return FALSE;
  165. if (objdatetm->tm_mday < 1)
  166. return FALSE;
  167. if (objdatetm->tm_mday > 31)
  168. return FALSE;
  169. if (objdatetm->tm_hour < 0)
  170. return FALSE;
  171. if (objdatetm->tm_hour > 23)
  172. return FALSE;
  173. if (objdatetm->tm_min < 0)
  174. return FALSE;
  175. if (objdatetm->tm_min > 59)
  176. return FALSE;
  177. return TRUE;
  178. } /* is_objdatetm() */
  179. /************************************************/
  180. /* */
  181. /* objdate2tm */
  182. /* */
  183. /************************************************/
  184. /* Converts DtSrObjdate formatted long int and returns
  185. * pointer to static, standard unix tm structure.
  186. * Bitfields map to struct tm fields (see fuzzy.h).
  187. * The output tm structure is suitable for mktime(),
  188. * but may only work for some formats of strftime().
  189. */
  190. struct tm *objdate2tm (DtSrObjdate objdate)
  191. {
  192. static struct tm mytm;
  193. memset (&mytm, 0, sizeof (struct tm));
  194. mytm.tm_year = objdate >> 20;
  195. mytm.tm_mon = (objdate >> 16) & 0xf;
  196. mytm.tm_mday = (objdate >> 11) & 0x1f;
  197. mytm.tm_hour = (objdate >> 6) & 0x1f;
  198. mytm.tm_min = objdate & 0x3f;
  199. mytm.tm_isdst = -1;
  200. return &mytm;
  201. } /* objdate2tm() */
  202. /************************************************/
  203. /* */
  204. /* objdate2fzkstr */
  205. /* */
  206. /************************************************/
  207. /* Converts DtSrObjdate formatted long int and returns pointer
  208. * to static date string in .fzk format for debugging.
  209. */
  210. char *objdate2fzkstr (DtSrObjdate objdate)
  211. {
  212. struct tm *tmptr;
  213. static char strbuf[36];
  214. if (objdate == 0L)
  215. return NULLDATESTR;
  216. tmptr = objdate2tm (objdate);
  217. sprintf (strbuf, OBJDATESTR,
  218. tmptr->tm_year, tmptr->tm_mon + 1,
  219. tmptr->tm_mday, tmptr->tm_hour, tmptr->tm_min);
  220. return strbuf;
  221. } /* objdate2fzkstr() */
  222. /************************************************/
  223. /* */
  224. /* tm2objdate */
  225. /* */
  226. /************************************************/
  227. /* Converts specific fields in a tm structure to
  228. * an DtSrObjdate formatted long int and returns it.
  229. * DtSrObjdate bitfields map to struct tm fields (see fuzzy.h).
  230. * Does not validate tm fields (use is_objdate() to confirm).
  231. */
  232. DtSrObjdate tm2objdate (struct tm *tmptr)
  233. {
  234. return ((tmptr->tm_year << 20) |
  235. (tmptr->tm_mon << 16) |
  236. (tmptr->tm_mday << 11) |
  237. (tmptr->tm_hour << 6) |
  238. tmptr->tm_min);
  239. } /* tm2objdate() */
  240. /************************************************/
  241. /* */
  242. /* objdate_in_range */
  243. /* */
  244. /************************************************/
  245. /* Returns TRUE if passed record objdate (presumably read from
  246. * an austext record) is within the objdate ranges specified
  247. * in usrblk. Returns FALSE if record is in any way disqualified
  248. * from inclusion on a hitlist because of date.
  249. * Zero in any of the three dates is automatic "in range" qualification.
  250. */
  251. int objdate_in_range (
  252. DtSrObjdate recdate,
  253. DtSrObjdate date1,
  254. DtSrObjdate date2)
  255. {
  256. if (recdate == 0L) /* Null dated record always qualifies */
  257. return TRUE;
  258. if (date1 != 0L && date1 > recdate)
  259. return FALSE;
  260. if (date2 != 0L && date2 < recdate)
  261. return FALSE;
  262. return TRUE;
  263. } /* objdate_in_range() */
  264. /********************** OBJDATE.C *************************/