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

📄 sds_list.c

📁 这是一个Linux下的集成开发环境
💻 C
📖 第 1 页 / 共 2 页
字号:
/* $Header: /cvsroot/sourcenav/src/snavigator/demo/c++_demo/sds/sds_list.c,v 1.1.1.1 2002/04/18 23:35:31 mdejong Exp $ */#include "Sds/sdsgen.h"#include "Sds/sds_lisd.h"#include "Sds/sds_externs.h"#include <string.h>#if defined (__MSDOS__)#include <alloc.h>#else#include <stdlib.h>#endif#define MBLOCK 16static int listindent = -1;static long listsleft = 0;/*   forward declarations */void                  dump_list(struct list_control *);int                   recoverlist(void);void                  list_init(struct list_control *, char *, int);int                   sds_add_node(struct list_control *,                                    struct node_control *);void                  sds_init_node(struct node_control *);int                   sds_delete_node(struct node_control *);void                  dumpnode(struct node_control *, void *);void                  sds_bank_compress(int);int                   sds_homogeneous_data(int,sds_code,sds_handle,sds_handle);struct list_control * goodlist(int);struct node_control * sds_new_node(void);struct node_control * sds_new_data(int,long,long,sds_handle,sds_handle,void *);int                   sds_allnodes(void);void                  sds_concat_nodes(sds_handle, struct node_control *,                         struct list_control *, long *, long *);int                   sds_concat_list(sds_handle,struct node_control *,                         struct list_control *,long *, long *, long);extern void pindent(int);/******************************************************************     EXPORT Level 1long sds_new_list(name, id)   Create a new list, with a name and id supplied by the user.   Space for the list is reclaimed from any deleted list, from the   already allocated array, or from new allocations if necessary.   Malloc failures result in exit, otherwise no error return.******************************************************************/intsds_new_list(name, id)char *name;int id;{  sds_listcon *slc = sds_listc();  slc->lastlist++;  if (slc->listsdeleted != 0) /* There are some list(s) marked 'deleted' */  {    long rlist;    if ((rlist = recoverlist()) >= 0)    {      list_init(&slc->lc[rlist],name, id);      return rlist;    }  }  if (listsleft == 0) /* I have to allocate some more space */  {    slc->nlists += MBLOCK;    listsleft = MBLOCK;    if (slc->lc == NULL)      slc->lc = (struct list_control *)sds_malloc(slc->nlists * sizeof(struct list_control));    else      slc->lc = (struct list_control *)sds_realloc((char *)slc->lc, slc->nlists * sizeof(struct list_control));  }  listsleft--;  list_init(&slc->lc[slc->lastlist - 1], name, id);  return slc->lastlist;}/******************************************************************     EXPORT Level 1int sds_homogeneous_struct(list_index, sds, object)int sds_homogeneous_array(list_index, type )     INTERNALint sds_homogeneous_data(list_index, datatype, sds, object)  These three functions declare that all the data objects in a list  will be of the same type - either primitive (sds_homogeneous_array)  or compound (sds_homogeneous_struct()). Multiplicities are given   when individual nodes in the list are declared. Both these user  functions use sds_homogeneous_data() for implementation.******************************************************************/intsds_homogeneous_struct(list_index, sds, object)int list_index;sds_handle sds,object;{  return sds_homogeneous_data(list_index,0, sds, object); }intsds_homogeneous_array(list_index, type )int list_index;sds_code type;{ return sds_homogeneous_data(list_index,type, SDS_NO_SUCH_SDS, 0); }intsds_homogeneous_data(list_index, datatype, sds, object)int list_index;sds_handle sds,object;sds_code datatype;{  struct list_control *list;  if ((list = goodlist(list_index)) == NULL)    return 0;  list->sds = sds;  list->object = object;  list->datatype = sds_obind2code(sds, object);  list->is_homogeneous = 1;  return 1;}/******************************************************************     EXPORT Level 1int sds_give_databank(list_index, pointer, size, type)  Instead of malloc()ing data space for new nodes, use a   pre-allocated block of data as a bank. This is mainly to  load Fortran commons.******************************************************************/intsds_give_databank(list_index, pointer, size, type)int list_index;void *pointer;long size;sds_code type;{  struct list_control *list;  if ((list = goodlist(list_index)) == NULL)    return 0;  list->banktype = type;  list->databank = pointer;  list->banksize = size * (long)sds_psize(type);  list->bankused = 0;  list->do_malloc = 0;  return 1;}/******************************************************************     INTERNALvoid list_init(list, name, id)  Initialise a new list with defaults.******************************************************************/void list_init(list, name, id)struct list_control *list;char *name;int id;{  list->head = (struct node_control *)0;  list->tail = (struct node_control *)0;  list->nnodes  = 0;  list->listid =id;  list->name = (char *)malloc(strlen(name) + 1);  strcpy(list->name, name);  list->current = (struct node_control *)0;  list->databank = (void *)0;  list->banksize = 0L;  list->bankused = 0L;  list->banktype = 0L;  list->sds = 0L;  list->object = 0L;  list->datatype = 0L;  list->is_deleted  = 0;  list->do_malloc  = 1;  list->has_malloc  = 0;  list->is_top  = 0;  list->is_bottom  = 0;  list->is_homogeneous  = 0;  list->done_output  = 0;}/******************************************************************     EXPORT Level 1int sds_delete_list(list_index)  Delete an existing list or warn that it doesn't exist.******************************************************************/int sds_delete_list(list_index)int list_index;{  sds_listcon *slc = sds_listc();  struct list_control *list;  struct node_control *this;  struct node_control *next;  if ((list = goodlist(list_index)) == NULL)    return 1;  this = list->head;  while (this != NULL)  {      next = this->next;    sds_delete_node(next);    this = next;  }  list->is_deleted = 1;  slc->listsdeleted++;  return 1;}/******************************************************************     INTERNALstruct list_control * goodlist(list_index)  Checks if the list_index is OK.******************************************************************/struct list_control *goodlist(list_index)int list_index;{  sds_listcon *slc = sds_listc();  if (list_index < 1 || list_index > slc->lastlist )  {    sds_push_error(SDS_NO_SUCH_LIST, SDS_ERROR, "List index out-of-bounds");    return (struct list_control *)0;  }  if (slc->lc[list_index - 1].is_deleted)  {    sds_push_error(SDS_NO_SUCH_LIST, SDS_ERROR, "List index deleted");    return (struct list_control *)0;  }  return &slc->lc[list_index - 1];}/******************************************************************     INTERNALint recoverlist(void)  Look for a list marked 'deleted' so it can be re-used.******************************************************************/intrecoverlist(VOIDDEF){  int i = 0;  sds_listcon *slc = sds_listc();  while(i < slc->nlists)  {    if (slc->lc[i].is_deleted)    {      slc->listsdeleted--;      slc->lc[i].is_deleted = 0;      return (long)i;    }    i++;  }  return 0;}/******************************************************************     INTERNALvoid clean_lists(void)  Cleans up the 'you've touched me' flags in the lists so that  you can see if a list has already been treated when you traverse  a tree of them.******************************************************************/voidclean_lists(VOIDDEF){  int i;  sds_listcon *slc = sds_listc();  for (i = 0;i < slc->lastlist; i++)    slc->lc[i].done_output = 0;}/******************************************************************     EXPORT Level 2void dump(void)  Print out all lists and their nodes.******************************************************************/voiddump(VOIDDEF){  int i;  sds_listcon *slc = sds_listc();  clean_lists();  printf("----Dump lists and content definitions----\n");  for (i = 0;i < slc->lastlist; i++)  {    if (!slc->lc[i].done_output)      dump_list(&slc->lc[i]);  }  clean_lists();}/******************************************************************     EXPORT Level 2void dump_list(list)  Print out this list and its nodes.******************************************************************/voiddump_list(list)struct list_control *list;{  struct node_control *node;  listindent++;  pindent(listindent);  if (list->is_deleted == 1)    printf("Deleted ");  printf("List start:id %ld: %s\n",list->listid, list->name);  list->done_output = 1;  if (list->databank != NULL)  {    pindent(listindent);    printf("Databank at %lx, length %ld, %ld used\n",             (unsigned long)list->databank,             list->banksize,             list->bankused);  }  node = list->head;  if (list->is_homogeneous)  {    pindent(listindent);    printf("Homogeneous list: each of %ld nodes controls:\n",list->nnodes);    if (node->sds > 0)      sds_list(node->sds,node->object,SDS_HEADER_ONLY);   }  else  {    while(node != NULL)    {      dumpnode(node, list->databank);      if (node->down != NULL)        dump_list(node->down);      node = node->next;    }  }  pindent(listindent);  printf("List end  :id %ld: %s\n",list->listid, list->name);  listindent--;}/******************************************************************     EXPORT Level 2void dumpnode(node, base)  Print out an node.******************************************************************/voiddumpnode(node, base)struct node_control *node;void *base;{  if (node->marked_for_delete)  {    pindent(listindent);    printf("Deleted->");  }  if (node->sds != 0)    sds_list(node->sds,node->object,SDS_HEADER_ONLY);   else  if (base == NULL)  {    pindent(listindent);    printf("%ld objects type %ld size %ld at %lx\n",            (long)node->nelems,            (long)node->datatype,            (long)node->datasize,            (unsigned long)node->data);   }   else   {    pindent(listindent);    printf("%ld objects type %ld size %ld offset %lx\n",            (long)node->nelems,            (long)node->datatype,            (long)node->datasize,            (unsigned long)((char *)node->data - (long)base));  }  if (align_delta((sds_off_t)node->data, node->align) != 0)  {    pindent(listindent);    printf("!!! alignment error\n\n");  }}/******************************************************************     EXPORT Level 1struct node_control * sds_new_array(list_index, multiplicity, datatype)  Book space for a new array of primitive type 'datatype',  and indicated multiplicity.******************************************************************/struct node_control *sds_new_array(list_index, multiplicity, datatype)int list_index;long multiplicity;long datatype;{  struct node_control *node;  node = sds_new_data(list_index, multiplicity, datatype, 0, -1, NULL);  if (node != NULL)  {    node->sds = 0;    node->datatype = datatype;  }  else    sds_push_error(SDS_NO_SUCH_OBJ, SDS_WARNING, "Array node not created");  return node;}/******************************************************************     EXPORT Level 1int sds_downlist(list_index, node)  Declare the node to have the indicated list as its downlist.******************************************************************/intsds_downlist(list_index, node)int list_index;struct node_control *node;{  struct list_control *list;  if ((list = goodlist(list_index)) == NULL)    return 0;  if (node->down != 0)    sds_push_error(0, SDS_WARNING, "Overwriting existing downlist");  node->down = list;  return 1;}/******************************************************************struct node_control *sds_new_struct()  Ask for the node to create a structure as defined by the sds,object pair.  Data space is taken from a databank or is malloc()'d******************************************************************/struct node_control *sds_new_struct(list_index, multiplicity, sds, object)int list_index;long multiplicity;sds_handle sds,object;{  struct node_control *node = NULL;  sds_handle code;  if ((code = sds_obind2code(sds,object)) == 0)  {    char string[128];    sprintf(string,"Object %ld does not exist", (long)sds);    sds_push_error(SDS_NO_SUCH_OBJ, SDS_WARNING, string);    return node;  }  node = sds_new_data(list_index, multiplicity, code, sds, object, NULL);  if (node == NULL)    sds_push_error(SDS_NO_SUCH_OBJ, SDS_WARNING, "Structure node not created");  return node;}/******************************************************************     INTERNALstruct node_control * sds_new_data()  Creates or reserves space, or, if data is not null, registers space  as being controlled by a node.******************************************************************/struct node_control *sds_new_data(list_index, multiplicity, datatype, sds, object, data)int list_index;long multiplicity;long datatype;sds_handle sds,object;void *data;{  struct list_control *list;  struct node_control *node = NULL;  long dsize;  char align;  if ((list = goodlist(list_index)) == NULL)    return NULL;  dsize = sds_psize(datatype);  align = sds_psize(datatype);  if (data == NULL) /* must create or reserve the data space */  {    if (list->do_malloc)    {      node = sds_new_node();      if (!sds_add_node(list, node))        sds_push_error(SDS_NO_SUCH_LIST, SDS_WARNING, "Failed to add node");      node->datatype = datatype;      node->sds = sds;      node->object = object;      node->datasize = dsize;      node->align = align;      node->nelems = multiplicity;      node->data = (void *)sds_malloc(node->datasize * multiplicity);      node->data_was_malloced = 1;    }    else    {      long shift = align_delta(list->bankused, align);      if ((list->banksize - list->bankused - shift) <                           dsize * multiplicity)        sds_bank_compress(list_index);      if ((list->banksize - list->bankused  -shift) >=                          dsize * multiplicity)      {        node = sds_new_node();        if (!sds_add_node(list, node))          sds_push_error(SDS_NO_SUCH_LIST, SDS_WARNING, "Failed to add node");        node->datatype = datatype;        node->sds = sds;        node->object = object;        node->datasize = dsize;        node->align = align;        node->nelems = multiplicity;        list->bankused += shift;        node->data = (char *)list->databank + list->bankused;        list->bankused += node->datasize * multiplicity;      }

⌨️ 快捷键说明

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