⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filefilemanagement.cpp

📁 funambol windows mobile plugin source code, the source code is taken from the funambol site
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    else if (dataType == constFavorites) {
        SHGetSpecialFolderPath(NULL, localTemp, CSIDL_FAVORITES, FALSE);
        wsprintf(internalPath, TEXT("%s\\"), localTemp);
        wsprintf(wfilename, TEXT("%s\\%s"), path, TEXT(FILENAME_FAVORITES));
        recursiveInDirectory = TRUE;
    }

    f = _wfopen(wfilename, TEXT("w+"));

    getFileList(internalPath, fileList, &count, TRUE, recursiveInDirectory, filterPattern);
    fileList = new wchar_t*[count];
    int m = 0;
    for (m = 0; m < count ; m++) {
        fileList[m] = NULL;
    }
    // to retrieve fileName
    getFileList(internalPath, fileList, &count, FALSE, recursiveInDirectory, filterPattern);

    wsprintf(localTemp, TEXT("%f\n"), getSystemTime());
    fwprintf(f, TEXT("%s"), localTemp);

    for (i = 0; i < count; i++) {
        unsigned long sizeFile = 0;
        getFileSize(fileList[i], &sizeFile);
        if (sizeFile > getMaxObjectSizeToSend()) {
            LOG.debug("writeCurrentFileItems: found file greater then maxObjectSize(%lu): sizefile = %lu", getMaxObjectSizeToSend(), sizeFile);
            continue;
        }

        fwprintf(f, TEXT("%s"), fileList[i]);
        fwprintf(f, TEXT("\n"));
    }
    fflush(f);
    fclose(f);


    for (i = 0; i < count; i++) {
        if (fileList[i] != NULL)
            delete [] fileList[i];
    }

    if (fileList != NULL)
        delete [] fileList;

}

int readFilenameFromFile(int dataType, const wchar_t* path, wchar_t*** ptrArrayFilename) {

    wchar_t wfilename [DIM_FILE];
    wchar_t pt        [DIM_FILE];
    wchar_t** ptr     = NULL;
    int i             = 0;

    if (dataType == constFiles)
        wsprintf(pt, TEXT(FILENAME_FILES));
    else if (dataType == constNotes)
        wsprintf(pt, TEXT(FILENAME_NOTES));
    else if (dataType == constFavorites)
        wsprintf(pt, TEXT(FILENAME_FAVORITES));

    wsprintf(wfilename, TEXT("%s\\%s"),  path, pt);

    wchar_t* element = NULL;
    wchar_t line[256];
    FILE* f;
    wchar_t* tmp = NULL;

    f = _wfopen(wfilename, TEXT("r"));

    if (f == NULL) {
        return 0;
    }

    while(fgetws(line, 255, f) != NULL) {
        i++;
    }
    fflush(f);

    ptr = new wchar_t* [i];

    i = 0;
    f = _wfreopen(wfilename, TEXT("r"), f);

    while(fgetws(line, 255, f) != NULL) {

        ptr[i] = new wchar_t[255];
        wsprintf(ptr[i], TEXT("%s"), line);

        //
        // to remove carriage/return at the end of file. CarriageReturn was introduced
        // by method writeCurrentFileItems to insert new line
        //
        removeEndCarriage(&ptr[i]);

        i++;
    }
    fflush(f);
    fclose(f);

    *ptrArrayFilename = ptr;
    return i - 1;
}
/*
* get file size
*/

BOOL getFileSize(wchar_t* filename, unsigned long *size) {


   HANDLE file = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
                              NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

   DWORD lpFileSizeHigh;
   DWORD dwSize;

   if( file ) {
        dwSize = GetFileSize(file, &lpFileSizeHigh);

        if (dwSize != 0xFFFFFFFF) {
            *size = dwSize;

        }
        else {
            *size = 0;
        }
   }

   if( file ) CloseHandle( file );

   return TRUE;
}

/*
*
*/

