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

📄 utils.cpp

📁 funambol windows mobile plugin source code, the source code is taken from the funambol site
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/*
 * 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
 */

#include <stdio.h>
#include <windows.h>
#include <Winbase.h>
#include <tchar.h>

#include "base/Log.h"
#include "base/util/utils.h"
#include "base/base64.h"

#include "pim/Utils.h"

#include "pim/SettingFunctions.h"

#include "spdm/ManagementNode.h"
#include "spdm/DMTree.h"
#include "spdm/DMTreeFactory.h"
#include "base/quoted-printable.h"
#include "spds/DataTransformerFactory.h"
#include "spds/DataTransformer.h"
#include "pim/ClientSettings.h"
#include "pim/OutlookApp.h"
#include "localizationUtils.h"
#ifdef WIN32_PLATFORM_WFSP
#include "funresourcesp.h"
#endif
#ifdef WIN32_PLATFORM_PSPC
#include "funresourceppc.h"
#endif


// Timezones that are changing their rule in 2007
#define TZ_EASTERN_US TEXT("Eastern Standard Time")
#define TZ_CENTRAL_US TEXT("Central Standard Time")
#define TZ_MOUNTAIN TEXT("Mountain Standard Time")
#define TZ_PACIFIC_US TEXT("Pacific Standard Time")
#define TZ_ALASKA TEXT("Alaska Standard Time")


// DST conversion items
#define IS_LEAP_YEAR(y) (((y) & 3) == 0)

#define BASE_DOW          4                  // 1/1/1970 was a Thursday.
#define SECONDS_IN_A_DAY  (24L * 60L * 60L)  // Number of seconds in one day.

// Month to Year Day conversion array.
static int M2YD[] = {
   0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
};
// Month to Leap Year Day conversion array.
static int M2LYD[] = {
   0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366
};

/**
* Changed in 6.0. Now it create a singleton and the first time it is used
* it creates the instance
* Open POOM Application object
*/
IP_OUTLOOK_APP* getOutlookApp() {
    
    OutlookApp* app = OutlookApp::getInstance();

    if (app) {
        return app->getPolApp();
    } else {
        return NULL;
    }
        

    // Initialization and creation IPOutlookApp
    /*
    IPOutlookApp2    *polApp; // outlook app
    HRESULT hr;
    if (FAILED (CoInitializeEx (NULL, 0)))
        return NULL;
    hr = CoCreateInstance (CLSID_Application, NULL, CLSCTX_INPROC_SERVER, IID_IPOutlookApp2, (LPVOID*)&polApp);
    if (FAILED(hr))
        return NULL;
    hr = polApp->Logon (NULL);
    if (FAILED (hr))
        return NULL;       
    
    return polApp;
    */
}

/**
* Deprecated: it doesn't anything now (from 6.0)
* The Outlook Application becomes a singleton to reuse the same object.
* Close POOM Application object.
*/

void releaseOutlookApp(IP_OUTLOOK_APP* polApp) {
    /*
    if (polApp) {
        polApp->Logoff ();
        polApp->Release ();
    }
    */    
}

/*
* It release the Outlook application in the endsync of the Windows Sync Source
*/
void disposeOutlookApp() {
    OutlookApp::dispose();
}


/*
 * TODO: use API method??
 *---------------------------
 * The method extracts the content of a tag into a XML message. It is supposed the
 * message is a valid XML message. It returns NULL in case the tag is not
 * found or the XML fragment is not in the expected form.
 * The returned pointer (if not NULL) is allocated with the new operator and
 * must be discarded with the operator delete.
 *
 * @param xml : the xml fragment
 * @param tag : the tag we want the content
 * @param pos : (OUTPUT) the position just after the closing tag (ignored if NULL)
 *
 */
