/* $XConsortium: AsciiSrc.c,v 1.66 95/01/16 20:10:20 kaleb Exp $ */
/*
Copyright (c) 1989, 1994 X Consortium
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of the X Consortium shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from the X Consortium.
*/
/*
* AsciiSrc.c - AsciiSrc object. (For use with the text widget).
*
*/
#include <X11/IntrinsicP.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <X11/StringDefs.h>
#ifndef X_NOT_STDC_ENV
#include <stdlib.h>
#endif
#include "XawInit.h"
#include "AsciiSrcP.h"
#include "MultiSrcP.h"
#if (defined(ASCII_STRING) || defined(ASCII_DISK))
# include "AsciiText.h" /* for Widget Classes. */
#endif
/****************************************************************
*
* Full class record constant
*
****************************************************************/
/* Private Data */
#ifdef ASCII_DISK
#endif /* ASCII_DISK */
};
static int ReplaceText();
static FILE * InitStringOrFile();
static void RemoveOldStringOrFile(), CvtStringToAsciiType();
#ifdef X_NOT_STDC_ENV
extern int errno;
#endif
#ifdef X_NOT_POSIX
#define Off_t long
#define Size_t unsigned int
#else
#endif
{
/* core_class fields */
/* class_name */ "AsciiSrc",
/* widget_size */ sizeof(AsciiSrcRec),
/* class_initialize */ ClassInitialize,
/* class_part_initialize */ NULL,
/* class_inited */ FALSE,
/* initialize */ Initialize,
/* initialize_hook */ NULL,
/* realize */ NULL,
/* actions */ NULL,
/* num_actions */ 0,
/* resources */ resources,
/* xrm_class */ NULLQUARK,
/* compress_motion */ FALSE,
/* compress_exposure */ FALSE,
/* compress_enterleave */ FALSE,
/* visible_interest */ FALSE,
/* destroy */ Destroy,
/* resize */ NULL,
/* expose */ NULL,
/* set_values */ SetValues,
/* set_values_hook */ NULL,
/* set_values_almost */ NULL,
/* get_values_hook */ GetValuesHook,
/* accept_focus */ NULL,
/* version */ XtVersion,
/* callback_private */ NULL,
/* tm_table */ NULL,
/* query_geometry */ NULL,
/* display_accelerator */ NULL,
/* extension */ NULL
},
/* textSrc_class fields */
{
/* Read */ ReadText,
/* Replace */ ReplaceText,
/* Scan */ Scan,
/* Search */ Search,
/* SetSelection */ XtInheritSetSelection,
/* ConvertSelection */ XtInheritConvertSelection
},
/* asciiSrc_class fields */
{
/* Keep the compiler happy */ '\0'
}
};
/************************************************************
*
* Semi-Public Interfaces.
*
************************************************************/
/* Function Name: ClassInitialize
* Description: Class Initialize routine, called only once.
* Arguments: none.
* Returns: none.
*/
static void
{
}
/* Function Name: Initialize
* Description: Initializes the simple menu widget
* Arguments: request - the widget requested by the argument list.
* new - the new widget with both resource and non
* resource values.
* Returns: none.
*/
/* ARGSUSED */
static void
{
/*
* Set correct flags (override resources) depending upon widget class.
*/
#ifdef ASCII_DISK
}
#endif
#ifdef ASCII_STRING
}
#endif
}
/* Function Name: ReadText
* Description: This function reads the source.
* Arguments: w - the AsciiSource widget.
* pos - position of the text to retreive.
* RETURNED text - text block that will contain returned text.
* length - maximum number of characters to read.
* Returns: The number of characters read into the buffer.
*/
static XawTextPosition
Widget w;
int length;
{
}
/* Function Name: ReplaceText.
* Description: Replaces a block of text with new text.
* Arguments: w - the AsciiSource widget.
* startPos, endPos - ends of text that will be removed.
* text - new text to be inserted into buffer at startPos.
* Returns: XawEditError or XawEditDone.
*/
/*ARGSUSED*/
static int
Widget w;
{
/*
* Editing a read only source is not allowed.
*/
return(XawEditError);
/*
* Remove Old Stuff.
*/
if (start_piece != end_piece) {
/*
* If empty and not the only piece then remove it.
*/
while (temp_piece != end_piece) {
}
}
else { /* We are fully in one piece. */
}
else {
}
}
/*
* Put in the New Stuff.
*/
while (length > 0) {
char * ptr;
int fill;
/*
* If we are in ascii string emulation mode. Then the
* string is not allowed to grow.
*/
return(XawEditError);
}
}
}
}
}
the buffer. */
return(XawEditDone);
}
/* Function Name: Scan
* Description: Scans the text source for the number and type
* of item specified.
* Arguments: w - the AsciiSource widget.
* position - the position to start scanning.
* type - type of thing to scan for.
* dir - direction to scan.
* count - which occurance if this thing to search for.
* include - whether or not to include the character found in
* the position that is returned.
* Returns: the position of the item found.
*
* Note: While there are only 'n' characters in the file there are n+1
* possible cursor positions (one before the first character and
* one after the last character.
*/
static
Widget w;
int count;
Boolean include;
{
int inc;
char* ptr;
if (dir == XawsdRight)
return(0); /* else. */
}
if ( dir == XawsdRight ) {
/*
* Scanning right from src->ascii_src.length???
*/
inc = 1;
}
else {
if (position == 0)
return(0); /* Scanning left from 0??? */
inc = -1;
position--;
}
/*
* If the buffer is empty then return 0.
*/
switch (type) {
case XawstEOL:
case XawstParagraph:
case XawstWhiteSpace:
/* CONSTCOND */
while (TRUE) {
unsigned char c = *ptr;
if (type == XawstWhiteSpace) {
if (isspace(c)) {
if (non_space)
break;
}
else
}
if (c == '\n') break;
}
else { /* XawstParagraph */
if (first_eol) {
if (c == '\n') {
}
}
else
if ( c == '\n')
break;
else if ( !isspace(c) )
}
return(0);
}
}
}
}
if (!include) {
if ( type == XawstParagraph)
}
break;
case XawstPositions:
break;
/* case XawstAll: ---- handled in special code above */
}
position++;
if (position < 0)
return(0);
return(position);
}
/* Function Name: Search
* Description: Searchs the text source for the text block passed
* Arguments: w - the AsciiSource Widget.
* position - the position to start scanning.
* dir - direction to scan.
* text - the text block to search for.
* Returns: the position of the item found.
*/
static XawTextPosition
Widget w;
XawTextBlock * text;
{
char * ptr;
char * buf;
if ( dir == XawsdRight )
inc = 1;
else {
inc = -1;
if (position == 0)
return(XawTextSearchError); /* scanning left from 0??? */
position--;
}
/* CONSTCOND */
while (TRUE) {
break;
else
count++;
}
else {
if (count != 0) {
}
count = 0;
}
return(XawTextSearchError);
}
}
return(XawTextSearchError);
}
}
}
return(position);
}
/* Function Name: SetValues
* Description: Sets the values for the AsciiSource.
* Arguments: current - current state of the widget.
* request - what was requested.
* new - what the widget will become.
* Returns: True if redisplay is needed.
*/
/* ARGSUSED */
static Boolean
{
int i;
"AsciiSrc: The XtNuseStringInPlace resource may not be changed.");
}
for (i = 0; i < *num_args ; i++ )
string_set = TRUE;
break;
}
what happened. */
total_reset = TRUE;
}
if ( !total_reset &&
}
return(FALSE);
}
/* Function Name: GetValuesHook
* Description: This is a get values hook routine that sets the
* values specific to the ascii source.
* Arguments: w - the AsciiSource Widget.
* args - the argument list.
* num_args - the number of args.
* Returns: none.
*/
static void
Widget w;
{
int i;
for (i = 0; i < *num_args ; i++ )
}
else {
if (XawAsciiSave(w)) /* If save sucessful. */
}
break;
}
}
}
/* Function Name: Destroy
* Description: Destroys an ascii source (frees all data)
* Arguments: src - the Ascii source Widget to free.
* Returns: none.
*/
static void
Destroy (w)
Widget w;
{
}
/************************************************************
*
* Public routines
*
************************************************************/
/* Function Name: XawAsciiSourceFreeString
* Description: Frees the string returned by a get values call
* on the string when the source is of type string.
* Arguments: w - the AsciiSrc widget.
* Returns: none.
*/
void
#else
Widget w;
#endif
{
/* If the src is really a multi, call the multi routine.*/
if ( XtIsSubclass( w, multiSrcObjectClass ) ) {
return;
}
else if ( !XtIsSubclass( w, asciiSrcObjectClass ) ) {
"XawAsciiSourceFreeString's parameter must be an asciiSrc or multiSrc.",
}
}
}
/* Function Name: XawAsciiSave
* Description: Saves all the pieces into a file or string as required.
* Arguments: w - the asciiSrc Widget.
* Returns: TRUE if the save was successful.
*/
#else
XawAsciiSave(w)
Widget w;
#endif
{
/* If the src is really a multi, call the multi save. */
if ( XtIsSubclass( w, multiSrcObjectClass ) )
return( _XawMultiSave( w ) );
else if ( !XtIsSubclass( w, asciiSrcObjectClass ) ) {
"XawAsciiSave's parameter must be an asciiSrc or multiSrc.",
}
/*
* If using the string in place then there is no need to play games
* to get the internal info into a readable string.
*/
return(TRUE);
char * string;
return(TRUE);
return(FALSE);
}
}
else {
else
}
return(TRUE);
}
/* Function Name: XawAsciiSaveAsFile
* Description: Save the current buffer as a file.
* Arguments: w - the AsciiSrc widget.
* name - name of the file to save this file into.
* Returns: True if the save was sucessful.
*/
#else
Widget w;
#endif
{
/* If the src is really a multi, call the multi save. - */
if ( XtIsSubclass( w, multiSrcObjectClass ) )
return( _XawMultiSaveAsFile( w, name ) );
else if ( !XtIsSubclass( w, asciiSrcObjectClass ) ) {
"XawAsciiSaveAsFile's 1st parameter must be an asciiSrc or multiSrc.",
}
return(ret);
}
/* Function Name: XawAsciiSourceChanged
* Description: Returns true if the source has changed since last saved.
* Arguments: w - the ascii source widget.
* Returns: a Boolean (see description).
*/
#else
Widget w;
#endif
{
if ( XtIsSubclass( w, multiSrcObjectClass ) )
if ( XtIsSubclass( w, asciiSrcObjectClass ) )
"XawAsciiSourceChanged parameter must be an asciiSrc or multiSrc.",
return( True ); /* for gcc -Wall */
}
/************************************************************
*
* Private Functions.
*
************************************************************/
static void
{
}
}
/* Function Name: WriteToFile
* Description: Write the string specified to the begining of the file
* specified.
* Arguments: string - string to write.
* name - the name of the file
* Returns: returns TRUE if sucessful, FALSE otherwise.
*/
static Boolean
{
int fd;
return(FALSE);
return(FALSE);
return(TRUE);
}
/* Function Name: StorePiecesInString
* Description: store the pieces in memory into a standard ascii string.
* Arguments: data - the ascii pointer data.
* Returns: none.
*/
static String
{
/*
* This will refill all pieces to capacity.
*/
}
return(string);
}
/* Function Name: InitStringOrFile.
* Description: Initializes the string or file.
* Arguments: src - the AsciiSource.
* Returns: none - May exit though.
*/
static FILE *
{
char * open_mode;
}
/* In case the length resource is incorrectly set */
else
}
return(NULL);
}
/*
* type is XawAsciiFile.
*/
case XawtextRead:
"Creating a read only disk widget and no file specified.",
NULL, 0);
open_mode = "r";
break;
case XawtextAppend:
case XawtextEdit:
open_mode = "w";
} else
open_mode = "r+";
break;
default:
"Bad editMode for ascii source; must be Read, Append or Edit.",
}
/* Allocate new memory for the temp filename, because it is held in
* a stack variable, not static memory. This widget does not need
* to keep the private state field is_tempfile -- it is only accessed
* in this routine, and its former setting is unused.
*/
}
return file;
} else {
"openError", "asciiSourceCreate", "XawWarning",
}
}
}
static void
char * string;
{
* sizeof(unsigned char));
}
}
else
}
else
return;
}
do {
* sizeof(unsigned char));
} while (left > 0);
}
/* Function Name: AllocNewPiece
* Description: Allocates a new piece of memory.
* Arguments: src - The AsciiSrc Widget.
* prev - the piece just before this one, or NULL.
* Returns: the allocated piece.
*/
static Piece *
{
}
else {
}
return(piece);
}
/* Function Name: FreeAllPieces
* Description: Frees all the pieces
* Arguments: src - The AsciiSrc Widget.
* Returns: none.
*/
static void
{
(void) printf("Xaw AsciiSrc Object: possible memory leak in FreeAllPieces().\n");
}
}
/* Function Name: RemovePiece
* Description: Removes a piece from the list.
* Arguments:
* piece - the piece to remove.
* Returns: none.
*/
static void
{
else
}
/* Function Name: FindPiece
* Description: Finds the piece containing the position indicated.
* Arguments: src - The AsciiSrc Widget.
* position - the position that we are searching for.
* RETURNED first - the position of the first character in this piece.
* Returns: piece - the piece that contains this position.
*/
static Piece *
{
return(piece);
}
return(old_piece); /* if we run off the end the return the last piece */
}
/* Function Name: MyStrncpy
* Description: Just like string copy, but slower and will always
* work on overlapping strings.
* Arguments: (same as strncpy) - s1, s2 - strings to copy (2->1).
* n - the number of chars to copy.
* Returns: s1.
*/
static String
int n;
{
char* temp;
if (n == 0) return s1;
return s1;
}
/* Function Name: BreakPiece
* Description: Breaks a full piece into two new pieces.
* Arguments: src - The AsciiSrc Widget.
* piece - the piece to break.
* Returns: none.
*/
static void
{
}
/* ARGSUSED */
static void
{
XrmQuark q;
if (XtQEstring == NULLQUARK) {
}
q = XrmStringToQuark(lowerName);
else {
return;
}
return;
}
}
#if (defined(ASCII_STRING) || defined(ASCII_DISK))
#endif
#ifdef ASCII_STRING
/************************************************************
*
* Compatability functions.
*
************************************************************/
/* Function Name: AsciiStringSourceCreate
* Description: Creates a string source.
* Arguments: parent - the widget that will own this source.
* args, num_args - the argument list.
* Returns: a pointer to the new text source.
*/
{
XtFree((char *)ascii_args);
return(src);
}
/*
* This is hacked up to try to emulate old functionality, it
* may not work, as I have not old code to test it on.
*
* Chris D. Peterson 8/31/89.
*/
void
Widget w;
{
}
#endif /* ASCII_STRING */
#ifdef ASCII_DISK
/* Function Name: AsciiDiskSourceCreate
* Description: Creates a disk source.
* Arguments: parent - the widget that will own this source.
* args, num_args - the argument list.
* Returns: a pointer to the new text source.
*/
{
int i;
num_args++;
for (i = 0; i < num_args; i++)
XtFree((char *)ascii_args);
return(src);
}
#endif /* ASCII_DISK */