⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 int_str.c

📁 LastWave
💻 C
📖 第 1 页 / 共 4 页
字号:
    while (*str == *str1 && *str != '\0') {str++; str1++;}    c = *str;    *str = '\0';    SetResultStr(str3);    *str = c;    return;  }      else Errorf("Unknown action '%s'",action);}    /****************************************************** * * Managing lists * ******************************************************/  int GetMaxSizeWord(char **beg,char **end, int n){  return((end[n]-beg[n])+5);}static char *theStrList = NULL;static int theMaxSizeAlloc = 0;static int theNWords = 0;static int curNWord = 0;static char *curStrList = 0;void StartAppendStr2StringList(char *strlist, int nWords, int maxSizeAlloc){  curStrList = theStrList = strlist;  curNWord = 0;  theNWords = nWords;  theMaxSizeAlloc = maxSizeAlloc;  *theStrList = '\0';}void EndAppendStr2StringList(void){  curStrList = theStrList = NULL;  curNWord  = 0;  theNWords = 0;}void AppendStr2StringList(char *str){  char flagBraces;    /* If not first word then ' ' */  if (curNWord != 0) {    if ((curStrList-theStrList+1)+1 >= theMaxSizeAlloc) {      Errorf("AppendStr2StringList() : You exceeded the allocation for your list");    }    *curStrList = ' ';    curStrList++;  }  /* Should we put braces around the result ? *//*  if (theNWords == 1) flagBraces = NO;  else if (*str == '\0') flagBraces = YES;  else flagBraces = IsList(str);  */  flagBraces = NO;    /* Let's do it */  if (!flagBraces) {    if ((curStrList-theStrList+1)+strlen(str) >= theMaxSizeAlloc) {      Errorf("AppendStr2StringList() : You exceeded the allocation for your list");    }    strcpy(curStrList,str);    curStrList += strlen(str);  }  else {    if ((curStrList-theStrList+1)+strlen(str)+2 >= theMaxSizeAlloc) {      Errorf("AppendStr2StringList() : You exceeded the allocation for your list");    }    *curStrList = '{';    curStrList++;    strcpy(curStrList,str);    curStrList += strlen(str);    *curStrList = '}';    curStrList++;  }    *curStrList = '\0';    curNWord++;}/* Delete a list */void DeleteList(char **list){  if (list == NULL) Errorf("DeleteList() : NULL argument");  if (*list) Free(*list);  Free(list);}/* A simplifier dans la copie *//* Copy a list */char **CopyList(char **list){  char **listOut;  int totalSize;  int nElem,i;  char flagList;  if (list == NULL) Errorf("CopyList() : cannot copy NULL list");    /* Case of an empty list */  if (*list == NULL) {    listOut = Malloc(sizeof(char *));    *listOut = NULL;    return(listOut);  }     /* Counting the number of elements of thelist AND checking if it is a list */   nElem = 0;  flagList = YES;  while (list[nElem] != NULL) {    if (nElem != 0 && flagList && strlen(list[nElem-1])+list[nElem-1]+1 != list[nElem]) flagList = NO;    nElem++;  }    /* Total size allocation of the list */  if (flagList) {    totalSize = list[nElem-1]-list[0] + strlen(list[nElem-1])+1;    if (totalSize < 0) Errorf("CopyList() : Weired bug");  }  else {    nElem = 0;    totalSize = 0;    while (list[nElem] != NULL) {      totalSize += strlen(list[nElem])+1;      nElem++;    }  }    /* Let's allocate */  listOut = Malloc((nElem+1)*sizeof(char*));  *listOut = Malloc(totalSize*sizeof(char));    /* We have to set the listOut[i] */  strcpy(listOut[0],list[0]);  for (i=1;i<nElem;i++) {    if (flagList) listOut[i] = (list[i]-list[i-1])+listOut[i-1];    else listOut[i] = listOut[i-1]+strlen(listOut[i-1])+1;    strcpy(listOut[i],list[i]);  }  listOut[nElem] = NULL;    return(listOut);}/* Copy a list and make the result temporary */char **TCopyList(char **list){  char **listOut = CopyList(list);    TempList(listOut);  return(listOut);}/* ?? a verifier *//* Convert an array of strings of size 'nElem'  into a list */char **Array2List(char **array, int nElem){    char **listOut;  int i,totalSize;    /* Case of an empty array */  if (nElem == 0) {    listOut = Malloc(sizeof(char *));    *listOut = NULL;    return(listOut);  }       /* Total size allocation of the list */  totalSize = 0;  for (i=0;i<nElem;i++) totalSize += strlen(array[i]) +1;    /* Let's allocate */  listOut = Malloc((nElem+1)*sizeof(char*));  *listOut = Malloc(totalSize*sizeof(char));    /* We have to set the listOut[i] */  strcpy(listOut[0],array[0]);  for (i=1;i<nElem;i++) {    listOut[i] = listOut[i-1]+strlen(listOut[i-1])+1;    strcpy(listOut[i],array[i]);  }  listOut[nElem] = NULL;    return(listOut);}/* Same as above but make the list temporary*/char **TArray2List(char **array, int nElem){  char **list = Array2List(array, nElem);  TempList(list);  return(list);}/* Convert a list to a string by separating each element of the list by 'separator' */char *List2Str(char **list,char *separator){  int size;  char **list1;  char *strOut;  int n;  if (list == NULL) Errorf("List2Str() : cannot convert a NULL list to a string");  /* Case of an empty list */  if (*list == NULL) {    strOut = CharAlloc(1);    *strOut = '\0';    return(strOut);  }        n = strlen(separator);    /* Total size allocation of the list */  size = 0;  list1 = list;  while (*list1 != NULL) {    size += strlen(*list1)+n;    list1++;  }  size-=n;    /* Allocation of the string */        strOut = CharAlloc(size+1);  strOut[0] = '\0';    /* Fill the string ?? merde a optimiser*/  list1 = list;  while (*list1 != NULL) {    strcat(strOut,*list1);    if (*(list1+1) != NULL) strcat(strOut,separator);    list1++;  }  return(strOut);}/* Convert a list to a string and make this string temporary */char *TList2Str(char **list,char *separator){  char * strOut  = List2Str(list,separator);    TempStr(strOut);  return(strOut);}/* * Convert a succession of strings into a list  *   The succession of strings is given by two arrays *   begStr[] and endStr[] (these arrays end when both == NULL) */ char **BegEndStr2List(char *begStr[],char *endStr[]){  char **listOut,**list;  int n,size,i;  /* Case the list should be the empty list */  if (begStr[0] == NULL || endStr[0] == NULL) {    listOut = Malloc(sizeof(char *));    *listOut = NULL;    return(listOut);  }  /* Let's compute what the total size the list should be */  size = 0;    n = 0;  while (begStr[n] != NULL) {     size += (endStr[n] - begStr[n]) + 2;    n++;  }    /* Let's alloc */      listOut = Malloc((n+1)*sizeof(char*));  *listOut = Malloc(size*sizeof(char));    list = listOut;  for (i=0;i<n;i++) {    strncpy(*list,begStr[i],endStr[i]-begStr[i]+1);    (*list)[endStr[i]-begStr[i]+1] = '\0';    *(list+1) = *list + (endStr[i]-begStr[i])+2;    list++;  }  *list = NULL;    return(listOut);}/* Same as above but make it temporary */ char **TBegEndStr2List(char *begStr[],char *endStr[]){  char **list = BegEndStr2List(begStr,endStr);  TempList(list);  return(list);}/* Convert a succession of strings into a list */#define MaxNumStrs 200char **Str2List(char *str,...){  va_list ap;  char **listOut,**list;  char *theStrs[MaxNumStrs+1];  int size[MaxNumStrs+1];  int n,i,tsize;  if (str == NULL) {    listOut = Malloc(sizeof(char*));    *listOut = NULL;    return(listOut);  }        va_start(ap,str);     theStrs[0] = str;  n=1;  tsize = 0;    while (n <= MaxNumStrs && theStrs[n-1] != NULL) {     size[n-1] = strlen(theStrs[n-1])+1;    tsize += size[n-1];    theStrs[n] = va_arg(ap,char *);    n++;  }    va_end(ap);    if (n>MaxNumStrs) Errorf("Str2List() : Too many strings to append (should be less than %d)",MaxNumStrs);      listOut = Malloc(n*sizeof(char*));  *listOut = Malloc(tsize*sizeof(char));    list = listOut;  for (i=0;i<n;i++) {    strcpy(*list,theStrs[i]);    (*list)[size[i]-1] = '\0';    *(list+1) = *list + size[i];    list++;  }  *list = NULL;    return(listOut);}/* Convert a succession of strings into a list and make it temporary */#define MaxNumStrs 200char **TStr2List(char *str,...){Errorf("Pas implement TStr2List");}/* Print each word of a list seprated with a 'c' character and then \n */void PrintList(char **list,char c){  while (*list) {    if (*(list+1) != NULL) Printf("%s%c",*list,c);    else Printf("%s\n",*list);  }}/* * Function that returns YES if 'str' is a list  * (i.e., braces must be placed around a string before concatenating with another string * in order to form a list). */char IsList(char *str){  char flagBraces;  int n;        flagBraces = NO;  n = 0;  while(1) {    if (*str == '\0') break;    if (*str == ' ' || *str == '\n' || *str == '\r') {flagBraces = YES; break;}    if (*str == '{') {      while (1) {        while (*str != '\0' && *str != '{' && *str != '}') str++;        if (*str == '{') {n++; str++; continue;}        if (*str == '}') {n--; str++; if (n == 0) break; else continue;}        flagBraces = YES;        break;      }    }    else str++;  }    return(flagBraces);}int GetListSize(char **list){  char **list1;    for(list1=list;*list1!=NULL;list1++) {};    return(list1-list);}/* Make a List temporary */void TempList(char **list){  if (list == NULL) Errorf("TempList() (weired bug) : list is NULL");  TempPtr(list);  if (list[0]) TempPtr(list[0]);} /* * Print successive strings in columns. * To initialize it one must call this function with str == NULL and size == size of a column * Then one calls it with str != NULL and 0 *//************************************************ * * Command on lists * ************************************************/void C_List(char **argv){  char *action,*str;  char **list,**list1;    argv = ParseArgv(argv,tWORD,&action,-1);    /* add action */  if (!strcmp(action,"add")) {    argv = ParseArgv(argv,tLIST,&list,tSTR,&str,0);      SetResultStr("");    while (*list) {      AppendListResultStr(*list);      list++;    }    AppendListResultStr(str);  }   /* append action */  else if (!strcmp(action,"append")) {    SetResultStr("");    while (1) {      argv = ParseArgv(argv,tLIST_,NULL,&list,-1);      if (list == NULL) break;      while (*list) {        AppendListResultStr(*list);        list++;      }    }    NoMoreArgs(argv);  }   /* reverse action */  else if (!strcmp(action,"reverse")) {    argv = ParseArgv(argv,tLIST,&list,0);    SetResultStr("");    list1 = list;    while(*list1) list1++;    list1--;    while (list1 != list) {      AppendListResultStr(*list1);      list1--;    }    AppendListResultStr(*list1);    }               else Errorf("Unknown action '%s'",action);}/* * The field list */struct field fieldsStr[] = {  "", GetExtractStrV, SetExtractStrV, GetExtractOptionsStrV, GetExtractInfoStrV,  "length", GetLengthStrV, NULL, NULL, NULL,  "llength", GetLLengthStrV, NULL, NULL, NULL,  "tonum", GetToNumStrV, NULL, NULL, NULL,  NULL, NULL, NULL, NULL, NULL};/* * The type structure for str */TypeStruct tsStr = {  "{{{&string} {This type corresponds to strings. \n \- Constructors : You can use the syntax \"string\" or 'string'. \n \- Operator + (and +=) : string1+string2, concatenation \n \- Operator * (and *=) : string*n,  repetition of the string n times.}} \{{&list} {This type corresponds to a string that can be interpreted as a list.}}}",  /* Documentation */  &strType,       /* The basic (unique) type name */  NULL,     /* The GetType function */                           DeleteStrValue,     /* The Delete function */  NewStrValue,     /* The Delete function */    NULL,       /* The copy function */  NULL,       /* The clear function */    ToStrStrContent,      /* String conversion */  PrintStrContent,   /* The Print function : print the object when 'print' is called */  PrintInfoStrContent,   /* The PrintInfo function : called by 'info' */  NULL,              /* The NumExtract function : used to deal with syntax like 10a */     fieldsStr,      /* The list of fields */};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -