/*--------------------------------------------------------------------------
  Project:  Manual 2D Library - m2d
  File:     m2d_langInfo.cpp

  Copyright (C)2007-2009 Nintendo  All Rights Reserved.

  These coded instructions, statements, and computer programs contain
  proprietary information of Nintendo of America Inc. and/or Nintendo
  Company Ltd., and are protected by Federal copyright law. They may
  not be disclosed to third parties or copied or duplicated in any form,
  in whole or in part, without the prior written consent of Nintendo.

  $Revision: 1758 $
 *-------------------------------------------------------------------------*/

#include <nitro.h>
#include <nnsys/misc.h>
#include <ntmv/m2d/common.h>
#include <ntmv/m2d/langInfo.h>
#include <ntmv/m2d/langInfoResource.h>
#include <ntmv/m2d/resourceAccessor.h>

namespace ntmv
{
namespace m2d
{

/*---------------------------------------------------------------------------*
  Name:         LoadLangInfoFile

  Description:  Loads the language information file. 

  Arguments:    pLangInfo: Pointer to a structure that stores the language information.
                fileName:  Filename
                pResAcsr:  A pointer to the resource accessor.

  Returns:      Returns TRUE if loading was successful.
 *---------------------------------------------------------------------------*/
bool
LoadLangInfoFile(
    LangInfo*           pLangInfo,
    const char*         fileName,
    ResourceAccessor*   pResAcsr
)
{
    // Load the layout resource
    const void* resBuf = pResAcsr->GetResource(res::RESOURCETYPE_LANGINFO, fileName);
    if (! resBuf)
    {
        return false;
    }

    const res::BinaryFileHeader *const pFileHead = static_cast<const res::BinaryFileHeader*>(resBuf);

    if (! detail::TestFileHeader(*pFileHead, res::FILESIGNATURE_NTLI))
    {
        NTMV_WARNING(false, "not manual lang info file.");
        return false;
    }

    if (! detail::TestFileVersion(*pFileHead))
    {
        return false;
    }

    const void* dataPtr = static_cast<const u8*>(resBuf) + pFileHead->headerSize;

    for (int i = 0; i < pFileHead->dataBlocks; ++i)
    {
        const res::DataBlockHeader* pDataBlockHead = static_cast<const res::DataBlockHeader*>(dataPtr);
        switch (detail::GetSignatureInt(pDataBlockHead->kind))
        {
        case res::DATABLOCKKIND_LANGINFO:
            {
                const res::LangInfo* pResLangInfo = static_cast<const res::LangInfo*>(dataPtr);
                pLangInfo->count = pResLangInfo->count;
                pLangInfo->langs = pResLangInfo->langs;
                return true;
            }
            break;
        }

        dataPtr = static_cast<const u8*>(dataPtr) + pDataBlockHead->size;    // Move to the next block position
    }

    return false;
}

}   // namespace m2d
}   // namespace ntmv
