| 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 |
|
|---|
| 14 | TGlobalFilelist* 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 |
|
|---|
| 45 | void CloseGlobalFilelist( TGlobalFilelist* pList )
|
|---|
| 46 | {
|
|---|
| 47 | ReleaseSubAllocatedSharedMemory( & pList->FMem );
|
|---|
| 48 | DosCloseMutexSem( pList->FMutex );
|
|---|
| 49 |
|
|---|
| 50 | free( pList );
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | TPOpenFileEntry GetHead( TGlobalFilelist* pList )
|
|---|
| 54 | {
|
|---|
| 55 | return (TPOpenFileEntry) pList->FMem.FMem.FPointer;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | /*
|
|---|
| 59 | procedure TGlobalFilelist.SetHead( pEntry: TPOpenFileEntry );
|
|---|
| 60 | begin
|
|---|
| 61 | TPOpenFileEntry( FMem.Data ^ ) := pEntry;
|
|---|
| 62 | end;
|
|---|
| 63 |
|
|---|
| 64 | procedure TGlobalFilelist.AddFile( const FilePath: string;
|
|---|
| 65 | Window: HWND );
|
|---|
| 66 | var
|
|---|
| 67 | pEntry: TPOpenFileEntry;
|
|---|
| 68 | begin
|
|---|
| 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;
|
|---|
| 94 | end;
|
|---|
| 95 | */
|
|---|
| 96 |
|
|---|
| 97 | TPOpenFileEntry 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 |
|
|---|
| 117 | HWND 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
|
|---|
| 136 | procedure TGlobalFilelist.RemoveFile( Window: HWND;
|
|---|
| 137 | const FilePath: string );
|
|---|
| 138 | var
|
|---|
| 139 | pEntry: TPOpenFileEntry;
|
|---|
| 140 | pPrevious: TPOpenFileEntry;
|
|---|
| 141 | begin
|
|---|
| 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 );
|
|---|
| 176 | end;
|
|---|
| 177 |
|
|---|
| 178 | procedure TGlobalFilelist.RemoveWindowFiles( Window: HWND );
|
|---|
| 179 | var
|
|---|
| 180 | pEntry: TPOpenFileEntry;
|
|---|
| 181 | pPrevious: TPOpenFileEntry;
|
|---|
| 182 | begin
|
|---|
| 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;
|
|---|
| 207 | end;
|
|---|
| 208 | */
|
|---|