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

📄 filefilemanagement.cpp

📁 funambol windows mobile plugin source code, the source code is taken from the funambol site
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright (C) 2003-2007 Funambol, Inc
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY, TITLE, NONINFRINGEMENT 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
 */

//
// @author Marco Magistrali
//
#include <windows.h>
#include "Winbase.h"
#include "pim/Utils.h"
#include "base/util/utils.h"
#include "base/Log.h"
#include "pim/FILEFileManagement.h"
#include "pim/SettingFunctions.h"
#include "pim/ClientSettings.h"

///// **********************************
//#include "RichInk.h"
// Defines and typedefs needed for the conversion:

#define EM_STREAMIN          (WM_USER + 73)
#define EM_STREAMOUT         (WM_USER + 74)

typedef DWORD (CALLBACK *EDITSTREAMCALLBACK)
      (DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb);

typedef struct _editstream
{
    DWORD dwCookie; /* user value passed to callback as first parameter */
    DWORD dwError;  /* last error */
    EDITSTREAMCALLBACK pfnCallback;
} EDITSTREAM;

typedef struct tagCOOKIE
{
    HANDLE hFile;
    LPBYTE pbStart;
    LPBYTE pbCur;
    LONG  bCount;
    DWORD dwError;
} COOKIE, * PCOOKIE;

// Stream formats:
#define SF_TEXT         0x0001
#define SF_RTF          0x0002
#define SF_UNICODE      0x0010      // Unicode data of some kind
#define SF_UTEXT        SF_TEXT | SF_UNICODE   // Unicode text file
#define SFF_PWI         0x0800
#define SF_PWI          ( SF_RTF | SFF_PWI | 0x010000 ) // PWI format

static int recursiveLevel = 1;
static int currentLevel   = 0;


static DWORD CALLBACK ReadCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb) {

    PCOOKIE pCookie = (PCOOKIE)dwCookie;
    // Zero the bytes read.

    *pcb = 0;

    // If there is an error return it.
    if (ReadFile(pCookie->hFile, pbBuff, cb, (unsigned long *)pcb, 0) == 0)

    return (pCookie->dwError = GetLastError());

    // Return no error.
    return 0;

} // ReadCallback()

static DWORD CALLBACK  WriteCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb) {
    PCOOKIE pCookie = (PCOOKIE)dwCookie;
    if (WriteFile(pCookie->hFile, pbBuff, cb,(unsigned long *) pcb, 0) == 0) {
        return (pCookie->dwError = GetLastError());
    } else {
        }

    return 0;
} // WriteCallback()


static DWORD CALLBACK  BufferReadCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
    PCOOKIE pCookie = (PCOOKIE)dwCookie;
    LONG   bytesLeft;
    LONG   bytesRead;

    // Calc the bytes read.
    bytesRead = pCookie->pbCur - pCookie->pbStart;

    // Calc bytes left to read.
    if (bytesRead < pCookie->bCount) {
      // Calc the bytes left to read.
        bytesLeft = pCookie->bCount - bytesRead;
    } else{
        bytesLeft = 0;
    }

    if (cb > bytesLeft)
        cb = bytesLeft;

    *pcb = cb;
    // Copy any bytes.
    if (cb) {
        memcpy(pbBuff, pCookie->pbCur, cb);
        pCookie->pbCur += cb;
    }

    return 0;
} // BufferReadCallback()

static DWORD CALLBACK BufferWriteCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
    PCOOKIE pCookie = (PCOOKIE)dwCookie;
    // Do not overwrite the end of the buffer.
    // If there is no output buffer, then we抮e
    // only here to determine the space required
    // for the stream out.

    if (pCookie->pbStart && (pCookie->pbCur + cb > pCookie->pbStart + pCookie->bCount)) {
        // Writing all this data would overflow the buffer.
        // So only write what will fit.
        cb = pCookie->pbStart + pCookie->bCount - pCookie->pbCur;
    }

    *pcb = cb;

    if (pCookie->pbStart)
        memcpy(pCookie->pbCur, pbBuff, cb); pCookie->pbCur += cb;
    return 0;
} // BufferWriteCallback()




int writeTextToFile(wchar_t* filename, wchar_t* text) {

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

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

    hwndInk = CreateWindow(lpszClass, NULL, WS_BORDER, 0, 0, 0, 0, NULL, 0, NULL, NULL);
    if (!hwndInk)
      return -1;


/* write back to note */

    HANDLE file = CreateFile(filename, GENERIC_WRITE, 0, //FILE_SHARE_READ | FILE_SHARE_WRITE,
                              NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

    memset(&cookie, 0, sizeof(cookie));
    cookie.dwError      = 0;
    cookie.pbStart      = (LPBYTE)text;
    cookie.pbCur        = cookie.pbStart;
    cookie.bCount       =  wcslen(text) * sizeof(wchar_t);
    // cookie.bCount   =  5 * sizeof(wchar_t);
    es.dwCookie         = (DWORD)&cookie;
    es.dwError          = 0;
    es.pfnCallback      = BufferReadCallback;
    SendMessage (hwndInk, EM_STREAMIN, (WPARAM)SF_UTEXT, (LPARAM)&es);

    cookie.dwError      = 0;
    cookie.pbStart      = 0;
    cookie.pbCur        = 0;
    cookie.bCount       = 0;
    cookie.hFile        = file;
    es.dwCookie         = (DWORD)&cookie;
    es.dwError          = 0;
    es.pfnCallback      = BufferWriteCallback;
    SendMessage (hwndInk, EM_STREAMOUT, (WPARAM)SF_PWI, (LPARAM)&es);

    sz = (LPWSTR)LocalAlloc(LPTR, (cbSz = cookie.pbCur - cookie.pbStart) + 2);

    cookie.dwError      = 0;
    cookie.pbStart      = (LPBYTE)sz;
    cookie.pbCur        = cookie.pbStart;
    cookie.bCount       = cbSz;
    //cookie.hFile = file;
    es.dwCookie         = (DWORD)&cookie;
    es.dwError          = 0;
    es.pfnCallback      = WriteCallback;
    SendMessage (hwndInk, EM_STREAMOUT, (WPARAM)SF_PWI, (LPARAM)&es);

    CloseHandle(file);

    return 0;
}


//~ not used
/*wchar_t* readTextFromArray(byte* ptrFileContent, int numBytes) {

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

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

    hwndInk = CreateWindow(lpszClass, NULL, WS_BORDER, 0,
       0, 0, 0, NULL, 0, NULL, NULL);
    if (!hwndInk)
      return ERROR;

    // Stream in the Ink Data from a memory block.
    memset(&cookie, 0, sizeof(cookie));
    cookie.dwError      = 0;
    cookie.pbStart      = ptrFileContent;// (LPBYTE)pb;      // Pointer to memory block
    cookie.pbCur        = cookie.pbStart;
    cookie.bCount       = numBytes;// cb;         // Size of ink data
    es.dwCookie         = (DWORD)&cookie;
    es.dwError          = 0;
    es.pfnCallback      = BufferReadCallback;
    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);

    ret = toWideChar((char*)sz); // TODO: is it possible to avoid some casts?

    return ret;

} */

/*
* Return the content of a binary (pwi as notes format) or text file.
* It reads contents directly from file in filesystem if exists
*/


wchar_t* readTextFromBinaryFile(wchar_t* filename) {

    HINSTANCE   hLib;
    HWND   hwndInk = NULL;
    LPTSTR lpszClass = TEXT("richink") ;
    EDITSTREAM   es;
    COOKIE      cookie;
    HLOCAL sz;
    UINT cbSz;
    wchar_t* 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);

    ret = toWideChar((char*)sz, "ascii"); // TODO: is it possible to avoid some casts?

    CloseHandle(hfile);

    return ret;
}