BOOL getLastWriteTime(wchar_t* filename, wchar_t* wtime, DATE* dtime) {


    HANDLE file = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
                              NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    // FILETIME time;

    FILETIME ftCreate, ftAccess, ftWrite, stUTC;
    SYSTEMTIME  stLocal;
    BOOL isUTC = TRUE;

    //wchar_t propertyValue[10];
    //getClientConfigurationInternal (NULL, PROPERTY_SOURCEPROP_SPDM_SYSTEMTIME_TO_LOCALTIME, propertyValue, FALSE);

    //if (wcscmp(propertyValue, TEXT("1")) == 0) {
    //    isUTC = TRUE;
    //}

    if( file ) {
        
        if (!GetFileTime(file, &ftCreate, &ftAccess, &ftWrite)) {
            return FALSE;
        }
        else {
        }

        if (isUTC) {
            FileTimeToSystemTime(&ftWrite, &stLocal);
        }
        else {
            FileTimeToLocalFileTime(&ftWrite, &stUTC);
            FileTimeToSystemTime   (&stUTC, &stLocal);
        }

        wchar_t month [10];
        wchar_t day   [10];
        wchar_t hour  [10];
        wchar_t minute[10];
        wchar_t second[10];

        // month
        if (stLocal.wMonth < 10)
            wsprintf(month, TEXT("0%i"), stLocal.wMonth);
        else
            wsprintf(month, TEXT("%i"), stLocal.wMonth);

        // day
        if (stLocal.wDay  < 10)
            wsprintf(day, TEXT("0%i"), stLocal.wDay);
        else
            wsprintf(day, TEXT("%i"), stLocal.wDay);

        // hour
        if (stLocal.wHour  < 10)
            wsprintf(hour, TEXT("0%i"), stLocal.wHour);
        else
            wsprintf(hour, TEXT("%i"), stLocal.wHour);

        // minute
        if (stLocal.wMinute < 10)
            wsprintf(minute, TEXT("0%i"), stLocal.wMinute);
        else
            wsprintf(minute, TEXT("%i"), stLocal.wMinute);

        // second
        if (stLocal.wSecond < 10)
            wsprintf(second, TEXT("0%i"), stLocal.wSecond);
        else
            wsprintf(second, TEXT("%i"), stLocal.wSecond);


        if (isUTC) {
            wsprintf(wtime, TEXT("%i%s%sT%s%s%sZ"), stLocal.wYear, month, day, hour, minute, second);
        } else {
            wsprintf(wtime, TEXT("%i%s%sT%s%s%s"), stLocal.wYear, month, day, hour, minute, second);
        }

        if (dtime != NULL)
            systemTimeToDouble(wtime, dtime, NULL);

   }
   else {
 }
   if( file ) CloseHandle( file );

   return TRUE;
}

BOOL getFileList(wchar_t *dir, wchar_t** ptrArray, int *count, BOOL onlyFilesNumber,
                 BOOL isRecursiveInDirectory) {

    return getFileList(dir, ptrArray, count, onlyFilesNumber, isRecursiveInDirectory, NULL);
}

/*
* method to retrieve number or filename directory dir.
* it may be recursive if isRecursiveDirectory is TRUE.
* if onlyFilesNumber is TRUE, count contains the number of file to process
*/

BOOL getFileList(wchar_t *dir, wchar_t** ptrArray, int *count, BOOL onlyFilesNumber,
                 BOOL isRecursiveInDirectory, wchar_t* filter) {

    WIN32_FIND_DATA FileData;

    HANDLE hFind;
    wchar_t localTemp [512];
    wchar_t toFind    [512];
    wchar_t szNewPath [512];
    wchar_t filterPattern [256];

    BOOL ret = TRUE;
    szNewPath[0] =   0;
    int lung = 0;

    DWORD dwAttrs;

    BOOL fFinished = FALSE;


    if (filter == NULL || wcslen(filter) == 0)
        wcscpy(filterPattern, TEXT("*.*"));
    else
        wsprintf(filterPattern, TEXT("%s"), filter);


    wsprintf(toFind, TEXT("%s%s"), dir, filterPattern);
    
    hFind = FindFirstFile(toFind, &FileData);

    int i = 0;

    if (hFind == INVALID_HANDLE_VALUE)
    {
        return FALSE;
    }
    else
    {
        if (onlyFilesNumber == TRUE)  {
            //only to retrieve number of files
            while (!fFinished)
            {
                wsprintf(localTemp, TEXT("%s%s"), dir, FileData.cFileName);

                dwAttrs = GetFileAttributes(localTemp);

                // if (dwAttrs & FILE_ATTRIBUTE_DIRECTORY) {
                if (dwAttrs == FILE_ATTRIBUTE_DIRECTORY) {

                    if (isRecursiveInDirectory == TRUE && currentLevel < recursiveLevel) {

                        currentLevel++;

                        wsprintf(localTemp, TEXT("%s%s%s"), dir, FileData.cFileName, TEXT("\\") );

                        int k = 0;
                        getFileList(localTemp, NULL, &k, TRUE, isRecursiveInDirectory, filterPattern);
                        i = i + k + 1; // + 1 is to count directory too!!

                        currentLevel--;

                    }

                }
                else {
                    i++;
                }
                if (!FindNextFile(hFind, &FileData))
                {
                    if (GetLastError() == ERROR_NO_MORE_FILES)     {
                        fFinished = TRUE;
                    } else {
                        return FALSE;
                    }
                }
            }
            *count = i;
        }

        else {

            while (!fFinished)
            {

                wsprintf(szNewPath, TEXT("%s%s"), dir, FileData.cFileName);

                dwAttrs = GetFileAttributes(szNewPath);

                if (dwAttrs == FILE_ATTRIBUTE_DIRECTORY) {

                    if (isRecursiveInDirectory == TRUE && currentLevel < recursiveLevel) {

                        currentLevel++;

                        wsprintf(localTemp, TEXT("%s%s%s"), dir, FileData.cFileName, TEXT("\\") );

                        int j = 0;
                        ptrArray[i] = new wchar_t[256];
                        wsprintf(ptrArray[i], TEXT("%s"), localTemp);
                        i++;
                        getFileList(localTemp, &ptrArray[i], &j, FALSE, isRecursiveInDirectory);
                        i = i + j;

                        currentLevel--;

                    }


                }
                else {
                    ptrArray[i] = new wchar_t[256];
                    wsprintf(ptrArray[i], TEXT("%s"), szNewPath);
                    i++;
                }

                wcscpy(szNewPath, TEXT(""));


                if (!FindNextFile(hFind, &FileData))
                    {
                    if (GetLastError() == ERROR_NO_MORE_FILES)
                    {

                        fFinished = TRUE;
                    }
                    else
                    {
                        return FALSE;
                    }
                }
                *count = i;
            }
        }

    // Close the search handle.

    FindClose(hFind);
    }
    return ret;

}

