Open64 (mfef90, whirl2f, and IR tools)
TAG: version-openad; SVN changeset: 916
|
00001 /* 00002 00003 Copyright (C) 2000, 2001 Silicon Graphics, Inc. All Rights Reserved. 00004 00005 This program is free software; you can redistribute it and/or modify it 00006 under the terms of version 2 of the GNU General Public License as 00007 published by the Free Software Foundation. 00008 00009 This program is distributed in the hope that it would be useful, but 00010 WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00012 00013 Further, this software is distributed without any warranty that it is 00014 free of the rightful claim of any third person regarding infringement 00015 or the like. Any license provided herein, whether implied or 00016 otherwise, applies only to this software file. Patent licenses, if 00017 any, provided herein do not apply to combinations of this program with 00018 other software, or any other product whatsoever. 00019 00020 You should have received a copy of the GNU General Public License along 00021 with this program; if not, write the Free Software Foundation, Inc., 59 00022 Temple Place - Suite 330, Boston MA 02111-1307, USA. 00023 00024 Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pky, 00025 Mountain View, CA 94043, or: 00026 00027 http://www.sgi.com 00028 00029 For further information regarding this notice, see: 00030 00031 http://oss.sgi.com/projects/GenInfo/NoticeExplan 00032 00033 */ 00034 00035 00036 /* 00037 * String utilities that use varying length strings 00038 */ 00039 #define USE_STANDARD_TYPES 00040 #include <stdarg.h> 00041 #include <stdlib.h> 00042 #include <stdio.h> 00043 #include <string.h> 00044 #include "vstring.h" 00045 #include "errors.h" 00046 00047 #define MAX(a,b) ((a>=b)?a:b) 00048 00049 #define vstr_max(v) ((v).max+0) 00050 #define set_vstr_max(v,m) (v).max = m 00051 #define set_vstr_len(v,l) (v).len = l 00052 #define set_vstr_str(v,s) (v).str = s 00053 00054 static vstring current_vstring = {0,0,NULL}; 00055 00056 static void 00057 Realloc_Vstring (vstring *v, int newlen) 00058 { 00059 /* make sure realloc doesn't just add 1 char */ 00060 newlen = MAX (newlen, vstr_max(*v)+80); 00061 set_vstr_max(*v, newlen); 00062 set_vstr_str(*v, (char*) realloc(vstr_str(*v), vstr_max(*v))); 00063 } 00064 00065 /* 00066 * For now, can only have one vstring buffer live at a time. 00067 * Someday should enhance this, but not needed now. 00068 * To do that, we could keep freelist of vstrings, 00069 * and reuse space that way. 00070 */ 00071 00072 /* must call vstr_{begin,end} around use of a vstring buffer */ 00073 00074 vstring 00075 vstr_begin (int len) 00076 { 00077 if (vstr_max(current_vstring) == 0) { 00078 set_vstr_str(current_vstring, (char*) malloc(len)); 00079 set_vstr_max(current_vstring, len); 00080 } 00081 else if (vstr_len(current_vstring) != 0) { 00082 DevWarn("vstr_begin before finishing old one?\n"); 00083 set_vstr_len(current_vstring, 0); 00084 } 00085 if (vstr_max(current_vstring) < len) { 00086 Realloc_Vstring (¤t_vstring, len); 00087 } 00088 return current_vstring; 00089 } 00090 00091 /* frees space used by vstring buffer */ 00092 void 00093 vstr_end (vstring v) 00094 { 00095 set_vstr_len(v, 0); 00096 v.str[0] = '\0'; 00097 current_vstring = v; /* so reused later */ 00098 } 00099 00100 /* add char to vstring */ 00101 vstring 00102 vstr_append (vstring v, const char c) 00103 { 00104 if (vstr_len(v) + 1 > vstr_max(v)) { 00105 Realloc_Vstring (&v, vstr_len(v) + 1); 00106 } 00107 v.str[v.len] = c; 00108 v.len++; 00109 return v; 00110 } 00111 00112 /* add string to vstring */ 00113 vstring 00114 vstr_concat (vstring v, const char *s) 00115 { 00116 int slen = strlen(s); 00117 if (vstr_len(v) + slen > vstr_max(v)) { 00118 Realloc_Vstring (&v, vstr_len(v) + slen); 00119 } 00120 /* may be nulls in vstr, so can't concat from beginning; 00121 * instead just copy onto end of string. */ 00122 strcpy(vstr_str(v)+vstr_len(v), s); 00123 set_vstr_len(v, vstr_len(v) + slen); 00124 return v; 00125 } 00126 00127 /* 00128 * sprintf that reallocs space if needed. 00129 * the string is formatted into the vstring v at index position. 00130 */ 00131 int 00132 vstr_sprintf (vstring *v, int index, const char *format, ... /* args */) 00133 { 00134 int len; 00135 va_list ap; 00136 char *p; 00137 len = strlen(format); 00138 va_start (ap, format); 00139 p = (char*) format; 00140 while (*p != '\0') { 00141 if (*p == '%') { 00142 p++; 00143 if (*p == '%') ; /* ignore */ 00144 else if (*p == 's') { 00145 len += strlen(va_arg(ap,char*)); 00146 } 00147 else { 00148 /* numeric */ 00149 va_arg(ap,int); 00150 len += 16; /* max numeric size? */ 00151 } 00152 } 00153 p++; 00154 } 00155 va_end(ap); 00156 if (len > vstr_max(*v)) { 00157 Realloc_Vstring (v, len); 00158 } 00159 va_start (ap, format); 00160 len = vsprintf(v->str+index, format, ap); 00161 set_vstr_len(*v, index + len); 00162 va_end(ap); 00163 if (vstr_len(*v) > vstr_max(*v)) 00164 Fatal_Error("vstr_sprintf overflowed"); 00165 return len; 00166 }