⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 memos.gml

📁 开放源码的编译器open watcom 1.6.0版的源代码
💻 GML
📖 第 1 页 / 共 2 页
字号:
    MEMO_EL * new_el;
    MEMO_EL * next;

    new_el = EnterAMemo();
    if( new_el == NULL ) {
        return( NULL );
    }
.code break
    if( el == NULL ) {
        next = MemoHead;
        MemoHead = new_el;
    } else {
        next = el->next;
        el->next = new_el;
    }
.code break
    new_el->prev = el;
    new_el->next = next;
    if( next != NULL ) {
        next->prev = new_el;
    }
    return( new_el );
}

.code break
static MEMO_EL * EnterAMemo( void )
/*********************************/

/* Read a memo from the keyboard, creating the memo structure
 * and filling it in. Return a pointer to the memo (NULL if
 * none read).
 */
{
    MEMO_EL *   el;
    int         len;
    TEXT_LINE * line;
    char        buffer[MAXLEN];

    printf( "What date do you want the memo displayed"
            " (YY/MM/DD)?\n" );
.code break
    if( gets( buffer ) == NULL ) {
        printf( "Error reading from terminal.\n" );
        return( NULL );
    }
.code break
    len = strlen( buffer );
.code break
    if( len != 0
            &&  (len != 8
                ||  buffer[2] != '/'
                || buffer[5] != '/') ) {
        printf( "Date is not valid.\n" );
        return( NULL );
    }
.code break
    el = MemoMAlloc( sizeof( MEMO_EL ) );
    el->text = NULL;
.code break
    strcpy( el->date, buffer );
    line = NULL;
.code break
    printf( "\nEnter the text of the memo.\n" );
    printf( "To terminate the memo,"
            " enter a line starting with =\n" );
.code break
    for( ;; ) {
        if( gets( buffer ) == NULL ) {
            printf( "Error reading from terminal.\n" );
            return( NULL );
        }
.code break
        if( buffer[0] == '=' ) {
            return( el );
        }
        line = AddLine( buffer, el, line );
    }
}

.code break
static MEMO_EL * DeleteMemo( MEMO_EL * el )
/*****************************************/

/* Delete the current memo.
 * Return a pointer to another memo, usually the following one.
 */
{
    MEMO_EL * prev;
    MEMO_EL * next;
    MEMO_EL * ret_el;

    if( el == NULL ) {
        return( MemoHead );
    }
.code break
    prev = el->prev;
    next = el->next;
    ret_el = next;
    if( ret_el == NULL ) {
        ret_el = prev;
    }

.code break
/* If it's the first memo, set a new MemoHead value.
 */
    if( prev == NULL ) {
        MemoHead = next;
        if( next != NULL ) {
            next->prev = NULL;
        }
.code break
    } else {
        prev->next = next;
        if( next != NULL ) {
            next->prev = prev;
        }
    }
.code break
    DisposeMemo( el );
    return( ret_el );
}

.code break
static MEMO_EL * DoUpAction( MEMO_EL * el )
/*****************************************/

/* Perform the UP action, including displaying the memo.
 */
{
    if( el == NULL ) {
        DisplayTop();
    } else {
        el = el->prev;
        DisplayMemo( el );
    }
    return( el );
}

.code break
static MEMO_EL * DoDownAction( MEMO_EL * el )
/*******************************************/

/* Perform the DOWN action, including displaying the memo.
 */
{
    MEMO_EL * next_el;

    next_el = (el == NULL) ? MemoHead : el->next;
    if( next_el == NULL ) {
        printf( "No more memos.\n" );
    } else {
        el = next_el;
        DisplayMemo( el );
    }
    return( el );
}

.code break
static MEMO_EL * ShowTodaysMemos( void )
/**************************************/

/* Show all memos that either:
 * (1) match today's date
 * (2) don't have a date stored.
 * Return a pointer to the last displayed memo.
 */
.code break
{
    MEMO_EL * el;
    MEMO_EL * last_el;
    time_t    timer;
    struct tm ltime;
    char      date[9];

.code break
/* Get today's time in YY/MM/DD format.
 */
    time( &timer );
    ltime = *localtime( &timer );
.code break
    strftime( date, 9, "%y/%m/%d", &ltime );
    last_el = NULL;
.code break
    for( el = MemoHead; el != NULL; el = el->next ) {
        if( el->date[0] == NULLCHAR
                ||  strcmp( date, el->date ) == 0 ) {
            DisplayMemo( el );
            last_el = el;
        }
    }
    return( last_el );
}

.code break
static void DisplayMemo( MEMO_EL * el )
/*************************************/

/* Display a memo on the screen.
 */
{
    TEXT_LINE * tline;

    if( el == NULL ) {
        DisplayTop();
        return;
    }
.code break
    if( el->date[0] == NULLCHAR ) {
        printf( "\nUndated memo\n" );
    } else {
        printf( "\nDated: %s\n", el->date );
    }
.code break
    for( tline = el->text; tline != NULL; tline = tline->next ) {
        printf( "    %s\n", tline->text );
    }
}

