MultiSrc.c revision 749
851N/A * Copyright 1991 by OMRON Corporation 32N/A * Permission to use, copy, modify, distribute, and sell this software and its 919N/A * documentation for any purpose is hereby granted without fee, provided that 919N/A * the above copyright notice appear in all copies and that both that 919N/A * copyright notice and this permission notice appear in supporting 919N/A * documentation, and that the name OMRON not be used in 919N/A * advertising or publicity pertaining to distribution of the software without 919N/A * specific, written prior permission. OMRON makes no representations 919N/A * about the suitability of this software for any purpose. It is provided 919N/A * "as is" without express or implied warranty. 919N/A * OMRON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 919N/A * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 919N/A * EVENT SHALL OMRON BE LIABLE FOR ANY SPECIAL, INDIRECT OR 919N/A * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 919N/A * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 919N/A * TORTUOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 919N/A * PERFORMANCE OF THIS SOFTWARE. 32N/A * Authors: Chris Peterson MIT X Consortium 32N/A * Li Yuhong OMRON Corporation 32N/A * Frank Sheeran OMRON Corporation 493N/A * Much code taken from X11R3 String and Disk Sources. 911N/ACopyright (c) 1991, 1994 X Consortium 911N/APermission is hereby granted, free of charge, to any person obtaining a copy 911N/Aof this software and associated documentation files (the "Software"), to deal 32N/Ain the Software without restriction, including without limitation the rights 32N/Ato use, copy, modify, merge, publish, distribute, sublicense, and/or sell 32N/Acopies of the Software, and to permit persons to whom the Software is 32N/Afurnished 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. * MultiSrc.c - MultiSrc object. (For use with the text widget). /**************************************************************** * Full class record constant ****************************************************************/ {
/* object_class fields */ /* class_name */ "MultiSrc",
/* class_part_initialize */ NULL,
/* class_inited */ FALSE,
/* initialize_hook */ NULL,
/* set_values_hook */ NULL,
/* callback_private */ NULL,
{
/* textSrc_class fields */ {
/* multiSrc_class fields */ /* Keep the compiler happy */ '\0' /************************************************************ * Semi-Public Interfaces. ************************************************************/ /* Function Name: ClassInitialize * Description: Class Initialize routine, called only once. /* 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 * Set correct flags (override resources) depending upon widget class. /* Function Name: ReadText * Description: This function reads the source. * Arguments: w - the MultiSource 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 Name: ReplaceText. * Description: Replaces a block of text with new text. * Arguments: w - the MultiSource widget. * startPos, endPos - ends of text that will be removed. * text - new text to be inserted into buffer at startPos. * Returns: XawEditError or XawEditDone. /* STEP 1: The user handed me a text block called `u_text' that may be * in either FMTWIDE or FMT8BIT (ie MB.) Later code needs the block * `text' to hold FMTWIDE. So, this copies `u_text' to `text', and if * `u_text' was MB, I knock it up to WIDE. */ if (
u_text_p->
length == 0 )
/* if so, the block contents never ref'd. */ /* text.format is unneeded */ /* WARNING! u_text->firstPos and length are in units of CHAR, not CHARACTERS! */ /* I assert the following assignment is not needed - since Step 4 depends on length, it has no need of a terminating NULL. I think the ASCII-version has the same needless NULL. */ /*((wchar_t*)text.ptr)[ text.length ] = NULL;*/ /* STEP 2: some initialization... */ /* STEP 3: remove the empty pieces... */ /* If empty and not the only piece then remove it. */ else {
/* We are fully in one piece. */ /*((TextWidget)src->object.parent)->text.lastPos = src->multi_src.length;*/ /* STEP 4: insert the new stuff */ * The string is used in place, then the string * is not allowed to grow. /*((TextWidget)src->object.parent)->text.lastPos = src->multi_src.length;*/ /* In other words, text is not the u_text that the user handed me but one I made myself. I only care, because I need to free the string. */ * Description: Scans the text source for the number and type * Arguments: w - the MultiSource 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. /* STEP 1: basic sanity checks */ if (
piece->
used == 0 )
return(0);
/* i.e., buffer is empty. */ else {
/* XawstParagraph */ /* case XawstAll: ---- handled in special code above */ * Description: Searchs the text source for the text block passed * Arguments: w - the MultiSource 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. /* STEP 1: First, a brief sanity check. */ /* STEP 2: Ensure I have a local wide string.. */ /* Since this widget stores 32bit chars, I check here to see if I'm being passed a string claiming to be 8bit chars (ie, MB text.) If that is the case, naturally I convert to 32bit format. */ /*if the block was FMT8BIT, length will convert to REAL wchar count below */ /* The following converts wtarget_len from byte len to wchar count */ /* OK, I can now assert that wtarget holds wide characters, wtarget_len holds an accurate count of those characters, and that firstPos has been effectively factored out of the following computations. */ /* Function Name: SetValues * Description: Sets the values for the MultiSource. * Arguments: current - current state of the widget. * request - what was requested. * new - what the widget will become. * Returns: True if redisplay is needed. "MultiSrc: The XtNuseStringInPlace resources may not be changed.");
/* Load pieces does this logic for us, but it shouldn't. Its messy.*/ /* If the buffer holds bad chars, don't touch it... */ "convertError",
"multiSource",
"XawError",
"convertError",
"multiSource",
"XawError",
"Non-character code(s) in buffer.",
NULL,
NULL );
/* Function Name: GetValuesHook * Description: This is a get values hook routine that sets the * values specific to the multi source. * Arguments: w - the MultiSource Widget. * args - the argument list. * num_args - the number of args. /* Function Name: Destroy * Description: Destroys an multi source (frees all data) * Arguments: src - the Multi source Widget to free. /************************************************************ ************************************************************/ /* Function Name: XawMultiSourceFreeString * Description: Frees the string returned by a get values call * on the string when the source is of type string. * Arguments: w - the MultiSrc widget. * The public interface is XawAsciiSourceFreeString! /*if (src->multi_src.allocated_string&& src->multi_src.type != XawAsciiFile) {*/ /* ASSERT: src->multi_src.allocated_string -> we MUST free .string! */ /* Function Name: _XawMultiSave * Description: Saves all the pieces into a file or string as required. * Arguments: w - the multiSrc Widget. * Returns: TRUE if the save was successful. * The public interface is XawAsciiSave(w)! * If using the string in place then there is no need to play games * to get the internal info into a readable string. /* If the buffer holds bad chars, don't touch it... */ "convertError",
"multiSource",
"XawError",
"Due to illegal characters, file not saved.",
NULL,
NULL);
/* THIS FUNCTIONALITY IS UNDOCUMENTED, probably UNNEEDED? The manual says this routine's only function is to save files to disk. -Sheeran */ /* If the buffer holds bad chars, don't touch it... */ "convertError",
"multiSource",
"XawError",
/* assert: mb_string holds good characters so the buffer is fine */ /* Function Name: XawMultiSaveAsFile * Description: Save the current buffer as a file. * Arguments: w - the MultiSrc widget. * name - name of the file to save this file into. * Returns: True if the save was sucessful. * The public interface is XawAsciiSaveAsFile! /* otherwise there was a conversion error. So print widget name too. */ "convertError",
"multiSource",
"XawError",
/************************************************************ ************************************************************/ /* Function Name: WriteToFile * Description: Write the string specified to the begining of the file * Arguments: string - string to write. * name - the name of the file * Returns: returns TRUE if sucessful, FALSE otherwise. /* Function Name: StorePiecesInString * Description: store the pieces in memory into a char string. * Arguments: src - the multiSrc to gather data from * Returns: char *mb_string. Caller must free. * or 0: conversion error. Caller must panic! /* I believe the char_count + 1 and the NULL termination are unneeded! FS*/ /* This will refill all pieces to capacity. */ /* Lastly, convert it to a MB format and send it back. */ /* NOTE THAT mb_string MAY BE ZERO IF THE CONVERSION FAILED. */ /* Function Name: InitStringOrFile. * Description: Initializes the string or file. * Arguments: src - the MultiSource. * Returns: none - May exit though. /* Wasteful, throwing away the WC string, but need side effect! */ /* In case the length resource is incorrectly set */ /*((TextWidget)src->object.parent)->text.lastPos = src->multi_src.length;*/ XtErrorMsg(
"NoFile",
"multiSourceCreate",
"XawError",
"Creating a read only disk widget and no file specified.",
XtErrorMsg(
"badMode",
"multiSourceCreate",
"XawError",
"Bad editMode for multi source; must be Read, Append or Edit.",
/* Allocate new memory for the temp filename, because it is held in * a stack memory buffer. We must verify that all routines that set * .string first check .allocated_string and free it - plumbing Sheeran. "openError",
"multiSourceCreate",
"XawWarning",
/* LoadPieces: This routine takes either the MB contents of open file `file' or the MB contents of string or the MB contents of src->multi_src.string and places them in Pieces in WC format. CAUTION: You must have src->multi_src.length set to file length bytes when src->multi_src.type == XawAsciiFile. src->multi_src.length must be the length of the parameter string if string is non-NULL. */ * This is tricky - the _XawTextMBtoWC converter uses its 3rd arg * in as MB length, out as WC length. We want local_length to be * ASSERT: IF our caller passed a non-null string, THEN * src->multi_src.length is currently string's * byte count, * AND string is in a MB format. * here, we are not changing the contents, just reloading, (
Size_t)
sizeof(
unsigned char),
"readError",
"multiSource",
"XawError",
"<<< FILE CONTENTS NOT REPRESENTABLE IN THIS LOCALE >>>";
"readLocaleError",
"multiSource",
"XawError",
"%s: The file `%s' contains characters not representable in this locale.",
}
else {
/*ASSERT that since following while loop looks at local_length this isn't needed. Sheeran, Omron KK, 1993/07/15 temp_mb_holder[src->multi_src.length] = '\0';*/ /* Function Name: AllocNewPiece * Description: Allocates a new piece of memory. * Arguments: src - The MultiSrc Widget. * prev - the piece just before this one, or NULL. * Returns: the allocated piece. /* Function Name: FreeAllPieces * Description: Frees all the pieces * Arguments: src - The MultiSrc Widget. printf(
"Xaw MultiSrc Object: possible memory leak in FreeAllPieces().\n");
/* Function Name: RemovePiece * Description: Removes a piece from the list. * piece - the piece to remove. /* Function Name: FindPiece * Description: Finds the piece containing the position indicated. * Arguments: src - The MultiSrc 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. return(
old_piece);
/* if we run off the end the return the last piece */ /* Function Name: BreakPiece * Description: Breaks a full piece into two new pieces. * Arguments: src - The MultiSrc Widget. * piece - the piece to break. /* Convert string "XawAsciiString" and "XawAsciiFile" to quarks. */