📄 int_str.c
字号:
/* * First we need to know how much room the result needs */ sizeList = 0; /* The loop in the case of *nolimit */ if (fsiList->options & FSIOptStrNoLimit) { FSI_FOR_START(fsiList); if (_i < 0 || _i>max-1) continue; sizeList += strlen(list[_i])+5; FSI_FOR_END; } /* The loop in the generic case */ else { FSI_FOR_START(fsiList); switch (bt) { case BorderPer : sizeList += strlen(list[BPER(_i,max)])+5; break; case BorderMir : sizeList += strlen(list[BMIR(_i,max)])+5; break; case BorderMir1 : sizeList += strlen(list[BMIR1(_i,max)])+5; break; default : sizeList += strlen(list[_i])+5; } FSI_FOR_END; } /* * Allocation of the result */ sizeList = MAX(MinStringSize,sizeList)+1; str = CharAlloc(sizeList); str[0] = '\0'; if (fsiList->nx1 >= 1) { str2 = str1 = CharAlloc(sizeList); str1[0] = '\0'; list2 = list1 = Malloc(sizeof(char *)*(_k+1)); *list1 = NULL; } else list2 = list1 = NULL; /* * Then we set the result ?????? se souvenir de la liste !! + faire optmisation si pas de *nolimit */ /* The loop in the case of *nolimit */ FSI_FOR_START(fsiList); if (fsiList->options & FSIOptStrNoLimit) { if (_i < 0 || _i>max-1) continue; str0 = list[_i]; } else { switch (bt) { case BorderPer : str0 = list[BPER(_i,max)]; break; case BorderMir : str0 = list[BMIR(_i,max)]; break; case BorderMir1 : str0 = list[BMIR1(_i,max)]; break; default : str0 = list[_i]; } } if (list2 != list1) strcat(str," "); strcat(str,str0); if (list2 != NULL) { strcpy(str2,str0); *list2 = str2; list2++; *list2 = NULL; str2 += strlen(str2)+1; } FSI_FOR_END; /* We want to return a STRVALUE so that we do not need to temp the string */ *pValue = (VALUE) NewNullStrValue(); ((STRVALUE) *pValue)->str = str; ((STRVALUE) *pValue)->list = list1; TempValue(*pValue); return(strType); }}void *GetStrFieldExtract(char *str, void **arg){ return(GetStrFieldExtract_(str, arg,NULL));}void *GetStrField(char *str, void **arg){ char *field = ARG_S_GetField(arg); FSIList *fsiList = ARG_S_GetFsiList(arg); void *res; ARG_S_SetField(arg,NULL); ARG_S_SetFsiList(arg,NULL); res = GetStrFieldExtract(str,arg); ARG_S_SetField(arg,field); ARG_S_SetFsiList(arg,fsiList); return(res);}/* * Get and extraction */ static void *GetExtractStrV(VALUE val, void **arg){ STRVALUE sc; char *str; /* Doc */ if (val == NULL) return(doc); sc = (STRVALUE) val; str = sc->str; return(GetStrFieldExtract_(str,arg,sc));}/* * Get the options for extraction (called for field NULL only) */static char *optionDoc = "{{*list,*nolimit,*bconst,*bmirror,*bmirror1,*bperiodic} \{*list : the string is considered as a list and all the extractions are performed using the list representation} \{*nolimit : indexes can be out of range} \{*bconst : border effect with same characters (last character for right handside and first character for left handside)} \{*bperiodic : periodic border effect)} \{*bmirror1 : mirror+periodic border effect (first and last points are repeated)} \{*bmirror : mirror+periodic border effect (first and last points are NOT repeated)}\}";static void *GetExtractOptionsStrV(VALUE val, void **arg){ /* Doc */ if (val == NULL) return(optionDoc); return(extractOptionsStr);}/* * Function to get the ExtractInfo for fields NULL */static void *GetExtractInfoStrV(VALUE val, void **arg){ static ExtractInfo extractInfo; static char flagInit = YES; char **list; STRVALUE sc = (STRVALUE) val; char *str = sc->str; char *field = ARG_EI_GetField(arg); unsigned long *options = ARG_EI_GetPOptions(arg); if (field != NULL) return(NULL); /* If *bperiodic,... then *nolimit must be off */ if (*options & (FSIOptStrBPer | FSIOptStrBMir | FSIOptStrBMir1)) *options &= ~FSIOptStrNoLimit; /* Init of the extraction info */ extractInfo.xmin = 0; /* ??????? */ if (flagInit) { extractInfo.xmin = 0; extractInfo.dx = 1; extractInfo.nSignals = 1; flagInit = NO; } /* '*list' is off : Get the maximum index */ if (!(*options & FSIOptStrList)) { extractInfo.xmax = strlen(str); if (extractInfo.xmax != 0) extractInfo.xmax--; } /* '*list' is on : Get the maximum index */ else { list = GetListFromStrValue(sc); if (list == NULL) return(NULL); extractInfo.xmax = GetListSize(list); if (extractInfo.xmax != 0) extractInfo.xmax--; } /* '*nolimit' option : set some flags */ if (*options & (FSIOptStrBPer | FSIOptStrBMir | FSIOptStrBMir1 | FSIOptStrNoLimit)) extractInfo.flags = EIIntIndex; else extractInfo.flags = EIErrorBound | EIIntIndex; return(&extractInfo);}/* * 'length' field */static char *lengthDoc = "{} {Get the number of characters of the string}";static void * GetLengthStrV(VALUE val, void **arg){ /* Documentation */ if (val == NULL) return(lengthDoc); return(GetIntField(strlen(((STRVALUE) val)->str),arg));}/* * 'llength' field */static char *llengthDoc = "{} {Get the number of elements of the string considered as a list}";static void * GetLLengthStrV(VALUE val, void **arg){ char **list; /* Documentation */ if (val == NULL) return(llengthDoc); list = GetListFromStrValue((STRVALUE) val); if (list == NULL) return(NULL); return(GetIntField(GetListSize(list),arg));}/* * 'tonum' field */static char *tonumDoc = "{} {Performs a conversion to a number if possible. Otherwise it returns 'null'}";static void * GetToNumStrV(VALUE val, void **arg){ LWFLOAT f; char *end; /* Documentation */ if (val == NULL) return(tonumDoc); f = strtod(((STRVALUE) val)->str,&end); if (*end != '\0') return(GetValueField(nullValue,arg)); return(GetFloatField(f,arg));}/* * Desallocation of a string content */ void DeleteStrValue(STRVALUE sc){ RemoveRefValue(sc); if (sc->nRef>0) return; if (sc->str != NULL) { Free(sc->str); sc->str = NULL; } if (sc->list != NULL) { DeleteList(sc->list); sc->list = NULL; }#ifdef DEBUGALLOCDebugType = "StrValue";#endif Free(sc);} /* * Alloc space for storing a string of size 'n' in a str content */char * StrValueStrAlloc(int n){ return(CharAlloc((MAX(MinStringSize,n)+1)));}/* * Init Null string Content */void InitNullStrValue(STRVALUE sc) { extern TypeStruct tsStr; InitValue(sc,&tsStr); sc->str = NULL; sc->list = NULL;}/* * Init string Content */void InitStrValue(STRVALUE sc) { InitNullStrValue(sc); sc->str = Malloc((MinStringSize+1)*sizeof(char)); sc->str[0] = '\0';} /* * Allocation of a string content with nothing in it */ STRVALUE NewNullStrValue(void){ STRVALUE sc;#ifdef DEBUGALLOCDebugType = "StrValue";#endif sc = Malloc(sizeof(struct strValue)); InitNullStrValue(sc); return(sc);}/* * Allocation of a string content with "" in it */ STRVALUE NewStrValue(void){ STRVALUE sc;#ifdef DEBUGALLOCDebugType = "StrValue";#endif sc = Malloc(sizeof(struct strValue)); InitStrValue(sc); return(sc);}STRVALUE TNewStrValue(void){ STRVALUE sc = NewStrValue(); TempValue( sc); return(sc);}void CopyStrValue(STRVALUE in, STRVALUE out){ if (strlen(in->str) <= MinStringSize || strlen(in->str)<=strlen(out->str)) strcpy(out->str,in->str); else { Free(out->str); out->str = CopyStr(in->str); } if (out->list) { DeleteList(out->list); out->list = NULL; } if (in->list) out->list = CopyList(in->list); }/* * Set a string content with a string (make a copy) */void SetStrValue(STRVALUE sc, char *str){ if (sc->str == NULL) { sc->str = str; } else if (sc->str == str) return; else if (strlen(str) <= MinStringSize || strlen(str)<=strlen(sc->str)) strcpy(sc->str,str); else { Free(sc->str); sc->str = CopyStr(str); } if (sc->list) { DeleteList(sc->list); sc->list = NULL; } }/* * Get a string from a string content */ char *GetStrFromStrValue(STRVALUE sc){ return(sc->str);}/* * Get a list from a str content */extern int ParseListBegEnd(char *theLine, char ***beg, char ***end); char **GetListFromStrValue(STRVALUE sc){ int n; char **beg,**end; if (sc->list) return(sc->list); n = ParseListBegEnd(sc->str,&beg,&end); if (n == -1) return(NULL); sc->list = BegEndStr2List(beg,end); return(sc->list);}/* * Concat two string contents */void ConcatStrValue(STRVALUE sc1,STRVALUE sc2,STRVALUE sc3){ char *str; int l3,l1,l2; l1 = strlen(sc1->str); l2 = strlen(sc2->str); l3 = l1+l2; l3 = MAX(l3,MinStringSize); str = CharAlloc(l3+1); strcpy(str,sc1->str); strcpy(str+l1,sc2->str); if (sc3->str) Free(sc3->str); sc3->str = str;}/* Multiply a string with a number */void MultStrValue(STRVALUE sc1, int n, STRVALUE sc3){ char *str,*str1; int j; int l3,l1 = strlen(sc1->str); if (n == 0) { SetStrValue(sc3,""); return; } l3 = l1*n; l3 = MAX(l3,MinStringSize); str = CharAlloc(l3+1); for (j=0,str1 = str;j<n;j++,str1+=l1) strcpy(str1,sc1->str); if (sc3->str) Free(sc3->str); sc3->str = str;}/* * Getting the string value of a variable named 'name' at a given level 'level' * (generate an error if not the right type) */ char * GetStrVariableLevel(LEVEL level,char *name){ VARIABLE var; VALUE val; STRVALUE sc; var = GetVariableLevel(level,name); val = ValueOf((VALUE) var); if (GetTypeValue(val) != strType) Errorf("GetStrVariableLevel() : Bad variable type '%s'",GetTypeValue(val)); sc = (STRVALUE) val; return(GetStrFromStrValue(sc));} char * GetStrVariable(char *name){ return(GetStrVariableLevel(levelCur,name));}/* * Setting a str content variable at a given level (creates it if it does not exist) * and sets it with string 'value' */ void SetStrVariableLevel(LEVEL level,char *name,char *value){ VARIABLE v; HASHTABLE t; char *left,flag,*type; VALUE *pcont; while (level->levelVar != level) level = level->levelVar; t = level->theVariables; /* Get the variable (eventually creates it) */ v = GetVariableHT(&t, YES, name, &left, &flag); if (v==NULL || flag != 0 || *left != '\0') Errorf1(""); /* Get a pointer to the content and the type */ pcont = GetVariablePContent(v, NO); type = GetTypeValue(*pcont); /* Check overwriting */ if (!DoesTypeOverwrite(type,strType)) Errorf("SetStrVariableLevel() : Cannot overwrite variable '%s' of type '%s' with '%s' typed value",name,type,strType); /* If the content is a strType can we use it ? */ if (type == strType && (*pcont)->nRef == 1) { SetStrValue((STRVALUE) (*pcont),value); return; } /* Delete the content */ DeleteValue(*pcont); /* Create it */ *pcont = (VALUE) NewStrValue(); /* Set it */ SetStrValue((STRVALUE) (*pcont),value);}void SetStrVariable(char *name,char *value){ SetStrVariableLevel(levelCur,name,value);} /* * Setting a string variable using a printf format (max length is 10000 !!) */void SetStrVariablef(char *name,char *format, ...){ va_list ap; char str[10000]; va_start(ap,format); vsprintf(str,format,ap); va_end(ap);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -