/* $XConsortium: MultiSrc.c,v 1.6 94/04/17 20:12:25 kaleb Exp $ */
/*
* Copyright 1991 by OMRON Corporation
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name OMRON not be used in
* advertising or publicity pertaining to distribution of the software without
* specific, written prior permission. OMRON makes no representations
* about the suitability of this software for any purpose. It is provided
* "as is" without express or implied warranty.
*
* OMRON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL OMRON BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTUOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*
* Authors: Chris Peterson MIT X Consortium
* Li Yuhong OMRON Corporation
* Frank Sheeran OMRON Corporation
*
* Much code taken from X11R3 String and Disk Sources.
*/
/*
Copyright (c) 1991, 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.
*/
/*
* MultiSrc.c - MultiSrc object. (For use with the text widget).
*
*/
#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include "MultiSrcP.h"
#include "XawI18n.h"
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
/****************************************************************
*
* Full class record constant
*
****************************************************************/
/* Private Data */
/* not used. */
};
static int ReplaceText();
static FILE * InitStringOrFile();
static void RemoveOldStringOrFile(), CvtStringToMultiType();
static String StorePiecesInString();
#ifndef MyWStrncpy
static void (MyWStrncpy)();
#endif
extern char *tmpnam();
#ifdef X_NOT_STDC_ENV
extern int errno;
#endif
#ifdef X_NOT_POSIX
#define Off_t long
#define Size_t unsigned int
#else
#endif
extern wchar_t* _XawTextMBToWC();
extern char *_XawTextWCToMB();
{ /* object_class fields */
/* class_name */ "MultiSrc",
/* widget_size */ sizeof(MultiSrcRec),
/* class_initialize */ ClassInitialize,
/* class_part_initialize */ NULL,
/* class_inited */ FALSE,
/* initialize */ Initialize,
/* initialize_hook */ NULL,
/* pad */ NULL,
/* pad */ NULL,
/* pad */ 0,
/* resources */ resources,
/* xrm_class */ NULLQUARK,
/* pad */ FALSE,
/* pad */ FALSE,
/* pad */ FALSE,
/* pad */ FALSE,
/* destroy */ Destroy,
/* pad */ NULL,
/* pad */ NULL,
/* set_values */ SetValues,
/* set_values_hook */ NULL,
/* pad */ NULL,
/* get_values_hook */ GetValuesHook,
/* pad */ NULL,
/* version */ XtVersion,
/* callback_private */ NULL,
/* pad */ NULL,
/* pad */ NULL,
/* pad */ NULL,
/* extension */ NULL
},
{ /* textSrc_class fields */
/* Read */ ReadText,
/* Replace */ ReplaceText,
/* Scan */ Scan,
/* Search */ Search,
/* SetSelection */ XtInheritSetSelection,
/* ConvertSelection */ XtInheritConvertSelection
},
{ /* multiSrc_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.
*/
}
/* 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.
*/
static XawTextPosition
Widget w;
int length;
{
}
/* 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.
*/
/*ARGSUSED*/
static int
Widget w;
{
/* 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. */
/* text.format is unneeded */
} else {
/* 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... */
return(XawEditError);
/* STEP 3: remove the empty pieces... */
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 {
}
}
/*((TextWidget)src->object.parent)->text.lastPos = src->multi_src.length;*/
/* STEP 4: insert the new stuff */
while (length > 0) {
int fill;
/*
* The string is used in place, then the string
* is not allowed to grow.
*/
/*((TextWidget)src->object.parent)->text.lastPos = src->multi_src.length;*/
return(XawEditError);
}
}
}
}
}
if ( local_artificial_block == True )
/* 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. */
return(XawEditDone);
}
/* Function Name: Scan
* Description: Scans the text source for the number and type
* of item specified.
* 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.
*/
static
Widget w;
int count;
Boolean include;
{
int inc;
if (dir == XawsdRight)
return(0); /* else. */
}
/* STEP 1: basic sanity checks */
if ( dir == XawsdRight ) {
inc = 1;
}
else {
if (position == 0)
return(0);
inc = -1;
position--;
}
switch (type) {
case XawstEOL:
case XawstParagraph:
case XawstWhiteSpace:
/* CONSTCOND */
while (TRUE) {
if (type == XawstWhiteSpace) {
if (iswspace(c)) {
if (non_space)
break;
}
else
}
if (c == _Xaw_atowc(XawLF)) break;
}
else { /* XawstParagraph */
if (first_eol) {
if (c == _Xaw_atowc(XawLF)) {
}
}
else
if ( c == _Xaw_atowc(XawLF))
break;
else if ( !iswspace(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 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.
*/
static XawTextPosition
Widget w;
{
int wtarget_len;
/* STEP 1: First, a brief sanity check. */
if ( dir == XawsdRight )
inc = 1;
else {
inc = -1;
if (position == 0)
return(XawTextSearchError); /* scanning left from 0??? */
position--;
}
/* 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 */
else
{
/* 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. */
/* STEP 3: SEARCH! */
/* 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 MultiSource.
* 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;
"MultiSrc: The XtNuseStringInPlace resources may not be changed.");
}
for (i = 0; i < *num_args ; i++ )
string_set = TRUE;
break;
}
/* Load pieces does this logic for us, but it shouldn't. Its messy.*/
/*if (old_src->multi_src.type == XawAsciiString)
LoadPieces(src, NULL, src->multi_src.string);
else*/
what happened. */
total_reset = TRUE;
}
if ( mb_string != 0 ) {
FreeAllPieces( old_src );
} else {
/* If the buffer holds bad chars, don't touch it... */
"convertError", "multiSource", "XawError",
"convertError", "multiSource", "XawError",
}
}
return(FALSE);
}
/* 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.
* Returns: none.
*/
static void
Widget w;
{
int i;
for (i = 0; i < *num_args ; i++ )
}
else {
if (_XawMultiSave(w)) /* If save sucessful. */
}
break;
}
}
}
/* Function Name: Destroy
* Description: Destroys an multi source (frees all data)
* Arguments: src - the Multi source Widget to free.
* Returns: none.
*/
static void
Destroy (w)
Widget w;
{
}
/************************************************************
*
* Public routines
*
************************************************************/
/* 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.
* Returns: none.
*
* The public interface is XawAsciiSourceFreeString!
*/
void
Widget w)
#else
Widget w;
#endif
{
/*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)!
*/
Widget w)
#else
Widget w;
#endif
{
char * mb_string;
/*
* 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);
return(TRUE);
if ( mb_string != 0 ) {
return( FALSE );
}
return( TRUE );
} else {
/* If the buffer holds bad chars, don't touch it... */
"convertError", "multiSource", "XawError",
return( FALSE );
}
}
else {
/* THIS FUNCTIONALITY IS UNDOCUMENTED, probably UNNEEDED? The manual
says this routine's only function is to save files to disk. -Sheeran */
if ( mb_string == 0 ) {
/* If the buffer holds bad chars, don't touch it... */
"convertError", "multiSource", "XawError",
return( FALSE );
}
/* assert: mb_string holds good characters so the buffer is fine */
else
}
return(TRUE);
}
/* 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!
*/
Widget w,
#else
Widget w;
#endif
{
if ( mb_string != 0 ) {
return( ret );
}
/* otherwise there was a conversion error. So print widget name too. */
"convertError", "multiSource", "XawError",
return( False );
}
/************************************************************
*
* 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 char string.
* Arguments: src - the multiSrc to gather data from
* Returns: char *mb_string. Caller must free.
* or 0: conversion error. Caller must panic!
*/
static String
{
char *mb_string;
/* I believe the char_count + 1 and the NULL termination are unneeded! FS*/
/* This will refill all pieces to capacity. */
FreeAllPieces( src );
}
/* Lastly, convert it to a MB format and send it back. */
wc_string, &char_count );
/* NOTE THAT mb_string MAY BE ZERO IF THE CONVERSION FAILED. */
return( mb_string );
}
/* Function Name: InitStringOrFile.
* Description: Initializes the string or file.
* Arguments: src - the MultiSource.
* Returns: none - May exit though.
*/
static FILE *
{
char * open_mode;
int length;
/* Wasteful, throwing away the WC string, but need side effect! */
} else {
/* In case the length resource is incorrectly set */
else
}
/*((TextWidget)src->object.parent)->text.lastPos = src->multi_src.length;*/
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 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.
*/
}
return file;
} else {
"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. */
static void
char* string;
{
/*
* This is tricky - the _XawTextMBtoWC converter uses its 3rd arg
* in as MB length, out as WC length. We want local_length to be
* WC count.
*/
/*
* 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,
* so don't change len...
*/
} else {
(Size_t)sizeof(unsigned char),
"readError", "multiSource", "XawError",
if ( local_str == 0 ) {
static char err_text[] =
"<<< FILE CONTENTS NOT REPRESENTABLE IN THIS LOCALE >>>";
num_params = 2;
"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';*/
}
}
return;
}
left = local_length;
do {
} while (left > 0);
if ( temp_mb_holder )
XtFree( (char*) temp_mb_holder );
}
/* 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.
*/
static MultiPiece *
MultiPiece * prev;
{
}
else {
}
return(piece);
}
/* Function Name: FreeAllPieces
* Description: Frees all the pieces
* Arguments: src - The MultiSrc Widget.
* Returns: none.
*/
static void
{
printf("Xaw MultiSrc 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 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.
*/
static MultiPiece *
{
return(piece);
}
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.
* Returns: none.
*/
static void
{
}
/* Convert string "XawAsciiString" and "XawAsciiFile" to quarks. */
/* ARGSUSED */
static void
{
XrmQuark q;
if (XtQEstring == NULLQUARK) {
}
q = XrmStringToQuark(lowerName);
else {
return;
}
return;
}
}