source: trunk/hlpmgr/globalfilelist.c@ 145

Last change on this file since 145 was 26, checked in by RBRi, 19 years ago

+ helpmgr files

  • Property svn:eol-style set to native
File size: 4.4 KB
Line 
1#include <string.h>
2#include <stdlib.h>
3
4#define INCL_DOSERRORS
5#define INCL_DOSSEMAPHORES
6#define INCL_DOSPROCESS
7#include <os2.h>
8
9#include "GlobalFilelist.h"
10
11// size of shared mem used for storing global filelist
12#define GLOBAL_FILELIST_SIZE 64000
13
14TGlobalFilelist* OpenGlobalFilelist()
15{
16 APIRET rc;
17 TGlobalFilelist* pList;
18
19 pList = (TGlobalFilelist*) malloc( sizeof( TGlobalFilelist ) );
20 if ( pList == NULL )
21 return NULL;
22
23 rc = GetSubAllocatedSharedMemory( "NEWVIEW_GLOBAL_FILELIST",
24 GLOBAL_FILELIST_SIZE,
25 4, // space for head pointer
26 & pList->FMem );
27 if ( rc != 0 )
28 {
29 free( pList );
30 return NULL;
31 }
32
33 rc = OpenOrCreateMutex( "NEWVIEW_FILELIST",
34 & pList->FMutex );
35 if ( rc != 0 )
36 {
37 ReleaseSubAllocatedSharedMemory( & pList->FMem );
38 free( pList );
39 return NULL;
40 }
41
42 return pList;
43}
44
45void CloseGlobalFilelist( TGlobalFilelist* pList )
46{
47 ReleaseSubAllocatedSharedMemory( & pList->FMem );
48 DosCloseMutexSem( pList->FMutex );
49
50 free( pList );
51}
52
53TPOpenFileEntry GetHead( TGlobalFilelist* pList )
54{
55 return (TPOpenFileEntry) pList->FMem.FMem.FPointer;
56}
57
58/*
59procedure TGlobalFilelist.SetHead( pEntry: TPOpenFileEntry );
60begin
61 TPOpenFileEntry( FMem.Data ^ ) := pEntry;
62end;
63
64procedure TGlobalFilelist.AddFile( const FilePath: string;
65 Window: HWND );
66var
67 pEntry: TPOpenFileEntry;
68begin
69 FMutex.Get;
70
71 try
72 FMem.Allocate( pEntry,
73 sizeof( TPOpenFileEntry )
74 + sizeof( HWND )
75 + 1 // string length byte
76 + Length( FilePath ) );
77 except
78 begin
79 // didn't fit: discard
80 FMutex.Release;
81 exit;
82 end;
83 end;
84
85 // store handle, filename
86 pEntry ^. Window := Window;
87 pEntry ^. FilePath := FilePath;
88
89 // link into list at head
90 pEntry ^. Next := GetHead;
91 SetHead( pEntry );
92
93 FMutex.Release;
94end;
95*/
96
97TPOpenFileEntry FindEntry( TGlobalFilelist* pList,
98 char* FilePath )
99{
100 TPOpenFileEntry Result;
101
102 Result = GetHead( pList );
103
104 while ( Result != NULL )
105 {
106 if ( stricmp( Result->FilePath, FilePath ) )
107 {
108 // found
109 return Result;
110 }
111 Result = Result->Next;
112 }
113 // not found
114 return NULL;
115}
116
117HWND FindFileInGlobalFilelist( TGlobalFilelist* pList,
118 char* FilePath )
119{
120 TPOpenFileEntry pEntry;
121
122 DosRequestMutexSem( pList->FMutex, SEM_INDEFINITE_WAIT );
123
124 pEntry = FindEntry( pList, FilePath );
125
126 DosReleaseMutexSem( pList->FMutex );
127
128 if ( pEntry != NULL )
129 return pEntry -> Window;
130 else
131 return NULLHANDLE;
132}
133
134/*
135// Remove specified file from list
136procedure TGlobalFilelist.RemoveFile( Window: HWND;
137 const FilePath: string );
138var
139 pEntry: TPOpenFileEntry;
140 pPrevious: TPOpenFileEntry;
141begin
142 FMutex.Get;
143
144 pEntry := GetHead;
145 pPrevious := nil;
146 while ( pEntry <> nil ) do
147 begin
148 if ( pEntry ^. Window = Window ) then
149 begin
150 if ( StringsSame( pEntry ^. FilePath, FilePath ) ) then
151 begin
152 // found
153 // remove node from list
154 if pPrevious = nil then
155 // head node has changed
156 SetHead( pEntry ^.Next )
157 else
158 // point previous node to next node
159 pPrevious ^.Next := pEntry ^.Next;
160
161 FMem.Free( pEntry );
162
163 // done
164 FMutex.Release;
165 exit;
166 end;
167 end;
168 pPrevious := pEntry;
169 pEntry := pEntry ^. Next;
170 end;
171
172 // not found! Bad programmer event
173 FMutex.Release;
174
175 raise Exception.Create( 'GlobalFilelist: File cannot be removed, since not added: ' + FilePath );
176end;
177
178procedure TGlobalFilelist.RemoveWindowFiles( Window: HWND );
179var
180 pEntry: TPOpenFileEntry;
181 pPrevious: TPOpenFileEntry;
182begin
183 FMutex.Get;
184
185 pEntry := GetHead;
186 pPrevious := nil;
187 while ( pEntry <> nil ) do
188 begin
189 if ( pEntry ^. Window = Window ) then
190 begin
191 // found
192 // remove node from list
193 if pPrevious = nil then
194 // head node has changed
195 SetHead( pEntry ^.Next )
196 else
197 // point previous node to next node
198 pPrevious ^.Next := pEntry ^.Next;
199
200 FMem.Free( pEntry );
201 // continue looking - there could be more for this window
202 end;
203 pPrevious := pEntry;
204 pEntry := pEntry ^. Next;
205 end;
206 FMutex.Release;
207end;
208*/
Note: See TracBrowser for help on using the repository browser.