//==============================================================================
// InitializeReporting                                           PUBLIC STATIC
//==============================================================================
//
OSCL_EXPORT_REF void
UT::CM::InitializeReporting
(
    const char* a_pszTestname,
    OSCL_HeapString<OsclMemAllocator>& a_filename,
    FILE* a_pFileStreamParent,
    FILE*& a_pFileStreamChild
)
{
    a_pFileStreamChild = a_pFileStreamParent;

    if (0 == a_pszTestname || 0 >= a_filename.get_size() || 0 == a_pFileStreamParent)
        return;

    Oscl_FileServer fs;
    fs.Connect();

    Oscl_File f;
    if (0 == f.Open(a_filename.get_cstr(), Oscl_File::MODE_READWRITE | Oscl_File::MODE_TEXT, fs))
    {
        _STRING xfr = xml_test_interpreter::unexpected_termination_interpretation(a_pszTestname);
        f.Write(xfr.c_str(), sizeof(char), oscl_strlen(xfr.c_str()));
        f.Close();
    }
    else
    {
        fprintf(a_pFileStreamParent, "ERROR: Failed to open XML test summary log file (%s).\n", a_filename.get_cstr());
    }

    fs.Close();

    OSCL_HeapString<OsclMemAllocator> outFilename(a_filename);
    outFilename += ".out";
    // open a new stream to file and return it to client
    a_pFileStreamChild = fopen(outFilename.get_cstr(), "w");
}
//==============================================================================
// FinalizeReporting                                             PUBLIC STATIC
//==============================================================================
//
OSCL_EXPORT_REF void
UT::CM::FinalizeReporting
(
    const char* a_pszTestname,
    OSCL_HeapString<OsclMemAllocator> &a_filename,
    const test_result& a_tr,
    FILE* a_pFileStreamParent,
    FILE* a_pFileStreamChild
)
{
    if (0 == a_pFileStreamChild)
        return;

    //                     report the textual representation of the test results
    text_test_interpreter interp;
    _STRING rs = interp.interpretation(a_tr);
    fprintf(a_pFileStreamChild, "%s", rs.c_str());

    if (0 == a_pszTestname || 0 >= a_filename.get_size() || 0 == a_pFileStreamParent)
        return;

    _STRING strChild;

    fclose(a_pFileStreamChild);                              // close the stream

    OSCL_HeapString<OsclMemAllocator> outFilename(a_filename);
    outFilename += ".out";

    FILE* pFile = fopen(outFilename.get_cstr(), "rb");
    if (0 == pFile)
        fprintf(a_pFileStreamParent, "ERROR: Failed to open file (%s) for capturing test output!\n", outFilename.get_cstr());
    else
    {
        fseek(pFile, 0, SEEK_END);
        long lSize = ftell(pFile);
        rewind(pFile);
        char* buffer = new char[lSize];
        fread(buffer, 1, lSize, pFile);
        strChild = _STRING(buffer, lSize);
        fprintf(a_pFileStreamParent, "%s", strChild.c_str()); // send the captured output back out the parent stream
        delete [] buffer;
        fclose(pFile);
    }

    Oscl_FileServer fs;
    fs.Connect();

    Oscl_File f;
    if (0 == f.Open(a_filename.get_str(), Oscl_File::MODE_READWRITE | Oscl_File::MODE_TEXT, fs))
    {
        _STRING xfr = xml_test_interpreter::interpretation(a_tr, a_pszTestname, &strChild);
        fprintf(a_pFileStreamParent, "\nWrote xml-formatted test results to file: %s\n", a_filename.get_cstr());
        f.Write(xfr.c_str(), sizeof(char), oscl_strlen(xfr.c_str()));
        f.Close();
    }
    else
    {
        fprintf(a_pFileStreamParent, "\nERROR: Failed to open file (%s) for xml-formatted test results\n", a_filename.get_cstr());
    }

    fs.Close();
}
OSCL_wHeapString<OsclMemAllocator> MovieHeaderAtom::convertTimeToDate(uint32 time)
{
    OSCL_HeapString<OsclMemAllocator> date;

    char buf[64];

    int32 numDaysInMonth[12] =
        {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    int32 numDaysInMonthLeap[12] =
        {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    int32 refYear = 1904;

    // (365*4 + 1) * 24 * 3600
    int32 numSecsInABlkofFourYears = 126230400;

    // 365*24*3600
    int32 numSecInYear = 31536000;

    int32 numBlksOfFour = (time / numSecsInABlkofFourYears);

    int32 leftOverSecs = (time - (numBlksOfFour * numSecsInABlkofFourYears));

    int32 leftOverYears = 0;
    int32 year    = 0;
    int32 numDays = 0;
    int32 numHrs  = 0;
    int32 numMins = 0;
    int32 month   = 0;
    int32 milliSecs = 0;

    if (leftOverSecs > numSecInYear)
    {
        /*To take in to consideration at least 1 year among the number of years
         as leap among leftOverYear*/

        leftOverSecs -= (24 * 3600);

        leftOverYears = (leftOverSecs / numSecInYear);

        leftOverSecs -= (leftOverYears * numSecInYear);


        numDays = (uint16)(leftOverSecs / (24 * 3600));

        leftOverSecs -= (numDays * 24 * 3600);


        for (uint32 i = 0; i < 12; i++)
        {
            if (numDays >= numDaysInMonth[i])
            {
                numDays = (numDays - numDaysInMonth[i]);
            }
            else
            {
                month = (i + 1);
                break;
            }
        }


    }
    else
    {
        numDays = (leftOverSecs / (24 * 3600));

        leftOverSecs -= (numDays * 24 * 3600);

        for (int32 i = 0; i < 12; i++)
        {
            if (numDays >= numDaysInMonthLeap[i])
            {
                numDays = (numDays - numDaysInMonthLeap[i]);
            }
            else
            {
                month = (i + 1);
                break;
            }
        }

    }

    // Considering a situation where by the date is 1st Jan of any year
    // the leftOverSecs would be less than the sec available in a day resulting numDays
    // as 0, however that is 1st Jan from user perspective. So a day adjustment factor
    // is added into numDays.


    numDays += ADJUST_DAY_COUNT_USER_PERSPECTIVE_FOR_FIRST_DAY_OF_YEAR;



    numHrs = (leftOverSecs / 3600);

    leftOverSecs -= (numHrs * 3600);

    numMins = (leftOverSecs / 60);

    leftOverSecs -= (numMins * 60);

    year = (refYear + (numBlksOfFour * 4) + leftOverYears);

    oscl_snprintf(buf,
                  256,
                  "%04d%02d%02dT%02d%02d%02d.%03dZ",
                  year, month, numDays, numHrs,
                  numMins, leftOverSecs, milliSecs);

    date += buf;

    /* convert to unicode */
    uint32 wDateBufLen = 64 * sizeof(oscl_wchar);
    oscl_wchar wDate[64*sizeof(oscl_wchar)];
    oscl_memset((OsclAny*)wDate, 0, wDateBufLen);
    oscl_UTF8ToUnicode(date.get_cstr(),
                       (int32)(date.get_size()),
                       wDate,
                       (int32)(wDateBufLen));

    OSCL_wHeapString<OsclMemAllocator> wDateStr;
    wDateStr += wDate;

    return (wDateStr);
}