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

📄 list_17_04.c

📁 This source code has been tested under OpenWindows 2.0, Sun s X11/NeWS server. For Xlib programs th
💻 C
字号:
/*************************************************************************                                                                    ****  listing_17_04.c                                                   ****                                                                    ****  File Browser, Edition 2. The file browser of Chapter 9, with      ****  the capability to read StdIn if no file is specified. It uses     ****  an input callback to provide this capability.                     ****                                                                    *************************************************************************/#include <stdio.h>#include <Xm/Text.h>void	    LoadFile();			/* FORWARD Definitions	      */void	    UseStdIn();void	    InputCB();Widget	    appshell,			/* Application Shell	      */	    the_text;			/* The text widget            */long	    text_cnt = 0;		/* Number of bytes in buffer  */void main( argc, argv )    int     argc;    char    *argv[];{    appshell = XtInitialize( argv[0], "Listing_17_04", NULL, 0, &argc, argv );    the_text = XmCreateScrolledText( appshell, "TheText", NULL, 0 );    XtManageChild( the_text );    if (argc > 2)        {	fprintf( stderr, "\nbrowser: Usage:\n" );	fprintf( stderr, "         browser FILENAME\n" );	fprintf( stderr, "         (or)\n" );	fprintf( stderr, "         ??? | browser\n" );	exit( 1 );	}    else if (argc == 2)        LoadFile( argv[1] );    else	UseStdIn();    XtRealizeWidget( appshell );    XtMainLoop();}/***** LoadFile( fname )****** Called when the text is to come from a file, this function*** opens the file and loads it into the text widget.**/void LoadFile( fname )    char    	*fname;{    FILE	*infile;    long    	fsize;    char    	*lclptr;    infile = fopen( fname, "r" );    if (infile == NULL)        {	perror( "browser: unable to open input file" );	exit( 2 );	}    fseek( infile, 0, 2 );    fsize = ftell( infile );    rewind( infile );    lclptr = (char *)XtMalloc( fsize + 1 );    fread( lclptr, sizeof(char), fsize, infile );    lclptr[fsize] = '\0';    XmTextSetString( the_text, lclptr );    XtFree( lclptr );    fclose( infile );}/***** UseStdIn()****** This function is called when input is to come from StdIn. It*** simply attaches an input callback.**/void UseStdIn(){    XtAddInput( fileno(stdin), XtInputReadMask, InputCB, NULL );}/***** InputCB( client_data, source, id )****** Called whenever data is present on StdIn. This function*** reads a single character, and appends it to the text buffer.**/void InputCB( client_data, source, id )    caddr_t	client_data;    int		*source;    XtInputId	*id;{    char	lcl_buf[2];    int		in_char;    if ((in_char = getchar()) != EOF)        {	lcl_buf[0] = in_char;	lcl_buf[1] = '\0';;	XmTextReplace( the_text, text_cnt, text_cnt, lcl_buf );	text_cnt++;	}    else        XtRemoveInput( *id );}

⌨️ 快捷键说明

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