📄 xcmscolnm.c
字号:
/* $XConsortium: XcmsColNm.c,v 1.25 92/03/03 12:16:07 rws Exp $" *//* * Code and supporting documentation (c) Copyright 1990 1991 Tektronix, Inc. * All Rights Reserved * * This file is a component of an X Window System-specific implementation * of Xcms based on the TekColor Color Management System. Permission is * hereby granted to use, copy, modify, sell, and otherwise distribute this * software and its documentation for any purpose and without fee, provided * that this copyright, permission, and disclaimer notice is reproduced in * all copies of this software and in supporting documentation. TekColor * is a trademark of Tektronix, Inc. * * Tektronix makes no representation about the suitability of this software * for any purpose. It is provided "as is" and with all faults. * * TEKTRONIX DISCLAIMS ALL WARRANTIES APPLICABLE TO THIS SOFTWARE, * INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * PARTICULAR PURPOSE. IN NO EVENT SHALL TEKTRONIX 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 TORTIOUS ACTION, ARISING OUT OF OR IN * CONNECTION WITH THE USE OR THE PERFORMANCE OF THIS SOFTWARE. * * NAME * XcmsColNm.c * * DESCRIPTION * Source for _XcmsLookupColorName(). * * */#include "Xlibint.h"#include "Xcmsint.h"#include <X11/Xos.h>#include <sys/stat.h>#include <stdio.h>#include <ctype.h>#define XK_LATIN1#include <X11/keysymdef.h>/* * EXTERNS * External declarations required locally to this package * that are not already declared in any of the included header * files (external includes or internal includes). */#ifdef X_NOT_STDC_ENVextern char *getenv();extern void qsort();extern char *bsearch();#endifextern XcmsColorSpace **_XcmsDIColorSpaces;static Status LoadColornameDB();void _XcmsCopyISOLatin1Lowered();/* * LOCAL DEFINES * #define declarations local to this package. */#ifndef XCMSDB#define XCMSDB "/usr/lib/X11/Xcms.txt"#endif#ifndef isgraph# define isgraph(c) (isprint((c)) && !isspace((c)))#endif#ifndef XCMSDB_MAXLINELEN# define XCMSDB_MAXLINELEN 256#endif#define FORMAT_VERSION "0.1"#define START_TOKEN "XCMS_COLORDB_START"#define END_TOKEN "XCMS_COLORDB_END"#define DELIM_CHAR '\t'#define NOT_VISITED 0x0#define VISITED 0x1#define CYCLE 0xFFFF#define XcmsDbInitNone -1#define XcmsDbInitFailure 0#define XcmsDbInitSuccess 1/* * LOCAL TYPEDEFS */typedef struct _XcmsPair { char *first; char *second; int flag;} XcmsPair;/* * LOCAL VARIABLES */static int XcmsColorDbState = XcmsDbInitNone;static int nEntries;static char *strings;static XcmsPair *pairs;static char whitePtStr[] = "WhitePoint";/************************************************************************ * * * PRIVATE ROUTINES * * * ************************************************************************//* * NAME * _XcmsColorSpaceOfString * * SYNOPSIS */static XcmsColorSpace *_XcmsColorSpaceOfString(ccc, color_string) XcmsCCC ccc; char *color_string;/* * DESCRIPTION * Returns a pointer to the color space structure * (XcmsColorSpace) associated with the specified color string. * * RETURNS * Pointer to matching XcmsColorSpace structure if found; * otherwise NULL. * * CAVEATS * */{ XcmsColorSpace **papColorSpaces; int n; char *pchar; if ((pchar = strchr(color_string, ':')) == NULL) { return(XcmsFailure); } n = (int)(pchar - color_string); if (ccc == NULL) { return(NULL); } /* * First try Device-Independent color spaces */ papColorSpaces = _XcmsDIColorSpaces; if (papColorSpaces != NULL) { while (*papColorSpaces != NULL) { if (strncmp((*papColorSpaces)->prefix, color_string, n) == 0 && !((*papColorSpaces)->prefix)[n]) { return(*papColorSpaces); } papColorSpaces++; } } /* * Next try Device-Dependent color spaces */ papColorSpaces = ((XcmsFunctionSet *)ccc->pPerScrnInfo->functionSet)->DDColorSpaces; if (papColorSpaces != NULL) { while (*papColorSpaces != NULL) { if (strncmp((*papColorSpaces)->prefix, color_string, n) == 0 && !((*papColorSpaces)->prefix)[n]) { return(*papColorSpaces); } papColorSpaces++; } } return(NULL);}/* * NAME * _XcmsParseColorString * * SYNOPSIS */static int_XcmsParseColorString(ccc, color_string, pColor) XcmsCCC ccc; char *color_string; XcmsColor *pColor;/* * DESCRIPTION * Assuming color_string contains a numerical string color * specification, attempts to parse a string into an * XcmsColor structure. * * RETURNS * 0 if failed; otherwise non-zero. * * CAVEATS * A color string containing a numerical color specification * must be in ISO Latin-1 encoding! */{ XcmsColorSpace *pColorSpace; char string_buf[64]; char *string_lowered; int len; int res; if (ccc == NULL) { return(0); } /* * While copying color_string to string_lowered, convert to lowercase */ if ((len = strlen(color_string)) >= sizeof(string_buf)) { string_lowered = (char *) Xmalloc(len+1); } else { string_lowered = string_buf; } _XcmsCopyISOLatin1Lowered(string_lowered, color_string); if (*string_lowered == '#') { if ((pColorSpace = _XcmsColorSpaceOfString(ccc, "rgb:")) != NULL) { res = (*pColorSpace->parseString)(string_lowered, pColor); if (len >= sizeof(string_buf)) Xfree(string_lowered); return res; } } if ((pColorSpace = _XcmsColorSpaceOfString(ccc, string_lowered)) != NULL) { res = (*pColorSpace->parseString)(string_lowered, pColor); if (len >= sizeof(string_buf)) Xfree(string_lowered); return res; } if (len >= sizeof(string_buf)) Xfree(string_lowered); return(0);}/* * NAME * FirstCmp - Compare color names of pair recs * * SYNOPSIS */static intFirstCmp(p1, p2)#if __STDC__ const void *p1, *p2;#else XcmsPair *p1, *p2;#endif/* * DESCRIPTION * Compares the color names of XcmsColorTuples. * This routine is public to allow access from qsort???. * * RETURNS * 0 if equal; * < 0 if first precedes second, * > 0 if first succeeds second. * */{ return(strcmp(((XcmsPair *)p1)->first, ((XcmsPair *)p2)->first));}/* * NAME * stringSectionSize - determine memory needed for strings * * SYNOPSIS */static voidSetNoVisit()/* * DESCRIPTION * * RETURNS * void * */{ int i; XcmsPair *pair = pairs; for (i = 0; i < nEntries; i++, pair++) { if (pair->flag != CYCLE) { pair->flag = NOT_VISITED; } }}/* * NAME * field2 - extract two fields * * SYNOPSIS */static intfield2(pBuf, delim, p1, p2) char *pBuf; char delim; /* in: field delimiter */ char **p1; /* in/out: pointer to pointer to field 1 */ char **p2; /* in/out: pointer to pointer to field 2 *//* * DESCRIPTION * Extracts two fields from a "record". * * RETURNS * XcmsSuccess if succeeded, otherwise XcmsFailure. * */{ *p1 = *p2 = NULL; /* Find Field 1 */ while (!isgraph(*pBuf)) { if ((*pBuf != '\n') || (*pBuf != '\0')) { return(XcmsFailure); } if (isspace(*pBuf) || (*pBuf == delim)) { pBuf++; } } *p1 = pBuf; /* Find end of Field 2 */ while (isprint(*pBuf) && (*pBuf != delim)) { pBuf++; } if ((*pBuf == '\n') || (*pBuf == '\0')) { return(XcmsFailure); } if ((*pBuf == ' ') || (*pBuf == delim)) { *pBuf++ = '\0'; /* stuff end of string character */ } else { return(XcmsFailure); } /* Find Field 2 */ while (!isgraph(*pBuf)) { if ((*pBuf == '\n') || (*pBuf == '\0')) { return(XcmsFailure); } if (isspace(*pBuf) || (*pBuf == delim)) { pBuf++; } } *p2 = pBuf; /* Find end of Field 2 */ while (isprint(*pBuf) && (*pBuf != delim)) { pBuf++; } if (*pBuf != '\0') { *pBuf = '\0'; /* stuff end of string character */ } return(XcmsSuccess);}/* * NAME * _XcmsLookupColorName - Lookup DB entry for a color name * * SYNOPSIS */static Status_XcmsLookupColorName(ccc, name, pColor) XcmsCCC ccc; char **name; XcmsColor *pColor;/* * DESCRIPTION * Searches for an entry in the Device-Independent Color Name * Database for the specified string. * * RETURNS * XcmsFailure if failed to find a matching entry in * the database. * XcmsSuccess if succeeded in converting color name to * XcmsColor. * _XCMS_NEWNAME if succeeded in converting color string (which * is a color name to yet another color name. Note * that the new name is passed back via 'name'. */ { Status retval = 0; char name_lowered_64[64]; char *name_lowered; register int i, j, left, right; int len; char *tmpName; XcmsPair *pair; /* * Check state of Database: * XcmsDbInitNone * XcmsDbInitSuccess * XcmsDbInitFailure */ if (XcmsColorDbState == XcmsDbInitFailure) { return(XcmsFailure); } if (XcmsColorDbState == XcmsDbInitNone) { if (!LoadColornameDB()) { return(XcmsFailure); } } SetNoVisit(); /* * While copying name to name_lowered, convert to lowercase */ tmpName = *name;Retry: if ((len = strlen(tmpName)) > 63) { name_lowered = (char *) Xmalloc(len+1); } else { name_lowered = name_lowered_64; } _XcmsCopyISOLatin1Lowered(name_lowered, tmpName); /* * Now, remove spaces. */ for (i = 0, j = 0; i < len; j++) { if (!isspace(name_lowered[j])) { name_lowered[i++] = name_lowered[j]; } } name_lowered[i] = '\0'; left = 0; right = nEntries - 1; while (left <= right) { i = (left + right) >> 1; pair = &pairs[i]; j = strcmp(name_lowered, pair->first); if (j < 0) right = i - 1; else if (j > 0) left = i + 1; else { break; } } if (len > 63) Xfree(name_lowered); if (left > right) { if (retval == 2) { if (*name != tmpName) { *name = tmpName; } return(_XCMS_NEWNAME); } return(XcmsFailure); } if (pair->flag == CYCLE) { return(XcmsFailure); } if (pair->flag == VISITED) { pair->flag = CYCLE; return(XcmsFailure); } if (_XcmsParseColorString(ccc, pair->second, pColor) == XcmsSuccess) { /* f2 contains a numerical string specification */ return(XcmsSuccess); } else { /* f2 does not contain a numerical string specification */ tmpName = pair->second; pair->flag = VISITED; retval = 2; goto Retry; }}/* * NAME * RemoveSpaces * * SYNOPSIS */static intRemoveSpaces(pString) char *pString;/* * DESCRIPTION * Removes spaces from string. * * RETURNS * Void * */{ int i, count = 0; char *cptr; /* REMOVE SPACES */ cptr = pString; for (i = strlen(pString); i; i--, cptr++) { if (!isspace(*cptr)) { *pString++ = *cptr; count++; } } *pString = '\0'; return(count);}/* * NAME * stringSectionSize - determine memory needed for strings * * SYNOPSIS */static intstringSectionSize(stream, pNumEntries, pSectionSize) FILE *stream; int *pNumEntries;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -