#include "stdafx.h"
#include "FITSFileBuffer.h"
/*
int IoAOldFITSFileBuffer::Allocate( int MaxKeywordCount, int MaxDataBytes, int MaxExtensionBytes )
{
    Free( );

    if ( MaxKeywordCount < 0 || MaxDataBytes < 0 || MaxExtensionBytes < 0 ) return FALSE;

    int HeaderBytes = 80 * 36 * ( ( MaxKeywordCount + 35 ) / 36 );

    int DataBytes = 2880 * ( ( MaxDataBytes + 2879 ) / 2880 );

    int ExtensionBytes = 2880 * ( ( MaxExtensionBytes + 2879 ) / 2880 );

    if ( ! IoAFileBuffer::Allocate( HeaderBytes + DataBytes + ExtensionBytes ) ) return FALSE;

    m_MaxKeywordCount   = MaxKeywordCount;
    m_MaxDataBytes      = MaxDataBytes;
    m_MaxExtensionBytes = MaxExtensionBytes;

    Clear( );  // blank fill header, zero counts

    return TRUE;
}

void IoAOldFITSFileBuffer::Free( )
{
    m_KeywordCount   = 0;
    m_DataBytes      = 0;
    m_ExtensionBytes = 0;

    m_MaxKeywordCount   = 0;
    m_MaxDataBytes      = 0;
    m_MaxExtensionBytes = 0;

    IoAFileBuffer::Free( );
}

int IoAOldFITSFileBuffer::NewKeyword( const char *Name )
{
    if ( ValidKeyword( Name ) )
    {
        // Make sure file buffer is allocated
        if ( IsAllocated( ) )
        {
            // See if there is space in the header
            if ( m_KeywordCount < m_MaxKeywordCount )
            {
                int KeywordIndex = m_KeywordCount;

                // Copy name into keyword record (convert string to upper-case)
                char *pRecord = HeaderAddress( ) + 80 * KeywordIndex;

                int nName = ::strlen( Name );

                for ( int iName = 0; iName < nName; iName++ ) pRecord[ iName ] = toupper( Name[ iName ] );

                // Pad rest of name field with blanks
                ::FillMemory( pRecord + nName, 8 - nName, ' ' );

                m_KeywordCount += 1;

                return KeywordIndex;
            }
        }
    }

    return -1;
}

int IoAOldFITSFileBuffer::KeywordIndex( const char *Name )
{
    if ( ValidKeyword( Name ) )
    {
        if ( IsAllocated( ) ) {
            // Search header for matching key name
            char *pRecord = HeaderAddress( );

            int nName = ::strlen( Name );

            for ( int KeywordIndex = 0; KeywordIndex < m_KeywordCount; KeywordIndex++ )
            {
                // Compare name strings
                int iName = 0;

                for ( ; iName < nName && pRecord[ iName ] == toupper( Name[ iName ] ); iName++ );

                // See if keyword name matches name argument
                if ( iName == nName )
                {
                    return KeywordIndex;
                }

                pRecord += 80;
            }
        }
    }

    return -1;
}

int IoAOldFITSFileBuffer::SetValue( int KeywordIndex, const char *Value, const char *Comment )
{
    if ( KeywordIndex < 0 || KeywordIndex >= m_KeywordCount ) return FALSE;

    if ( ! IsAllocated( ) ) return FALSE;

    char *pRecord = HeaderAddress( ) + 80 * KeywordIndex;

    int iRecord = 8;

    // See if value is defined
    if ( Value != NULL )
    {
        int nValue = ::strlen( Value );

        if ( nValue > 0 )
        {
            if ( nValue > 70 ) return FALSE;  // too big to fit in header record

            ::CopyMemory( pRecord + iRecord, "= ", 2 );  // columns 9 and 10 set to "= "
            
            iRecord += 2;

            // Value <= 20 characters => fixed format, right-justify value in columns 11 to 30
            if ( nValue <= 20 )
            {
                int nBlank = 20 - nValue;

                ::FillMemory( pRecord + iRecord, nBlank, ' ' );  // pad with blanks
                
                iRecord += nBlank;

                ::CopyMemory( pRecord + iRecord, Value, nValue );  // copy value
                
                iRecord += nValue;
            }
            // Otherwise => free format, left-justify value in columns 11 to 80
            else
            {
                ::CopyMemory( pRecord + iRecord, Value, nValue );  // copy value
                
                iRecord += nValue;
            }
        }
    }

    // See if comment string is defined
    if ( Comment != NULL )
    {
        int nSpace = 80 - iRecord;

        // See if there is any space left for comment separator
        if ( nSpace >= 3 )
        {
            // Comment separator
            ::CopyMemory( pRecord + iRecord, " / ", 3 );
            
            iRecord += 3;

            nSpace -= 3;
        }

        int nComment = ::strlen( Comment );

        if ( nComment > 0 )
        {
            if ( nComment > nSpace ) nComment = nSpace;  // truncate comment string to fit remaining space

            ::CopyMemory( pRecord + iRecord, Comment, nComment );  // copy comment
            
            iRecord += nComment;
        }
    }

    // Pad rest of record with blanks
    ::FillMemory( pRecord + iRecord, 80 - iRecord, ' ' );

    return TRUE;
}

int IoAOldFITSFileBuffer::GetValue( int KeywordIndex, char *Value )
{
    int nValue = 0;

    if ( 0 <= KeywordIndex && KeywordIndex < m_KeywordCount )
    {
        if ( Value != NULL )
        {
            char *pRecord = HeaderAddress( ) + 80 * KeywordIndex;

            // See if columns 8 and 9 set to "= "
            if ( pRecord[ 8 ] == '=' && pRecord[ 9 ] == ' ' )
            {
                int iStart = 10;

                // Search for first non-blank character
                while ( iStart < 80 && pRecord[ iStart ] == ' ' ) iStart += 1;

                if ( iStart < 80 )  // non-blank start character
                {
                    int iStop = iStart;

                    // Search for comment separator character
                    for ( ; iStop < 80 && pRecord[ iStop ] != '/'; iStop += 1 );

                    // Search backwards for first non-blank character
                    for ( iStop -= 1; iStop >= iStart && pRecord[ iStop ] == ' '; iStop -= 1 );

                    // Copy character string value
                    nValue = iStop - iStart + 1;

                    ::CopyMemory( Value, pRecord + iStart, nValue );
                }
            }

            Value[ nValue ] = '\0';  // terminate string
        }
    }

    // Return string length
    return nValue;
}

int IoAOldFITSFileBuffer::NewComment( const char *Name, const char *Comment )
{
    if ( Name != NULL )
    {
        int KeywordIndex = NewKeyword( Name );

        if ( KeywordIndex >= 0 )
        {
            char *pRecord = HeaderAddress( ) + 80 * KeywordIndex;

            int iRecord = 8;

            if ( Comment != NULL )
            {
                int nComment = ::strlen( Comment );

                if ( nComment > 72 ) nComment = 72;  // truncate comment string to fit remaining space

                ::CopyMemory( pRecord + iRecord, Name, nComment );
                
                iRecord += nComment;
            }

            // Pad rest of record with blanks
            ::FillMemory( pRecord + iRecord, 80 - iRecord, ' ' );

            return KeywordIndex;
        }
    }

    return -1;
}

int IoAOldFITSFileBuffer::NewData( const void *Data, int DataBytes )
{
    if ( Data != NULL )
    {
        if ( 0 < DataBytes && DataBytes <= m_MaxDataBytes - m_DataBytes )
        {
            unsigned char *pData = (unsigned char *) DataAddress( );

            int iData = m_DataBytes;

            ::CopyMemory( pData + iData, Data, DataBytes );
            
            iData += DataBytes;

            int nZero = 2880 * ( ( iData + 2879 ) / 2880 ) - iData;

            ::FillMemory( pData + iData, nZero, 0 );

            m_DataBytes = iData;

            return iData;
        }
    }

    return -1;
}

int IoAOldFITSFileBuffer::GetData( int DataOffset, void *Data, int DataBytes )
{
    if ( DataOffset > 0 && Data != NULL && DataBytes > 0 )
    {
        if ( DataOffset + DataBytes <= m_DataBytes )
        {
            unsigned char *pData = (unsigned char *) DataAddress( );

            ::CopyMemory( Data, pData + DataOffset, DataBytes );

            return DataBytes;
        }
    }

    return 0;
}

int IoAOldFITSFileBuffer::NewExtension( const void *Extension, int ExtensionBytes )
{
    if ( Extension != NULL )
    {
        if ( 0 < ExtensionBytes && ExtensionBytes <= m_MaxExtensionBytes - m_ExtensionBytes )
        {
            unsigned char *pExtension = (unsigned char *) ExtensionAddress( );
            
            int iExtension = m_ExtensionBytes;

            ::CopyMemory( pExtension + iExtension, Extension, ExtensionBytes );
            
            iExtension += ExtensionBytes;

            int nBlank = 2880 * ( ( iExtension + 2879 ) / 2880 ) - iExtension;

            ::FillMemory( pExtension + iExtension, nBlank, ' ' );

            m_ExtensionBytes = iExtension;

            return iExtension;
        }
    }

    return -1;
}

int IoAOldFITSFileBuffer::GetExtension( int ExtensionOffset, void *Extension, int ExtensionBytes )
{
    if ( ExtensionOffset > 0 && Extension != NULL && ExtensionBytes > 0 )
    {
        if ( ExtensionOffset + ExtensionBytes <= m_ExtensionBytes )
        {
            unsigned char *pExtension = (unsigned char *) ExtensionAddress( );

            ::CopyMemory( Extension, pExtension + ExtensionOffset, ExtensionBytes );

            return ExtensionBytes;
        }
    }

    return 0;
}
*/


