Previous 15994 Revisions Next

r8213 Friday 4th June, 2010 at 08:40:14 UTC by Miodrag Milanović
[MESSUI] Cleared software list code a bit
[src/mess/osd/winui]messui.c softwarelist.c

src/mess/osd/winui/messui.c
r8212r8213
462462               {
463463                  for (software_info *swinfo = software_list_first(list); swinfo != NULL; swinfo = software_list_next(list))
464464                  {                     
465                     SoftwareList_AddFile(hwndSoftwareList, core_strdup(swinfo->shortname), core_strdup(swlist->list_name[i]), core_strdup(swinfo->longname), core_strdup(swinfo->publisher), core_strdup(swinfo->year));
465                     SoftwareList_AddFile(hwndSoftwareList, swinfo->shortname, swlist->list_name[i], swinfo->longname, swinfo->publisher, swinfo->year);
466466                  }
467467
468468                  software_list_close(list);
src/mess/osd/winui/softwarelist.c
r8212r8213
3838typedef struct _file_info file_info;
3939struct _file_info
4040{
41   const device_config *device;
42
43   // hash information
44   char hash_string[HASH_BUF_SIZE];
45   BOOL hash_realized;
46   const hash_info *hashinfo;
47
48   const char *zip_entry_name;
4941   char list_name[256];
5042   char description[256];
5143   char file_name[256];
r8212r8213
5345   char year[10];
5446};
5547
56typedef struct _directory_search_info directory_search_info;
57struct _directory_search_info
58{
59   directory_search_info *next;
60   HANDLE find_handle;
61   WIN32_FIND_DATA fd;
62   char directory_name[1];
63};
64
6548typedef struct _software_list_info software_list_info;
6649struct _software_list_info
6750{
6851   WNDPROC old_window_proc;
6952   file_info **file_index;
7053   int file_index_length;
71   int hashes_realized;
72   int current_position;
73   directory_search_info *first_search_info;
74   directory_search_info *last_search_info;
7554   const software_config *config;
7655};
7756
r8212r8213
10786
10887
10988
110const device_config *SoftwareList_LookupDevice(HWND hwndPicker, int nIndex)
111{
112   software_list_info *pPickerInfo;
113   pPickerInfo = GetSoftwareListInfo(hwndPicker);
114   if ((nIndex < 0) || (nIndex >= pPickerInfo->file_index_length))
115   {
116      return NULL;
117   }
118   return pPickerInfo->file_index[nIndex]->device;
119}
120
121
122
12389int SoftwareList_LookupIndex(HWND hwndPicker, LPCSTR pszFilename)
12490{
12591   software_list_info *pPickerInfo;
r8212r8213
138104
139105iodevice_t SoftwareList_GetImageType(HWND hwndPicker, int nIndex)
140106{
141   iodevice_t type;
142   const device_config *device = SoftwareList_LookupDevice(hwndPicker, nIndex);
143
144   if (device != NULL)
145   {
146      type = image_device_getinfo(GetSoftwareListInfo(hwndPicker)->config->mconfig, device).type;
147   }
148   else
149   {
150      type = IO_UNKNOWN;
151   }
152   return type;
107   return IO_UNKNOWN;
153108}
154109
155110
156111
157112void SoftwareList_SetDriver(HWND hwndPicker, const software_config *config)
158113{
159   int i;
160114   software_list_info *pPickerInfo;
161115
162116   pPickerInfo = GetSoftwareListInfo(hwndPicker);
163117   pPickerInfo->config = config;
164
165   // invalidate the hash "realization"
166   for (i = 0; i < pPickerInfo->file_index_length; i++)
167   {
168      pPickerInfo->file_index[i]->hashinfo = NULL;
169      pPickerInfo->file_index[i]->hash_realized = FALSE;
170   }
171   pPickerInfo->hashes_realized = 0;
172118}
173119
174120
175
176static void ComputeFileHash(software_list_info *pPickerInfo,
177   file_info *pFileInfo, const unsigned char *pBuffer, unsigned int nLength)
178{
179   image_device_info info;
180   unsigned int functions;
181
182   // get the device info
183   info = image_device_getinfo(pPickerInfo->config->mconfig, pFileInfo->device);
184
185   // determine which functions to use
186   functions = hashfile_functions_used(pPickerInfo->config->hashfile, info.type);
187
188   // compute the hash
189   image_device_compute_hash(pFileInfo->hash_string, pFileInfo->device, pBuffer, nLength, functions);
190}
191
192
193
194static BOOL SoftwareList_CalculateHash(HWND hwndPicker, int nIndex)
195{
196   software_list_info *pPickerInfo;
197   file_info *pFileInfo;
198   LPSTR pszZipName;
199   BOOL rc = FALSE;
200   unsigned char *pBuffer;
201   unsigned int nLength;
202   HANDLE hFile, hFileMapping;
203   LVFINDINFO lvfi;
204   BOOL res;
205
206   pPickerInfo = GetSoftwareListInfo(hwndPicker);
207   assert((nIndex >= 0) && (nIndex < pPickerInfo->file_index_length));
208   pFileInfo = pPickerInfo->file_index[nIndex];
209
210   if (pFileInfo->zip_entry_name)
211   {
212      // this is in a ZIP file
213      zip_file *zip;
214      zip_error ziperr;
215      const zip_file_header *zipent;
216
217      // open the ZIP file
218      nLength = pFileInfo->zip_entry_name - pFileInfo->file_name;
219      pszZipName = (LPSTR) alloca(nLength);
220      memcpy(pszZipName, pFileInfo->file_name, nLength);
221      pszZipName[nLength - 1] = '\0';
222
223      // get the entry name
224      ziperr = zip_file_open(pszZipName, &zip);
225      if (ziperr == ZIPERR_NONE)
226      {
227         zipent = zip_file_first_file(zip);
228         while(!rc && zipent)
229         {
230            if (!mame_stricmp(zipent->filename, pFileInfo->zip_entry_name))
231            {
232               pBuffer = (unsigned char *)malloc(zipent->uncompressed_length);
233               if (pBuffer)
234               {
235                  ziperr = zip_file_decompress(zip, pBuffer, zipent->uncompressed_length);
236                  if (ziperr == ZIPERR_NONE)
237                  {
238                     ComputeFileHash(pPickerInfo, pFileInfo, pBuffer, zipent->uncompressed_length);
239                     rc = TRUE;
240                  }
241                  free(pBuffer);
242               }
243            }
244            zipent = zip_file_next_file(zip);
245         }
246         zip_file_close(zip);
247      }
248   }
249   else
250   {
251      // plain open file; map it into memory and calculate the hash
252      hFile = win_create_file_utf8(pFileInfo->file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
253         OPEN_EXISTING, 0, NULL);
254      if (hFile != INVALID_HANDLE_VALUE)
255      {
256         nLength = GetFileSize(hFile, NULL);
257         hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
258         if (hFileMapping)
259         {
260            pBuffer = (unsigned char *)MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0);
261            if (pBuffer)
262            {
263               ComputeFileHash(pPickerInfo, pFileInfo, pBuffer, nLength);
264               UnmapViewOfFile(pBuffer);
265               rc = TRUE;
266            }
267            CloseHandle(hFileMapping);
268         }
269         CloseHandle(hFile);
270      }
271   }
272
273   if (rc)
274   {
275      memset(&lvfi, 0, sizeof(lvfi));
276      lvfi.flags = LVFI_PARAM;
277      lvfi.lParam = nIndex;
278      nIndex = ListView_FindItem(hwndPicker, -1, &lvfi);
279      if (nIndex > 0)
280         res = ListView_RedrawItems(hwndPicker, nIndex, nIndex);
281   }
282
283   return rc;
284}
285
286
287
288static void SoftwareList_RealizeHash(HWND hwndPicker, int nIndex)
289{
290   software_list_info *pPickerInfo;
291   file_info *pFileInfo;
292   unsigned int nHashFunctionsUsed = 0;
293   unsigned int nCalculatedHashes = 0;
294   iodevice_t type;
295
296   pPickerInfo = GetSoftwareListInfo(hwndPicker);
297   assert((nIndex >= 0) && (nIndex < pPickerInfo->file_index_length));
298   pFileInfo = pPickerInfo->file_index[nIndex];
299
300   // Determine which hash functions we need to use for this file, and which hashes
301   // have already been calculated
302   if ((pPickerInfo->config->hashfile != NULL) && (pFileInfo->device != NULL))
303   {
304      image_device_info info = image_device_getinfo(pPickerInfo->config->mconfig, pFileInfo->device);
305      type = info.type;
306      if (type < IO_COUNT)
307           nHashFunctionsUsed = hashfile_functions_used(pPickerInfo->config->hashfile, type);
308      nCalculatedHashes = hash_data_used_functions(pFileInfo->hash_string);
309   }
310
311   // Did we fully compute all hashes?
312   if ((nHashFunctionsUsed & ~nCalculatedHashes) == 0)
313   {
314      // We have calculated all hashs for this file; mark it as realized
315      pPickerInfo->file_index[nIndex]->hash_realized = TRUE;
316      pPickerInfo->hashes_realized++;
317
318      if (pPickerInfo->config->hashfile)
319      {
320         pPickerInfo->file_index[nIndex]->hashinfo = hashfile_lookup(pPickerInfo->config->hashfile,
321            pPickerInfo->file_index[nIndex]->hash_string);
322      }
323   }
324}
325
326
327121BOOL SoftwareList_AddFile(HWND hwndPicker,LPCSTR pszName, LPCSTR pszListname, LPCSTR pszDescription, LPCSTR pszPublisher, LPCSTR pszYear)
328122{
329123   Picker_ResetIdle(hwndPicker);
r8212r8213
332126   file_info **ppNewIndex;
333127   file_info *pInfo;
334128   int nIndex, nSize;
335//   LPCSTR pszExtension = NULL;
336   const device_config *device = NULL;
337129
338130   // first check to see if it is already here
339131   if (SoftwareList_LookupIndex(hwndPicker, pszName) >= 0)
r8212r8213
354146   strcpy(pInfo->description, pszDescription);
355147   strcpy(pInfo->publisher, pszPublisher);
356148   strcpy(pInfo->year, pszYear);
357
358   // set up device and CRC, if specified
359   pInfo->device = device;
360149   
361150   ppNewIndex = (file_info**)malloc((pPickerInfo->file_index_length + 1) * sizeof(*pPickerInfo->file_index));
362151   memcpy(ppNewIndex,pPickerInfo->file_index,pPickerInfo->file_index_length * sizeof(*pPickerInfo->file_index));
r8212r8213
368157   pPickerInfo->file_index = ppNewIndex;
369158   pPickerInfo->file_index[nIndex] = pInfo;
370159
371   // Realize the hash
372   SoftwareList_RealizeHash(hwndPicker, nIndex);
373
374160   // Actually insert the item into the picker
375161   Picker_InsertItemSorted(hwndPicker, nIndex);
376162   return TRUE;
r8212r8213
382168}
383169
384170
385static void SoftwareList_FreeSearchInfo(directory_search_info *pSearchInfo)
386{
387   if (pSearchInfo->find_handle != INVALID_HANDLE_VALUE)
388      FindClose(pSearchInfo->find_handle);
389   free(pSearchInfo);
390}
391
392
393
394171static void SoftwareList_InternalClear(software_list_info *pPickerInfo)
395172{
396   directory_search_info *p;
397173   int i;
398174
399175   for (i = 0; i < pPickerInfo->file_index_length; i++)
400176      free(pPickerInfo->file_index[i]);
401177
402   while(pPickerInfo->first_search_info)
403   {
404      p = pPickerInfo->first_search_info->next;
405      SoftwareList_FreeSearchInfo(pPickerInfo->first_search_info);
406      pPickerInfo->first_search_info = p;
407   }
408
409178   pPickerInfo->file_index = NULL;
410179   pPickerInfo->file_index_length = 0;
411   pPickerInfo->current_position = 0;
412   pPickerInfo->last_search_info = NULL;
413180}
414181
415182
r8212r8213
427194
428195BOOL SoftwareList_Idle(HWND hwndPicker)
429196{
430   software_list_info *pPickerInfo;
431   file_info *pFileInfo;
432   static const char szWildcards[] = "\\*.*";
433   directory_search_info *pSearchInfo;
434   LPSTR pszFilter;
435   BOOL bSuccess;
436   int nCount;
437   BOOL bDone = FALSE;
438   image_device_info info;
439
440   pPickerInfo = GetSoftwareListInfo(hwndPicker);
441
442   pSearchInfo = pPickerInfo->first_search_info;
443
444   if (pSearchInfo)
445   {
446      // searching through directories
447      if (pSearchInfo->find_handle != INVALID_HANDLE_VALUE)
448      {
449         bSuccess = FindNextFile(pSearchInfo->find_handle, &pSearchInfo->fd);
450      }
451      else
452      {
453         pszFilter = (LPSTR)alloca(strlen(pSearchInfo->directory_name) + strlen(szWildcards) + 1);
454         strcpy(pszFilter, pSearchInfo->directory_name);
455         strcat(pszFilter, szWildcards);
456         pSearchInfo->find_handle = win_find_first_file_utf8(pszFilter, &pSearchInfo->fd);
457         bSuccess = pSearchInfo->find_handle != INVALID_HANDLE_VALUE;
458      }
459
460      if (bSuccess)
461      {
462         //SoftwareList_AddEntry(hwndPicker, pSearchInfo);
463      }
464      else
465      {
466         pPickerInfo->first_search_info = pSearchInfo->next;
467         if (!pPickerInfo->first_search_info)
468            pPickerInfo->last_search_info = NULL;
469         SoftwareList_FreeSearchInfo(pSearchInfo);
470      }
471   }
472   else if (pPickerInfo->config!=NULL  && pPickerInfo->config->hashfile && (pPickerInfo->hashes_realized
473      < pPickerInfo->file_index_length))
474   {
475      // time to realize some hashes
476      nCount = 100;
477
478      while((nCount > 0) && pPickerInfo->file_index[pPickerInfo->current_position]->hash_realized)
479      {
480         pPickerInfo->current_position++;
481         pPickerInfo->current_position %= pPickerInfo->file_index_length;
482         nCount--;
483      }
484
485      pFileInfo = pPickerInfo->file_index[pPickerInfo->current_position];
486      if (!pFileInfo->hash_realized)
487      {
488         info = image_device_getinfo(pPickerInfo->config->mconfig, pFileInfo->device);
489         if (hashfile_functions_used(pPickerInfo->config->hashfile, info.type))
490         {
491            // only calculate the hash if it is appropriate for this device
492            if (!SoftwareList_CalculateHash(hwndPicker, pPickerInfo->current_position))
493               return FALSE;
494         }
495         SoftwareList_RealizeHash(hwndPicker, pPickerInfo->current_position);
496
497         // under normal circumstances this will be redundant, but in the unlikely
498         // event of a failure, we do not want to keep running into a brick wall
499         // by calculating this hash over and over
500         if (!pPickerInfo->file_index[pPickerInfo->current_position]->hash_realized)
501         {
502            pPickerInfo->file_index[pPickerInfo->current_position]->hash_realized = TRUE;
503            pPickerInfo->hashes_realized++;
504         }
505      }
506   }
507   else
508   {
509      // we are done!
510      bDone = TRUE;
511   }
512
513   return !bDone;
197   return FALSE;
514198}
515199
516200
r8212r8213
531215
532216   switch(nColumn)
533217   {
534      case MESS_COLUMN_IMAGES:
218      case 0:
535219         t_buf = tstring_from_utf8(pFileInfo->file_name);
536220         if( !t_buf )
537221            return s;

Previous 15994 Revisions Next


© 1998-2018 The MESS Team