sorthelp.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 651 行 · 第 1/2 页

C
651
字号
            buf++;
        buf++;
    }
    buf++;
    str = buf;
    while( *buf != '"' && *buf != '\0' ) {
        if( *buf == HELP_ESCAPE )
            buf++;
        buf++;
    }
    len = buf - str;
    str[len] = '\0';
    return( str );
}

bool pass1( FILE *fin, char **helpstr )
{
    char            buffer[ BUFFER_SIZE ];
    int             buflen;
    long            fpos;
    a_helpnode      *h;
    a_helpnode      **hn;
    char            *ptr;
    int             cmp;
    char            *namebuff;
    unsigned        namebuff_len;
    int             count;
    int             len;
    unsigned        topic_len;
    unsigned        desc_len;

    namebuff = NULL;
    namebuff_len = 0;
    topic_len = strlen( DEFTOPIC );
    desc_len = strlen( DESCRIPTION );

    printf( "Pass One:\n" );
    while( !feof( fin ) ) {
        fpos = ftell( fin );
        fgetstring( buffer, BUFFER_SIZE, fin );
        if( memcmp( buffer, DEFTOPIC, topic_len ) == 0 ) {
            if( helpstr[0] != NULL ) {
                PrintError( "more than one DEFTOPIC found\n" );
            } else {
                if( !GenStrings || !GenIndex ) {
                    PrintError( "DEFTOPIC string ignored with this format.\n" );
                }
                if( GenStrings ) {
                    ptr = find_str( &buffer[topic_len] );
                    helpstr[0] = HelpMemAlloc( strlen( ptr ) + 1 );
                    strcpy( helpstr[0], ptr);
                }
            }
        } else if( memcmp( buffer, DESCRIPTION, desc_len ) == 0 ) {
            if( helpstr[1] != NULL ) {
                PrintError( "more than one DESCRIPTION found\n" );
            } else {
                if( !GenStrings || !GenIndex ) {
                    PrintError( "DESCRIPTION string ignored with this format.\n" );
                }
                if( GenStrings ) {
                    ptr = find_str( &buffer[desc_len] );
                    helpstr[1] = HelpMemAlloc( strlen( ptr ) + 1 );
                    strcpy( helpstr[1], ptr );
                }
            }
        } else if( memcmp( buffer, "::::", 4 ) == 0 )
            break;
    }
    while( !feof( fin ) ) {
        h = (a_helpnode *)HelpMemAlloc( sizeof( a_helpnode ) );
        h->fpos = fpos;
        buflen = strlen( buffer );
        if( buffer[ buflen-1 ] == '\n' ) {
            buffer[ buflen-1 ] = '\0';
        }
        h->maxrow = 0;
        h->maxcol = 0;
        h->row = -1;
        h->col = -1;
        h->lines = -1;
        ptr = &buffer[4];
        if( *ptr == '"' ) {
            ptr ++;
            while( *ptr != '\0' && *ptr != '"' ) {
                if( *ptr == HELP_ESCAPE )
                    ptr++;
                ptr++;
            }
            len = ptr - &buffer[5];
            if( namebuff_len <= len ) {
                HelpMemFree( namebuff );
                namebuff = HelpMemAlloc( len + 1 );
                namebuff_len = len + 1;
            }
            memcpy( namebuff, &buffer[5], len );
            namebuff[len] = '\0';
            if( *ptr == '"' )
                ++ptr;
        } else {
            for( ; *ptr != '\0'  &&  !isspace(*ptr); ++ptr )
                ;
            while( *ptr != '\0'  &&  !isspace(*ptr) ) {
                if( *ptr == HELP_ESCAPE )
                    ptr++;
                ptr++;
            }
            len = ptr - &buffer[4];
            if( namebuff_len <= len ) {
                HelpMemFree( namebuff );
                namebuff = HelpMemAlloc( len + 1 );
                namebuff_len = len + 1;
            }
            memcpy( namebuff, &buffer[4], len );
            namebuff[len] = '\0';
        }
        while( isspace( *ptr ) )
            ++ptr;
        if( *ptr != '\0' ) {
            count = sscanf( ptr, "%d %d %d %d %d",
                    &h->maxrow, &h->maxcol, &h->row, &h->col,
                    &h->lines );
            if( count != 2  && count != 4  &&  count != 5 ) {
                PrintError( "invalid help topic line '%s'\n", buffer );
            }
        }
        h->name = strdup( namebuff );
        if( Verbose ) {
            printf( "   %s\n", h->name );
        }
        for( hn=&HelpNodes; *hn != NULL; hn=&(*hn)->next ) {
            cmp = stricmp( h->name, (*hn)->name );
            if( cmp == 0 ) {
                PrintError( "Duplicate Help Topic '%s'\n", h->name );
            }
            if( cmp <= 0 ) {
                h->next = *hn;
                *hn = h;
                break;
            }
        }
        if( *hn == NULL ) {
            h->next = NULL;
            *hn = h;
        }
        if( h->row == -1 ) {
            h->row = 0;
            h->col = 0;
        }
        h->maxcol = 0;
        h->maxrow = 0;
        h->lines = 0;
        while( !feof( fin ) ) {
            fpos = ftell( fin );
            fgetstring( buffer, BUFFER_SIZE, fin );
            if( memcmp( buffer, "::::", 4 ) == 0 )
                break;
            if( strnicmp( buffer, ":eh", 3 ) == 0
                || strnicmp( buffer, ":et", 3 ) == 0 ) {
                h->lines = 0;
            } else if( strnicmp( buffer, ":h", 2 ) == 0
                    || strnicmp( buffer, ":t", 2 ) == 0  ) {
            } else {
                buflen = line_len( buffer );
                if( buflen - 1 > h->maxcol ){
                    h->maxcol = buflen - 1;
                }
                h->lines += 1;
                h->maxrow += 1;
            }
        }
    }
    HelpMemFree( namebuff );
    return( TRUE );
}

