Removed panic when compiling on 64bit machines and simplified int to time conversion code.
1 | /* |
2 | * The contents of this file are subject to the AOLserver Public License |
3 | * Version 1.1 (the "License"); you may not use this file except in |
4 | * compliance with the License. You may obtain a copy of the License at |
5 | * http://aolserver.com/. |
6 | * |
7 | * Software distributed under the License is distributed on an "AS IS" |
8 | * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See |
9 | * the License for the specific language governing rights and limitations |
10 | * under the License. |
11 | * |
12 | * The Original Code is AOLserver Code and related documentation |
13 | * distributed by AOL. |
14 | * |
15 | * The Initial Developer of the Original Code is America Online, |
16 | * Inc. Portions created by AOL are Copyright (C) 1999 America Online, |
17 | * Inc. All Rights Reserved. |
18 | * |
19 | * Alternatively, the contents of this file may be used under the terms |
20 | * of the GNU General Public License (the "GPL"), in which case the |
21 | * provisions of GPL are applicable instead of those above. If you wish |
22 | * to allow use of your version of this file only under the terms of the |
23 | * GPL and not to allow others to use your version of this file under the |
24 | * License, indicate your decision by deleting the provisions above and |
25 | * replace them with the notice and other provisions required by the GPL. |
26 | * If you do not delete the provisions above, a recipient may use your |
27 | * version of this file under either the License or the GPL. |
28 | */ |
29 | |
30 | |
31 | /* |
32 | * tcltime.c -- |
33 | * |
34 | * Implement Tcl_Obj type for AOLserver Ns_Time. |
35 | */ |
36 | |
37 | static const char *RCSID = "@(#) $Header: /cvsroot-fuse/aolserver/aolserver/nsd/tclobj.c,v 1.7 2006/04/13 19:06:54 jgdavidson Exp $, compiled: " __DATE__ " " __TIME__; |
38 | |
39 | #include "nsd.h" |
40 | |
41 | /* |
42 | * Prototypes for procedures defined later in this file: |
43 | */ |
44 | |
45 | static void <a href="/cvs/aolserver/aolserver/nsd/tclobj.c#A_SetTimeInternalRep">SetTimeInternalRep</a>(Tcl_Obj *objPtr, Ns_Time *timePtr); |
46 | static int <a href="/cvs/aolserver/aolserver/nsd/tclobj.c#A_SetTimeFromAny">SetTimeFromAny</a> (Tcl_Interp *interp, Tcl_Obj *objPtr); |
47 | static void <a href="/cvs/aolserver/aolserver/nsd/tclobj.c#A_UpdateStringOfTime">UpdateStringOfTime</a>(Tcl_Obj *objPtr); |
48 | |
49 | /* |
50 | * The following type defines the Ns_Time type. |
51 | */ |
52 | |
53 | static Tcl_ObjType timeType = { |
54 | "ns:time", |
55 | (Tcl_FreeInternalRepProc *) NULL, |
56 | (Tcl_DupInternalRepProc *) NULL, |
57 | <a href="/cvs/aolserver/aolserver/nsd/tclobj.c#A_UpdateStringOfTime">UpdateStringOfTime</a>, |
58 | <a href="/cvs/aolserver/aolserver/nsd/tclobj.c#A_SetTimeFromAny">SetTimeFromAny</a> |
59 | }; |
60 | |
61 | static Tcl_ObjType *intTypePtr; |
62 | |
63 | |
64 | /* |
65 | *---------------------------------------------------------------------- |
66 | * |
67 | * <a href="/cvs/aolserver/aolserver/nsd/tclobj.c#A_NsTclInitTimeType">NsTclInitTimeType</a> -- |
68 | * |
69 | * Initialize Ns_Time Tcl_Obj type. |
70 | * |
71 | * Results: |
72 | * None. |
73 | * |
74 | * Side effects: |
75 | * None. |
76 | * |
77 | *---------------------------------------------------------------------- |
78 | */ |
79 | |
80 | void |
81 | <a href="/cvs/aolserver/aolserver/nsd/tclobj.c#A_NsTclInitTimeType">NsTclInitTimeType</a>() |
82 | { |
83 | Tcl_Obj obj; |
84 | |
85 | if (sizeof(obj.internalRep) < sizeof(Ns_Time)) { |
86 | Tcl_Panic("NsTclInitObjs: sizeof(obj.internalRep) < sizeof(Ns_Time)"); |
87 | } |
88 | intTypePtr = Tcl_GetObjType("int"); |
89 | if (intTypePtr == NULL) { |
90 | Tcl_Panic("NsTclInitObjs: no int type"); |
91 | } |
92 | Tcl_RegisterObjType(&timeType); |
93 | } |
94 | |
95 | |
96 | /* |
97 | *---------------------------------------------------------------------- |
98 | * |
99 | * <a href="/cvs/aolserver/aolserver/nsd/tclobj.c#A_Ns_TclSetTimeObj">Ns_TclSetTimeObj</a> -- |
100 | * |
101 | * Set a Tcl_Obj to an Ns_Time object. |
102 | * |
103 | * Results: |
104 | * None. |
105 | * |
106 | * Side effects: |
107 | * String rep is invalidated and internal rep is set. |
108 | * |
109 | *---------------------------------------------------------------------- |
110 | */ |
111 | |
112 | void |
113 | <a href="/cvs/aolserver/aolserver/nsd/tclobj.c#A_Ns_TclSetTimeObj">Ns_TclSetTimeObj</a>(Tcl_Obj *objPtr, Ns_Time *timePtr) |
114 | { |
115 | Tcl_InvalidateStringRep(objPtr); |
116 | <a href="/cvs/aolserver/aolserver/nsd/tclobj.c#A_SetTimeInternalRep">SetTimeInternalRep</a>(objPtr, timePtr); |
117 | } |
118 | |
119 | |
120 | /* |
121 | *---------------------------------------------------------------------- |
122 | * |
123 | * <a href="/cvs/aolserver/aolserver/nsd/tclobj.c#A_Ns_TclGetTimeFromObj">Ns_TclGetTimeFromObj</a> -- |
124 | * |
125 | * Return the internal value of an Ns_Time Tcl_Obj. |
126 | * |
127 | * Results: |
128 | * TCL_OK or TCL_ERROR if not a valid Ns_Time. |
129 | * |
130 | * Side effects: |
131 | * Object is set to Ns_Time type if necessary. |
132 | * |
133 | *---------------------------------------------------------------------- |
134 | */ |
135 | |
136 | int |
137 | <a href="/cvs/aolserver/aolserver/nsd/tclobj.c#A_Ns_TclGetTimeFromObj">Ns_TclGetTimeFromObj</a>(Tcl_Interp *interp, Tcl_Obj *objPtr, Ns_Time *timePtr) |
138 | { |
139 | if (objPtr->typePtr == intTypePtr) { |
140 | if (Tcl_GetLongFromObj(interp, objPtr, &timePtr->sec) != TCL_OK) { |
141 | return TCL_ERROR; |
142 | } |
143 | timePtr->usec = 0; |
144 | } else { |
145 | if (Tcl_ConvertToType(interp, objPtr, &timeType) != TCL_OK) { |
146 | return TCL_ERROR; |
147 | } |
148 | *timePtr = *((Ns_Time *) &objPtr->internalRep); |
149 | } |
150 | return TCL_OK; |
151 | } |
152 | |
153 | |
154 | /* |
155 | *---------------------------------------------------------------------- |
156 | * |
157 | * <a href="/cvs/aolserver/aolserver/nsd/tclobj.c#A_UpdateStringOfTime">UpdateStringOfTime</a> -- |
158 | * |
159 | * Update the string representation for an Ns_Time object. |
160 | * Note: This procedure does not free an existing old string rep |
161 | * so storage will be lost if this has not already been done. |
162 | * |
163 | * Results: |
164 | * None. |
165 | * |
166 | * Side effects: |
167 | * The object's string is set to a valid string that results from |
168 | * the Ns_Time-to-string conversion. |
169 | * |
170 | *---------------------------------------------------------------------- |
171 | */ |
172 | |
173 | static void |
174 | <a href="/cvs/aolserver/aolserver/nsd/tclobj.c#A_UpdateStringOfTime">UpdateStringOfTime</a>(objPtr) |
175 | register Tcl_Obj *objPtr; /* Int object whose string rep to update. */ |
176 | { |
177 | Ns_Time *timePtr = (Ns_Time *) &objPtr->internalRep; |
178 | size_t len; |
179 | char buf[100]; |
180 | |
181 | Ns_AdjTime(timePtr); |
182 | if (timePtr->usec == 0) { |
183 | len = sprintf(buf, "%ld", timePtr->sec); |
184 | } else { |
185 | len = sprintf(buf, "%ld:%ld", timePtr->sec, timePtr->usec); |
186 | } |
187 | objPtr->length = len; |
188 | objPtr->bytes = ckalloc(len + 1); |
189 | memcpy(objPtr->bytes, buf, len + 1); |
190 | } |
191 | |
192 | |
193 | /* |
194 | *---------------------------------------------------------------------- |
195 | * |
196 | * <a href="/cvs/aolserver/aolserver/nsd/tclobj.c#A_SetTimeFromAny">SetTimeFromAny</a> -- |
197 | * |
198 | * Attempt to generate an Ns_Time internal form for the Tcl object. |
199 | * |
200 | * Results: |
201 | * The return value is a standard object Tcl result. If an error occurs |
202 | * during conversion, an error message is left in the interpreter's |
203 | * result unless "interp" is NULL. |
204 | * |
205 | * Side effects: |
206 | * If no error occurs, an int is stored as "objPtr"s internal |
207 | * representation. |
208 | * |
209 | *---------------------------------------------------------------------- |
210 | */ |
211 | |
212 | static int |
213 | <a href="/cvs/aolserver/aolserver/nsd/tclobj.c#A_SetTimeFromAny">SetTimeFromAny</a>(Tcl_Interp *interp, Tcl_Obj *objPtr) |
214 | { |
215 | char *str; |
216 | Ns_Time time; |
217 | |
218 | str = Tcl_GetString(objPtr); |
219 | if (objPtr->typePtr == intTypePtr || strchr(str, ':') == NULL) { |
220 | if (Tcl_GetLongFromObj(interp, objPtr, &time.sec) != TCL_OK) { |
221 | return TCL_ERROR; |
222 | } |
223 | time.usec = 0; |
224 | } else if (sscanf(str, "%ld:%ld", &time.sec, &time.usec) != 2) { |
225 | Tcl_AppendResult(interp, "invalid time spec \"", str, |
226 | "\": expected sec:usec", NULL); |
227 | return TCL_ERROR; |
228 | } |
229 | Ns_AdjTime(&time); |
230 | <a href="/cvs/aolserver/aolserver/nsd/tclobj.c#A_SetTimeInternalRep">SetTimeInternalRep</a>(objPtr, &time); |
231 | return TCL_OK; |
232 | } |
233 | |
234 | |
235 | /* |
236 | *---------------------------------------------------------------------- |
237 | * |
238 | * <a href="/cvs/aolserver/aolserver/nsd/tclobj.c#A_SetTimeInternalRep">SetTimeInternalRep</a> -- |
239 | * |
240 | * Set the internal Ns_Time, freeing a previous internal rep if |
241 | * necessary. |
242 | * |
243 | * Results: |
244 | * None. |
245 | * |
246 | * Side effects: |
247 | * Object will be an Ns_Time type. |
248 | * |
249 | *---------------------------------------------------------------------- |
250 | */ |
251 | |
252 | static void |
253 | <a href="/cvs/aolserver/aolserver/nsd/tclobj.c#A_SetTimeInternalRep">SetTimeInternalRep</a>(Tcl_Obj *objPtr, Ns_Time *timePtr) |
254 | { |
255 | Tcl_ObjType *typePtr = objPtr->typePtr; |
256 | |
257 | if (typePtr != NULL && typePtr->freeIntRepProc != NULL) { |
258 | (*typePtr->freeIntRepProc)(objPtr); |
259 | } |
260 | objPtr->typePtr = &timeType; |
261 | *((Ns_Time *) &objPtr->internalRep) = *timePtr; |
262 | Tcl_InvalidateStringRep(objPtr); |
263 | objPtr->length = 0; /* ensure there's no stumbling */ |
264 | } |
Copyright © 2010 Geeknet, Inc. All rights reserved. Terms of Use