2009-02-10 18:37:48 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2005 Kimmo Pekkola
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <time.h>
|
|
|
|
#include <shlwapi.h>
|
2011-02-03 18:09:24 +00:00
|
|
|
#include "../../Library/Export.h" // Rainmeter's exported functions
|
|
|
|
#include "../../Library/DisableThreadLibraryCalls.h" // contains DllMain entry point
|
2011-01-30 09:31:41 +00:00
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
#define BUFFER_SIZE 4096
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
struct MeasureData
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
|
|
|
std::wstring pathname;
|
|
|
|
std::wstring separator;
|
|
|
|
std::vector<std::wstring> files;
|
|
|
|
std::wstring value;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string ConvertToAscii(LPCTSTR str)
|
|
|
|
{
|
|
|
|
std::string szAscii;
|
|
|
|
|
2009-09-12 11:11:40 +00:00
|
|
|
if (str && *str)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
int strLen = (int)wcslen(str);
|
2009-09-12 11:11:40 +00:00
|
|
|
int bufLen = WideCharToMultiByte(CP_ACP, 0, str, strLen, NULL, 0, NULL, NULL);
|
|
|
|
if (bufLen > 0)
|
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
szAscii.resize(bufLen);
|
|
|
|
WideCharToMultiByte(CP_ACP, 0, str, strLen, &szAscii[0], bufLen, NULL, NULL);
|
2009-09-12 11:11:40 +00:00
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
return szAscii;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::wstring ConvertToWide(LPCSTR str)
|
|
|
|
{
|
|
|
|
std::wstring szWide;
|
|
|
|
|
2009-09-12 11:11:40 +00:00
|
|
|
if (str && *str)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
int strLen = (int)strlen(str);
|
2009-09-12 11:11:40 +00:00
|
|
|
int bufLen = MultiByteToWideChar(CP_ACP, 0, str, strLen, NULL, 0);
|
|
|
|
if (bufLen > 0)
|
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
szWide.resize(bufLen);
|
|
|
|
MultiByteToWideChar(CP_ACP, 0, str, strLen, &szWide[0], bufLen);
|
2009-09-12 11:11:40 +00:00
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
return szWide;
|
|
|
|
}
|
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
void ScanFolder(std::vector<std::wstring>& files, std::vector<std::wstring>& filters, bool bSubfolders, const std::wstring& path)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
|
|
|
// Get folder listing
|
|
|
|
WIN32_FIND_DATA fileData; // Data structure describes the file found
|
|
|
|
HANDLE hSearch; // Search handle returned by FindFirstFile
|
|
|
|
|
|
|
|
std::wstring searchPath = path + L"*";
|
|
|
|
|
|
|
|
hSearch = FindFirstFile(searchPath.c_str(), &fileData);
|
|
|
|
do
|
|
|
|
{
|
2011-03-29 19:21:57 +00:00
|
|
|
if (hSearch == INVALID_HANDLE_VALUE) break; // No more files found
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2011-03-29 19:21:57 +00:00
|
|
|
if (fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
if (bSubfolders &&
|
|
|
|
wcscmp(fileData.cFileName, L".") != 0 &&
|
|
|
|
wcscmp(fileData.cFileName, L"..") != 0)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
ScanFolder(files, filters, bSubfolders, path + fileData.cFileName + L"\\");
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
if (!filters.empty())
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
for (int i = 0; i < filters.size(); ++i)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
if (!filters[i].empty() && PathMatchSpec(fileData.cFileName, filters[i].c_str()))
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
files.push_back(path + fileData.cFileName);
|
2009-02-10 18:37:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
files.push_back(path + fileData.cFileName);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-03-29 19:21:57 +00:00
|
|
|
while (FindNextFile(hSearch, &fileData));
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
PLUGIN_EXPORT void Initialize(void** data)
|
|
|
|
{
|
|
|
|
MeasureData* measure = new MeasureData;
|
|
|
|
*data = measure;
|
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
PLUGIN_EXPORT void Reload(void* data, void* rm, double* maxValue)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
MeasureData* measure = (MeasureData*)data;
|
|
|
|
|
|
|
|
measure->pathname = RmReadPath(rm, L"PathName", L"");
|
|
|
|
|
|
|
|
if (PathIsDirectory(measure->pathname.c_str()))
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
std::vector<std::wstring> fileFilters;
|
|
|
|
LPCWSTR filter = RmReadString(rm, L"FileFilter", L"");
|
|
|
|
if (*filter)
|
|
|
|
{
|
|
|
|
std::wstring ext = filter;
|
|
|
|
|
|
|
|
size_t start = 0;
|
|
|
|
size_t pos = ext.find(L';');
|
|
|
|
while (pos != std::wstring::npos)
|
|
|
|
{
|
|
|
|
fileFilters.push_back(ext.substr(start, pos - start));
|
|
|
|
start = pos + 1;
|
|
|
|
pos = ext.find(L';', pos + 1);
|
|
|
|
}
|
|
|
|
fileFilters.push_back(ext.substr(start));
|
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
if (measure->pathname[measure->pathname.size() - 1] != L'\\')
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
measure->pathname += L"\\";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan files
|
|
|
|
measure->files.clear();
|
|
|
|
bool bSubfolders = RmReadInt(rm, L"Subfolders", 1) == 1;
|
|
|
|
ScanFolder(measure->files, fileFilters, bSubfolders, measure->pathname);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
measure->separator = RmReadString(rm, L"Separator", L"\n");
|
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
srand((unsigned)time(NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
PLUGIN_EXPORT double Update(void* data)
|
|
|
|
{
|
|
|
|
MeasureData* measure = (MeasureData*)data;
|
|
|
|
|
|
|
|
if (measure->files.empty())
|
|
|
|
{
|
|
|
|
BYTE buffer[BUFFER_SIZE + 2];
|
|
|
|
buffer[BUFFER_SIZE] = 0;
|
|
|
|
|
|
|
|
// Read the file
|
|
|
|
FILE* file = _wfopen(measure->pathname.c_str(), L"r");
|
|
|
|
if (file)
|
|
|
|
{
|
|
|
|
// Check if the file is unicode or ascii
|
|
|
|
fread(buffer, sizeof(WCHAR), 1, file);
|
|
|
|
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
long size = ftell(file);
|
|
|
|
|
|
|
|
if (size > 0)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
// Go to a random place
|
|
|
|
int pos = rand() % size;
|
|
|
|
fseek(file, (pos / 2) * 2, SEEK_SET);
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
measure->value.clear();
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
if (0xFEFF == *(WCHAR*)buffer)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
// It's unicode
|
|
|
|
WCHAR* wBuffer = (WCHAR*)buffer;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
// Read until we find the first separator
|
|
|
|
WCHAR* sepPos1 = NULL;
|
|
|
|
WCHAR* sepPos2 = NULL;
|
|
|
|
do
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
size_t len = fread(buffer, sizeof(BYTE), BUFFER_SIZE, file);
|
|
|
|
buffer[len] = 0;
|
|
|
|
buffer[len + 1] = 0;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
sepPos1 = wcsstr(wBuffer, measure->separator.c_str());
|
|
|
|
if (sepPos1 == NULL)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
// The separator wasn't found
|
|
|
|
if (feof(file))
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
// End of file reached -> read from start
|
|
|
|
fseek(file, 2, SEEK_SET);
|
|
|
|
len = fread(buffer, sizeof(BYTE), BUFFER_SIZE, file);
|
|
|
|
buffer[len] = 0;
|
|
|
|
buffer[len + 1] = 0;
|
|
|
|
sepPos1 = wBuffer;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
2012-01-08 17:35:29 +00:00
|
|
|
// else continue reading
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sepPos1 += measure->separator.size();
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
2012-01-08 17:35:29 +00:00
|
|
|
}
|
|
|
|
while (sepPos1 == NULL);
|
2009-07-27 20:06:07 +00:00
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
// Find the second separator
|
|
|
|
do
|
|
|
|
{
|
|
|
|
sepPos2 = wcsstr(sepPos1, measure->separator.c_str());
|
|
|
|
if (sepPos2 == NULL)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
// The separator wasn't found
|
|
|
|
if (feof(file))
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
// End of file reached -> read the rest
|
|
|
|
measure->value += sepPos1;
|
|
|
|
break;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
2009-07-27 20:06:07 +00:00
|
|
|
else
|
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
measure->value += sepPos1;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
// else continue reading
|
|
|
|
size_t len = fread(buffer, sizeof(BYTE), BUFFER_SIZE, file);
|
|
|
|
buffer[len] = 0;
|
|
|
|
buffer[len + 1] = 0;
|
|
|
|
sepPos1 = wBuffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (sepPos2)
|
|
|
|
{
|
|
|
|
*sepPos2 = 0;
|
2009-07-27 20:06:07 +00:00
|
|
|
}
|
2012-01-08 17:35:29 +00:00
|
|
|
|
|
|
|
// Read until we find the second separator
|
|
|
|
measure->value += sepPos1;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-08 17:35:29 +00:00
|
|
|
while (sepPos2 == NULL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// It's ascii
|
|
|
|
char* aBuffer = (char*)buffer;
|
|
|
|
|
|
|
|
// Read until we find the first separator
|
|
|
|
char* sepPos1 = NULL;
|
|
|
|
char* sepPos2 = NULL;
|
|
|
|
do
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
size_t len = fread(buffer, sizeof(char), BUFFER_SIZE, file);
|
|
|
|
aBuffer[len] = 0;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
sepPos1 = strstr(aBuffer, ConvertToAscii(measure->separator.c_str()).c_str());
|
|
|
|
if (sepPos1 == NULL)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
// The separator wasn't found
|
|
|
|
if (feof(file))
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
// End of file reached -> read from start
|
|
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
len = fread(buffer, sizeof(char), BUFFER_SIZE, file);
|
|
|
|
aBuffer[len] = 0;
|
|
|
|
sepPos1 = aBuffer;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
2012-01-08 17:35:29 +00:00
|
|
|
// else continue reading
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sepPos1 += measure->separator.size();
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
2012-01-08 17:35:29 +00:00
|
|
|
}
|
|
|
|
while (sepPos1 == NULL);
|
2009-07-27 20:06:07 +00:00
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
// Find the second separator
|
|
|
|
do
|
|
|
|
{
|
|
|
|
sepPos2 = strstr(sepPos1, ConvertToAscii(measure->separator.c_str()).c_str());
|
|
|
|
if (sepPos2 == NULL)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
// The separator wasn't found
|
|
|
|
if (feof(file))
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
// End of file reached -> read the rest
|
|
|
|
measure->value += ConvertToWide(sepPos1);
|
|
|
|
break;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
2009-07-27 20:06:07 +00:00
|
|
|
else
|
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
measure->value += ConvertToWide(sepPos1);
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
// else continue reading
|
|
|
|
size_t len = fread(buffer, sizeof(char), BUFFER_SIZE, file);
|
|
|
|
aBuffer[len] = 0;
|
|
|
|
sepPos1 = aBuffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (sepPos2)
|
|
|
|
{
|
|
|
|
*sepPos2 = 0;
|
2009-07-27 20:06:07 +00:00
|
|
|
}
|
2012-01-08 17:35:29 +00:00
|
|
|
|
|
|
|
// Read until we find the second separator
|
|
|
|
measure->value += ConvertToWide(sepPos1);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-08 17:35:29 +00:00
|
|
|
while (sepPos2 == NULL);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-08 17:35:29 +00:00
|
|
|
|
|
|
|
fclose(file);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-08 17:35:29 +00:00
|
|
|
else
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
// Select the filename
|
|
|
|
measure->value = measure->files[rand() % measure->files.size()];
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
return 0;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
PLUGIN_EXPORT LPCWSTR GetString(void* data)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
MeasureData* measure = (MeasureData*)data;
|
|
|
|
return measure->value.c_str();
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
PLUGIN_EXPORT void Finalize(void* data)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
MeasureData* measure = (MeasureData*)data;
|
|
|
|
delete measure;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|