.code break
static int SaveMemos( void )
/**************************/

/* Save the memos to the memos file.
 */
{
    FILE *      fid;
    MEMO_EL *   el;
    TEXT_LINE * tline;
    char        buffer[20];

    if( MemoHead == NULL ) {
        printf( "No memos to save.\n" );
        return( FALSE );
    }

.code break
/* Open a temporary filename in case something goes wrong
 * during the save.
 */
    fid = fopen( TempName, "w" );
    if( fid == NULL ) {
        printf( "Unable to open \"%s\" for writing.\n", TempName );
        printf( "Save not performed.\n" );
        return( FALSE );
    }
.code break
    for( el = MemoHead; el != NULL; el = el->next ) {
        sprintf( buffer, "Date:%s", el->date );
        if( !WriteLine( buffer, fid ) ) {
            return( FALSE );
        }
        tline = el->text;
.code break
        for( ; tline != NULL; tline = tline->next ) {
            if( !WriteLine( tline->text, fid ) ) {
                return( FALSE );
            }
        }
.code break
        if( !WriteLine( "====", fid ) ) {
            return( FALSE );
        }
    }

.code break
/* Now get rid of the old file, if it's there, then rename
 * the new one.
 */
    fclose( fid );
    fid = fopen( FileName, "r" );
.code break
    if( fid != NULL ) {
        fclose( fid );
        if( remove( FileName ) != 0 ) {
            perror( "Can't remove old memos file" );
            return( FALSE );
        }
    }
.code break
    if( rename( TempName, FileName ) != 0 ) {
        perror( "Can't rename new memos file" );
        return( FALSE );
    }
    return( TRUE );
}

.code break
static int WriteLine( char * text, FILE * fid )
/*********************************************/
{
    if( fprintf( fid, "%s\n", text ) < 0 ) {
        perror( "Error writing memos file" );
        return( FALSE );
    }
    return( TRUE );
}

.code break
/* Routines for displaying HELP and other simple text.
 */

.code break
static void Usage( void )
/***********************/
{
    printf( "Usage:\n" );
    printf( "    memos ?\n" );
    printf( "         displays this text\n" );
.code break
    printf( "  or\n" );
.code break
    printf( "    memos\n" );
    printf( "         prompts for all actions.\n" );
.code break
    printf( "  or\n" );
.code break
    printf( "    memos action\n" );
    printf( "         performs the action.\n" );
    printf( "         More than one action may be specified.\n" );
    printf( "         action is one of:\n" );
    ShowActions();
}

.code break
static void ShowActions( void )
/*****************************/
{
    printf( "            Help    (display this text)\n" );
.code break
    printf( "            Add     (add new memo here)\n" );
.code break
    printf( "            DELete  (delete current memo)\n" );
.code break
    printf( "            REPlace (replace current memo)\n" );
.code break
    printf( "            SHow    (show the current memo again)\n" );
.code break
    printf( "            Up      (move up one memo)\n" );
.code break
    printf( "            Down    (move down one memo)\n" );
.code break
    printf( "            TOP     (move to the top of the list\n" );
.code break
    printf( "            TODay   (display today's memos)\n" );
.code break
    printf( "            SAve    (write the memos to disk)\n" );
}

.code break
static void Help( void )
/**********************/
{
    printf( "Choose one of:\n" );
    ShowActions();
    printf( "            Quit\n" );
}

.code break
static void DisplayTop( void )
/****************************/
{
    printf( "Top of memos.\n" );
}

.code break
static int WantToQuit( void )
/***************************/

/* Check to see if the memos have been modified, but not saved.
 * If so, query the user to make sure that he/she wants to quit
 * without saving the memos.
 */
{
    char buffer[MAXLEN];

    if( !MemosModified  ||  MemoHead == NULL ) {
        return( TRUE );
    }
.code break
    printf( "\nThe memos have been modified but not saved.\n" );
    printf( "Do you want to leave without saving them?\n" );
.code break
    gets( buffer );
    return( tolower( buffer[0] ) == 'y' );
}

.code break
static void BadFormat( void )
/***************************/
{
    printf( "Invalid format for memos file\n" );
    abort();
}

.code break
static void * MemoMAlloc( int size )
/**********************************/

/* Allocate the specified size of memory, dealing with the
 * case of a failure by displaying a message and quitting.
 */
{
    register char * mem;

    mem = malloc( size );
    if( mem == NULL ) {
        printf( "Unable to allocate %d characters of memory\n",
                size );
        abort();
    }
    return( mem );

}

.code break
static void DisposeMemo( MEMO_EL * el )
/*************************************/

/* Dispose of a memo, including its lines.
 */
{
    TEXT_LINE * tline;
    TEXT_LINE * next;

    tline = el->text;
    while( tline != NULL ) {
        next = tline->next;
        free( tline );
        tline = next;
    }
    free( el );
}
.code end

⌨️ 快捷键说明

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