//////////////////////////////////////////////////////////////////////////////
// File:        pkgutils.cpp
// Purpose:     wxPackager utilities
// Author:      Francesco Montorsi
// Modified by:
// Created:     2006-06-08
// RCS-ID:      $Id: miscutils.cpp,v 1.3 2007/01/14 10:36:38 frm Exp $
// Copyright:   (c) Francesco Montorsi
// Licence:     wxWidgets licence
//////////////////////////////////////////////////////////////////////////////


// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all 'standard' wxWidgets headers)
#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

#include "wx/filename.h"
#include "wx/miscutils.h"



//----------------------------------------------------------------------------
// wxPackager utilities
//----------------------------------------------------------------------------

void wxFlags2CheckListBox(wxCheckListBox *lb, long flags, size_t max)
{
    for (size_t i=0; i<max; i++)
        lb->Check(i, (flags & (1 << i)) != 0);
}

long wxCheckListBox2Flags(const wxCheckListBox *lb)
{
    long ret = 0;
    for (size_t i=0; i<lb->GetCount(); i++)
        if (lb->IsChecked(i))
            ret |= (1 << i);
    return ret;
}

void wxCheckAll(wxCheckListBox *lb, bool check)
{
    for (size_t i=0; i<lb->GetCount(); i++)
        lb->Check(i, check);
}

bool wxIsSubfolderOf(const wxString &root, const wxString &subfolder)
{
    return wxIsSubfolderOf(wxFileName::DirName(root), wxFileName::DirName(subfolder));
}

bool wxIsSubfolderOf(const wxFileName &root, const wxFileName &subfolder)
{
    // NB: this function does _not_ check for directory existence!

    if (subfolder.GetDirCount() < root.GetDirCount())
        return false;

#ifdef __WXMSW__
    // comparison must be case insensitive
    if (subfolder.GetVolume().CmpNoCase(root.GetVolume()) != 0)
        return false;
#endif

    for (size_t i=0; i<root.GetDirCount(); i++)
    {
        if (!root.GetDirs().Item(i).IsSameAs(subfolder.GetDirs().Item(i),
                                             wxFileName::IsCaseSensitive()))
        {
            return false;
        }
    }

    return true;
}