void writeTextToTextFile(wchar_t* fName, wchar_t* text) {

    if (text == NULL)
        return;

    FILE* f;

    // specific for favorites. If file doesn't end with .url, it is a directory. So create it.
    if (wcsstr(fName, URL_EXTENSION) == NULL) {
        if (CreateDirectory(fName, NULL) == TRUE) {
        } else {
            LOG.error("Dir NOT correctly created");
        }
        return;
    }

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

    if( f != NULL ) {
        fwprintf(f, text);
    } else {
        wchar_t localDir     [DIM_LOCAL_TEMP];
        wchar_t internalPath [DIM_LOCAL_TEMP];
        getDirName(fName, localDir);

        SHGetSpecialFolderPath(NULL, internalPath, CSIDL_FAVORITES, FALSE);
        wcscat(internalPath, TEXT("\\"));
        wcscat(internalPath, localDir);

        CreateDirectory(internalPath, NULL);

        f = _wfopen(fName, TEXT("w+"));
        fwprintf(f, text);

    }

    fflush(f);
    fclose(f);
}


void writeByteToFile(wchar_t* fName, byte *ptr, int numBytes) {

    wchar_t wfilename[DIM_FILE];
    wchar_t* pt = NULL;
    char filename [DIM_FILE];
    BOOL usedDefaultChar;

    wcscpy(wfilename, fName);

    WideCharToMultiByte (CP_ACP, 0, wfilename, DIM_FILE -1, filename, DIM_FILE -1, "?", &usedDefaultChar);

    int numwritten = 0;
    FILE* f;

    f = fopen(filename, "w+b");

    if( f != NULL ) {
        numwritten = fwrite( ptr, sizeof( byte ), numBytes, f );
    }

    fflush(f);
    fclose(f);
}

void readByteFromFile(wchar_t* fName, byte *ptr, int* numBytes) {

    wchar_t wfilename[DIM_FILE];
    wchar_t* pt = NULL;
    char filename [DIM_FILE];
    BOOL usedDefaultChar;

    wcscpy(wfilename, fName);

    WideCharToMultiByte (CP_ACP, 0, wfilename, DIM_FILE -1, filename, DIM_FILE -1, "?", &usedDefaultChar);

    wchar_t* element = NULL;

    byte line[DIMLINE];
    int numread = 0;
    int total = 0;
    FILE* f;

    f = fopen(filename, "rb");

    while( !feof( f ) ) {

        numread = fread( line, sizeof( byte ), DIMLINE, f );

        memcpy(ptr + total, line, numread);
        total = total + numread;
    }

    fflush(f);
    fclose(f);

    *numBytes = total;

}

/*
* Method to write current file name and time of writing in file to use to
* compare with others in order to search for modified files...
*/


void writeCurrentFileItems(int dataType, const wchar_t* path, const wchar_t* dir) {

    wchar_t localTemp[DIM_LOCAL_TEMP];
    wchar_t wfilename[DIM_FILE];
    wchar_t internalPath[DIM_LOCAL_TEMP];
    wchar_t filterPattern[256];

    wchar_t** fileList = NULL;
    int count = 0;
    int i = 0;
    FILE* f;

    /*
    * 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;

    BOOL recursiveInDirectory = FALSE;
    filterPattern [0] = 0;

    if (dataType == constFiles) {
        wsprintf(internalPath, TEXT("%s\\"), dir);
        wsprintf(wfilename, TEXT("%s\\%s"), path, TEXT(FILENAME_FILES));
    }
    else if (dataType == constNotes) {
        wsprintf(internalPath, TEXT("%s\\"), dir);
        wsprintf(wfilename, TEXT("%s\\%s"), path, TEXT(FILENAME_NOTES));
        wcscpy(filterPattern, PWI_EXTENSION_PATTERN);
    }

⌨️ 快捷键说明

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