void lookup_name( a_helpnode *h, char *name )
{
    a_helpnode      *hptr;
    int             cmp;

    for( hptr = HelpNodes; hptr != NULL; hptr=hptr->next ) {
        cmp = stricmp( name, hptr->name );
        if( cmp < 0 )
            break;
        if( cmp == 0 )
            return;
    }
    PrintError( "Unknown help topic '%s' found in '%s'\n", name, h->name );
}

void fgetstring( char *buffer, int max, FILE *f )
{
    int         curr;
    int         offset;
    int         ch;

    -- max;
    curr = 0;
    offset = 0;
    for( ;; ) {
        ch = fgetc( f );
        switch( ch ) {
        case EOF:
        case '\n':
            *buffer++ = '\r';       /* ignore trailing spaces */
            *buffer++ = '\n';
            *buffer = '\0';
            return;
        case ' ':
            ++ offset;
            break;
        case '\t':
            offset = (offset + 8) & (-8);
            break;
        default:
            while( ++curr <= offset ) {
                *buffer++ = ' ';
            }
            *buffer++ = ch;
            ++ offset;
        }
        if( offset >= max ) {
            *buffer = '\0';
            for( ;; ) {
                ch = fgetc( f );
                if( ch == EOF || ch == '\n' )
                    break;
            }
            return;
        }
    }
}

static char         *nameBuf;
static unsigned     nameBufLen;

static void checkBufCB( TokenType type, HyperLinkInfo *info,
            a_helpnode *node )
{
    if( type == TK_PLAIN_LINK || type == TK_GOOFY_LINK ) {
        if( nameBufLen <= info->topic_len ) {
            HelpMemFree( nameBuf );
            nameBuf = HelpMemAlloc( info->topic_len + 1 );
            nameBufLen = info->topic_len + 1;
        }
        memcpy( nameBuf, info->topic, info->topic_len );
        nameBuf[ info->topic_len ] = '\0';
        if( info->hfname_len == 0 ) {
            lookup_name( node, nameBuf );
        }
    }
}

void check_buffer( a_helpnode *h, char *buffer )
{
    ScanLine( buffer, checkBufCB, h );
    HelpMemFree( nameBuf );
    nameBuf = NULL;
    nameBufLen = 0;
}

bool pass2( FILE *fin, int fout, char **helpstr )
{
    char            buffer[ BUFFER_SIZE ];
    a_helpnode      *h;
    unsigned long   indexlen;

    printf( "Pass Two:\n" );
    if( GenIndex ) {
        indexlen = CalcIndexSize( helpstr, GenStrings );
        lseek( fout, indexlen, SEEK_SET );
    }
    for( h = HelpNodes; h != NULL; h = h->next ) {
        if( Verbose ) {
            printf( "   %s %d %d", h->name, h->maxrow, h->maxcol );
            if( h->row != -1 ) {
                printf( "%d %d", h->row, h->col );
            }
            if( h->lines != -1 ) {
                printf( "%d", h->lines );
            }
            printf( "\n" );
        }
        fseek( fin, h->fpos, SEEK_SET );
        fgetstring( buffer, BUFFER_SIZE, fin );
        h->maxcol += 1;
        h->maxcol = (h->maxcol / 2) * 2;
        if( h->maxcol > MaxCol ) {
            PrintError( "%s %d %d image too wide\n",
                     h->name, h->maxrow, h->maxcol );
            h->maxcol = MaxCol;
        }
        if( h->maxcol > MaxCol-10 ) {
            h->maxcol = MaxCol;
        }
        if( h->maxrow > MaxRow ) {
            if( h->maxrow > MaxRow ) {
                h->maxrow = MaxRow;
            }
        }
        if( Height ) {
            h->maxrow = Height;
        }
        if( Width ) {
            h->maxcol = Width;
        }
        sprintf( buffer, "::::\"%s\" %d %d %d %d %d\r\n",
                 h->name, h->maxrow, h->maxcol, h->row, h->col, h->lines );
        if( GenIndex ) {
            h->fpos = tell( fout );
        }
        write( fout, buffer, strlen( buffer ) );

        while( !feof( fin ) ) {
            fgetstring( buffer, BUFFER_SIZE, fin );
            if( memcmp( buffer, "::::", 4 ) == 0 )
                break;
            check_buffer( h, buffer );
            write( fout, buffer, strlen( buffer ) );
        }
    }
    return( TRUE );
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?