/*
* The key can be a file name so it can contain some special char to be encoded.
* The method get an array of wchar and overwrite the current key with the encoded one.
*/
void encodeKey(wchar_t* key) {

    if (IS_KEY_BASE64 == FALSE)
        return;

    wchar_t* encoded = NULL;
    encoded = encodeBase64UTF8(key);

    wcscpy(key, encoded);

    if (encoded != NULL)
        delete [] encoded;

}

/*
* The key can be a file name so it can contain some special char to be encoded.
* The method get an array of wchar and overwrite the current key with the encoded one.
*/
void decodeKey(wchar_t* key) {

    if (IS_KEY_BASE64 == FALSE)
        return;

    wchar_t* decoded = NULL;
    decoded = decodeBase64WUTF8(key);

    wcscpy(key, decoded);

    if (decoded != NULL)
        delete [] decoded;

}


char* readTextFromBinaryFileChar(wchar_t* filename) {

    HINSTANCE   hLib;
    HWND   hwndInk = NULL;
    LPTSTR lpszClass = TEXT("richink") ;
    EDITSTREAM   es;
    COOKIE      cookie;
    HLOCAL sz;
    UINT cbSz;
    char* ret = NULL;

    // open file if only if exists
    HANDLE hfile = CreateFile(filename, GENERIC_READ, 0,
                              NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    hLib = LoadLibrary( TEXT("richink.dll") );
    if (!hLib)
      return NULL;

    hwndInk = CreateWindow(lpszClass, NULL, WS_BORDER, 0, 0, 0, 0, NULL, 0, NULL, NULL);

    if (!hwndInk)
      return NULL;

    // Stream in the Ink Data from a memory block.
    memset (&cookie, 0, sizeof(cookie));
    cookie.dwError      = 0;
    cookie.pbStart      = 0;
    cookie.pbCur        = cookie.pbStart;
    cookie.bCount       = 0;
    cookie.hFile        = hfile;
    es.dwCookie         = (DWORD)&cookie;
    es.dwError          = 0;
    es.pfnCallback      = ReadCallback;
    SendMessage (hwndInk, EM_STREAMIN, (WPARAM)SF_PWI, (LPARAM)&es);

    // Next call stream out to get the size of the Unicode text.
    cookie.dwError      = 0;
    cookie.pbStart      = 0;
    cookie.pbCur        = 0;
    cookie.bCount       = 0;
    es.dwCookie         = (DWORD)&cookie;
    es.dwError          = 0;
    es.pfnCallback      = BufferWriteCallback;
    SendMessage (hwndInk, EM_STREAMOUT, (WPARAM)SF_TEXT, (LPARAM)&es);

    // Allocate memory and stream out the Unicode text to it.
    sz = (LPWSTR)LocalAlloc(LPTR, (cbSz = cookie.pbCur - cookie.pbStart) + 2);

    // Check for alloc err.
    cookie.dwError      = 0;
    cookie.pbStart      = (LPBYTE)sz;
    cookie.pbCur        = cookie.pbStart;
    cookie.bCount       = cbSz;
    es.dwCookie         = (DWORD)&cookie;
    es.dwError          = 0;
    es.pfnCallback      = BufferWriteCallback;
    SendMessage (hwndInk, EM_STREAMOUT, (WPARAM)SF_TEXT, (LPARAM)&es);

    wchar_t* tt = toWideChar((char*)sz, "ascii");
    ret = toMultibyte(tt);

    CloseHandle(hfile);

    if (tt) { delete [] tt; tt = NULL; }

    return ret;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -