00001 /* 00002 namedins.h: 00003 00004 Copyright (C) 2002 Istvan Varga 00005 00006 This file is part of Csound. 00007 00008 The Csound Library is free software; you can redistribute it 00009 and/or modify it under the terms of the GNU Lesser General Public 00010 License as published by the Free Software Foundation; either 00011 version 2.1 of the License, or (at your option) any later version. 00012 00013 Csound is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU Lesser General Public License for more details. 00017 00018 You should have received a copy of the GNU Lesser General Public 00019 License along with Csound; if not, write to the Free Software 00020 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 00021 02111-1307 USA 00022 */ 00023 00024 /* namedins.h -- written by Istvan Varga, Oct 2002 */ 00025 00026 #ifndef CSOUND_NAMEDINS_H 00027 #define CSOUND_NAMEDINS_H 00028 00029 #include "csoundCore.h" /* for INSTRTXT */ 00030 00031 #ifdef __cplusplus 00032 extern "C" { 00033 #endif 00034 00035 /* check if the string s is a valid instrument or opcode name */ 00036 /* return value is zero if the string is not a valid name */ 00037 00038 int check_instr_name(char *); 00039 00040 /* find the instrument number for the specified name */ 00041 /* return value is zero if none was found */ 00042 00043 long named_instr_find(CSOUND *, char *); 00044 00045 /* allocate entry for named instrument ip with name s (s must not be freed */ 00046 /* after the call, because only the pointer is stored); instrument number */ 00047 /* is set to insno */ 00048 /* returns zero if the named instr entry could not be allocated */ 00049 /* (e.g. because it already exists) */ 00050 00051 int named_instr_alloc(CSOUND *, char *, INSTRTXT *, long); 00052 00053 /* assign instrument numbers to all named instruments */ 00054 /* called by otran */ 00055 00056 void named_instr_assign_numbers(CSOUND *); 00057 00058 /* convert opcode string argument to instrument number */ 00059 /* return value is -1 if the instrument cannot be found */ 00060 /* (in such cases, csoundInitError() is also called) */ 00061 00062 long strarg2insno(CSOUND *, void *, int); 00063 00064 /* same as strarg2insno, but runs at perf time, */ 00065 /* and does not support numbered instruments */ 00066 /* (used by opcodes like event or schedkwhen) */ 00067 00068 long strarg2insno_p(CSOUND *, char *); 00069 00070 /* convert opcode string argument to instrument number */ 00071 /* (also allows user defined opcode names); if the integer */ 00072 /* argument is non-zero, only opcode names are searched */ 00073 /* return value is -1 if the instrument cannot be found */ 00074 /* (in such cases, csoundInitError() is also called) */ 00075 00076 long strarg2opcno(CSOUND *, void *, int, int); 00077 00078 /* create file name from opcode argument (string or MYFLT) */ 00079 /* CSOUND *csound: */ 00080 /* pointer to Csound instance */ 00081 /* char *s: */ 00082 /* output buffer, should have enough space; if NULL, the */ 00083 /* required amount of memory is allocated and returned */ 00084 /* void *p: */ 00085 /* opcode argument, is interpreted as char* or MYFLT*, */ 00086 /* depending on the 'is_string' parameter */ 00087 /* const char *baseName: */ 00088 /* name prefix to be used if the 'p' argument is MYFLT, */ 00089 /* and it is neither SSTRCOD, nor a valid index to strset */ 00090 /* space. */ 00091 /* For example, if "soundin." is passed as baseName, file */ 00092 /* names in the format "soundin.%d" will be generated. */ 00093 /* baseName may be an empty string, but should not be NULL */ 00094 /* int is_string: */ 00095 /* if non-zero, 'p' is interpreted as a char* pointer and */ 00096 /* is used as the file name. Otherwise, it is expected to */ 00097 /* point to a MYFLT value, and the following are tried: */ 00098 /* 1. if the value is SSTRCOD, the string argument of */ 00099 /* the current score event is used (string p-field) */ 00100 /* 2. if the value, rounded to the nearest integer, is a */ 00101 /* valid index to strset space, the strset string is */ 00102 /* used */ 00103 /* 3. the file name is generated using baseName and the */ 00104 /* value rounded to the nearest integer, as described */ 00105 /* above */ 00106 /* 'is_string' is usually p->XSTRCODE for an opcode with */ 00107 /* only one string argument, otherwise it is */ 00108 /* p->XSTRCODE & (1 << (argno - 1)) */ 00109 /* return value: */ 00110 /* pointer to the output string; if 's' is not NULL, it is */ 00111 /* always the same as 's', otherwise it is allocated with */ 00112 /* mmalloc() and the caller is responsible for freeing the */ 00113 /* allocated memory with mfree() or csound->Free() */ 00114 00115 char *strarg2name(CSOUND *, char *, void *, const char *, int); 00116 00117 /* ----------------------------------------------------------------------- */ 00118 /* the following functions are for efficient management of the opcode list */ 00119 00120 /* find opcode with the specified name in opcode list */ 00121 /* returns index to opcodlst[], or zero if the opcode cannot be found */ 00122 00123 int find_opcode(CSOUND *, char *); 00124 00125 /* ----------------------------------------------------------------------- */ 00126 /* These functions replace the functionality of strsav() in rdorch.c. */ 00127 00128 /* Allocate space for strsav (called once from rdorchfile()). */ 00129 00130 void strsav_create(CSOUND *); 00131 00132 /* Locate string s in database, and return address of stored string (not */ 00133 /* necessarily the same as s). If the string is not defined yet, it is */ 00134 /* copied to the database (in such cases, it is allowed to free s after */ 00135 /* the call). */ 00136 00137 char *strsav_string(CSOUND *, char *); 00138 00139 /* ----------------------------------------------------------------------- */ 00140 00141 extern const unsigned char strhash_tabl_8[256]; 00142 extern unsigned int csound_str_hash_32(const char *s); 00143 00144 static inline unsigned char name_hash(CSOUND *csound, const char *s) 00145 { 00146 const unsigned char *c = (const unsigned char*) &(s[0]); 00147 unsigned int h = 0U; 00148 #ifdef LINUX 00149 for ( ; *c != (unsigned char) 0; c++) 00150 h = csound->strhash_tabl_8[h ^ *c]; 00151 #else 00152 (void) csound; 00153 for ( ; *c != (unsigned char) 0; c++) 00154 h = strhash_tabl_8[h ^ *c]; 00155 #endif 00156 return (unsigned char) h; 00157 } 00158 00159 /* faster version that assumes a non-empty string */ 00160 00161 static inline unsigned char name_hash_2(CSOUND *csound, const char *s) 00162 { 00163 const unsigned char *c = (const unsigned char*) &(s[0]); 00164 unsigned int h = 0U; 00165 #ifdef LINUX 00166 do { 00167 h = csound->strhash_tabl_8[h ^ *c]; 00168 } while (*(++c) != (unsigned char) 0); 00169 #else 00170 (void) csound; 00171 do { 00172 h = strhash_tabl_8[h ^ *c]; 00173 } while (*(++c) != (unsigned char) 0); 00174 #endif 00175 return (unsigned char) h; 00176 } 00177 00178 static inline int sCmp(const char *x, const char *y) 00179 { 00180 int i = 0; 00181 while (x[i] == y[i] && x[i] != (char) 0) 00182 i++; 00183 return (x[i] != y[i]); 00184 } 00185 00186 /* ----------------------------------------------------------------------- */ 00187 00188 #ifdef __cplusplus 00189 } 00190 #endif 00191 00192 #endif /* CSOUND_NAMEDINS_H */ 00193