wchar_t* getElementContent(const wchar_t* xml, wchar_t* tag, unsigned int* pos) {
    wchar_t* p1       = NULL;
    wchar_t* p2       = NULL;
    wchar_t* ret      = NULL;
    BOOL charFound    = FALSE;

    wchar_t openTag[50];
    wchar_t closeTag[50];

    unsigned int xmlLength = wcslen(xml);
    unsigned int l = wcslen(tag);

    if (pos != NULL) {
        *pos = 0;
    }

    wsprintf(openTag, TEXT("<%s>"), tag);
    wsprintf(closeTag, TEXT("</%s>"), tag);

    p1 = wcsstr(xml, openTag);

    if (p1 == NULL) { // tag can have attributes
        //
        // This is abcxyz
        //
        //goto finally;

        // if p1 is null I try to discover the next '>' char to close the tag. If does not exist
        // return NULL

        // try to find "<tagName/>". If found it return null.
        wsprintf(openTag, TEXT("<%s/>"), tag);
        p1 = wcsstr(xml, openTag);

        if (p1 != NULL) {
            ret = new wchar_t[2];
            wsprintf(ret, TEXT(""));
            if (pos != NULL) {
                *pos = p1-xml+l+3;
            }
            goto finally;
        }

        // try to find "<tagName"
        wsprintf(openTag, TEXT("<%s"), tag);
        p1 = wcsstr(xml, openTag);

        if (p1 == NULL) {
            goto finally;
        }

        p1 = p1 + l + 1;   // <body_

        for (unsigned int k = 0; k < xmlLength; k++) { // Suppose max length as the xml string
            p1 = p1 + 1;
            if (*p1 == 0) {
                goto finally;
            }
            else if (*p1 == '>') {
                charFound = TRUE;
                p1 = p1 + 1;
                break;
            }
        }
        if (!charFound)
            goto finally;


    } else {  // tag doesn't have attribute. Original version

        p1 = p1+l+2;

    }

    if (*p1 == 0) {
        //
        // This is abc<tag>\0
        //
        goto finally;
    }

    p2 = wcsstr(p1, closeTag);

    if (p2 == NULL) {
        //
        // This is abc<tag>xyz\0
        //
        goto finally;
    }

    ret = new wchar_t[p2-p1+1];

    wcsncpy(ret, p1, p2-p1);
    ret[p2-p1] = 0;

    if (pos != NULL) {
        *pos = p2-xml+l+3;
    }

finally:

    return ret;

}

/*
* The method is used to convert VARIANT_BOOL type in the same string type
* With variant bool TRUE = -1, FALSE = 0
* Translate in BOOL i which TRUE = 1 and FALSE = 0
* @param localTemp : the variable that contain old and new value
*/

void normalizeBoolean(wchar_t* localTemp) {

    if (wcscmp(localTemp, TEXT("-1")) == 0) {
        wsprintf(localTemp, TEXT("1"));
    } else {
        wsprintf(localTemp, TEXT("0"));
    }
    /*
    if (wcscmp(localTemp, TEXT("-1")) == 0) {
        wsprintf(localTemp, TEXT("True"));
    } else {
        wsprintf(localTemp, TEXT("False"));
    }*/

}

/*
* The method replaces &lt; or &gt; with < or > in server response syncML message.
*
* @param s : the string to be replaced
*/
void replaceAmpLGt(std::wstring &s) {

    int position = 0;

    // search for &lt;
    //position = pos(TEXT("&lt;"), s);
    position = s.find(_T("&lt;"));
    while (position != std::wstring::npos) {
    // del(s, position, 4);
        s.replace(position,4, _T(""));
        // ins(TEXT("<"), s, position);
    s.insert(position, _T("<"), 1);

        // position = pos(TEXT("&lt;"), s);
    position = s.find(_T("&lt;"));
    }
    // now search for &lt;
    position = 0;
    position = s.find(_T("&gt;"));
    while (position != std::wstring::npos) {
        s.replace(position,4,_T(""));
        s.insert(position, _T(">"), 1);
        position = s.find(_T("&gt;"));
    }

}

