// File: rurban[P]dgnlib/cpl_conv.cpp
// Function: CPLFGets
char *CPLFGets( char *pszBuffer, int nBufferSize, FILE * fp )

{
    int nActuallyRead, nOriginalOffset;

    if ( nBufferSize == 0 || pszBuffer == NULL || fp == NULL )
        return NULL;

/* -------------------------------------------------------------------- */
/*      Let the OS level call read what it things is one line.  This    */
/*      will include the newline.  On windows, if the file happens      */
/*      to be in text mode, the CRLF will have been converted to        */
/*      just the newline (LF).  If it is in binary mode it may well     */
/*      have both.                                                      */
/* -------------------------------------------------------------------- */
    nOriginalOffset = VSIFTell( fp );
    if( VSIFGets( pszBuffer, nBufferSize, fp ) == NULL )
        return NULL;
    
    nActuallyRead = strlen(pszBuffer);
    if ( nActuallyRead == 0 )
        return NULL;

/* -------------------------------------------------------------------- */
/*      If we found \r and out buffer is full, it is possible there     */
/*      is also a pending \n.  Check for it.                            */
/* -------------------------------------------------------------------- */
    if( nBufferSize == nActuallyRead+1
        && pszBuffer[nActuallyRead-1] == 13 )
    {
        int chCheck;
        chCheck = fgetc( fp );
        if( chCheck != 10 )
        {
            // unget the character.
            VSIFSeek( fp, nOriginalOffset+nActuallyRead, SEEK_SET );
        }
    }

/* -------------------------------------------------------------------- */
/*      Trim off \n, \r or \r\n if it appears at the end.  We don't     */
/*      need to do any "seeking" since we want the newline eaten.       */
/* -------------------------------------------------------------------- */
    if( nActuallyRead > 1 
        && pszBuffer[nActuallyRead-1] == 10 
        && pszBuffer[nActuallyRead-2] == 13 )
    {
        pszBuffer[nActuallyRead-2] = '\0';
    }
    else if( pszBuffer[nActuallyRead-1] == 10 
             || pszBuffer[nActuallyRead-1] == 13 )
    {
        pszBuffer[nActuallyRead-1] = '\0';
    }

/* -------------------------------------------------------------------- */
/*      Search within the string for a \r (MacOS convention             */
/*      apparently), and if we find it we need to trim the string,      */
/*      and seek back.                                                  */
/* -------------------------------------------------------------------- */
    char *pszExtraNewline = strchr( pszBuffer, 13 );
    
    if( pszExtraNewline != NULL )
    {
        int chCheck;

        nActuallyRead = pszExtraNewline - pszBuffer + 1;
        
        *pszExtraNewline = '\0';
        VSIFSeek( fp, nOriginalOffset + nActuallyRead - 1, SEEK_SET );

        /* 
         * This hackery is necessary to try and find our correct
         * spot on win32 systems with text mode line translation going 
         * on.  Sometimes the fseek back overshoots, but it doesn't
         * "realize it" till a character has been read. Try to read till
         * we get to the right spot and get our CR. 
         */ 
        chCheck = fgetc( fp );
        while( (chCheck != 13 && chCheck != EOF)
               || VSIFTell(fp) < nOriginalOffset + nActuallyRead )
        {
            static volatile int bWarned = FALSE;

            if( !bWarned )
            {
                bWarned = TRUE;
                CPLDebug( "CPL", "CPLFGets() correcting for DOS text mode translation seek problem." );
            }
            chCheck = fgetc( fp );
        }
    }

    return pszBuffer;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x3267
cmpq $0x0, -0x10(%rbp)
je 0x3267
cmpq $0x0, -0x20(%rbp)
jne 0x3274
movq $0x0, -0x8(%rbp)
jmp 0x3461
movq -0x20(%rbp), %rdi
callq 0x9d00
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movq -0x20(%rbp), %rdx
callq 0x9de0
cmpq $0x0, %rax
jne 0x32a3
movq $0x0, -0x8(%rbp)
jmp 0x3461
movq -0x10(%rbp), %rdi
callq 0x2120
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x32c2
movq $0x0, -0x8(%rbp)
jmp 0x3461
movl -0x14(%rbp), %eax
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jne 0x330d
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xd, %eax
jne 0x330d
movq -0x20(%rbp), %rdi
callq 0x2480
movl %eax, -0x2c(%rbp)
cmpl $0xa, -0x2c(%rbp)
je 0x330b
movq -0x20(%rbp), %rdi
movl -0x28(%rbp), %eax
addl -0x24(%rbp), %eax
movslq %eax, %rsi
xorl %edx, %edx
callq 0x9cd0
jmp 0x330d
cmpl $0x1, -0x24(%rbp)
jle 0x3352
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0x3352
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
subl $0x2, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xd, %eax
jne 0x3352
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
subl $0x2, %ecx
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
jmp 0x3391
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
je 0x337e
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xd, %eax
jne 0x338f
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
jmp 0x3391
movq -0x10(%rbp), %rdi
movl $0xd, %esi
callq 0x20e0
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x3459
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
subq %rcx, %rax
addq $0x1, %rax
movl %eax, -0x24(%rbp)
movq -0x38(%rbp), %rax
movb $0x0, (%rax)
movq -0x20(%rbp), %rdi
movl -0x28(%rbp), %eax
addl -0x24(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rsi
xorl %edx, %edx
callq 0x9cd0
movq -0x20(%rbp), %rdi
callq 0x2480
movl %eax, -0x3c(%rbp)
cmpl $0xd, -0x3c(%rbp)
je 0x33fb
movb $0x1, %al
cmpl $-0x1, -0x3c(%rbp)
movb %al, -0x3d(%rbp)
jne 0x3416
movq -0x20(%rbp), %rdi
callq 0x9d00
movl -0x28(%rbp), %ecx
addl -0x24(%rbp), %ecx
movslq %ecx, %rcx
cmpq %rcx, %rax
setl %al
movb %al, -0x3d(%rbp)
movb -0x3d(%rbp), %al
testb $0x1, %al
jne 0x341f
jmp 0x3457
movl 0x1c1cb(%rip), %eax     # 0x1f5f0
cmpl $0x0, %eax
jne 0x3449
movl $0x1, 0x1c1bc(%rip)     # 0x1f5f0
leaq 0x15f3b(%rip), %rdi     # 0x19376
leaq 0x15f38(%rip), %rsi     # 0x1937a
movb $0x0, %al
callq 0x5a00
movq -0x20(%rbp), %rdi
callq 0x2480
movl %eax, -0x3c(%rbp)
jmp 0x33ea
jmp 0x3459
movq -0x10(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)