/* /////////////////////////////////////////////////////////////////////////////
 * File:        version_info_test.cpp
 *
 * Purpose:     Implementation file for the version_info_test project.
 *
 * Created:     21st July 2004
 * Updated:     25th July 2004
 *
 * Status:      Wizard-generated
 *
 * License:     (Licensed under the Synesis Software Open License)
 *
 *              Copyright (C) 1999-2004, Synesis Software Pty Ltd.
 *              All rights reserved.
 *
 *              www:        http://www.synesis.com.au/software
 *
 *              email:      software@synesis.com.au
 *
 *              This source code is placed into the public domain 2004
 *              by Synesis Software Pty Ltd. There are no restrictions
 *              whatsoever to your use of the software. 
 *
 *              This source code is provided by Synesis Software Pty Ltd "as is"
 *              and any warranties, whether expressed or implied, including, but
 *              not limited to, the implied warranties of merchantability and
 *              fitness for a particular purpose are disclaimed. In no event
 *              shall the Synesis Software Pty Ltd be liable for any direct,
 *              indirect, incidental, special, exemplary, or consequential
 *              damages (including, but not limited to, procurement of
 *              substitute goods or services; loss of use, data, or profits; or
 *              business interruption) however caused and on any theory of
 *              liability, whether in contract, strict liability, or tort
 *              (including negligence or otherwise) arising in any way out of
 *              the use of this software, even if advised of the possibility of
 *              such damage. 
 *
 *              Neither the name of Synesis Software Pty Ltd nor the names of
 *              any subdivisions, employees or agents of Synesis Software Pty
 *              Ltd, nor the names of any other contributors to this software
 *              may be used to endorse or promote products derived from this
 *              software without specific prior written permission. 
 *
 * ////////////////////////////////////////////////////////////////////////// */

#include <windows.h>
#include <stdio.h>

#include <stlsoft.h>

#include <winstl.h>
#include <winstl_version_info.h>

/* /////////////////////////////////////////////////////////////////////////////
 * Forward declarations
 */

char const  *lookupFileOS(DWORD dw);
char const  *lookupFileType(DWORD dw);
char const  *lookupFileSubtype(DWORD dw);

/* ////////////////////////////////////////////////////////////////////////// */

int main(int argc, char *argv[])
{
#if 0
    ::Sleep(100000);
#endif /* 0 */

    int         iRet    =   0;
    int         i;
    char const  *module =   NULL;

#if defined(_DEBUG) || \
    defined(__SYNSOFT_DBS_DEBUG)
    puts("version_info_test: " __STLSOFT_COMPILER_LABEL_STRING);
#endif /* debug */

    for(i = 1; i < argc; ++i)
    {
        const char  *arg    =   argv[i];

        if(arg[0] == '-')
        {
            if(arg[1] == '-')
            {
                /* -- arguments */
            }
            else
            {
                /* - arguments */
            }
        }
        else
        {
            /* other arguments */
            module = arg;
        }
    }

    /* . */
    if(NULL == module)
    {
        module = "USER32";
    }

#ifdef __STLSOFT_CF_EXCEPTION_SUPPORT
    try
    {
#endif /* __STLSOFT_CF_EXCEPTION_SUPPORT */
        ::winstl::version_info  info(module);


        ::winstl::version_info  &verInfo    =   info;

        if(verInfo.Length())
        {
            printf("FFI: Struct version:  %d.%d\n", verInfo.FixedFileInfo().ApiVerHigh(), verInfo.FixedFileInfo().ApiVerLow());
            printf("FFI: File version:    %d.%d.%d.%d\n", verInfo.FixedFileInfo().FileVerMajor(), verInfo.FixedFileInfo().FileVerMinor(), verInfo.FixedFileInfo().FileVerRevision(), verInfo.FixedFileInfo().FileVerBuild());
            printf("FFI: Product version: %d.%d.%d.%d\n", verInfo.FixedFileInfo().ProductVerMajor(), verInfo.FixedFileInfo().ProductVerMinor(), verInfo.FixedFileInfo().ProductVerRevision(), verInfo.FixedFileInfo().ProductVerBuild());
            printf("FFI: File Flags:\n");
            printf("            mask:     0x%08x\n", verInfo.FixedFileInfo().FileFlagsMask());
            printf("            value:    0x%08x\n", verInfo.FixedFileInfo().FileFlags());
            printf("FFI: File OS:         %d (%s)\n", verInfo.FixedFileInfo().FileOS(), lookupFileOS(verInfo.FixedFileInfo().FileOS()));
            printf("FFI: File Type:\n");
            printf("            main:     %d (%s)\n", verInfo.FixedFileInfo().FileType(), lookupFileType(verInfo.FixedFileInfo().FileType()));
            printf("            sub:      %d (%s)\n", verInfo.FixedFileInfo().FileSubtype(), lookupFileSubtype(verInfo.FixedFileInfo().FileSubtype()));

            SYSTEMTIME          st;

            FileTimeToSystemTime(&verInfo.FixedFileInfo().FileDateTime(), &st);

            if(verInfo.HasVarFileInfo())
            {
                ::winstl::VsVarFileInfo info = verInfo.VarFileInfo();

                printf(" VFI: %S\n", info.Key());

                ::winstl::VsVarFileInfo::const_iterator begin   =   info.begin();
                ::winstl::VsVarFileInfo::const_iterator end     =   info.end();

                for(; begin != end; ++begin)
                {
                    ::winstl::VsVar var =   *begin;

                    for(size_t j = 0; j < var.length(); ++j)
                    {
                        ::winstl::VsVar::LangCodePage const &value  =   var[j];

                        printf(" VFI: %04x%04x\n", value.language, value.codePage);
                    }
                }
            }

            if(verInfo.HasStringFileInfo())
            {
                ::winstl::VsStringFileInfo  info = verInfo.StringFileInfo();

                printf(" SFI: %S\n", info.Key());

                ::winstl::VsStringFileInfo::const_iterator begin    =   info.begin();
                ::winstl::VsStringFileInfo::const_iterator end      =   info.end();

                for(; begin != end; ++begin)
                {
                    ::winstl::VsStringTable strtbl  =   *begin;

                    printf("  ST: %S\n", strtbl.Key());

                    ::winstl::VsStringTable::const_iterator begin   =   strtbl.begin();
                    ::winstl::VsStringTable::const_iterator end     =   strtbl.end();

                    for(; begin != end; ++begin)
                    {
                        ::winstl::VsString  str =   *begin;

                        printf("   Str: %S=%S\n", str.name(), str.value());
                    }
                }
            }
        }
#ifdef __STLSOFT_CF_EXCEPTION_SUPPORT
    }
    catch(::winstl::version_info_exception &/* x */)
    {
        printf("Failed to load version information from %s\n", module);

        return 1;
    }
#endif /* __STLSOFT_CF_EXCEPTION_SUPPORT */

    return iRet;
}


/* ////////////////////////////////////////////////////////////////////////// */

char const  *lookupFileOS(DWORD dw)
{
    return "";
}

char const  *lookupFileType(DWORD dw)
{
    return "";
}

char const  *lookupFileSubtype(DWORD dw)
{
    return "";
}

/* ////////////////////////////////////////////////////////////////////////// */
