lib/subText.c

Go to the documentation of this file.
00001 /* subText - Stuff to do text substitutions. 
00002  *
00003  * This file is copyright 2002 Jim Kent, but license is hereby
00004  * granted for all use - public, private or commercial. */
00005 
00006 #include "common.h"
00007 #include "subText.h"
00008 
00009 static char const rcsid[] = "$Id: subText.c,v 1.11 2005/04/10 14:41:26 markd Exp $";
00010 
00011 struct subText *subTextNew(char *in, char *out)
00012 /* Make new substitution structure. */
00013 {
00014 struct subText *sub;
00015 AllocVar(sub);
00016 sub->in = cloneString(in);
00017 sub->out = cloneString(out);
00018 sub->inSize = strlen(in);
00019 sub->outSize = strlen(out);
00020 return sub;
00021 }
00022 
00023 void subTextFree(struct subText **pSub)
00024 /* Free a subText. */
00025 {
00026 struct subText *sub = *pSub;
00027 if (sub != NULL)
00028     {
00029     freeMem(sub->in);
00030     freeMem(sub->out);
00031     freez(pSub);
00032     }
00033 }
00034 
00035 void subTextFreeList(struct subText **pList)
00036 /* Free a list of dynamically allocated subText's */
00037 {
00038 struct subText *el, *next;
00039 
00040 for (el = *pList; el != NULL; el = next)
00041     {
00042     next = el->next;
00043     subTextFree(&el);
00044     }
00045 *pList = NULL;
00046 }
00047 
00048 #if 0 /* unused */
00049 static boolean firstSame(char *s, char *t, int len)
00050 /* Return TRUE if  the  first len characters of the strings s and t
00051  * are the same. */
00052 {
00053 while (--len >= 0)
00054     {
00055     if (*s++ != *t++)
00056         return FALSE;
00057     }
00058 return TRUE;
00059 }
00060 #endif
00061 
00062 static struct subText *firstInList(struct subText *l, char *name)
00063 /* Return first element in Sub list who's in string matches the
00064  * first part of name. */
00065 {
00066 struct subText *text = l;
00067 char *start;
00068 char *end;
00069 char *cmp;
00070 
00071 for(; text; text = text->next)
00072     {
00073     start = text->in;
00074     end = &start[text->inSize];
00075     cmp = name;
00076     for(;(start < end) && (*start == *cmp); start++, cmp++)
00077         ;
00078     if (start == end)
00079         return text;
00080     }
00081 return NULL;
00082 }
00083 
00084 
00085 int subTextSizeAfter(struct subText *subList, char *in)
00086 /* Return size string will be after substitutions. */
00087 {
00088 struct subText *sub;
00089 char *s;
00090 int size = 0;
00091 
00092 s = in;
00093 while (*s)
00094     {
00095     if ((sub = firstInList(subList, s)) != NULL)
00096         {
00097         s += sub->inSize;
00098         size += sub->outSize;
00099         }
00100     else
00101         {
00102         size += 1;
00103         s += 1;
00104         }
00105     }
00106 return size;
00107 }
00108 
00109 
00110 static void doSub(char *in, char *buf, struct subText *subList)
00111 /* Do substitutions in list while copying from in to buf.  This
00112  * cheap little routine doesn't check that out is big enough.... */
00113 {
00114 struct subText *sub;
00115 char *s, *d;
00116 
00117 s = in;
00118 d = buf;
00119 while (*s)
00120     {
00121     if ((sub = firstInList(subList, s)) != NULL)
00122         {
00123         s += sub->inSize;
00124         memcpy(d, sub->out, sub->outSize);
00125         d += strlen(sub->out);
00126         }
00127     else
00128         {
00129         *d++ = *s++;
00130         }
00131     }
00132 *d++ = 0;
00133 }
00134 
00135 void subTextStatic(struct subText *subList, char *in, char *out, int outMaxSize)
00136 /* Do substition to output buffer of given size.  Complain
00137  * and die if not big enough. */
00138 {
00139 int newSize = subTextSizeAfter(subList, in);
00140 if (newSize >= outMaxSize)
00141     errAbort("%s would expand to more than %d bytes", in, outMaxSize);
00142 doSub(in, out, subList);
00143 }
00144 
00145 char *subTextString(struct subText *subList, char *in)
00146 /* Return string with substitutions in list performed.  freeMem
00147  * this string when done. */
00148 {
00149 int size = subTextSizeAfter(subList, in);
00150 char *out = needMem(size+1);
00151 doSub(in, out, subList);
00152 return out;
00153 }
00154 

Generated on Tue Dec 25 18:39:32 2007 for blat by  doxygen 1.5.2