summaryrefslogtreecommitdiff
path: root/src/AsciiSrc.c
diff options
context:
space:
mode:
authorKaleb Keithley <kaleb@freedesktop.org>2003-11-14 16:48:48 +0000
committerKaleb Keithley <kaleb@freedesktop.org>2003-11-14 16:48:48 +0000
commit384ac455a6cd5d23dfa24f9939f3ec04f1e5de46 (patch)
treecaf2569ba305a1d32fed698546a300d6bb22a6a3 /src/AsciiSrc.c
parent81ad93fde745d556aaa3880deabf3674bb3db49e (diff)
downloadxorg-lib-libXaw-384ac455a6cd5d23dfa24f9939f3ec04f1e5de46.tar.gz
Diffstat (limited to 'src/AsciiSrc.c')
-rw-r--r--src/AsciiSrc.c2635
1 files changed, 1555 insertions, 1080 deletions
diff --git a/src/AsciiSrc.c b/src/AsciiSrc.c
index f377640..4e45979 100644
--- a/src/AsciiSrc.c
+++ b/src/AsciiSrc.c
@@ -26,1294 +26,1763 @@ in this Software without prior written authorization from The Open Group.
*/
+/* $XFree86: xc/lib/Xaw/AsciiSrc.c,v 1.34 2002/11/21 16:22:52 paulo Exp $ */
+
/*
* AsciiSrc.c - AsciiSrc object. (For use with the text widget).
*
*/
-#include <X11/IntrinsicP.h>
#include <stdio.h>
+#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
+#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include <X11/Xos.h>
-#ifndef X_NOT_STDC_ENV
-#include <stdlib.h>
-#endif
#include <X11/Xfuncs.h>
+#include <X11/Xmu/CharSet.h>
+#include <X11/Xmu/Misc.h>
#include <X11/Xaw/XawInit.h>
#include <X11/Xaw/AsciiSrcP.h>
-#include <X11/Xmu/Misc.h>
-#include <X11/Xmu/CharSet.h>
-#include <X11/Xaw/MultiSrcP.h>
+#include <X11/Xaw/MultiSrcP.h>
+#ifndef OLDXAW
+#include <X11/Xaw/TextSinkP.h>
+#include <X11/Xaw/AsciiSinkP.h>
+#endif
+#include "Private.h"
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
#if (defined(ASCII_STRING) || defined(ASCII_DISK))
-# include <X11/Xaw/AsciiText.h> /* for Widget Classes. */
+#include <X11/Xaw/AsciiText.h> /* for Widget Classes */
#endif
+#ifdef X_NOT_POSIX
+#define Off_t long
+#define Size_t unsigned int
+#else
+#define Off_t off_t
+#define Size_t size_t
+#endif
-/****************************************************************
- *
- * Full class record constant
- *
- ****************************************************************/
+#define MAGIC_VALUE ((XawTextPosition)-1)
+#define streq(a, b) (strcmp((a), (b)) == 0)
-/* Private Data */
+/*
+ * Class Methods
+ */
+static void XawAsciiSrcClassInitialize(void);
+static void XawAsciiSrcDestroy(Widget);
+static void XawAsciiSrcGetValuesHook(Widget, ArgList, Cardinal*);
+static void XawAsciiSrcInitialize(Widget, Widget, ArgList, Cardinal*);
+static Boolean XawAsciiSrcSetValues(Widget, Widget, Widget,
+ ArgList, Cardinal*);
+static XawTextPosition ReadText(Widget, XawTextPosition, XawTextBlock*, int);
+static int ReplaceText(Widget, XawTextPosition, XawTextPosition,
+ XawTextBlock*);
+static XawTextPosition Scan(Widget, XawTextPosition, XawTextScanType,
+ XawTextScanDirection, int, Bool);
+static XawTextPosition Search(Widget, XawTextPosition, XawTextScanDirection,
+ XawTextBlock*);
-static int magic_value = MAGIC_VALUE;
+/*
+ * Prototypes
+ */
+static Piece *AllocNewPiece(AsciiSrcObject, Piece*);
+static void BreakPiece(AsciiSrcObject, Piece*);
+static Boolean CvtAsciiTypeToString(Display*, XrmValuePtr, Cardinal*,
+ XrmValuePtr, XrmValuePtr, XtPointer*);
+static void CvtStringToAsciiType(XrmValuePtr, Cardinal*,
+ XrmValuePtr, XrmValuePtr);
+static Piece *FindPiece(AsciiSrcObject, XawTextPosition, XawTextPosition*);
+static void FreeAllPieces(AsciiSrcObject);
+static FILE *InitStringOrFile(AsciiSrcObject, Bool);
+static void LoadPieces(AsciiSrcObject, FILE*, char*);
+static void RemoveOldStringOrFile(AsciiSrcObject, Bool);
+static void RemovePiece(AsciiSrcObject, Piece*);
+static String StorePiecesInString(AsciiSrcObject);
+static Bool WriteToFile(String, String, unsigned);
+static Bool WritePiecesToFile(AsciiSrcObject, String);
+static void GetDefaultPieceSize(Widget, int, XrmValue*);
-#define offset(field) XtOffsetOf(AsciiSrcRec, ascii_src.field)
+/*
+ * More Prototypes
+ */
+#ifdef ASCII_DISK
+Widget XawAsciiDiskSourceCreate(Widget, ArgList, Cardinal);
+#endif
+#ifdef ASCII_STRING
+Widget XawStringSourceCreate(Widget, ArgList, Cardinal);
+void XawTextSetLastPos(Widget, XawTextPosition);
+#endif
+/*
+ * Initialization
+ */
+#define offset(field) XtOffsetOf(AsciiSrcRec, ascii_src.field)
static XtResource resources[] = {
- {XtNstring, XtCString, XtRString, sizeof (char *),
- offset(string), XtRString, NULL},
- {XtNtype, XtCType, XtRAsciiType, sizeof (XawAsciiType),
- offset(type), XtRImmediate, (XtPointer)XawAsciiString},
- {XtNdataCompression, XtCDataCompression, XtRBoolean, sizeof (Boolean),
- offset(data_compression), XtRImmediate, (XtPointer) TRUE},
- {XtNpieceSize, XtCPieceSize, XtRInt, sizeof (XawTextPosition),
- offset(piece_size), XtRImmediate, (XtPointer) BUFSIZ},
- {XtNcallback, XtCCallback, XtRCallback, sizeof(XtPointer),
- offset(callback), XtRCallback, (XtPointer)NULL},
- {XtNuseStringInPlace, XtCUseStringInPlace, XtRBoolean, sizeof (Boolean),
- offset(use_string_in_place), XtRImmediate, (XtPointer) FALSE},
- {XtNlength, XtCLength, XtRInt, sizeof (int),
- offset(ascii_length), XtRInt, (XtPointer) &magic_value},
-
+ {
+ XtNstring,
+ XtCString,
+ XtRString,
+ sizeof(char*),
+ offset(string),
+ XtRString,
+ NULL
+ },
+ {
+ XtNtype,
+ XtCType,
+ XtRAsciiType,
+ sizeof(XawAsciiType),
+ offset(type),
+ XtRImmediate,
+ (XtPointer)XawAsciiString
+ },
+ {
+ XtNdataCompression,
+ XtCDataCompression,
+ XtRBoolean,
+ sizeof(Boolean),
+ offset(data_compression),
+ XtRImmediate,
+ (XtPointer)True
+ },
+ {
+ XtNpieceSize,
+ XtCPieceSize,
+ XtRInt,
+ sizeof(XawTextPosition),
+ offset(piece_size),
+ XtRCallProc,
+ (XtPointer)GetDefaultPieceSize
+ },
+#ifdef OLDXAW
+ {
+ XtNcallback,
+ XtCCallback,
+ XtRCallback,
+ sizeof(XtPointer),
+ offset(callback),
+ XtRCallback,
+ (XtPointer)NULL
+ },
+#endif
+ {
+ XtNuseStringInPlace,
+ XtCUseStringInPlace,
+ XtRBoolean,
+ sizeof(Boolean),
+ offset(use_string_in_place),
+ XtRImmediate,
+ (XtPointer)False
+ },
+ {
+ XtNlength,
+ XtCLength,
+ XtRInt,
+ sizeof(int),
+ offset(ascii_length),
+ XtRImmediate,
+ (XtPointer)MAGIC_VALUE
+ },
#ifdef ASCII_DISK
- {XtNfile, XtCFile, XtRString, sizeof (String),
- offset(filename), XtRString, NULL},
+ {
+ XtNfile,
+ XtCFile,
+ XtRString,
+ sizeof(String),
+ offset(filename),
+ XtRString,
+ NULL
+ },
#endif /* ASCII_DISK */
};
#undef offset
-static XawTextPosition Scan(), Search(), ReadText();
-static int ReplaceText();
-static Piece * FindPiece(), * AllocNewPiece();
-static FILE * InitStringOrFile();
-static void FreeAllPieces(), RemovePiece(), BreakPiece(), LoadPieces();
-static void RemoveOldStringOrFile(), CvtStringToAsciiType();
-static void ClassInitialize(), Initialize(), Destroy(), GetValuesHook();
-static String MyStrncpy(), StorePiecesInString();
-static Boolean SetValues(), WriteToFile();
-#ifdef X_NOT_STDC_ENV
-extern int errno;
-#endif
-#ifdef X_NOT_POSIX
-#define Off_t long
-#define Size_t unsigned int
-#else
-#define Off_t off_t
-#define Size_t size_t
-#endif
-
-#define superclass (&textSrcClassRec)
+#define Superclass (&textSrcClassRec)
AsciiSrcClassRec asciiSrcClassRec = {
+ /* object */
{
-/* core_class fields */
- /* superclass */ (WidgetClass) superclass,
- /* 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,
- /* num_resources */ XtNumber(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
+ (WidgetClass)Superclass, /* superclass */
+ "AsciiSrc", /* class_name */
+ sizeof(AsciiSrcRec), /* widget_size */
+ XawAsciiSrcClassInitialize, /* class_initialize */
+ NULL, /* class_part_initialize */
+ False, /* class_inited */
+ XawAsciiSrcInitialize, /* initialize */
+ NULL, /* initialize_hook */
+ NULL, /* realize */
+ NULL, /* actions */
+ 0, /* num_actions */
+ resources, /* resources */
+ XtNumber(resources), /* num_resources */
+ NULLQUARK, /* xrm_class */
+ False, /* compress_motion */
+ False, /* compress_exposure */
+ False, /* compress_enterleave */
+ False, /* visible_interest */
+ XawAsciiSrcDestroy, /* destroy */
+ NULL, /* resize */
+ NULL, /* expose */
+ XawAsciiSrcSetValues, /* set_values */
+ NULL, /* set_values_hook */
+ NULL, /* set_values_almost */
+ XawAsciiSrcGetValuesHook, /* get_values_hook */
+ NULL, /* accept_focus */
+ XtVersion, /* version */
+ NULL, /* callback_private */
+ NULL, /* tm_table */
+ NULL, /* query_geometry */
+ NULL, /* display_accelerator */
+ NULL, /* extension */
},
-/* textSrc_class fields */
+ /* text_src */
{
- /* Read */ ReadText,
- /* Replace */ ReplaceText,
- /* Scan */ Scan,
- /* Search */ Search,
- /* SetSelection */ XtInheritSetSelection,
- /* ConvertSelection */ XtInheritConvertSelection
+ ReadText, /* Read */
+ ReplaceText, /* Replace */
+ Scan, /* Scan */
+ Search, /* Search */
+ XtInheritSetSelection, /* SetSelection */
+ XtInheritConvertSelection, /* ConvertSelection */
},
-/* asciiSrc_class fields */
+ /* ascii_src */
{
- /* Keep the compiler happy */ '\0'
- }
+ NULL, /* extension */
+ },
};
WidgetClass asciiSrcObjectClass = (WidgetClass)&asciiSrcClassRec;
-/************************************************************
- *
- * Semi-Public Interfaces.
- *
- ************************************************************/
+static XrmQuark Qstring, Qfile;
-/* Function Name: ClassInitialize
- * Description: Class Initialize routine, called only once.
- * Arguments: none.
- * Returns: none.
+/*
+ * Implementation
+ */
+/*
+ * Function:
+ * XawAsciiSrcClassInitialize()
+ *
+ * Description:
+ * Initializes the asciiSrcObjectClass and install the converters for
+ * AsciiType <-> String.
*/
-
static void
-ClassInitialize()
+XawAsciiSrcClassInitialize(void)
{
- XawInitializeWidgetSet();
- XtAddConverter( XtRString, XtRAsciiType, CvtStringToAsciiType,
- NULL, (Cardinal) 0);
+ XawInitializeWidgetSet();
+ Qstring = XrmPermStringToQuark(XtEstring);
+ Qfile = XrmPermStringToQuark(XtEfile);
+ XtAddConverter(XtRString, XtRAsciiType, CvtStringToAsciiType, NULL, 0);
+ XtSetTypeConverter(XtRAsciiType, XtRString, CvtAsciiTypeToString,
+ NULL, 0, XtCacheNone, NULL);
}
-/* 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.
+/*
+ * Function:
+ * XawAsciiSrcInitialize
+ *
+ * Parameters:
+ * request - widget requested by the argument list
+ * cnew - new widget with both resource and non resource values
+ * args - (unused)
+ * num_args - (unused)
+ *
+ * Description:
+ * Initializes the ascii src object.
*/
-
-/* ARGSUSED */
+/*ARGSUSED*/
static void
-Initialize(request, new, args, num_args)
-Widget request, new;
-ArgList args;
-Cardinal *num_args;
+XawAsciiSrcInitialize(Widget request, Widget cnew,
+ ArgList args, Cardinal *num_args)
{
- AsciiSrcObject src = (AsciiSrcObject) new;
- FILE * file;
-
-/*
- * Set correct flags (override resources) depending upon widget class.
- */
+ AsciiSrcObject src = (AsciiSrcObject)cnew;
+ FILE *file;
- src->text_src.text_format = XawFmt8Bit; /* data format. */
+ /*
+ * Set correct flags (override resources) depending upon widget class
+ */
+ src->text_src.text_format = XawFmt8Bit;
#ifdef ASCII_DISK
- if (XtIsSubclass(XtParent(new), asciiDiskWidgetClass)) {
- src->ascii_src.type = XawAsciiFile;
- src->ascii_src.string = src->ascii_src.filename;
- }
+ if (XtIsSubclass(XtParent(cnew), asciiDiskWidgetClass)) {
+ src->ascii_src.type = XawAsciiFile;
+ src->ascii_src.string = src->ascii_src.filename;
+ }
#endif
#ifdef ASCII_STRING
- if (XtIsSubclass(XtParent(new), asciiStringWidgetClass)) {
- src->ascii_src.use_string_in_place = TRUE;
- src->ascii_src.type = XawAsciiString;
- }
+ if (XtIsSubclass(XtParent(cnew), asciiStringWidgetClass)) {
+ src->ascii_src.use_string_in_place = True;
+ src->ascii_src.type = XawAsciiString;
+ }
#endif
- src->ascii_src.changes = FALSE;
- src->ascii_src.allocated_string = FALSE;
+#ifdef OLDXAW
+ src->ascii_src.changes = False;
+#else
+ src->text_src.changed = False;
+#endif
+ src->ascii_src.allocated_string = False;
+
+ if (src->ascii_src.use_string_in_place && src->ascii_src.string == NULL)
+ src->ascii_src.use_string_in_place = False;
- file = InitStringOrFile(src, src->ascii_src.type == XawAsciiFile);
- LoadPieces(src, file, NULL);
+ file = InitStringOrFile(src, src->ascii_src.type == XawAsciiFile);
+ LoadPieces(src, file, NULL);
- if (file != NULL) fclose(file);
+ if (file != NULL)
+ fclose(file);
}
-/* 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.
+/*
+ * Function:
+ * ReadText
+ *
+ * Parameters:
+ * w - AsciiSource widget
+ * pos - position of the text to retreive.
+ * text - text block that will contain returned text
+ * length - maximum number of characters to read
+ *
+ * Description:
+ * This function reads the source.
+ *
+ * Returns:
+ * The character position following the retrieved text.
*/
-
static XawTextPosition
-ReadText(w, pos, text, length)
-Widget w;
-XawTextPosition pos;
-XawTextBlock *text;
-int length;
+ReadText(Widget w, XawTextPosition pos, XawTextBlock *text, int length)
{
- AsciiSrcObject src = (AsciiSrcObject) w;
- XawTextPosition count, start;
- Piece * piece = FindPiece(src, pos, &start);
-
- text->firstPos = pos;
- text->ptr = piece->text + (pos - start);
- count = piece->used - (pos - start);
- text->length = (length > count) ? count : length;
- return(pos + text->length);
-}
+ AsciiSrcObject src = (AsciiSrcObject)w;
+ XawTextPosition count, start;
+ Piece *piece;
+#ifndef OLDXAW
+ XawTextAnchor *anchor;
+ XawTextEntity *entity;
+ XawTextPosition offset, end = pos + length;
+ Bool state;
+
+ end = XawMin(end, src->ascii_src.length);
+ while ((state = XawTextSourceAnchorAndEntity(w, pos, &anchor, &entity)) &&
+ (entity->flags & XAW_TENTF_HIDE))
+ pos = anchor->position + entity->offset + entity->length;
+ if (state == False ||
+ !(entity->flags & XAW_TENTF_REPLACE)) {
+ while (entity) {
+ offset = anchor->position + entity->offset;
+ if (offset >= end)
+ break;
+ if (offset > pos &&
+ (entity->flags & (XAW_TENTF_HIDE | XAW_TENTF_REPLACE))) {
+ end = XawMin(end, offset);
+ break;
+ }
+ if ((entity = entity->next) == NULL &&
+ (anchor = XawTextSourceNextAnchor(w, anchor)) != NULL)
+ entity = anchor->entities;
+ }
+ }
+ else if (state && (entity->flags & XAW_TENTF_REPLACE) && pos < end) {
+ XawTextBlock *block = (XawTextBlock*)entity->data;
+
+ offset = anchor->position + entity->offset;
+ end = XawMin(end, offset + block->length);
+ if ((length = end - pos) < 0)
+ length = 0;
+ text->length = length;
+ text->format = XawFmt8Bit;
+ if (length == 0) {
+ text->firstPos = end = offset + entity->length;
+ text->ptr = "";
+ }
+ else {
+ text->firstPos = pos;
+ text->ptr = block->ptr + (pos - offset);
+ if (pos + length < offset + block->length)
+ end = pos + length; /* there is data left to be read */
+ else
+ end = offset + entity->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.
- */
+ return (end);
+ }
-/*ARGSUSED*/
-static int
-ReplaceText (w, startPos, endPos, text)
-Widget w;
-XawTextPosition startPos, endPos;
-XawTextBlock *text;
-{
- AsciiSrcObject src = (AsciiSrcObject) w;
- Piece *start_piece, *end_piece, *temp_piece;
- XawTextPosition start_first, end_first;
- int length, firstPos;
+ if ((length = end - pos) < 0)
+ length = 0;
+#endif
+
+ piece = FindPiece(src, pos, &start);
+ text->firstPos = pos;
+ text->ptr = piece->text + (pos - start);
+ count = piece->used - (pos - start);
+ text->length = Max(0, (length > count) ? count : length);
+ text->format = XawFmt8Bit;
+
+ return (pos + text->length);
+}
/*
- * Editing a read only source is not allowed.
+ * Function:
+ * ReplaceText
+ *
+ * Parameters:
+ * w - AsciiSource object
+ * startPos - ends of text that will be replaced
+ * endPos - ""
+ * text - new text to be inserted into buffer at startPos
+ *
+ * Description:
+ * Replaces a block of text with new text.
+ *
+ * Returns:
+ * XawEditDone on success, XawEditError otherwise
*/
+/*ARGSUSED*/
+static int
+ReplaceText(Widget w, XawTextPosition startPos, XawTextPosition endPos,
+ XawTextBlock *text)
+{
+ AsciiSrcObject src = (AsciiSrcObject)w;
+ Piece *start_piece, *end_piece, *temp_piece;
+ XawTextPosition start_first, end_first;
+ int length, firstPos;
- if (src->text_src.edit_mode == XawtextRead)
- return(XawEditError);
-
- start_piece = FindPiece(src, startPos, &start_first);
- end_piece = FindPiece(src, endPos, &end_first);
+ /*
+ * Editing a read only source is not allowed
+ */
+ if (src->text_src.edit_mode == XawtextRead)
+ return (XawEditError);
- src->ascii_src.changes = TRUE; /* We have changed the buffer. */
+ start_piece = FindPiece(src, startPos, &start_first);
+ end_piece = FindPiece(src, endPos, &end_first);
-/*
- * Remove Old Stuff.
- */
+#ifndef OLDXAW
+ /*
+ * This is a big hack, but I can't think about a clever way to know
+ * if the character being moved forward has a negative lbearing.
+ *
+ */
+ if (start_piece->used) {
+ int i;
+
+ for (i = 0; i < src->text_src.num_text; i++) {
+ int line;
+ TextWidget ctx = (TextWidget)src->text_src.text[i];
+
+ for (line = 0; line < ctx->text.lt.lines; line++)
+ if (startPos < ctx->text.lt.info[line + 1].position)
+ break;
+ if (i < ctx->text.lt.lines &&
+ startPos > ctx->text.lt.info[i].position) {
+ AsciiSinkObject sink = (AsciiSinkObject)ctx->text.sink;
+ XawTextAnchor *anchor;
+ XawTextEntity *entity;
+ XawTextProperty *property;
+ XFontStruct *font;
+
+ if (XawTextSourceAnchorAndEntity(w, startPos, &anchor, &entity) &&
+ (property = XawTextSinkGetProperty(ctx->text.sink,
+ entity->property)) != NULL &&
+ (property->mask & XAW_TPROP_FONT))
+ font = property->font;
+ else
+ font = sink->ascii_sink.font;
+
+ if (font->min_bounds.lbearing < 0) {
+ int lbearing = font->min_bounds.lbearing;
+ unsigned char c = *(unsigned char*)
+ (start_piece->text + (startPos - start_first));
+
+ if (c == '\t' || c == '\n')
+ c = ' ';
+ else if ((c & 0177) < XawSP || c == 0177) {
+ if (sink->ascii_sink.display_nonprinting)
+ c = c > 0177 ? '\\' : c + '^';
+ else
+ c = ' ';
+ }
+ if (font->per_char &&
+ (c >= font->min_char_or_byte2 && c <= font->max_char_or_byte2))
+ lbearing = font->per_char[c - font->min_char_or_byte2].lbearing;
+ if (lbearing < 0)
+ _XawTextNeedsUpdating(ctx, startPos - 1, startPos);
+ }
+ }
+ }
+ }
- if (start_piece != end_piece) {
- temp_piece = start_piece->next;
-/*
- * If empty and not the only piece then remove it.
- */
+#endif
- if ( ((start_piece->used = startPos - start_first) == 0) &&
- !((start_piece->next == NULL) && (start_piece->prev == NULL)) )
- RemovePiece(src, start_piece);
+ /*
+ * Remove Old Stuff
+ */
+ if (start_piece != end_piece) {
+ temp_piece = start_piece->next;
+
+ /*
+ * If empty and not the only piece then remove it.
+ */
+ if (((start_piece->used = startPos - start_first) == 0)
+ && !(start_piece->next == NULL && start_piece->prev == NULL))
+ RemovePiece(src, start_piece);
+
+ while (temp_piece != end_piece) {
+ temp_piece = temp_piece->next;
+ RemovePiece(src, temp_piece->prev);
+ }
- while (temp_piece != end_piece) {
- temp_piece = temp_piece->next;
- RemovePiece(src, temp_piece->prev);
- }
- end_piece->used -= endPos - end_first;
- if (end_piece->used != 0)
- MyStrncpy(end_piece->text, (end_piece->text + endPos - end_first),
- (int) end_piece->used);
- }
- else { /* We are fully in one piece. */
- if ( (start_piece->used -= endPos - startPos) == 0) {
- if ( !((start_piece->next == NULL) && (start_piece->prev == NULL)) )
- RemovePiece(src, start_piece);
+ end_piece->used -= endPos - end_first;
+ if (end_piece->used != 0)
+ memmove(end_piece->text, end_piece->text + endPos - end_first,
+ (unsigned)end_piece->used);
}
- else {
- MyStrncpy(start_piece->text + (startPos - start_first),
- start_piece->text + (endPos - start_first),
- (int) (start_piece->used - (startPos - start_first)) );
- if ( src->ascii_src.use_string_in_place &&
- ((src->ascii_src.length - (endPos - startPos)) <
- (src->ascii_src.piece_size - 1)) )
- start_piece->text[src->ascii_src.length - (endPos - startPos)] = '\0';
+ else { /* We are fully in one piece */
+ if ((start_piece->used -= endPos - startPos) == 0) {
+ if (!(start_piece->next == NULL && start_piece->prev == NULL))
+ RemovePiece(src, start_piece);
+ }
+ else {
+ memmove(start_piece->text + (startPos - start_first),
+ start_piece->text + (endPos - start_first),
+ (unsigned)(start_piece->used - (startPos - start_first)));
+ if (src->ascii_src.use_string_in_place
+ && src->ascii_src.length - (endPos - startPos)
+ < src->ascii_src.piece_size - 1)
+ start_piece->text[src->ascii_src.length - (endPos - startPos)] =
+ '\0';
+ }
}
- }
- src->ascii_src.length += -(endPos - startPos) + text->length;
+ src->ascii_src.length += -(endPos - startPos) + text->length;
- if ( text->length != 0) {
+ if ( text->length != 0) {
+ /*
+ * Put in the New Stuff
+ */
+ start_piece = FindPiece(src, startPos, &start_first);
- /*
- * Put in the New Stuff.
- */
-
- start_piece = FindPiece(src, startPos, &start_first);
-
- length = text->length;
- firstPos = text->firstPos;
-
- while (length > 0) {
- char * ptr;
- int fill;
-
- if (src->ascii_src.use_string_in_place) {
- if (start_piece->used == (src->ascii_src.piece_size - 1)) {
- /*
- * If we are in ascii string emulation mode. Then the
- * string is not allowed to grow.
- */
- start_piece->used = src->ascii_src.length =
- src->ascii_src.piece_size - 1;
- start_piece->text[src->ascii_src.length] = '\0';
- return(XawEditError);
- }
- }
+ length = text->length;
+ firstPos = text->firstPos;
+
+ while (length > 0) {
+ char *ptr;
+ int fill;
+
+ if (src->ascii_src.use_string_in_place) {
+ if (start_piece->used == src->ascii_src.piece_size - 1) {
+ /*
+ * If we are in ascii string emulation mode. Then the
+ * string is not allowed to grow
+ */
+ start_piece->used = src->ascii_src.length =
+ src->ascii_src.piece_size - 1;
+ start_piece->text[src->ascii_src.length] = '\0';
+ return (XawEditError);
+ }
+ }
+ if (start_piece->used == src->ascii_src.piece_size) {
+ BreakPiece(src, start_piece);
+ start_piece = FindPiece(src, startPos, &start_first);
+ }
- if (start_piece->used == src->ascii_src.piece_size) {
- BreakPiece(src, start_piece);
- start_piece = FindPiece(src, startPos, &start_first);
- }
-
- fill = Min((int)(src->ascii_src.piece_size - start_piece->used), length);
-
- ptr = start_piece->text + (startPos - start_first);
- MyStrncpy(ptr + fill, ptr,
- (int) start_piece->used - (startPos - start_first));
- strncpy(ptr, text->ptr + firstPos, fill);
-
- startPos += fill;
- firstPos += fill;
- start_piece->used += fill;
- length -= fill;
- }
- }
+ fill = Min((int)(src->ascii_src.piece_size - start_piece->used),
+ length);
- if (src->ascii_src.use_string_in_place)
- start_piece->text[start_piece->used] = '\0';
+ ptr = start_piece->text + (startPos - start_first);
+ memmove(ptr + fill, ptr,
+ (unsigned)(start_piece->used - (startPos - start_first)));
+ memcpy(ptr, text->ptr + firstPos, (unsigned)fill);
- XtCallCallbacks(w, XtNcallback, NULL); /* Call callbacks, we have changed
- the buffer. */
+ startPos += fill;
+ firstPos += fill;
+ start_piece->used += fill;
+ length -= fill;
+ }
+ }
- return(XawEditDone);
-}
+ if (src->ascii_src.use_string_in_place)
+ start_piece->text[start_piece->used] = '\0';
-/* 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.
- */
+#ifdef OLDXAW
+ src->ascii_src.changes = True;
+ XtCallCallbacks(w, XtNcallback, NULL);
+#endif
-static
-XawTextPosition
-Scan (w, position, type, dir, count, include)
-Widget w;
-XawTextPosition position;
-XawTextScanType type;
-XawTextScanDirection dir;
-int count;
-Boolean include;
-{
- AsciiSrcObject src = (AsciiSrcObject) w;
- int inc;
- Piece* piece;
- XawTextPosition first, first_eol_position;
- char* ptr;
-
- if (type == XawstAll) { /* Optomize this common case. */
- if (dir == XawsdRight)
- return(src->ascii_src.length);
- return(0); /* else. */
- }
-
- if (position > src->ascii_src.length)
- position = src->ascii_src.length;
-
- if ( dir == XawsdRight ) {
- if (position == src->ascii_src.length)
-/*
- * Scanning right from src->ascii_src.length???
- */
- return(src->ascii_src.length);
- inc = 1;
- }
- else {
- if (position == 0)
- return(0); /* Scanning left from 0??? */
- inc = -1;
- position--;
- }
-
- piece = FindPiece(src, position, &first);
+ return (XawEditDone);
+}
/*
- * If the buffer is empty then return 0.
+ * Function:
+ * Scan
+ *
+ * Parameters:
+ * w - AsciiSource object
+ * position - 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
+ *
+ * Description:
+ * Scans the text source for the number and type of item specified.
+ *
+ * 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
*/
-
- if ( piece->used == 0 ) return(0);
-
- ptr = (position - first) + piece->text;
-
- switch (type) {
- case XawstEOL:
- case XawstParagraph:
- case XawstWhiteSpace:
- for ( ; count > 0 ; count-- ) {
- Boolean non_space = FALSE, first_eol = TRUE;
- /* CONSTCOND */
- while (TRUE) {
- unsigned char c = *ptr;
-
- ptr += inc;
- position += inc;
-
- if (type == XawstWhiteSpace) {
- if (isspace(c)) {
- if (non_space)
- break;
- }
- else
- non_space = TRUE;
+static XawTextPosition
+Scan(Widget w, register XawTextPosition position, XawTextScanType type,
+ XawTextScanDirection dir, int count, Bool include)
+{
+ AsciiSrcObject src = (AsciiSrcObject)w;
+ Piece *piece;
+ XawTextPosition first, first_eol_position = 0;
+ register char *ptr, *lim;
+ register int cnt = count;
+ register unsigned char c;
+
+ if (dir == XawsdLeft) {
+ if (position <= 0)
+ return (0);
+ --position;
+ }
+ else if (position >= src->ascii_src.length)
+ return (src->ascii_src.length);
+
+ piece = FindPiece(src, position, &first);
+ if (piece->used == 0)
+ return (0);
+
+ ptr = (position - first) + piece->text;
+
+ if (dir == XawsdRight) {
+ lim = piece->text + piece->used;
+ switch (type) {
+ case XawstEOL:
+ case XawstParagraph:
+ case XawstWhiteSpace:
+ case XawstAlphaNumeric:
+ for (; cnt > 0; cnt--) {
+ Bool non_space = False, first_eol = True;
+
+ /*CONSTCOND*/
+ while (True) {
+ if (ptr >= lim) {
+ piece = piece->next;
+ if (piece == NULL) /* End of text */
+ return (src->ascii_src.length);
+ ptr = piece->text;
+ lim = piece->text + piece->used;
+ }
+
+ c = *ptr++;
+ ++position;
+
+ if (type == XawstEOL) {
+ if (c == '\n')
+ break;
+ }
+ else if (type == XawstAlphaNumeric) {
+ if (!isalnum(c)) {
+ if (non_space)
+ break;
+ }
+ else
+ non_space = True;
+ }
+ else if (type == XawstWhiteSpace) {
+ if (isspace(c)) {
+ if (non_space)
+ break;
+ }
+ else
+ non_space = True;
+ }
+ else { /* XawstParagraph */
+ if (first_eol) {
+ if (c == '\n') {
+ first_eol_position = position;
+ first_eol = False;
+ }
+ }
+ else if (c == '\n')
+ break;
+ else if (!isspace(c))
+ first_eol = True;
+ }
+ }
+ }
+ break;
+ case XawstPositions:
+ position += count;
+ return (position < src->ascii_src.length ?
+ position : src->ascii_src.length);
+ case XawstAll:
+ return (src->ascii_src.length);
+ default:
+ break;
}
- else if (type == XawstEOL) {
- if (c == '\n') break;
+ if (!include) {
+ if (type == XawstParagraph)
+ position = first_eol_position;
+ if (count)
+ --position;
}
- else { /* XawstParagraph */
- if (first_eol) {
- if (c == '\n') {
- first_eol_position = position;
- first_eol = FALSE;
- }
- }
- else
- if ( c == '\n')
- break;
- else if ( !isspace(c) )
- first_eol = TRUE;
- }
-
-
- if ( ptr < piece->text ) {
- piece = piece->prev;
- if (piece == NULL) /* Begining of text. */
- return(0);
- ptr = piece->text + piece->used - 1;
+ }
+ else {
+ lim = piece->text;
+ switch (type) {
+ case XawstEOL:
+ case XawstParagraph:
+ case XawstWhiteSpace:
+ case XawstAlphaNumeric:
+ for (; cnt > 0; cnt--) {
+ Bool non_space = False, first_eol = True;
+
+ /*CONSTCOND*/
+ while (True) {
+ if (ptr < lim) {
+ piece = piece->prev;
+ if (piece == NULL) /* Begining of text */
+ return (0);
+ ptr = piece->text + piece->used - 1;
+ lim = piece->text;
+ }
+
+ c = *ptr--;
+ --position;
+
+ if (type == XawstEOL) {
+ if (c == '\n')
+ break;
+ }
+ else if (type == XawstAlphaNumeric) {
+ if (!isalnum(c)) {
+ if (non_space)
+ break;
+ }
+ else
+ non_space = True;
+ }
+ else if (type == XawstWhiteSpace) {
+ if (isspace(c)) {
+ if (non_space)
+ break;
+ }
+ else
+ non_space = True;
+ }
+ else { /* XawstParagraph */
+ if (first_eol) {
+ if (c == '\n') {
+ first_eol_position = position;
+ first_eol = False;
+ }
+ }
+ else if (c == '\n')
+ break;
+ else if (!isspace(c))
+ first_eol = True;
+ }
+ }
+ }
+ break;
+ case XawstPositions:
+ position -= count - 1;
+ return (position > 0 ? position : 0);
+ case XawstAll:
+ return (0);
+ default:
+ break;
}
- else if ( ptr >= (piece->text + piece->used) ) {
- piece = piece->next;
- if (piece == NULL) /* End of text. */
- return(src->ascii_src.length);
- ptr = piece->text;
+ if (!include) {
+ if (type == XawstParagraph)
+ position = first_eol_position;
+ if (count)
+ ++position;
}
- }
+ position++;
}
- if (!include) {
- if ( type == XawstParagraph)
- position = first_eol_position;
- position -= inc;
- }
- break;
- case XawstPositions:
- position += count * inc;
- break;
-/* case XawstAll: ---- handled in special code above */
- }
-
- if ( dir == XawsdLeft )
- position++;
-
- if (position >= src->ascii_src.length)
- return(src->ascii_src.length);
- if (position < 0)
- return(0);
-
- return(position);
+
+ 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.
+/*
+ * Function:
+ * Search
+ *
+ * Parameters:
+ * w - AsciiSource object
+ * position - the position to start scanning
+ * dir - direction to scan
+ * text - text block to search for
+ *
+ * Description:
+ * Searchs the text source for the text block passed.
+ *
+ * Returns:
+ * The position of the item found
*/
-
static XawTextPosition
-Search(w, position, dir, text)
-Widget w;
-XawTextPosition position;
-XawTextScanDirection dir;
-XawTextBlock * text;
+Search(Widget w, register XawTextPosition position, XawTextScanDirection dir,
+ XawTextBlock *text)
{
- AsciiSrcObject src = (AsciiSrcObject) w;
- int inc, count = 0;
- char * ptr;
- Piece * piece;
- char * buf;
- XawTextPosition first;
-
- if ( dir == XawsdRight )
- inc = 1;
- else {
- inc = -1;
- if (position == 0)
- return(XawTextSearchError); /* scanning left from 0??? */
- position--;
- }
-
- buf = XtMalloc((unsigned)sizeof(unsigned char) * text->length);
- strncpy(buf, (text->ptr + text->firstPos), text->length);
- piece = FindPiece(src, position, &first);
- ptr = (position - first) + piece->text;
-
- /* CONSTCOND */
- while (TRUE) {
- if (*ptr == ((dir == XawsdRight) ? *(buf + count)
- : *(buf + text->length - count - 1)) ) {
- if (count == (text->length - 1))
- break;
- else
- count++;
- }
- else {
- if (count != 0) {
- position -=inc * count;
- ptr -= inc * count;
- }
- count = 0;
+ AsciiSrcObject src = (AsciiSrcObject)w;
+ register int count = 0;
+ register char *ptr, c;
+ char *str;
+ Piece *piece;
+ char *buf;
+ XawTextPosition first;
+ int cnt, case_sensitive;
+
+ if (dir == XawsdLeft) {
+ if (position == 0)
+ return (XawTextSearchError);
+ position--;
}
- ptr += inc;
- position += inc;
-
- while ( ptr < piece->text ) {
- piece = piece->prev;
- if (piece == NULL) { /* Begining of text. */
- XtFree(buf);
- return(XawTextSearchError);
- }
- ptr = piece->text + piece->used - 1;
+ buf = XtMalloc((unsigned)sizeof(unsigned char) * text->length);
+ memcpy(buf, text->ptr, (unsigned)text->length);
+ piece = FindPiece(src, position, &first);
+ ptr = (position - first) + piece->text;
+ case_sensitive = text->firstPos;
+
+ if (dir == XawsdRight) {
+ str = buf;
+ c = *str;
+ /*CONSTCOND*/
+ while (1) {
+ if (*ptr++ == c
+ || (case_sensitive && isalpha(c) && isalpha(ptr[-1])
+ && toupper(c) == toupper(ptr[-1]))) {
+ if (++count == text->length)
+ break;
+ c = *++str;
+ }
+ else if (count) {
+ ptr -= count;
+ str -= count;
+ position -= count;
+ count = 0;
+ c = *str;
+
+ if (ptr < piece->text) {
+ do {
+ cnt = piece->text - ptr;
+ piece = piece->prev;
+ if (piece == NULL) {
+ XtFree(buf);
+ return (XawTextSearchError);
+ }
+ ptr = piece->text + piece->used - cnt;
+ } while (ptr < piece->text);
+ }
+ }
+ position++;
+ if (ptr >= (piece->text + piece->used)) {
+ do {
+ cnt = ptr - (piece->text + piece->used);
+ piece = piece->next;
+ if (piece == NULL) {
+ XtFree(buf);
+ return (XawTextSearchError);
+ }
+ ptr = piece->text + cnt;
+ } while (ptr >= (piece->text + piece->used));
+ }
+ }
+
+ position -= text->length - 1;
}
-
- while ( ptr >= (piece->text + piece->used) ) {
- piece = piece->next;
- if (piece == NULL) { /* End of text. */
- XtFree(buf);
- return(XawTextSearchError);
- }
- ptr = piece->text;
+ else {
+ str = buf + text->length - 1;
+ c = *str;
+ /*CONSTCOND*/
+ while (1) {
+ if (*ptr-- == c
+ || (case_sensitive && isalpha(c) && isalpha(ptr[1])
+ && toupper(c) == toupper(ptr[1]))) {
+ if (++count == text->length)
+ break;
+ c = *--str;
+ }
+ else if (count) {
+ ptr += count;
+ str += count;
+ position += count;
+ count = 0;
+ c = *str;
+
+ if (ptr >= (piece->text + piece->used)) {
+ do {
+ cnt = ptr - (piece->text + piece->used);
+ piece = piece->next;
+ if (piece == NULL) {
+ XtFree(buf);
+ return (XawTextSearchError);
+ }
+ ptr = piece->text + cnt;
+ } while (ptr >= (piece->text + piece->used));
+ }
+ }
+ position--;
+ if (ptr < piece->text) {
+ do {
+ cnt = piece->text - ptr;
+ piece = piece->prev;
+ if (piece == NULL) {
+ XtFree(buf);
+ return (XawTextSearchError);
+ }
+ ptr = piece->text + piece->used - cnt;
+ } while (ptr < piece->text);
+ }
+ }
}
- }
- XtFree(buf);
- if (dir == XawsdLeft)
- return(position);
- return(position - (text->length - 1));
+ XtFree(buf);
+
+ 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.
+/*
+ * Function:
+ * XawAsciiSrcSetValues
+ *
+ * Parameters:
+ * current - current state of the widget
+ * request - what was requested
+ * cnew - what the widget will become
+ * args - representation of changed resources
+ * num_args - number of resources that have changed
+ *
+ * Description:
+ * Sets the values for the AsciiSource.
+ *
+ * Returns:
+ * True if redisplay is needed
*/
-
-/* ARGSUSED */
static Boolean
-SetValues(current, request, new, args, num_args)
-Widget current, request, new;
-ArgList args;
-Cardinal * num_args;
+XawAsciiSrcSetValues(Widget current, Widget request, Widget cnew,
+ ArgList args, Cardinal *num_args)
{
- AsciiSrcObject src = (AsciiSrcObject) new;
- AsciiSrcObject old_src = (AsciiSrcObject) current;
- Boolean total_reset = FALSE, string_set = FALSE;
- FILE * file;
- int i;
-
- if ( old_src->ascii_src.use_string_in_place !=
- src->ascii_src.use_string_in_place ) {
- XtAppWarning( XtWidgetToApplicationContext(new),
- "AsciiSrc: The XtNuseStringInPlace resource may not be changed.");
- src->ascii_src.use_string_in_place =
- old_src->ascii_src.use_string_in_place;
- }
-
- for (i = 0; i < *num_args ; i++ )
- if (streq(args[i].name, XtNstring)) {
- string_set = TRUE;
- break;
- }
-
- if ( string_set || (old_src->ascii_src.type != src->ascii_src.type) ) {
- RemoveOldStringOrFile(old_src, string_set); /* remove old info. */
- file = InitStringOrFile(src, string_set); /* Init new info. */
- LoadPieces(src, file, NULL); /* load new info into internal buffers. */
- if (file != NULL) fclose(file);
- XawTextSetSource( XtParent(new), new, 0); /* Tell text widget
- what happened. */
- total_reset = TRUE;
- }
-
- if ( old_src->ascii_src.ascii_length != src->ascii_src.ascii_length )
- src->ascii_src.piece_size = src->ascii_src.ascii_length;
-
- if ( !total_reset &&
- (old_src->ascii_src.piece_size != src->ascii_src.piece_size) ) {
- String string = StorePiecesInString(old_src);
- FreeAllPieces(old_src);
- LoadPieces(src, NULL, string);
- XtFree(string);
- }
-
- return(FALSE);
+ AsciiSrcObject src = (AsciiSrcObject)cnew;
+ AsciiSrcObject old_src = (AsciiSrcObject)current;
+ Bool total_reset = False, string_set = False;
+ FILE *file;
+ unsigned int i;
+
+ if (old_src->ascii_src.use_string_in_place
+ != src->ascii_src.use_string_in_place) {
+ XtAppWarning(XtWidgetToApplicationContext(cnew),
+ "AsciiSrc: The XtNuseStringInPlace resource may "
+ "not be changed.");
+ src->ascii_src.use_string_in_place =
+ old_src->ascii_src.use_string_in_place;
+ }
+
+ for (i = 0; i < *num_args ; i++)
+ if (streq(args[i].name, XtNstring)) {
+ string_set = True;
+ break;
+ }
+
+ if (string_set || (old_src->ascii_src.type != src->ascii_src.type)) {
+ RemoveOldStringOrFile(old_src, string_set); /* remove old info */
+ file = InitStringOrFile(src, string_set); /* Init new info */
+ LoadPieces(src, file, NULL); /* load new info into internal buffers */
+ if (file != NULL)
+ fclose(file);
+#ifndef OLDXAW
+ for (i = 0; i < src->text_src.num_text; i++)
+ /* Tell text widget what happened */
+ XawTextSetSource(src->text_src.text[i], cnew, 0);
+#else
+ XawTextSetSource(XtParent(cnew), cnew, 0);
+#endif
+ total_reset = True;
+ }
+
+ if (old_src->ascii_src.ascii_length != src->ascii_src.ascii_length)
+ src->ascii_src.piece_size = src->ascii_src.ascii_length + 1;
+
+ if (!total_reset &&
+ old_src->ascii_src.piece_size != src->ascii_src.piece_size) {
+ String string = StorePiecesInString(old_src);
+
+ FreeAllPieces(old_src);
+ LoadPieces(src, NULL, string);
+ XtFree(string);
+ }
+
+ 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.
+/*
+ * Function:
+ * XawAsciiSrcGetValuesHook
+ *
+ * Parameters:
+ * w - AsciiSource Widget
+ * args - argument list
+ * num_args - number of args
+ *
+ * Description:
+ * This is a get values hook routine that sets the
+ * values specific to the ascii source.
*/
-
static void
-GetValuesHook(w, args, num_args)
-Widget w;
-ArgList args;
-Cardinal * num_args;
+XawAsciiSrcGetValuesHook(Widget w, ArgList args, Cardinal *num_args)
{
- AsciiSrcObject src = (AsciiSrcObject) w;
- int i;
-
- if (src->ascii_src.type == XawAsciiString) {
- for (i = 0; i < *num_args ; i++ )
- if (streq(args[i].name, XtNstring)) {
- if (src->ascii_src.use_string_in_place) {
- *((char **) args[i].value) = src->ascii_src.first_piece->text;
- }
- else {
- if (XawAsciiSave(w)) /* If save sucessful. */
- *((char **) args[i].value) = src->ascii_src.string;
- }
- break;
- }
- }
-}
-
-/* Function Name: Destroy
- * Description: Destroys an ascii source (frees all data)
- * Arguments: src - the Ascii source Widget to free.
- * Returns: none.
- */
+ AsciiSrcObject src = (AsciiSrcObject)w;
+ unsigned int i;
+
+ if (src->ascii_src.type == XawAsciiString) {
+ for (i = 0; i < *num_args ; i++)
+ if (streq(args[i].name, XtNstring)) {
+ if (src->ascii_src.use_string_in_place)
+ *((char **)args[i].value) = src->ascii_src.first_piece->text;
+ else if (XawAsciiSave(w)) /* If save sucessful */
+ *((char **)args[i].value) = src->ascii_src.string;
+ break;
+ }
+ }
+ }
-static void
-Destroy (w)
-Widget w;
+/*
+ * Function:
+ * XawAsciiSrcDestroy
+ *
+ * Parameters:
+ * src - Ascii source object to free
+ *
+ * Description:
+ * Destroys an ascii source (frees all data)
+ */
+static void
+XawAsciiSrcDestroy(Widget w)
{
- RemoveOldStringOrFile((AsciiSrcObject) w, True);
+ RemoveOldStringOrFile((AsciiSrcObject) w, True);
}
-/************************************************************
- *
+/*
* Public routines
+ */
+/*
+ * Function:
+ * XawAsciiSourceFreeString
*
- ************************************************************/
-
-/* 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.
+ * Parameters:
+ * w - AsciiSrc widget
+ *
+ * Description:
+ * Frees the string returned by a get values call
+ * on the string when the source is of type string.
*/
-
void
-#if NeedFunctionPrototypes
XawAsciiSourceFreeString(Widget w)
-#else
-XawAsciiSourceFreeString(w)
-Widget w;
-#endif
{
- AsciiSrcObject src = (AsciiSrcObject) w;
-
- /* If the src is really a multi, call the multi routine.*/
-
- if ( XtIsSubclass( w, multiSrcObjectClass ) ) {
- _XawMultiSourceFreeString( w );
- return;
- }
-
- else if ( !XtIsSubclass( w, asciiSrcObjectClass ) ) {
- XtErrorMsg("bad argument", "asciiSource", "XawError",
- "XawAsciiSourceFreeString's parameter must be an asciiSrc or multiSrc.",
- NULL, NULL);
- }
-
- if (src->ascii_src.allocated_string && src->ascii_src.type != XawAsciiFile) {
- src->ascii_src.allocated_string = FALSE;
- XtFree(src->ascii_src.string);
- src->ascii_src.string = NULL;
- }
+ AsciiSrcObject src = (AsciiSrcObject)w;
+
+ /* If the src is really a multi, call the multi routine */
+ if (XtIsSubclass(w, multiSrcObjectClass)) {
+ _XawMultiSourceFreeString(w);
+ return;
+ }
+ else if (!XtIsSubclass(w, asciiSrcObjectClass)) {
+ XtErrorMsg("bad argument", "asciiSource", "XawError",
+ "XawAsciiSourceFreeString's parameter must be "
+ "an asciiSrc or multiSrc.",
+ NULL, NULL);
+ }
+
+ if (src->ascii_src.allocated_string && src->ascii_src.type != XawAsciiFile) {
+ src->ascii_src.allocated_string = False;
+ XtFree(src->ascii_src.string);
+ src->ascii_src.string = NULL;
+ }
}
-/* 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.
+/*
+ * Function:
+ * XawAsciiSave
+ *
+ * Parameters:
+ * w - asciiSrc Widget
+ *
+ * Description:
+ * Saves all the pieces into a file or string as required.
+ *
+ * Returns:
+ * True if the save was successful
*/
-
-Boolean
-#if NeedFunctionPrototypes
+Bool
XawAsciiSave(Widget w)
-#else
-XawAsciiSave(w)
-Widget w;
-#endif
{
- AsciiSrcObject src = (AsciiSrcObject) w;
+ AsciiSrcObject src = (AsciiSrcObject)w;
- /* If the src is really a multi, call the multi save. */
+ /* If the src is really a multi, call the multi save */
+ if (XtIsSubclass(w, multiSrcObjectClass ))
+ return (_XawMultiSave(w));
- if ( XtIsSubclass( w, multiSrcObjectClass ) )
- return( _XawMultiSave( w ) );
-
- else if ( !XtIsSubclass( w, asciiSrcObjectClass ) ) {
- XtErrorMsg("bad argument", "asciiSource", "XawError",
- "XawAsciiSave's parameter must be an asciiSrc or multiSrc.",
+ else if (!XtIsSubclass(w, asciiSrcObjectClass))
+ XtErrorMsg("bad argument", "asciiSource", "XawError",
+ "XawAsciiSave's parameter must be an asciiSrc or multiSrc.",
NULL, NULL);
- }
-
-/*
- * If using the string in place then there is no need to play games
- * to get the internal info into a readable string.
- */
-
- if (src->ascii_src.use_string_in_place)
- return(TRUE);
- if (src->ascii_src.type == XawAsciiFile) {
- char * string;
+ /*
+ * If using the string in place then there is no need to play games
+ * to get the internal info into a readable string.
+ */
+ if (src->ascii_src.use_string_in_place)
+ return (True);
- if (!src->ascii_src.changes) /* No changes to save. */
- return(TRUE);
+ if (src->ascii_src.type == XawAsciiFile) {
+#ifdef OLDXAW
+ if (!src->ascii_src.changes)
+#else
+ if (!src->text_src.changed) /* No changes to save */
+#endif
+ return (True);
- string = StorePiecesInString(src);
+ if (WritePiecesToFile(src, src->ascii_src.string) == False)
+ return (False);
+ }
+ else {
+ if (src->ascii_src.allocated_string == True)
+ XtFree(src->ascii_src.string);
+ else
+ src->ascii_src.allocated_string = True;
- if (WriteToFile(string, src->ascii_src.string) == FALSE) {
- XtFree(string);
- return(FALSE);
+ src->ascii_src.string = StorePiecesInString(src);
}
- XtFree(string);
- }
- else {
- if (src->ascii_src.allocated_string == TRUE)
- XtFree(src->ascii_src.string);
- else
- src->ascii_src.allocated_string = TRUE;
-
- src->ascii_src.string = StorePiecesInString(src);
- }
- src->ascii_src.changes = FALSE;
- return(TRUE);
+#ifdef OLDXAW
+ src->ascii_src.changes = False;
+#else
+ src->text_src.changed = False;
+#endif
+
+ 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.
+/*
+ * Function:
+ * XawAsciiSaveAsFile
+ *
+ * Arguments:
+ * w - AsciiSrc widget
+ * name - name of the file to save this file into
+ *
+ * Description:
+ * Save the current buffer as a file.
+ *
+ * Returns:
+ * True if the save was sucessful
*/
-
-Boolean
-#if NeedFunctionPrototypes
-XawAsciiSaveAsFile(Widget w, _Xconst char* name)
-#else
-XawAsciiSaveAsFile(w, name)
-Widget w;
-String name;
-#endif
+Bool
+XawAsciiSaveAsFile(Widget w, _Xconst char *name)
{
- AsciiSrcObject src = (AsciiSrcObject) w;
- String string;
- Boolean ret;
+ AsciiSrcObject src = (AsciiSrcObject)w;
+ Bool ret;
- /* If the src is really a multi, call the multi save. - */
+ /* If the src is really a multi, call the multi save */
- if ( XtIsSubclass( w, multiSrcObjectClass ) )
- return( _XawMultiSaveAsFile( w, name ) );
+ if (XtIsSubclass( w, multiSrcObjectClass))
+ return (_XawMultiSaveAsFile(w, name));
- else if ( !XtIsSubclass( w, asciiSrcObjectClass ) ) {
- XtErrorMsg("bad argument", "asciiSource", "XawError",
- "XawAsciiSaveAsFile's 1st parameter must be an asciiSrc or multiSrc.",
+ else if (!XtIsSubclass(w, asciiSrcObjectClass))
+ XtErrorMsg("bad argument", "asciiSource", "XawError",
+ "XawAsciiSaveAsFile's 1st parameter must be an "
+ "asciiSrc or multiSrc.",
NULL, NULL);
- }
- string = StorePiecesInString(src);
+ if (src->ascii_src.type == XawAsciiFile)
+ ret = WritePiecesToFile(src, (String)name);
+ else {
+ String string = StorePiecesInString(src);
+
+ ret = WriteToFile(string, (String)name, src->ascii_src.length);
+ XtFree(string);
+ }
- ret = WriteToFile(string, name);
- XtFree(string);
- return(ret);
+ 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).
+/*
+ * Function:
+ * XawAsciiSourceChanged
+ *
+ * Parameters:
+ * w - ascii source widget
+ *
+ * Description:
+ * Returns true if the source has changed since last saved.
+ *
+ * Returns:
+ * A Boolean (see description).
*/
-
-Boolean
-#if NeedFunctionPrototypes
+Bool
XawAsciiSourceChanged(Widget w)
+{
+#ifdef OLDXAW
+ if (XtIsSubclass(w, multiSrcObjectClass))
+ return (((MultiSrcObject)w)->multi_src.changes);
+
+ if (XtIsSubclass(w, asciiSrcObjectClass))
+ return (((AsciiSrcObject)w)->ascii_src.changes);
#else
-XawAsciiSourceChanged(w)
-Widget w;
+ if (XtIsSubclass(w, textSrcObjectClass))
+ return (((TextSrcObject)w)->textSrc.changed);
#endif
-{
- if ( XtIsSubclass( w, multiSrcObjectClass ) )
- return( ( (MultiSrcObject) w )->multi_src.changes );
+ XtErrorMsg("bad argument", "asciiSource", "XawError",
+ "XawAsciiSourceChanged parameter must be an "
+ "asciiSrc or multiSrc.",
+ NULL, NULL);
- if ( XtIsSubclass( w, asciiSrcObjectClass ) )
- return( ( (AsciiSrcObject) w)->ascii_src.changes );
+ return (True);
+}
- XtErrorMsg("bad argument", "asciiSource", "XawError",
- "XawAsciiSourceChanged parameter must be an asciiSrc or multiSrc.",
- NULL, NULL);
+/*
+ * Private Functions
+ */
+static void
+RemoveOldStringOrFile(AsciiSrcObject src, Bool checkString)
+{
+ FreeAllPieces(src);
- return( True ); /* for gcc -Wall */
+ if (checkString && src->ascii_src.allocated_string) {
+ XtFree(src->ascii_src.string);
+ src->ascii_src.allocated_string = False;
+ src->ascii_src.string = NULL;
+ }
}
-
-/************************************************************
+
+/*
+ * Function:
+ * WriteToFile
*
- * Private Functions.
+ * Parameters:
+ * string - string to write
+ * name - the name of the file
*
- ************************************************************/
-
-static void
-RemoveOldStringOrFile(src, checkString)
-AsciiSrcObject src;
-Boolean checkString;
+ * Description:
+ * Write the string specified to the begining of the file specified.
+ *
+ * Returns:
+ * returns True if sucessful, False otherwise
+ */
+static Bool
+WriteToFile(String string, String name, unsigned length)
{
- FreeAllPieces(src);
+ int fd;
- if (checkString && src->ascii_src.allocated_string) {
- XtFree(src->ascii_src.string);
- src->ascii_src.allocated_string = False;
- src->ascii_src.string = NULL;
- }
+ if ((fd = creat(name, 0666)) == -1
+ || write(fd, string, length) == -1)
+ return (False);
+
+ if (close(fd) == -1)
+ return (False);
+
+ return (True);
}
-/* 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.
+/*
+ * Function:
+ * WritePiecesToFile
+ *
+ * Parameters:
+ * src - ascii source object
+ * name - name of the file
+ *
+ * Description:
+ * Almost identical to WriteToFile, but only works for ascii src objects
+ * of type XawAsciiFile. This function avoids allocating temporary memory,
+ * what can be useful when editing very large files.
+ *
+ * Returns:
+ * returns True if sucessful, False otherwise
*/
-
-static Boolean
-WriteToFile(string, name)
-String string, name;
+static Bool
+WritePiecesToFile(AsciiSrcObject src, String name)
{
- int fd;
-
- if ( ((fd = creat(name, 0666)) == -1 ) ||
- (write(fd, string, sizeof(unsigned char) * strlen(string)) == -1) )
- return(FALSE);
+ Piece *piece;
+ int fd;
+
+ if (src->ascii_src.data_compression) {
+ Piece *tmp;
+
+ piece = src->ascii_src.first_piece;
+ while (piece) {
+ int bytes = src->ascii_src.piece_size - piece->used;
+
+ if (bytes > 0 && (tmp = piece->next) != NULL) {
+ bytes = XawMin(bytes, tmp->used);
+ memcpy(piece->text + piece->used, tmp->text, bytes);
+ memmove(tmp->text, tmp->text + bytes, tmp->used - bytes);
+ piece->used += bytes;
+ if ((tmp->used -= bytes) == 0) {
+ RemovePiece(src, tmp);
+ continue;
+ }
+ }
+ piece = piece->next;
+ }
+ }
- if ( close(fd) == -1 )
- return(FALSE);
+ if ((fd = creat(name, 0666)) == -1)
+ return (False);
- return(TRUE);
+ for (piece = src->ascii_src.first_piece; piece; piece = piece->next)
+ if (write(fd, piece->text, piece->used) == -1)
+ return (False);
+
+ if (close(fd) == -1)
+ 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.
+/*
+ * Function:
+ * StorePiecesInString
+ *
+ * Parameters:
+ * data - ascii pointer data
+ *
+ * Description:
+ * Store the pieces in memory into a standard ascii string.
*/
-
static String
-StorePiecesInString(src)
-AsciiSrcObject src;
+StorePiecesInString(AsciiSrcObject src)
{
- String string;
- XawTextPosition first;
- Piece * piece;
+ String string;
+ XawTextPosition first;
+ Piece *piece;
- string = XtMalloc((unsigned) sizeof(unsigned char) *
- src->ascii_src.length + 1);
-
- for (first = 0, piece = src->ascii_src.first_piece ; piece != NULL;
- first += piece->used, piece = piece->next)
- strncpy(string + first, piece->text, piece->used);
+ string = XtMalloc((unsigned)(src->ascii_src.length + 1));
- string[src->ascii_src.length] = '\0'; /* NULL terminate this sucker. */
+ for (first = 0, piece = src->ascii_src.first_piece ; piece != NULL;
+ first += piece->used, piece = piece->next)
+ memcpy(string + first, piece->text, (unsigned)piece->used);
-/*
- * This will refill all pieces to capacity.
- */
+ string[src->ascii_src.length] = '\0';
- if (src->ascii_src.data_compression) {
- FreeAllPieces(src);
- LoadPieces(src, NULL, string);
- }
+ /*
+ * This will refill all pieces to capacity
+ */
+ if (src->ascii_src.data_compression) {
+ FreeAllPieces(src);
+ LoadPieces(src, NULL, string);
+ }
- return(string);
+ return (string);
}
-/* Function Name: InitStringOrFile.
- * Description: Initializes the string or file.
- * Arguments: src - the AsciiSource.
- * Returns: none - May exit though.
+/*
+ * Function:
+ * InitStringOrFile
+ *
+ * Parameters:
+ * src - AsciiSource
+ *
+ * Description:
+ * Initializes the string or file.
*/
-
static FILE *
-InitStringOrFile(src, newString)
-AsciiSrcObject src;
-Boolean newString;
+InitStringOrFile(AsciiSrcObject src, Bool newString)
{
- char * open_mode;
- FILE * file;
- char fileName[TMPSIZ];
+ mode_t open_mode = 0;
+ const char *fdopen_mode = NULL;
+ int fd;
+ FILE *file;
if (src->ascii_src.type == XawAsciiString) {
-
if (src->ascii_src.string == NULL)
src->ascii_src.length = 0;
- else if (! src->ascii_src.use_string_in_place) {
+ else if (!src->ascii_src.use_string_in_place) {
src->ascii_src.string = XtNewString(src->ascii_src.string);
src->ascii_src.allocated_string = True;
src->ascii_src.length = strlen(src->ascii_src.string);
}
if (src->ascii_src.use_string_in_place) {
+ if (src->ascii_src.string != NULL)
src->ascii_src.length = strlen(src->ascii_src.string);
/* In case the length resource is incorrectly set */
if (src->ascii_src.length > src->ascii_src.ascii_length)
src->ascii_src.ascii_length = src->ascii_src.length;
- if (src->ascii_src.ascii_length == MAGIC_VALUE)
+ if (src->ascii_src.ascii_length == MAGIC_VALUE)
src->ascii_src.piece_size = src->ascii_src.length;
else
src->ascii_src.piece_size = src->ascii_src.ascii_length + 1;
}
-
- return(NULL);
+
+ return (NULL);
}
-/*
- * type is XawAsciiFile.
- */
-
- src->ascii_src.is_tempfile = FALSE;
+ /*
+ * type is XawAsciiFile
+ */
+ src->ascii_src.is_tempfile = False;
switch (src->text_src.edit_mode) {
- case XawtextRead:
- if (src->ascii_src.string == NULL)
- XtErrorMsg("NoFile", "asciiSourceCreate", "XawError",
- "Creating a read only disk widget and no file specified.",
- NULL, 0);
- open_mode = "r";
- break;
- case XawtextAppend:
- case XawtextEdit:
- if (src->ascii_src.string == NULL) {
- src->ascii_src.string = fileName;
- (void) tmpnam(src->ascii_src.string);
- src->ascii_src.is_tempfile = TRUE;
- open_mode = "w";
- } else
- open_mode = "r+";
- break;
- default:
- XtErrorMsg("badMode", "asciiSourceCreate", "XawError",
- "Bad editMode for ascii source; must be Read, Append or Edit.",
- NULL, NULL);
+ case XawtextRead:
+ if (src->ascii_src.string == NULL)
+ XtErrorMsg("NoFile", "asciiSourceCreate", "XawError",
+ "Creating a read only disk widget and no file specified.",
+ NULL, 0);
+ open_mode = O_RDONLY;
+ fdopen_mode = "r";
+ break;
+ case XawtextAppend:
+ case XawtextEdit:
+ if (src->ascii_src.string == NULL) {
+ src->ascii_src.string = "*ascii-src*";
+ src->ascii_src.is_tempfile = True;
+ }
+ else {
+/* O_NOFOLLOW is a FreeBSD & Linux extension */
+#ifdef O_NOFOLLOW
+ open_mode = O_RDWR | O_NOFOLLOW;
+#else
+ open_mode = O_RDWR; /* unsafe; subject to race conditions */
+#endif /* O_NOFOLLOW */
+ fdopen_mode = "r+";
+ }
+ break;
+ default:
+ XtErrorMsg("badMode", "asciiSourceCreate", "XawError",
+ "Bad editMode for ascii source; must be Read, "
+ "Append or Edit.",
+ NULL, NULL);
}
- /* 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.
- */
+ /* If is_tempfile, allocate a private copy of the text
+ * Unlikely to be changed, just to set allocated_string */
if (newString || src->ascii_src.is_tempfile) {
src->ascii_src.string = XtNewString(src->ascii_src.string);
- src->ascii_src.allocated_string = TRUE;
+ src->ascii_src.allocated_string = True;
}
-
+
if (!src->ascii_src.is_tempfile) {
- if ((file = fopen(src->ascii_src.string, open_mode)) != 0) {
- (void) fseek(file, (Off_t)0, 2);
- src->ascii_src.length = (XawTextPosition) ftell(file);
- return file;
- } else {
+ if ((fd = open(src->ascii_src.string, open_mode, 0666)) != -1) {
+ if ((file = fdopen(fd, fdopen_mode))) {
+ (void)fseek(file, 0, SEEK_END);
+ src->ascii_src.length = (XawTextPosition)ftell(file);
+ return (file);
+ }
+ }
+ {
String params[2];
Cardinal num_params = 2;
-
+
params[0] = src->ascii_src.string;
params[1] = strerror(errno);
XtAppWarningMsg(XtWidgetToApplicationContext((Widget)src),
"openError", "asciiSourceCreate", "XawWarning",
"Cannot open file %s; %s", params, &num_params);
}
- }
+ }
src->ascii_src.length = 0;
- return((FILE *)NULL);
+ return (NULL);
}
static void
-LoadPieces(src, file, string)
-AsciiSrcObject src;
-FILE * file;
-char * string;
+LoadPieces(AsciiSrcObject src, FILE *file, char *string)
{
- char *local_str, *ptr;
- Piece * piece = NULL;
- XawTextPosition left;
+ char *ptr;
+ Piece *piece = NULL;
+ XawTextPosition left;
+
+ if (string == NULL) {
+ if (src->ascii_src.type == XawAsciiFile) {
+ if (src->ascii_src.length != 0) {
+ int len;
+
+ left = 0;
+ fseek(file, 0, 0);
+ while (left < src->ascii_src.length) {
+ ptr = XtMalloc((unsigned)src->ascii_src.piece_size);
+ if ((len = fread(ptr, (Size_t)sizeof(unsigned char),
+ (Size_t)src->ascii_src.piece_size, file)) < 0)
+ XtErrorMsg("readError", "asciiSourceCreate", "XawError",
+ "fread returned error.", NULL, NULL);
+ piece = AllocNewPiece(src, piece);
+ piece->text = ptr;
+ piece->used = XawMin(len, src->ascii_src.piece_size);
+ left += piece->used;
+ }
+ }
+ else {
+ piece = AllocNewPiece(src, NULL);
+ piece->text = XtMalloc((unsigned)src->ascii_src.piece_size);
+ piece->used = 0;
+ }
+ return;
+ }
+ else
+ string = src->ascii_src.string;
+ }
- if (string == NULL) {
- if (src->ascii_src.type == XawAsciiFile) {
- local_str = XtMalloc((unsigned) (src->ascii_src.length + 1)
- * sizeof(unsigned char));
- if (src->ascii_src.length != 0) {
- fseek(file, (Off_t)0, 0);
- src->ascii_src.length = fread(local_str, (Size_t)sizeof(unsigned char),
- (Size_t)src->ascii_src.length, file);
- if (src->ascii_src.length <= 0)
- XtErrorMsg("readError", "asciiSourceCreate", "XawError",
- "fread returned error.", NULL, NULL);
- }
- local_str[src->ascii_src.length] = '\0';
+ if (src->ascii_src.use_string_in_place) {
+ piece = AllocNewPiece(src, piece);
+ piece->used = XawMin(src->ascii_src.length, src->ascii_src.piece_size);
+ piece->text = src->ascii_src.string;
+ return;
}
- else
- local_str = src->ascii_src.string;
- }
- else
- local_str = string;
-
- if (src->ascii_src.use_string_in_place) {
- piece = AllocNewPiece(src, piece);
- piece->used = Min(src->ascii_src.length, src->ascii_src.piece_size);
- piece->text = src->ascii_src.string;
- return;
- }
-
- ptr = local_str;
- left = src->ascii_src.length;
-
- do {
- piece = AllocNewPiece(src, piece);
-
- piece->text = XtMalloc((unsigned)src->ascii_src.piece_size
- * sizeof(unsigned char));
- piece->used = Min(left, src->ascii_src.piece_size);
- if (piece->used != 0)
- strncpy(piece->text, ptr, piece->used);
-
- left -= piece->used;
- ptr += piece->used;
- } while (left > 0);
-
- if ( (src->ascii_src.type == XawAsciiFile) && (string == NULL) )
- XtFree(local_str);
+
+ ptr = string;
+ left = src->ascii_src.length;
+ do {
+ piece = AllocNewPiece(src, piece);
+
+ piece->text = XtMalloc((unsigned)src->ascii_src.piece_size);
+ piece->used = XawMin(left, src->ascii_src.piece_size);
+ if (piece->used != 0)
+ memcpy(piece->text, ptr, (unsigned)piece->used);
+
+ left -= piece->used;
+ ptr += piece->used;
+ } 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.
+/*
+ * Function:
+ * AllocNewPiece
+ *
+ * Parameters:
+ * src - AsciiSrc Widget
+ * prev - piece just before this one, or NULL
+ *
+ * Description:
+ * Allocates a new piece of memory.
+ *
+ * Returns:
+ * The allocated piece
*/
-
static Piece *
-AllocNewPiece(src, prev)
-AsciiSrcObject src;
-Piece * prev;
+AllocNewPiece(AsciiSrcObject src, Piece *prev)
{
- Piece * piece = XtNew(Piece);
-
- if (prev == NULL) {
- src->ascii_src.first_piece = piece;
- piece->next = NULL;
- }
- else {
- if (prev->next != NULL)
- (prev->next)->prev = piece;
- piece->next = prev->next;
- prev->next = piece;
- }
-
- piece->prev = prev;
-
- return(piece);
+ Piece *piece = XtNew(Piece);
+
+ if (prev == NULL) {
+ src->ascii_src.first_piece = piece;
+ piece->next = NULL;
+ }
+ else {
+ if (prev->next != NULL)
+ (prev->next)->prev = piece;
+ piece->next = prev->next;
+ prev->next = piece;
+ }
+
+ piece->prev = prev;
+
+ return (piece);
}
-/* Function Name: FreeAllPieces
- * Description: Frees all the pieces
- * Arguments: src - The AsciiSrc Widget.
- * Returns: none.
+/*
+ * Function:
+ * FreeAllPieces
+ *
+ * Parameters:
+ * src - AsciiSrc Widget
+ *
+ * Description:
+ * Frees all the pieces.
*/
-
-static void
-FreeAllPieces(src)
-AsciiSrcObject src;
+static void
+FreeAllPieces(AsciiSrcObject src)
{
- Piece * next, * first = src->ascii_src.first_piece;
+ Piece *next, * first = src->ascii_src.first_piece;
- if (first->prev != NULL)
- (void) printf("Xaw AsciiSrc Object: possible memory leak in FreeAllPieces().\n");
+#ifdef DEBUG
+ if (first->prev != NULL)
+ printf("Xaw AsciiSrc Object: possible memory leak in FreeAllPieces().\n");
+#endif
- for ( ; first != NULL ; first = next ) {
- next = first->next;
- RemovePiece(src, first);
- }
+ for (; first != NULL ; first = next) {
+ next = first->next;
+ RemovePiece(src, first);
+ }
}
-
-/* Function Name: RemovePiece
- * Description: Removes a piece from the list.
- * Arguments:
- * piece - the piece to remove.
- * Returns: none.
- */
+/*
+ * Function:
+ * RemovePiece
+ *
+ * Parameters:
+ * piece - piece to remove
+ *
+ * Description:
+ * Removes a piece from the list.
+ */
static void
-RemovePiece(src, piece)
-AsciiSrcObject src;
-Piece * piece;
+RemovePiece(AsciiSrcObject src, Piece *piece)
{
- if (piece->prev == NULL)
- src->ascii_src.first_piece = piece->next;
- else
- (piece->prev)->next = piece->next;
+ if (piece->prev == NULL)
+ src->ascii_src.first_piece = piece->next;
+ else
+ piece->prev->next = piece->next;
- if (piece->next != NULL)
- (piece->next)->prev = piece->prev;
+ if (piece->next != NULL)
+ piece->next->prev = piece->prev;
- if (!src->ascii_src.use_string_in_place)
- XtFree(piece->text);
+ if (!src->ascii_src.use_string_in_place)
+ XtFree(piece->text);
- XtFree((char *)piece);
+ XtFree((char *)piece);
}
-/* 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.
+/*
+ * Function:
+ * FindPiece
+ *
+ * Parameters:
+ * src - AsciiSrc Widget
+ * position - position that we are searching for
+ * first - position of the first character in this piece (return)
+ *
+ * Description:
+ * Finds the piece containing the position indicated.
+ *
+ * Returns:
+ * the piece that contains this position
*/
-
static Piece *
-FindPiece(src, position, first)
-AsciiSrcObject src;
-XawTextPosition position, *first;
+FindPiece(AsciiSrcObject src, XawTextPosition position, XawTextPosition *first)
{
- Piece * old_piece, * piece = src->ascii_src.first_piece;
- XawTextPosition temp;
+ Piece *old_piece, *piece;
+ XawTextPosition temp;
+
+ for (old_piece = NULL, piece = src->ascii_src.first_piece, temp = 0;
+ piece; old_piece = piece, piece = piece->next)
+ if ((temp += piece->used) > position) {
+ *first = temp - piece->used;
+ return (piece);
+ }
- for ( temp = 0 ; piece != NULL ; temp += piece->used, piece = piece->next ) {
- *first = temp;
- old_piece = piece;
+ *first = temp - (old_piece ? old_piece->used : 0);
- if ((temp + piece->used) > position)
- return(piece);
- }
- return(old_piece); /* if we run off the end the return the last 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.
+/*
+ * Function:
+ * BreakPiece
+ *
+ * Parameters:
+ * src - AsciiSrc Widget
+ * piece - piece to break
+ *
+ * Description:
+ * Breaks a full piece into two new pieces.
*/
-
-static String
-MyStrncpy(s1, s2, n)
-char * s1, * s2;
-int n;
+#define HALF_PIECE (src->ascii_src.piece_size >> 1)
+static void
+BreakPiece(AsciiSrcObject src, Piece *piece)
{
- char buf[256];
- char* temp;
-
- if (n == 0) return s1;
+ Piece *cnew = AllocNewPiece(src, piece);
- if (n < sizeof buf) temp = buf;
- else temp = XtMalloc((unsigned)sizeof(unsigned char) * n);
+ cnew->text = XtMalloc((unsigned)src->ascii_src.piece_size);
+ memcpy(cnew->text, piece->text + HALF_PIECE,
+ (unsigned)(src->ascii_src.piece_size - HALF_PIECE));
+ piece->used = HALF_PIECE;
+ cnew->used = src->ascii_src.piece_size - HALF_PIECE;
+}
- strncpy(temp, s2, n); /* Saber has a bug that causes it to generate*/
- strncpy(s1, temp, n); /* a bogus warning message here (CDP 6/32/89)*/
+/*ARGSUSED*/
+static void
+CvtStringToAsciiType(XrmValuePtr args, Cardinal *num_args,
+ XrmValuePtr fromVal, XrmValuePtr toVal)
+{
+ static XawAsciiType type;
+ XrmQuark q;
+ char name[7];
+
+ XmuNCopyISOLatin1Lowered(name, (char *)fromVal->addr, sizeof(name));
+ q = XrmStringToQuark(name);
+
+ if (q == Qstring)
+ type = XawAsciiString;
+ else if (q == Qfile)
+ type = XawAsciiFile;
+ else {
+ toVal->size = 0;
+ toVal->addr = NULL;
+ XtStringConversionWarning((char *)fromVal->addr, XtRAsciiType);
+ }
- if (temp != buf) XtFree(temp);
- return s1;
+ toVal->size = sizeof(XawAsciiType);
+ toVal->addr = (XPointer)&type;
}
-
-/* Function Name: BreakPiece
- * Description: Breaks a full piece into two new pieces.
- * Arguments: src - The AsciiSrc Widget.
- * piece - the piece to break.
- * Returns: none.
- */
-
-#define HALF_PIECE (src->ascii_src.piece_size/2)
-static void
-BreakPiece(src, piece)
-AsciiSrcObject src;
-Piece * piece;
+/*ARGSUSED*/
+static Boolean
+CvtAsciiTypeToString(Display *dpy, XrmValuePtr args, Cardinal *num_args,
+ XrmValuePtr fromVal, XrmValuePtr toVal,
+ XtPointer *data)
{
- Piece * new = AllocNewPiece(src, piece);
-
- new->text = XtMalloc(src->ascii_src.piece_size * sizeof(unsigned char));
- strncpy(new->text, piece->text + HALF_PIECE,
- src->ascii_src.piece_size - HALF_PIECE);
- piece->used = HALF_PIECE;
- new->used = src->ascii_src.piece_size - HALF_PIECE;
+ static String buffer;
+ Cardinal size;
+
+ switch (*(XawAsciiType *)fromVal->addr) {
+ case XawAsciiFile:
+ buffer = XtEfile;
+ break;
+ case XawAsciiString:
+ buffer = XtEstring;
+ break;
+ default:
+ XawTypeToStringWarning(dpy, XtRAsciiType);
+ toVal->addr = NULL;
+ toVal->size = 0;
+ return (False);
+ }
+
+ size = strlen(buffer) + 1;
+ if (toVal->addr != NULL) {
+ if (toVal->size < size) {
+ toVal->size = size;
+ return (False);
+ }
+ strcpy((char *)toVal->addr, buffer);
+ }
+ else
+ toVal->addr = (XPointer)buffer;
+ toVal->size = sizeof(String);
+
+ return (True);
}
-/* ARGSUSED */
+/*ARGSUSED*/
static void
-CvtStringToAsciiType(args, num_args, fromVal, toVal)
-XrmValuePtr args; /* unused */
-Cardinal *num_args; /* unused */
-XrmValuePtr fromVal;
-XrmValuePtr toVal;
+GetDefaultPieceSize(Widget w, int offset, XrmValue *value)
{
- static XawAsciiType type;
- static XrmQuark XtQEstring = NULLQUARK;
- static XrmQuark XtQEfile;
- XrmQuark q;
- char lowerName[40];
-
- if (XtQEstring == NULLQUARK) {
- XtQEstring = XrmPermStringToQuark(XtEstring);
- XtQEfile = XrmPermStringToQuark(XtEfile);
- }
-
- if (strlen ((char*)fromVal->addr) < sizeof lowerName) {
- XmuCopyISOLatin1Lowered(lowerName, (char *) fromVal->addr);
- q = XrmStringToQuark(lowerName);
-
- if (q == XtQEstring) type = XawAsciiString;
- else if (q == XtQEfile) type = XawAsciiFile;
- else {
- toVal->size = 0;
- toVal->addr = NULL;
- return;
+ static XPointer pagesize;
+
+ if (pagesize == 0) {
+ pagesize = (XPointer)((long)_XawGetPageSize());
+ if (pagesize < (XPointer)BUFSIZ)
+ pagesize = (XPointer)BUFSIZ;
}
- toVal->size = sizeof type;
- toVal->addr = (XPointer) &type;
- return;
- }
- toVal->size = 0;
- toVal->addr = NULL;
+
+ value->addr = (XPointer)&pagesize;
}
#if (defined(ASCII_STRING) || defined(ASCII_DISK))
@@ -1321,37 +1790,40 @@ XrmValuePtr toVal;
#endif
#ifdef ASCII_STRING
-/************************************************************
- *
+/*
* Compatability functions.
+ */
+/*
+ * Function:
+ * AsciiStringSourceCreate
+ *
+ * Parameters:
+ * parent - widget that will own this source
+ * args - the argument list
+ * num_args - ""
+ *
+ * Description:
+ * Creates a string source.
*
- ************************************************************/
-
-/* 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.
+ * Returns:
+ * A pointer to the new text source.
*/
-
Widget
-XawStringSourceCreate(parent, args, num_args)
-Widget parent;
-ArgList args;
-Cardinal num_args;
+XawStringSourceCreate(Widget parent, ArgList args, Cardinal num_args)
{
- XawTextSource src;
- ArgList ascii_args;
- Arg temp[2];
-
- XtSetArg(temp[0], XtNtype, XawAsciiString);
- XtSetArg(temp[1], XtNuseStringInPlace, TRUE);
- ascii_args = XtMergeArgLists(temp, TWO, args, num_args);
-
- src = XtCreateWidget("genericAsciiString", asciiSrcObjectClass, parent,
- ascii_args, num_args + TWO);
- XtFree((char *)ascii_args);
- return(src);
+ XawTextSource src;
+ ArgList ascii_args;
+ Arg temp[2];
+
+ XtSetArg(temp[0], XtNtype, XawAsciiString);
+ XtSetArg(temp[1], XtNuseStringInPlace, True);
+ ascii_args = XtMergeArgLists(temp, TWO, args, num_args);
+
+ src = XtCreateWidget("genericAsciiString", asciiSrcObjectClass, parent,
+ ascii_args, num_args + TWO);
+ XtFree((char *)ascii_args);
+
+ return (src);
}
/*
@@ -1360,49 +1832,52 @@ Cardinal num_args;
*
* Chris D. Peterson 8/31/89.
*/
-
-void
-XawTextSetLastPos (w, lastPos)
-Widget w;
-XawTextPosition lastPos;
+void
+XawTextSetLastPos(Widget w, XawTextPosition lastPos)
{
- AsciiSrcObject src = (AsciiSrcObject) XawTextGetSource(w);
+ AsciiSrcObject src = (AsciiSrcObject)XawTextGetSource(w);
- src->ascii_src.piece_size = lastPos;
+ src->ascii_src.piece_size = lastPos;
}
#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.
+/*
+ * Function:
+ * AsciiDiskSourceCreate
+ *
+ * Parameters:
+ * parent - widget that will own this source
+ * args - argument list
+ * num_args - ""
+ *
+ * Description:
+ * Creates a disk source.
+ *
+ * Returns:
+ * A pointer to the new text source
*/
-
Widget
-XawDiskSourceCreate(parent, args, num_args)
-Widget parent;
-ArgList args;
-Cardinal num_args;
+XawDiskSourceCreate(Widget parent, ArgList args, Cardinal num_args)
{
- XawTextSource src;
- ArgList ascii_args;
- Arg temp[1];
- int i;
-
- XtSetArg(temp[0], XtNtype, XawAsciiFile);
- ascii_args = XtMergeArgLists(temp, ONE, args, num_args);
- num_args++;
-
- for (i = 0; i < num_args; i++)
- if (streq(ascii_args[i].name, XtNfile) ||
- streq(ascii_args[i].name, XtCFile))
- ascii_args[i].name = XtNstring;
-
- src = XtCreateWidget("genericAsciiDisk", asciiSrcObjectClass, parent,
- ascii_args, num_args);
- XtFree((char *)ascii_args);
- return(src);
+ XawTextSource src;
+ ArgList ascii_args;
+ Arg temp[1];
+ int i;
+
+ XtSetArg(temp[0], XtNtype, XawAsciiFile);
+ ascii_args = XtMergeArgLists(temp, ONE, args, num_args);
+ num_args++;
+
+ for (i = 0; i < num_args; i++)
+ if (streq(ascii_args[i].name, XtNfile)
+ || streq(ascii_args[i].name, XtCFile))
+ ascii_args[i].name = XtNstring;
+
+ src = XtCreateWidget("genericAsciiDisk", asciiSrcObjectClass, parent,
+ ascii_args, num_args);
+ XtFree((char *)ascii_args);
+
+ return (src);
}
#endif /* ASCII_DISK */