/*
* The method removes \n from the start if there is.
* The start 1n was a bug that has been fixed in new SyncClient API version.
* It works on original array to save memory.
*
* @param ptr : the array to be modify
*/

void removeStartCarriage(wchar_t** ptr) {

    // if start with CRLF,
    if ( ptr != NULL &&
        *ptr != NULL &&
        (int)((*ptr)[0]) == 13 && (int)((*ptr)[1]) == 10)
        *ptr = &((*ptr)[2]);


}


/*
* The method removes \r\n from the end if there is.
* This method is used to remove carriage\return in reading line from file notes.dat,
* briefcase.dat and favorites.dat.
* So it will be possible compare properly file names retrived by file system
* and these file name
*
* @param ptr : the filename ended with \r\n or \n
*/

void removeEndCarriage(wchar_t** ptr) {
    int length = 0;
    if (*ptr != NULL)
        length = wcslen(*ptr);


    // if end with \n,
    if ( ptr != NULL &&
        *ptr != NULL &&
        (int)((*ptr)[length -1]) == 10)
        (*ptr)[length -1] = 0;

    if (*ptr != NULL)
        length = wcslen(*ptr);

    // if there is also \r
    if ( ptr != NULL &&
        *ptr != NULL &&
        (int)((*ptr)[length -1]) == 13)
        (*ptr)[length -1] = 0;

}

void encodeSpecialChar(std::wstring &s) {

    int position = 0, previousPosition = 0;

    //
    // &
    //
    position = s.find(_T("&"));
    while (position != std::wstring::npos) {
      s.insert(position,_T("&amp;"));
      s.replace(position +5, 1, _T(""));
          position = position + 2;
          //previousPosition = position;

      position = s.find(_T("&"), position); //s[position]

      if (position == std::wstring::npos) {
             break;
        }
    }

    //
    // <
    //
    position = 0;
    previousPosition = 0;
    position = s.find(TEXT("<"));
    while (position != std::wstring::npos) {
      s.insert(position, _T("&lt;"));
      s.replace(position +4, 1, _T(""));
          position = position + 2;
        //previousPosition = position;
        //position = pos(TEXT("<"), &s[position]);
      position = s.find(_T("<"), position); // TODO: !!!!!

    if (position == std::wstring::npos) {
            break;
        }
    }

    //
    // >
    //
    position = 0;
    previousPosition = 0;
    position = s.find(_T(">"));
    while (position != std::wstring::npos) {
      s.insert(position,_T("&gt;"));
      s.replace(position + 4, 1, _T(""));
          position = position + 2;
         //previousPosition = position;
      position = s.find(_T(">"), position);

          if (position == std::wstring::npos) {
            break;
           }
    }

}


void decodeSpecialChar(std::wstring &s) {


    unsigned int position = 0, previousPosition = 0;

    //
    // &
    //
    position = s.find(_T("&amp;"));
    while (position != std::wstring::npos) {
    s.insert(position, _T("&"));
    s.replace(position + 1, 5, _T(""));
        if (position + 1 >= s.length()) {
            position = std::wstring::npos;
            break;
        }
        position = position + 1;
    position = s.find(_T("&amp;"), position);
    if (position == std::wstring::npos) {
            break;
        }
    }

    //
    // <
    //
    position = 0;
    previousPosition = 0;
    position = s.find(_T("&lt;"));
    while (position != std::wstring::npos) {
      s.insert(position, _T("<"));
      s.replace(position +1, 4, _T(""));
          if (position + 1 >= s.length()) {
            position = std::wstring::npos;
            break;
          }
          position = position + 1;
      position = s.find(_T("&lt;"), position);

      if (position == std::wstring::npos) {
            break;
          }
    }

    //
    // >
    //
    position = 0;
    previousPosition = 0;
    position = s.find(_T("&gt;"));
    while (position != std::wstring::npos) {
      s.insert(position, _T(">"));
      s.replace(position + 1, 4, _T(""));

⌨️ 快捷键说明

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