123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- #include "main.h"
- #include "./iconStore.h"
- #include <vector>
- typedef struct IconStoreRecord
- {
- unsigned int width;
- unsigned int height;
- wchar_t *path;
- } IconStoreRecord;
- typedef std::vector<IconStoreRecord> RecodList;
- struct IconStore
- {
- RecodList list;
- wchar_t *basePath;
- };
- IconStore *
- IconStore_Create()
- {
- IconStore *self;
-
- self = new IconStore();
- if (NULL == self)
- return NULL;
- self->basePath = NULL;
- return self;
- }
- void
- IconStore_Destroy(IconStore *self)
- {
- size_t index;
- IconStoreRecord *record;
- if (NULL == self)
- return;
- index = self->list.size();
- while(index--)
- {
- record = &self->list[index];
- String_Free(record->path);
- }
- String_Free(self->basePath);
- }
- BOOL
- IconStore_Add(IconStore *self, const wchar_t *path, unsigned int width, unsigned int height)
- {
- IconStoreRecord record, *prec;
- size_t index;
- if (NULL == self)
- return FALSE;
-
- if(width < 1)
- width = 1;
- if(height < 1)
- height = 1;
-
- index = self->list.size();
- while(index--)
- {
- prec = &self->list[index];
- if (width == prec->width &&
- height == prec->height)
- {
- String_Free(prec->path);
- prec->path = String_Duplicate(path);
- return TRUE;
- }
- }
- record.path = String_Duplicate(path);
- record.width = width;
- record.height = height;
- self->list.push_back(record);
- return TRUE;
- }
- BOOL
- IconStore_RemovePath(IconStore *self, const wchar_t *path)
- {
- size_t index;
- IconStoreRecord *record;
- if (NULL == self)
- return FALSE;
- index = self->list.size();
- while(index--)
- {
- record = &self->list[index];
- if(CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE, record->path, -1, path, -1))
- {
- self->list.eraseAt(index);
- }
- }
- return TRUE;
- }
- BOOL
- IconStore_Remove(IconStore *self, unsigned int width, unsigned int height)
- {
- size_t index;
- IconStoreRecord *record;
- if (NULL == self)
- return FALSE;
- index = self->list.size();
- while(index--)
- {
- record = &self->list[index];
- if(record->width == width &&
- record->height == height)
- {
- self->list.eraseAt(index);
- }
- }
- return TRUE;
- }
- static BOOL
- IconStore_GetFullPath(IconStore *self, const wchar_t *path, wchar_t *buffer, size_t bufferMax)
- {
- if (NULL == buffer)
- return FALSE;
- if (NULL == self)
- return FALSE;
- if (FALSE != IS_STRING_EMPTY(path))
- {
- *buffer = L'\0';
- return TRUE;
- }
- if (FALSE == PathIsRelative(path) ||
- IS_STRING_EMPTY(self->basePath))
- {
- if (0 == String_CopyTo(buffer, path, bufferMax) &&
- FALSE == IS_STRING_EMPTY(path))
- return FALSE;
- else
- return TRUE;
- }
- if (NULL == PathCombine(buffer, self->basePath, path))
- return FALSE;
- return TRUE;
- }
- BOOL
- IconStore_Get(IconStore *self, wchar_t *buffer, size_t bufferMax, unsigned int width, unsigned int height)
- {
- const wchar_t *icon;
- IconStoreRecord *record;
- size_t index;
- double widthDbl, heightDbl;
- double scaleMin, scaleHorz, scaleVert;
- if (NULL == self)
- return FALSE;
- if (NULL == buffer)
- return FALSE;
- icon = NULL;
-
- widthDbl = width;
- heightDbl = height;
- index = self->list.size();
- if (index > 0)
- {
- record = &self->list[--index];
- scaleHorz = widthDbl/record->width;
- scaleVert = heightDbl/record->height;
- scaleMin = (scaleHorz < scaleVert) ? scaleHorz : scaleVert;
- icon = record->path;
- if (1.0 != scaleMin)
- {
- scaleMin = fabs(1.0 - scaleMin);
- while(index--)
- {
- record = &self->list[index];
- scaleHorz = widthDbl/record->width;
- scaleVert = heightDbl/record->height;
- if (scaleHorz > scaleVert)
- scaleHorz = scaleVert;
-
- if (1.0 == scaleHorz)
- {
- icon = record->path;
- break;
- }
-
- scaleHorz = fabs(1.0 - scaleHorz);
- if (scaleHorz < scaleMin)
- {
- scaleMin = scaleHorz;
- icon = record->path;
- }
- }
- }
- }
- return IconStore_GetFullPath(self, icon, buffer, bufferMax);
- }
- BOOL
- IconStore_SetBasePath(IconStore *self, const wchar_t *path)
- {
- if (NULL == self)
- return FALSE;
- String_Free(self->basePath);
- self->basePath = String_Duplicate(path);
- return TRUE;
- }
- IconStore *
- IconStore_Clone(IconStore *self)
- {
- size_t index;
- IconStore *clone;
- IconStoreRecord targetRec;
- const IconStoreRecord *sourceRec;
-
- if (NULL == self)
- return NULL;
- clone = IconStore_Create();
- if (NULL == clone)
- return NULL;
- clone->basePath = String_Duplicate(self->basePath);
- for(index = 0; index < self->list.size(); index++)
- {
- sourceRec = &self->list[index];
- targetRec.path = String_Duplicate(sourceRec->path);
- targetRec.width = sourceRec->width;
- targetRec.height = sourceRec->height;
- clone->list.push_back(targetRec);
- }
- return clone;
- }
- BOOL
- IconStore_Enumerate(IconStore *self, IconEnumerator callback, void *user)
- {
- size_t index;
- wchar_t buffer[MAX_PATH*2];
- IconStoreRecord *record;
- if (NULL == self || NULL == callback)
- return FALSE;
- index = self->list.size();
- while(index--)
- {
- record = &self->list[index];
- if (FALSE != IconStore_GetFullPath(self, record->path, buffer, ARRAYSIZE(buffer)))
- {
- if (FALSE == callback(buffer, record->width, record->height, user))
- return TRUE;
- }
- }
- return TRUE;
- }
|