summaryrefslogtreecommitdiff
path: root/plugins/Folders/src/utils.cpp
diff options
context:
space:
mode:
authorGeorge Hazan <george.hazan@gmail.com>2012-08-07 12:10:19 +0000
committerGeorge Hazan <george.hazan@gmail.com>2012-08-07 12:10:19 +0000
commit52a0c6e6efb7014b87552097c13ceea6175ce377 (patch)
treedc4d3e62d27d6538672c9e80166ed945cf659a2f /plugins/Folders/src/utils.cpp
parent97ecd2b560357a1c7756d7b93d0b820f63071080 (diff)
folders -> TCHAR
git-svn-id: http://svn.miranda-ng.org/main/trunk@1389 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins/Folders/src/utils.cpp')
-rw-r--r--plugins/Folders/src/utils.cpp331
1 files changed, 87 insertions, 244 deletions
diff --git a/plugins/Folders/src/utils.cpp b/plugins/Folders/src/utils.cpp
index 64623b63f1..df4131ff84 100644
--- a/plugins/Folders/src/utils.cpp
+++ b/plugins/Folders/src/utils.cpp
@@ -20,50 +20,19 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "utils.h"
-int Log(char *format, ...)
-{
-#ifdef _DEBUG
- char str[4096];
- va_list vararg;
- int tBytes;
- FILE *fout = fopen("folders.log", "at");
-
- va_start(vararg, format);
-
- tBytes = _vsnprintf(str, sizeof(str), format, vararg);
- if (tBytes > 0)
- {
- str[tBytes] = 0;
- }
-
- va_end(vararg);
- if (str[strlen(str) - 1] != '\n')
- {
- strcat(str, "\n");
- }
- fputs(str, fout);
- fclose(fout);
-#endif
- return 0;
-}
-
char *StrCopy(char *source, size_t index, const char *what, size_t count)
{
- size_t i;
- for (i = 0; i < count; i++)
- {
- source[index + i] = what[i];
- }
+ for (size_t i = 0; i < count; i++)
+ source[index + i] = what[i];
+
return source;
}
wchar_t *StrCopy(wchar_t *source, size_t index, const wchar_t *what, size_t count)
{
- size_t i;
- for (i = 0; i < count; i++)
- {
- source[index + i] = what[i];
- }
+ for (size_t i = 0; i < count; i++)
+ source[index + i] = what[i];
+
return source;
}
@@ -73,38 +42,32 @@ char *StrDelete(char *source, size_t index, size_t count)
size_t i;
count = (count + index > len) ? len - index : count;
for (i = index; i + count <= len; i++)
- {
- source[i] = source[i + count];
- }
+ source[i] = source[i + count];
+
return source;
}
wchar_t *StrDelete(wchar_t *source, size_t index, size_t count)
{
size_t len = wcslen(source);
- size_t i;
count = (count + index > len) ? len - index : count;
- for (i = index; i + count <= len; i++)
- {
- source[i] = source[i + count];
- }
+ for (size_t i = index; i + count <= len; i++)
+ source[i] = source[i + count];
+
return source;
}
-
char *StrInsert(char *source, size_t index, const char *what)
{
size_t whatLen = strlen(what);
size_t sourceLen = strlen(source);
size_t i;
for (i = sourceLen; i >= index; i--)
- {
- source[i + whatLen] = source[i];
- }
+ source[i + whatLen] = source[i];
+
for (i = 0; i < whatLen; i++)
- {
- source[index + i] = what[i];
- }
+ source[index + i] = what[i];
+
return source;
}
@@ -114,68 +77,51 @@ wchar_t *StrInsert(wchar_t *source, size_t index, const wchar_t *what)
size_t sourceLen = wcslen(source);
size_t i;
for (i = sourceLen; i >= index; i--)
- {
- source[i + whatLen] = source[i];
- }
+ source[i + whatLen] = source[i];
+
for (i = 0; i < whatLen; i++)
- {
- source[index + i] = what[i];
- }
+ source[index + i] = what[i];
+
return source;
}
-
char *StrReplace(char *source, const char *what, const char *withWhat)
{
- char *pos;
size_t whatLen = strlen(what);
size_t withWhatLen = strlen(withWhat);
- size_t minLen;
- size_t index;
- while ((pos = strstr(source, what)))
- {
- minLen = min(whatLen, withWhatLen);
- StrCopy(source, pos - source, withWhat, minLen);
- index = pos - source + minLen;
- if (whatLen > withWhatLen)
- {
- StrDelete(source, index, whatLen - withWhatLen);
- }
- else{
- if (whatLen < withWhatLen)
- {
- StrInsert(source, index, withWhat + minLen);
- }
- }
- }
+ char *pos;
+ while ((pos = strstr(source, what))) {
+ size_t minLen = min(whatLen, withWhatLen);
+ StrCopy(source, pos - source, withWhat, minLen);
+ size_t index = pos - source + minLen;
+ if (whatLen > withWhatLen)
+ StrDelete(source, index, whatLen - withWhatLen);
+ else {
+ if (whatLen < withWhatLen)
+ StrInsert(source, index, withWhat + minLen);
+ }
+ }
return source;
}
wchar_t *StrReplace(wchar_t *source, const wchar_t *what, const wchar_t *withWhat)
{
- wchar_t *pos;
size_t whatLen = wcslen(what);
size_t withWhatLen = wcslen(withWhat);
- size_t minLen;
- size_t index;
-
- while ((pos = wcsstr(source, what)))
- {
- minLen = min(whatLen, withWhatLen);
- StrCopy(source, pos - source, withWhat, minLen);
- index = pos - source + minLen;
- if (whatLen > withWhatLen)
- {
- StrDelete(source, index, whatLen - withWhatLen);
- }
- else{
- if (whatLen < withWhatLen)
- {
- StrInsert(source, index, withWhat + minLen);
- }
- }
- }
+
+ wchar_t *pos;
+ while ((pos = wcsstr(source, what))) {
+ size_t minLen = min(whatLen, withWhatLen);
+ StrCopy(source, pos - source, withWhat, minLen);
+ size_t index = pos - source + minLen;
+ if (whatLen > withWhatLen)
+ StrDelete(source, index, whatLen - withWhatLen);
+ else {
+ if (whatLen < withWhatLen)
+ StrInsert(source, index, withWhat + minLen);
+ }
+ }
return source;
}
@@ -183,18 +129,15 @@ char *StrTrim(char *szText, const char *szTrimChars)
{
size_t i = strlen(szText) - 1;
while ((i >= 0) && (strchr(szTrimChars, szText[i])))
- {
- szText[i--] = '\0';
- }
+ szText[i--] = '\0';
+
i = 0;
while ((i < strlen(szText)) && (strchr(szTrimChars, szText[i])))
- {
- i++;
- }
+ i++;
+
if (i)
- {
- StrDelete(szText, 0, i);
- }
+ StrDelete(szText, 0, i);
+
return szText;
}
@@ -202,156 +145,56 @@ wchar_t *StrTrim(wchar_t *szText, const wchar_t *szTrimChars)
{
size_t i = wcslen(szText) - 1;
while ((i >= 0) && (wcschr(szTrimChars, szText[i])))
- {
- szText[i--] = '\0';
- }
+ szText[i--] = '\0';
+
i = 0;
while ((i < wcslen(szText)) && (wcschr(szTrimChars, szText[i])))
- {
- i++;
- }
- if (i)
- {
- StrDelete(szText, 0, i);
- }
- return szText;
-}
-
-void CreateDirectories(char *path)
-{
- char *pos = path;
- char tmp;
- while (pos = strchr(pos, '\\'))
- {
- tmp = pos[0];
- pos[0] = '\0';
- CreateDirectoryA(path, NULL);
- pos[0] = tmp;
- pos++;
- }
- CreateDirectoryA(path, NULL);
- GetLastError();
-}
-
-void CreateDirectories(wchar_t *path)
-{
- wchar_t *pos = path;
- wchar_t tmp;
- while (pos = wcschr(pos, '\\'))
- {
- tmp = pos[0];
- pos[0] = '\0';
- CreateDirectoryW(path, NULL);
- pos[0] = tmp;
- pos++;
- }
- CreateDirectoryW(path, NULL);
- GetLastError();
-}
+ i++;
-void RemoveDirectories(char *path)
-{
- char *pos;
- char *buffer = _strdup(path);
- if (!(GetFileAttributesA(buffer) & FILE_ATTRIBUTE_REPARSE_POINT)) { RemoveDirectoryA(buffer); }
- while (pos = strrchr(buffer, '\\'))
- {
- pos[0] = '\0';
- if (!(GetFileAttributesA(buffer) & FILE_ATTRIBUTE_REPARSE_POINT)) { RemoveDirectoryA(buffer); }
- }
- free(buffer);
-}
-
-void RemoveDirectories(wchar_t *path)
-{
- wchar_t *pos;
- wchar_t *buffer = _wcsdup(path);
- if (!(GetFileAttributesW(buffer) & FILE_ATTRIBUTE_REPARSE_POINT)) { RemoveDirectoryW(buffer); }
- while (pos = wcsrchr(buffer, '\\'))
- {
- pos[0] = '\0';
- if (!(GetFileAttributesW(buffer) & FILE_ATTRIBUTE_REPARSE_POINT)) { RemoveDirectoryW(buffer); }
- }
- free(buffer);
-}
+ if (i)
+ StrDelete(szText, 0, i);
-int DirectoryExists(char *path)
-{
- char buffer[4096];
- GetCurrentDirectoryA(sizeof(buffer), buffer);
- int res = SetCurrentDirectoryA(path);
- SetCurrentDirectoryA(buffer);
- return res;
+ return szText;
}
-int DirectoryExists(wchar_t *path)
+void RemoveDirectories(TCHAR *path)
{
- wchar_t buffer[4096];
- GetCurrentDirectoryW(sizeof(buffer), buffer);
- int res = SetCurrentDirectoryW(path);
- SetCurrentDirectoryW(buffer);
- return res;
+ TCHAR *pos;
+ TCHAR *buffer = NEWWSTR_ALLOCA(path);
+ if (!(GetFileAttributes(buffer) & FILE_ATTRIBUTE_REPARSE_POINT))
+ RemoveDirectory(buffer);
+ while (pos = _tcsrchr(buffer, '\\')) {
+ pos[0] = '\0';
+ if (!(GetFileAttributes(buffer) & FILE_ATTRIBUTE_REPARSE_POINT))
+ RemoveDirectory(buffer);
+ }
}
-
-int GetStringFromDatabase(char *szSettingName, const char *szError, char *szResult, size_t size)
+int DirectoryExists(TCHAR *path)
{
- DBVARIANT dbv = {0};
- int res = 1;
- size_t len;
- dbv.type = DBVT_ASCIIZ;
- if (DBGetContactSetting(NULL, ModuleName, szSettingName, &dbv) == 0)
- {
- res = 0;
- size_t tmp = strlen(dbv.pszVal);
- len = (tmp < size - 1) ? tmp : size - 1;
- strncpy(szResult, dbv.pszVal, len);
- szResult[len] = '\0';
- mir_free(dbv.pszVal);
- }
- else{
- res = 1;
- size_t tmp = strlen(szError);
- len = (tmp < size - 1) ? tmp : size - 1;
- strncpy(szResult, szError, len);
- szResult[len] = '\0';
- }
+ TCHAR buffer[4096];
+ GetCurrentDirectory( SIZEOF(buffer), buffer);
+ int res = SetCurrentDirectory(path);
+ SetCurrentDirectory(buffer);
return res;
}
-int GetStringFromDatabase(char *szSettingName, const wchar_t *szError, wchar_t *szResult, size_t size)
+int GetStringFromDatabase(char *szSettingName, const wchar_t *szError, TCHAR *szResult, size_t size)
{
- DBVARIANT dbv = {0};
- int res = 1;
size_t len;
- dbv.type = DBVT_WCHAR;
- if (DBGetContactSettingWString(NULL, ModuleName, szSettingName, &dbv) == 0)
- //if (DBGetContactSetting(NULL, ModuleName, szSettingName, &dbv) == 0)
- {
- res = 0;
- size_t tmp = wcslen(dbv.pwszVal);
- len = (tmp < size - 1) ? tmp : size - 1;
- wcsncpy(szResult, dbv.pwszVal, len);
- szResult[len] = '\0';
- mir_free(dbv.pwszVal);
- }
- else{
- res = 1;
- size_t tmp = wcslen(szError);
- len = (tmp < size - 1) ? tmp : size - 1;
- wcsncpy(szResult, szError, len);
- szResult[len] = '\0';
- }
- return res;
-}
-
-
-int WriteStringToDatabase(char *szSettingName, const char *szValue)
-{
- return DBWriteContactSettingString(NULL, ModuleName, szSettingName, szValue);
+ DBVARIANT dbv;
+ if ( DBGetContactSettingWString(NULL, ModuleName, szSettingName, &dbv) == 0) {
+ size_t tmp = _tcslen(dbv.ptszVal);
+ len = (tmp < size - 1) ? tmp : size - 1;
+ _tcsncpy(szResult, dbv.ptszVal, len);
+ szResult[len] = '\0';
+ db_free(&dbv);
+ return 0;
+ }
+
+ size_t tmp = _tcslen(szError);
+ len = (tmp < size - 1) ? tmp : size - 1;
+ _tcsncpy(szResult, szError, len);
+ szResult[len] = '\0';
+ return 1;
}
-
-int WriteStringToDatabase(char *szSettingName, const wchar_t *szValue)
-{
- return DBWriteContactSettingWString(NULL, ModuleName, szSettingName, szValue);
-} \ No newline at end of file