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
|
2012-01-23 06:36:15 +00:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2009-02-10 18:37:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <time.h>
|
|
|
|
#include <shlwapi.h>
|
2013-01-27 10:58:16 +00:00
|
|
|
#include "../API/RainmeterAPI.h"
|
2011-02-03 18:09:24 +00:00
|
|
|
#include "../../Library/DisableThreadLibraryCalls.h" // contains DllMain entry point
|
2013-01-27 10:58:16 +00:00
|
|
|
#include "../../Common/StringUtil.h"
|
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;
|
|
|
|
};
|
|
|
|
|
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);
|
2012-08-27 21:04:27 +00:00
|
|
|
if (hSearch == INVALID_HANDLE_VALUE) return; // No more files found
|
|
|
|
|
2009-02-10 18:37:48 +00:00
|
|
|
do
|
|
|
|
{
|
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));
|
2012-08-27 21:04:27 +00:00
|
|
|
|
|
|
|
FindClose(hSearch);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
2012-02-02 07:39:14 +00:00
|
|
|
PLUGIN_EXPORT void Initialize(void** data, void* rm)
|
2012-01-08 17:35:29 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2013-05-31 14:28:39 +00:00
|
|
|
srand((unsigned)time(nullptr));
|
2012-01-08 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2013-05-31 14:28:39 +00:00
|
|
|
WCHAR* sepPos1 = nullptr;
|
|
|
|
WCHAR* sepPos2 = nullptr;
|
2012-01-08 17:35:29 +00:00
|
|
|
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());
|
2013-05-31 14:28:39 +00:00
|
|
|
if (sepPos1 == nullptr)
|
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
|
|
|
}
|
2013-05-31 14:28:39 +00:00
|
|
|
while (sepPos1 == nullptr);
|
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());
|
2013-05-31 14:28:39 +00:00
|
|
|
if (sepPos2 == nullptr)
|
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
|
|
|
}
|
|
|
|
}
|
2013-05-31 14:28:39 +00:00
|
|
|
while (sepPos2 == nullptr);
|
2012-01-08 17:35:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// It's ascii
|
|
|
|
char* aBuffer = (char*)buffer;
|
|
|
|
|
2013-01-27 10:58:16 +00:00
|
|
|
const std::string separator = StringUtil::Narrow(measure->separator);
|
|
|
|
const char* separatorSz = separator.c_str();
|
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
// Read until we find the first separator
|
2013-05-31 14:28:39 +00:00
|
|
|
char* sepPos1 = nullptr;
|
|
|
|
char* sepPos2 = nullptr;
|
2012-01-08 17:35:29 +00:00
|
|
|
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
|
|
|
|
2013-01-27 10:58:16 +00:00
|
|
|
sepPos1 = strstr(aBuffer, separatorSz);
|
2013-05-31 14:28:39 +00:00
|
|
|
if (sepPos1 == nullptr)
|
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
|
|
|
|
{
|
2013-01-27 10:58:16 +00:00
|
|
|
sepPos1 += separator.size();
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
2012-01-08 17:35:29 +00:00
|
|
|
}
|
2013-05-31 14:28:39 +00:00
|
|
|
while (sepPos1 == nullptr);
|
2009-07-27 20:06:07 +00:00
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
// Find the second separator
|
|
|
|
do
|
|
|
|
{
|
2013-01-27 10:58:16 +00:00
|
|
|
sepPos2 = strstr(sepPos1, separatorSz);
|
2013-05-31 14:28:39 +00:00
|
|
|
if (sepPos2 == nullptr)
|
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
|
2013-01-27 10:58:16 +00:00
|
|
|
measure->value += StringUtil::Widen(sepPos1);
|
2012-01-08 17:35:29 +00:00
|
|
|
break;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
2009-07-27 20:06:07 +00:00
|
|
|
else
|
|
|
|
{
|
2013-01-27 10:58:16 +00:00
|
|
|
measure->value += StringUtil::Widen(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
|
2013-01-27 10:58:16 +00:00
|
|
|
measure->value += StringUtil::Widen(sepPos1);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-31 14:28:39 +00:00
|
|
|
while (sepPos2 == nullptr);
|
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
|
|
|
}
|