/*
This source file is part of Scol
For the latest info, see http://www.scolring.org

Copyright (c) 2011 Stephane Bisaro, aka Iri <iri@irizone.net>

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser 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, or go to
http://www.gnu.org/copyleft/lesser.txt

For others informations, please contact us from http://www.scolring.org/
*/

#include "main_misc.h"

/*
    Méthode bourrine, ok ... !
    Remplace les anti-slashes par des slashes et les espaces par des "\ "
    pour avoir un retour du genre :
    in -> c:\a\b\c d\e.xml
    out -> c:/a/b/c\ d/e.xml
*/
void s_xml2_replaceFilename (char *in, char **out)
{
    int i = 0, j = 0, len = strlen (in);

    for (i = 0; i < len; i++)
    {
        if (in[i] == '\\')
        {
            (*out)[j] = '/';
            j++;
        }
        else if (in[i] == ' ')
        {
            (*out)[j] = '\\';
            j++;
            (*out)[j] = ' ';
            j++;
        }
        else
        {
            (*out)[j] = in[i];
            j++;
        }
    }
    (*out)[j] = '\0';
    return;
}




/*
    Create a Scol object

    Several functions :
    - void s_xml2create_object (mmachine m, xmlParserCtxtPtr s);

    create an object from ths xmlParserCtxt structure pointer

    - void s_xml2create_object_sax (mmachine m, s_xml s);

    create an object from the S_xml structure only (SAX Scol API)

    - void s_xml2create_object_reader (mmachine m, s_xml s);

    create an object from S_xml structure only (Reader Scol API)
*/
void s_xml2create_object (mmachine m, xmlParserCtxtPtr s)
{
    int tab;

    tab = MMmalloc (m, sizeof (struct _xmlParserCtxt)+1, TYPETAB);
    if ((tab == NIL))
    {
        MMechostr (0, "s_xml2create_object error : not enough memory\n");
        MMpull (m);
        MMpush (m, NIL);
        return;
    }
    MMstore (m, tab, S_LIBSAX2_HANDLE, (int) s);
    MMpush (m, PTOM (tab));
    OBJcreate (m, ObjSax2, (int) s, -1, -1);
    return;
}

static int s_xml2create_object_s_xml (mmachine m, s_xml s, int flag)
{
    int tab;

    tab = MMmalloc (m, sizeof (struct S_xml)+1, TYPETAB);
    if ((tab == NIL))
    {
        MMechostr (0, "s_xml2create_object_s_xml error : not enough memory\n");
        MMpull (m);
        MMpush (m, NIL);
        return 1;
    }
    MMstore (m, tab, flag, (int) s);
    MMpush (m, PTOM (tab));
    return 0;
}

void s_xml2create_object_sax (mmachine m, s_xml s)
{
    int res;
    res = s_xml2create_object_s_xml (m, s, S_LIBSAX2_HANDLE);
    if (!res)
        OBJcreate (m, ObjSax2, (int) s, -1, -1);
    return;
}

void s_xml2create_object_reader (mmachine m, s_xml s)
{
    int res;
    res = s_xml2create_object_s_xml (m, s, S_LIBREADER_HANDLE);
    if (!res)
        OBJcreate (m, ObjXmlReader, (int) s, -1, -1);
    return;
}

void s_xml2create_object_tree (mmachine m, s_xml s)
{
    int res;
    res = s_xml2create_object_s_xml (m, s, S_LIBTREE_HANDLE);
    if (!res)
        OBJcreate (m, ObjXmlTree, (int) s, -1, -1);
    return;
}


#if ((defined _WIN32) || (defined __WIN32__))
void winHandleThreadError (LPTSTR funName)
{
    DWORD dw;
    LPVOID lpMsgBuf;

    dw = GetLastError ();
    FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
                       NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
    MMechostr (MSKDEBUG, "%s error number %d : %s\n", (LPCTSTR) funName, dw, lpMsgBuf);
    LocalFree(lpMsgBuf);
}
#endif
