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

📄 xfilter.sel.c

📁 `smith.motif.tar.Z includes the source code for the book "Designing X clients with Xt/Motif," by Je
💻 C
字号:
/****  xfilter.sel.c  ****//***********************************************************************  Copyright (c) 1991, 1992 Iris Computing Laboratories.**  This software is provided for demonstration purposes only.  As*  freely-distributed, modifiable source code, this software carries*  absolutely no warranty.  Iris Computing Laboratories disclaims*  all warranties for this software, including any implied warranties*  of merchantability and fitness, and shall not be liable for*  damages of any type resulting from its use.*  Permission to use, copy, modify, and distribute this source code*  for any purpose and without fee is hereby granted, provided that*  the above copyright and this permission notice appear in all copies*  and supporting documentation, and provided that Iris Computing*  Laboratories not be named in advertising or publicity pertaining*  to the redistribution of this software without specific, written*  prior permission.**********************************************************************/#include "xfilter.h"#include "xfilter.sel.h"/*Private functions:*/static void do_filter_operation();static void set_cut_buffer(), set_clipboard_selection(),	set_text_window(), set_file();static int apply_filter();static char *load_buffer();static int store_buffer();/*Private callbacks:*/static void ProcessSelection();static Boolean ClipboardRequest();static void ClipboardLoseSelection();/*Private globals:*/static Atom CLIPBOARD, LENGTH, TARGETS;static char *filter_text = NULL;static int filter_text_len;static Display *display;static void (*free_func)();		/* ptr. to the appropriate `free' function *//*Public functions:*//*initialize_selection_structures() sets up the atoms usedby this client.*/void initialize_selection_structures(w)Widget w;{	display = XtDisplay(w);	CLIPBOARD = XmInternAtom(display, "CLIPBOARD", False);	LENGTH = XmInternAtom(display, "LENGTH", False);	TARGETS = XmInternAtom(display, "TARGETS", False);}	/* initialize_selection_structures *//*free_selection_structures() frees dynamic resources.*/void free_selection_structures(){	if (filter_text)				/* obtained in several ways */		free_func(filter_text);}	/* free_selection_structures *//*filter_primary_selection() hides the details from `xfilter.c'.*/int filter_primary_selection(w)Widget w;{	XtGetSelectionValue(w, XA_PRIMARY, XA_STRING, ProcessSelection,		(XtPointer) NULL, XtLastTimestampProcessed(display));}	/* filter_primary_selection *//*filter_clipboard_selection() hides the details from `xfilter.c'.*/int filter_clipboard_selection(w)Widget w;{	XtGetSelectionValue(w, CLIPBOARD, XA_STRING, ProcessSelection,		(XtPointer) NULL, XtLastTimestampProcessed(display));}	/* filter_primary_selection *//*filter_cut_buffer() gets cut-buffer 0 data and then callsdo_filter_operation() to complete the output side of thefilter operation.  Note that the value returned by XFetchBytes()may not be null-terminated.*/void filter_cut_buffer(w)Widget w;{	char *buffer;	if (filter_text)		free_func(filter_text);	buffer = XFetchBytes(display, &filter_text_len);	if ((filter_text =	(char *) malloc((unsigned) (filter_text_len + 2))) != NULL) {		memcpy(filter_text, buffer, filter_text_len);		filter_text[filter_text_len] = '\0';		free(buffer);	}	else {		user_message("Error:  memory allocation.");		fprintf(stderr, "xfilter:  memory allocation error.\n");	}	free_func = (void *) free;	do_filter_operation(w);}	/* filter_cut_buffer *//*filter_file() gets the text for the filter operation froman existing file.*/void filter_file(w, file_spec)Widget w;char *file_spec;{	if (filter_text)		free_func(filter_text);	filter_text = load_buffer(file_spec, &filter_text_len);	free_func = (void *) free;	do_filter_operation(w);}	/* filter_file *//*file_size() checks for the existence of a file using stat(),returning -1 for a nonexistent file and the file size otherwise.*/int file_size(file_spec)char *file_spec;{	struct stat	statbuf;	if (stat(file_spec, &statbuf) < 0)		return -1;	else		return (int) statbuf.st_size;}    /* file_size *//*Private callbacks:*//*ProcessSelection() stores the text locally that's returned as aresult of the call to XtGetSelectionValue().  A more user-orientedmessage may be needed here.*//*ARGSUSED*/static void ProcessSelection(w, client_data, selection, type, value,	length, format)Widget w;XtPointer client_data;Atom *selection, *type;XtPointer value;unsigned long *length;int *format;{	if (*type == XA_STRING && *format == 8 && value) {		if (filter_text)			free_func(filter_text);		filter_text = (char *) value;		filter_text_len = (int) *length;		free_func = (void *) XtFree;		do_filter_operation(w);	}	else		user_message("The requested selection is unavailable.");}	/* ProcessSelection *//*ClipboardRequest() honors a request for X clipboard-related data.*//*ARGSUSED*/static Boolean ClipboardRequest(w, selection, target, ret_type,	ret_value, ret_len, ret_format)Widget w;Atom *selection, *target, *ret_type;XtPointer *ret_value;unsigned long *ret_len;int *ret_format;{	if (*target == TARGETS) {	   	Atom *targets;	   	int num = 2;					/* magic number */		*ret_type = XA_ATOM;		targets = (Atom *) XtMalloc(sizeof(Atom) * num);		targets[0] = XA_STRING;    	targets[1] = LENGTH;		*ret_value = (XtPointer) targets;		*ret_len = (unsigned long) num;		*ret_format = 32;		return TRUE;	}	else if (*target == LENGTH) {		int *length;		*ret_type = XA_INTEGER;		length = (int *) XtMalloc(sizeof(int));		*length = filter_text_len;		*ret_value = (XtPointer) length;		*ret_len = (unsigned long) 1;		*ret_format = 32;		return TRUE;	}	else if (*target == XA_STRING) {		*ret_type = XA_STRING;		*ret_value = (XtPointer) XtNewString((String) filter_text);		*ret_len = (unsigned long) filter_text_len;		*ret_format = 8;		return TRUE;			}	else		return FALSE;}	/* ClipboardRequest *//*ClipboardLoseSelection() frees clipboard-related data.*//*ARGSUSED*/static void ClipboardLoseSelection(w, selection)Widget w;Atom *selection;{	/* nothing */}	/* ClipboardLoseSelection *//*Private functions:*//*do_filter_operation() takes the input for the filter, appliesthe filter, and deposits the filtered output at the properlocation.  There is no attempt to own the CLIPBOARD selectionuntil the user chooses the X clipboard as the output destinationand presses the "Filter" button to begin the data transfer.Once clipboard ownership is acquired, however, it is retaineduntil another client asserts ownership, e.g., `xclipboard'.*/static void do_filter_operation(w)Widget w;{	if (!filter_text || !*filter_text)		return;	if (!apply_filter()) {		user_message("Unknown error while applying filter.");		return;	}	switch (get_output_type()) {		case xfilter_CUT_BUFFER:			set_cut_buffer();			break;		case xfilter_XCLIPBOARD:			set_clipboard_selection(w);			break;		case xfilter_TEXTWIN:			set_text_window();			break;		case xfilter_FILE:			set_file();			break;		default:			break;	}}	/* do_filter_operation *//*set_cut_buffer() performs output to cut-buffer 0.*/static void set_cut_buffer(){	XStoreBytes(display, filter_text, filter_text_len);}	/* set_cut_buffer *//*set_clipboard_selection() handles output to the X clipboard.  Amore user-oriented message may be needed here.*/static void set_clipboard_selection(w)Widget w;{	if (!XtOwnSelection(w, CLIPBOARD, XtLastTimestampProcessed(display),			ClipboardRequest, ClipboardLoseSelection,			(XtSelectionDoneProc) NULL)) {		user_message("Can't acquire clipboard selection.");	}}	/* set_clipboard_selection *//*set_text_window() handles output to `xfilter's text window.*/static void set_text_window(){	XmTextSetString(get_text_win_widget(), filter_text);}	/* set_text_window *//*set_file() handles output for files.*/static void set_file(){	char *file_spec = XmTextGetString(get_output_text_widget());	store_buffer(file_spec, filter_text, filter_text_len);	XtFree(file_spec);}	/* set_file *//*apply_filter() uses a pedestrian approach to performing thefilter operation.  The input for the filter is written to a(temporary) file and redirected as standard input to thefilter per se.  Output from the filter is redirected to atemporary file and then read into a buffer for subsequentprocessing.  If the filter command area is empty, the inputsimply becomes the output with no file processing involved.*/static int apply_filter(){	char filter_file1[30], filter_file2[30];	/* hard-coded constants */	char *buffer, *cmd, *filter_with_options;	int fsize, file_error = FALSE, unknown_error = TRUE;	filter_with_options = XmTextGetString(get_filter_text_widget());	if (!filter_with_options)			/* return silently */		return FALSE;	if (!*filter_with_options) {		/* no filter specified */		XtFree(filter_with_options);		return TRUE;	}	sprintf(filter_file1, "%s.%d", "/tmp/xfilter.file1", getpid());	sprintf(filter_file2, "%s.%d", "/tmp/xfilter.file2", getpid());	unlink(filter_file1);		/* if it exists */	unlink(filter_file2);		/* if it exists */	if ((fsize =	store_buffer(filter_file1, filter_text, filter_text_len)) != -1) {		if (fsize != filter_text_len) {			user_message("Error:  temporary file.");			fprintf(stderr, "xfilter:  temporary file error.\n");			unlink(filter_file1);			return FALSE;		}		if ((cmd = (char *) malloc((unsigned) (strlen(filter_with_options) +				strlen(filter_file1) + strlen(filter_file2) + 10))) == NULL) {			user_message("Error:  memory allocation.");			fprintf(stderr, "xfilter:  memory allocation error.\n");			XtFree(filter_with_options);			unlink(filter_file1);			return FALSE;		}		sprintf(cmd, "%s < %s > %s", filter_with_options,			filter_file1, filter_file2);		XtFree(filter_with_options);		system(cmd);		free(cmd);		if ((buffer = load_buffer(filter_file2, &fsize)) != NULL) {			if (filter_text)				free_func(filter_text);			filter_text = buffer;			filter_text_len = fsize;			free_func = (void *) free;			unknown_error = FALSE;		}		else			file_error = TRUE;	}	else		file_error = TRUE;	if (file_error) {		user_message("Error:  temporary file.");		fprintf(stderr, "xfilter:  temporary file error.\n");	}	else if (unknown_error) {		user_message("Error:  unknown error during filter operation.");		fprintf(stderr, "xfilter:  unknown error during filter operation.\n");	}	unlink(filter_file1);	unlink(filter_file2);	return !(file_error || unknown_error);}	/* apply_filter *//*load_buffer() loads a file into dynamic memory andreturns a pointer to the buffer.*/static char *load_buffer(file_spec, len)char *file_spec;int *len;{	FILE *fp;	char *buffer;	int fsize;	*len = 0;	if ((fp = fopen(file_spec, "r")) != NULL) {		if ((fsize = file_size(file_spec)) != -1) {			if ((buffer = (char *) malloc((unsigned) fsize + 1)) != NULL) {				fsize = (int) fread(buffer, sizeof(char), fsize, fp);				buffer[fsize] = '\0';				*len = fsize;				return buffer;			}			else {				user_message("Error:  memory allocation.");				fprintf(stderr, "xfilter:  memory allocation error.\n");			}		}		fclose(fp);	}	return NULL;}	/* load_buffer *//*store_buffer() writes the contents of dynamic memory to a file.*/static int store_buffer(file_spec, buffer, len)char *file_spec, *buffer;int len;{	FILE *fp;	int fsize;	if ((fp = fopen(file_spec, "w")) != NULL) {		fsize = (int) fwrite(buffer, sizeof(char), len, fp);		fclose(fp);		return fsize;	}	return -1;}	/* store_buffer */

⌨️ 快捷键说明

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