1 /***************************************************************************
2  *                                                                         *
3  *  Squish Developers Kit Source, Version 2.00                             *
4  *  Copyright 1989-1994 by SCI Communications.  All rights reserved.       *
5  *                                                                         *
6  *  USE OF THIS FILE IS SUBJECT TO THE RESTRICTIONS CONTAINED IN THE       *
7  *  SQUISH DEVELOPERS KIT LICENSING AGREEMENT IN SQDEV.PRN.  IF YOU DO NOT *
8  *  FIND THE TEXT OF THIS AGREEMENT IN THE AFOREMENTIONED FILE, OR IF YOU  *
9  *  DO NOT HAVE THIS FILE, YOU SHOULD IMMEDIATELY CONTACT THE AUTHOR AT    *
10  *  ONE OF THE ADDRESSES LISTED BELOW.  IN NO EVENT SHOULD YOU PROCEED TO  *
11  *  USE THIS FILE WITHOUT HAVING ACCEPTED THE TERMS OF THE SQUISH          *
12  *  DEVELOPERS KIT LICENSING AGREEMENT, OR SUCH OTHER AGREEMENT AS YOU ARE *
13  *  ABLE TO REACH WITH THE AUTHOR.                                         *
14  *                                                                         *
15  *  You can contact the author at one of the address listed below:         *
16  *                                                                         *
17  *  Scott Dudley       FidoNet     1:249/106                               *
18  *  777 Downing St.    Internet    sjd@f106.n249.z1.fidonet.org            *
19  *  Kingston, Ont.     CompuServe  >INTERNET:sjd@f106.n249.z1.fidonet.org  *
20  *  Canada  K7M 5N3    BBS         1-613-634-3058, V.32bis                 *
21  *                                                                         *
22  ***************************************************************************/
23 /*
24 #pragma off(unreferenced)
25 static char rcs_id[]="$Id$";
26 #pragma on(unreferenced)
27 */
28 #define MSGAPI_HANDLERS
29 #define MSGAPI_NO_OLD_TYPES
30 
31 
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <string.h>
35 #include <fcntl.h>
36 
37 #include <huskylib/compiler.h>
38 
39 #ifdef HAS_IO_H
40 #  include <io.h>
41 #endif
42 #ifdef HAS_SHARE_H
43 #include <share.h>
44 #endif
45 #ifdef HAS_MALLOC_H
46 #include <malloc.h>
47 #endif
48 
49 #include <huskylib/huskylib.h>
50 
51 /* Swith for build DLL */
52 #define DLLEXPORT
53 #include <huskylib/huskyext.h>
54 
55 #include "old_msg.h"
56 #include "msgapi.h"
57 #include "api_sq.h"
58 #include "api_sqp.h"
59 #include "apidebug.h"
60 
61 
62 
63 /* This function returns the UMSGID that will be used by the next message   *
64  * to be created.                                                           */
65 
apiSquishGetNextUid(HAREA ha)66 UMSGID _XPENTRY apiSquishGetNextUid(HAREA ha)
67 {
68   return Sqd->uidNext;
69 }
70 
71 
72 
73 /* This function converts the message number 'dwMsg' into a unique          *
74  * message idenfitier (UMSGID).                                             */
75 
apiSquishMsgnToUid(HAREA ha,dword dwMsg)76 UMSGID _XPENTRY apiSquishMsgnToUid(HAREA ha, dword dwMsg)
77 {
78   SQIDX sqi;
79 
80   if (MsgInvalidHarea(ha))
81     return (UMSGID)0L;
82 
83 
84   /* Make sure that it's a valid message number */
85 
86   if (dwMsg==0 || dwMsg > ha->num_msg)
87   {
88     msgapierr=MERR_NOENT;
89 
90     return (UMSGID)0L;
91   }
92 
93 
94   if (!SidxGet(Sqd->hix, dwMsg, &sqi))
95   {
96     return (UMSGID)0L;
97   }
98 
99   return sqi.umsgid;
100 }
101 
102 
103 /* This function converts the UMSGID in 'uid' into a real message number */
104 
apiSquishUidToMsgn(HAREA ha,UMSGID uid,word wType)105 dword _XPENTRY apiSquishUidToMsgn(HAREA ha, UMSGID uid, word wType)
106 {
107   SQIDX sqi;
108   dword rc=0;
109   sdword stLow, stHigh, stTry;
110   dword dwMax;
111 
112   if (MsgInvalidHarea(ha))
113     return (UMSGID)0L;
114 
115   /* Don't let the user access msg 0 */
116 
117   if (uid==(UMSGID)0L)
118   {
119     msgapierr=MERR_NOENT;
120     return 0L;
121   }
122 
123 /* OG: Exlusive access is required when caching the index */
124 /*
125   if (!_SquishExclusiveBegin(ha))
126   {
127     return 0;
128   }
129 */
130 
131   /* Read the index into memory */
132 
133   if (apiSquishLock(ha) == -1)
134   {
135       apiSquishUnlock(ha);
136       return (dword)0;
137   }
138 
139   /* Set up intial bounds (inclusive) */
140 
141   dwMax=_SquishIndexSize(Sqd->hix) / SQIDX_SIZE;
142   stLow=1;
143   stHigh=(sdword)dwMax;
144   stTry=1;
145 
146   /* Start off with a 0 umsgid */
147 
148   (void)memset(&sqi, 0, sizeof sqi);
149 
150   /* While we still have a search range... */
151 
152   while (stLow <= stHigh)
153   {
154     stTry=(stLow+stHigh) / 2;
155 
156     /* If we got an exact match */
157 
158     if (!SidxGet(Sqd->hix, (dword)stTry, &sqi))
159       break;
160 
161     if (sqi.umsgid==uid)
162     {
163       rc=(dword)stTry;
164       break;
165     }
166     else if (uid > sqi.umsgid)
167       stLow=stTry+1;
168     else stHigh=stTry-1;
169   }
170 
171 
172   /* If we couldn't find it exactly, try the next/prior match */
173 
174   if (!rc)
175   {
176     if (wType==UID_PREV)
177     {
178       if (sqi.umsgid < uid)
179         rc=(dword)stTry;
180       else if (stTry==1)
181         rc=(dword)0;
182       else
183         rc=(dword)(stTry-1L);
184     }
185     else if (wType==UID_NEXT)
186     {
187       if (sqi.umsgid > uid || stTry==(long)dwMax)
188         rc=(dword)stTry;
189       else
190         rc=(dword)(stTry+1L);
191     }
192     else
193       msgapierr=MERR_NOENT;
194   }
195 
196 
197   /* Free the memory used by the index */
198 /*
199   if (! _SquishEndBuffer(Sqd->hix))
200     rc=(dword)0;
201 
202   _SquishExclusiveEnd(ha);
203 */
204 
205   if (apiSquishUnlock(ha) == -1)
206     rc=(dword)0;
207 
208   return rc;
209 }
210 
211 
212 
213