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

📄 fileclient2server.cpp

📁 funambol windows mobile plugin source code, the source code is taken from the funambol site
💻 CPP
📖 第 1 页 / 共 2 页
字号:

    readByteFromFile(filename, ptrFileContent, &numBytes);

    if (numBytes != (int)sizeFile) {
        LOG.error("The number of byte read and size are different");
        goto finally;
    }

    encodeLen = lengthForB64(numBytes);
    base64 = new char[encodeLen + 1];

    if (base64 == NULL) {
        LOG.error("not enough memory to instantiate array...");
    }

    memset(base64, 0, encodeLen + 1);

    b64_encode(base64, ptrFileContent, numBytes);

finally:

    if (ptrFileContent != NULL)
        delete ptrFileContent; ptrFileContent = NULL;

    return base64;
}

*/

/*
* Method to understand if file are new, deleted or updated.
*/

void setModifiedItemsFILE(Container* c, int dataType, const wchar_t* path, const wchar_t* dir) {

    LOG.debug("Enter in setModifiedItemsFILE... ");

    wchar_t   localTemp     [DIM_LOCAL_TEMP];
    wchar_t   internalPath  [DIM_LOCAL_TEMP];
    wchar_t   filterPattern [DIM_LOCAL_TEMP];
    wchar_t   wtime         [DIM_LOCAL_TEMP];

    localTemp[0]        = 0;
    internalPath[0]     = 0;
    wtime[0]            = 0;
    filterPattern[0]    = 0;

    wchar_t** ptrFilenameInDir      = NULL;
    wchar_t*  ptrFileContent        = NULL;
    char* fileContent               = NULL;
    wchar_t** ptrFilenameFromFile   = NULL;
    char* pp = NULL;

    SyncItem* syncItem = NULL;

    int numBytes            = 0;
    int countFileInDir      = 0;
    int countFileInFile     = 0;
    int countNewItems       = 0;
    int countUpdatedItems   = 0;
    int countDeletedItems   = 0;

    int j                   = 1;
    int i                   = 0;
    unsigned long sizeFile  = 0;
    DATE dtime              = 0.0;
    DATE timeInFile         = 0.0;

    BOOL found = FALSE;
    BOOL recursiveInDirectory = FALSE;

    filterPattern[0] = 0;

    wstring key_s;

    if (dataType == constFiles)
        wsprintf(internalPath, TEXT("%s%s"), dir, TEXT("\\"));
    else if (dataType == constNotes) {
        wsprintf(internalPath, TEXT("%s%s"), dir, TEXT("\\"));
        wcscpy(filterPattern, PWI_EXTENSION_PATTERN);
    }
    else if (dataType == constFavorites) {
        SHGetSpecialFolderPath(NULL, internalPath, CSIDL_FAVORITES, FALSE);
        wcscat(internalPath, TEXT("\\"));
        recursiveInDirectory = TRUE;
    }
    /*
    * It avoids to send object greater then MaxObjSize.
    * In next releases it must me inserted into the
    * source.
    */

    //ClientSettings cs(APPLICATION_URI);
    //cs.readConfig();
    int fileRemoved = 0;

    // to retrieve num of files in directory
    getFileList(internalPath, ptrFilenameInDir, &countFileInDir, TRUE, recursiveInDirectory, filterPattern);
    ptrFilenameInDir = new wchar_t*[countFileInDir];

    // to retrieve fileName
    getFileList(internalPath, ptrFilenameInDir, &countFileInDir, FALSE, recursiveInDirectory, filterPattern);

    // to retrieve num of files in files.dat/notes.dat
    countFileInFile = readFilenameFromFile(dataType, path, &ptrFilenameFromFile);

    // if there is no backup file...
    if (!ptrFilenameFromFile)
        return;


    // first element in file is timestamp
    timeInFile = wcstod(ptrFilenameFromFile[0], NULL);

    //
    // to get the new or update items that are files
    //

    for (i = 0; i < countFileInDir; i++) {

        found = FALSE;

        for (j = 1; j <= countFileInFile; j++) {

            if (wcscmp(ptrFilenameInDir[i], ptrFilenameFromFile[j]) == 0) {

                found = TRUE;
                getLastWriteTime(ptrFilenameInDir[i], wtime, &dtime);

                if (dtime > timeInFile) {

                    if (dataType == constFiles) {
                        unsigned long sizeFile = 0;
                        getFileSize(ptrFilenameInDir[i], &sizeFile);
                        if (sizeFile == 0)
                            return;
                        if (sizeFile > getMaxObjectSizeToSend()) {
                            LOG.debug("Found file greater then maxObjectSize(%lu): sizefile = %lu", getMaxObjectSizeToSend(), sizeFile);
                            fileRemoved++;
                            continue;
                        }
                        fileContent = new char[sizeFile];

                        readByteFromFile(ptrFilenameInDir[i], (byte*)fileContent, &numBytes);
                        // only file name without path. (e.g. example.bin)
                        FileData fileData;
                        getFileName(ptrFilenameInDir[i], localTemp);
                        fileData.setName(localTemp);
                        fileData.setSize(numBytes);
                        fileData.setEnc(TEXT("base64"));
                        fileData.setBody(fileContent, numBytes);
                        delete [] fileContent; fileContent = NULL;
                        fileContent = fileData.format();
                        numBytes = strlen(fileContent);

                    }
                    else if (dataType == constNotes) {

                        pp = readTextFromBinaryFileChar(ptrFilenameInDir[i]);
                        getFileName(ptrFilenameInDir[i], localTemp);
                        if (pp != NULL) {
                            wstring note = getXMLNote(pp, localTemp);
                            fileContent = toMultibyte((wchar_t*)note.data());
                            numBytes = strlen(fileContent);
                        }

                    }
                    else if (dataType == constFavorites) {
                        ptrFileContent = readTextFromBinaryFile(ptrFilenameInDir[i]);

                        if (ptrFileContent != NULL) {
                            fileContent = toMultibyte(ptrFileContent);
                            numBytes = strlen(fileContent);
                        }

                        wcscpy(localTemp, ptrFilenameInDir[i]);

                        wchar_t* t = wcsstr(ptrFilenameInDir[i], internalPath);
                        if (t != NULL) {
                            wsprintf(localTemp, t + wcslen(internalPath));
                        }
                        if (FILE_SYSTEM_SYNC_SOURCE == TRUE)
                            convertSlashAt(localTemp);

                    }

                    encodeKey (localTemp);
                    key_s = wstring(localTemp);
                    //encodeSpecialChar(key_s);

                    syncItem = new SyncItem((wchar_t*)key_s.data());

                    // without CDATA

                        if (fileContent == NULL) {
                        syncItem->setData("", 1*sizeof(char));
                    }
                    else {
                        syncItem->setData(fileContent, numBytes*sizeof(char));
                    }
                    countUpdatedItems++;

                    c->addItemToUpdatedItems(syncItem);

                    if (syncItem)
                        delete syncItem;
                    syncItem = NULL;

                    if (ptrFileContent != NULL)
                        delete [] ptrFileContent;
                    ptrFileContent = NULL;
                    if (fileContent != NULL)
                        delete [] fileContent;
                    fileContent = NULL;
                    pp = NULL;

                }
                else {

                }
                break;
            }

        }

        if (found == FALSE) {

            if (dataType == constFiles) {

                unsigned long sizeFile = 0;
                getFileSize(ptrFilenameInDir[i], &sizeFile);
                if (sizeFile == 0)
                    return;
                if (sizeFile > getMaxObjectSizeToSend()) {
                    LOG.debug("Found file greater then maxObjectSize(%lu): sizefile = %lu", getMaxObjectSizeToSend(), sizeFile);
                    fileRemoved++;
                    continue;
                }
                fileContent = new char[sizeFile];

                readByteFromFile(ptrFilenameInDir[i], (byte*)fileContent, &numBytes);
                //getFileName(ptrFilenameInDir[i], localTemp);
                FileData fileData;
                getFileName(ptrFilenameInDir[i], localTemp);
                fileData.setName(localTemp);
                fileData.setSize(numBytes);
                fileData.setEnc(TEXT("base64"));
                fileData.setBody(fileContent, numBytes);
                delete [] fileContent; fileContent = NULL;
                fileContent = fileData.format();
                numBytes = strlen(fileContent);
            }
            else if (dataType == constNotes) {

                pp = readTextFromBinaryFileChar(ptrFilenameInDir[i]);
                getFileName(ptrFilenameInDir[i], localTemp);
                if (pp != NULL) {
                    wstring note = getXMLNote(pp, localTemp);
                    fileContent = toMultibyte((wchar_t*)note.data());
                    numBytes = strlen(fileContent);
                }
            }
            else if (dataType == constFavorites) {
                ptrFileContent = readTextFromBinaryFile(ptrFilenameInDir[i]);

                if (ptrFileContent != NULL) {
                    fileContent = toMultibyte(ptrFileContent);
                    numBytes = strlen(fileContent);
                }

                wcscpy(localTemp, ptrFilenameInDir[i]);

                wchar_t* t = wcsstr(ptrFilenameInDir[i], internalPath);

                if (t != NULL) {
                     wsprintf(localTemp, t + wcslen(internalPath));
                }
                if (FILE_SYSTEM_SYNC_SOURCE == TRUE) {
                    convertSlashAt(localTemp);
                }

            }

            encodeKey (localTemp);
            key_s = wstring(localTemp);
            //encodeSpecialChar(key_s);

            syncItem = new SyncItem((wchar_t*)key_s.data());

            if (fileContent == NULL) {
                syncItem->setData("", 1*sizeof(char));
            }
            else {
                syncItem->setData(fileContent, numBytes*sizeof(char));
            }
            countNewItems++;

            c->addItemToNewItems(syncItem);

            if (syncItem)
                delete syncItem;
            syncItem = NULL;

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

            if (fileContent != NULL)
                delete [] fileContent;
            fileContent = NULL;
            pp = NULL;
        }

    }

    //
    // to get the delete items that are files
    //

    for (j = 1; j <= countFileInFile; j++) {

        found = FALSE;
        for (i = 0; i < countFileInDir; i++) {
            if (wcscmp( ptrFilenameFromFile[j], ptrFilenameInDir[i]) == 0) {
                found = TRUE;
            }
        }
        if (found == FALSE) {

            if (dataType == constFiles || dataType == constNotes) {
                getFileName(ptrFilenameFromFile[j], localTemp);
            }
            else if (dataType == constFavorites) {
                wcscpy(localTemp, ptrFilenameFromFile[j]);
                wchar_t* t = wcsstr(ptrFilenameFromFile[j], internalPath);
                if (t != NULL) {
                    wsprintf(localTemp, t + wcslen(internalPath));
                }
                if (FILE_SYSTEM_SYNC_SOURCE == TRUE)
                    convertSlashAt(localTemp);
            }

            encodeKey (localTemp);
            key_s = wstring(localTemp);
            //encodeSpecialChar(key_s);

            syncItem = new SyncItem((wchar_t*)key_s.data());

            c->addItemToDeletedItems(syncItem);

            if (syncItem)
                delete syncItem;
            syncItem = NULL;

            countDeletedItems++;

        }
    }

    // delete array and pointer...
    for (j = 0; j < countFileInDir; j++) {
        if (ptrFilenameInDir[j] != NULL)
            delete ptrFilenameInDir[j];
    }
    if (ptrFilenameInDir != NULL)
        delete [] ptrFilenameInDir;

    for (j = 0; j < countFileInFile; j++) {
        if (ptrFilenameFromFile[j] != NULL)
            delete ptrFilenameFromFile[j];
    }
    if (ptrFilenameFromFile != NULL)
        delete [] ptrFilenameFromFile;

    LOG.debug("Exiting from setModifiedItemsFILE... ");
}

⌨️ 快捷键说明

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