genlist.c

来自「在ARM7和UC/OSII的平台上实现了GPS自动报站的功能,涉及GPS模块LE」· C语言 代码 · 共 907 行 · 第 1/2 页

C
907
字号
      return   NICHE_LISTPTR_INVALID;

   tmp=prev=list->head;

   /* Check if list is empty */
   if ( tmp == NULL )
   {
      return   NICHE_DEL_LIST_EMPTY ;
   }

   /* Delete all the HEAD elements that match our condition */
   do
   {
      /* Check if the element is the first one in the list */
      if ( tmp->p_data->id == id ) 
      {
         /* match found */
         list->head=tmp->next;
         GEN_FREE(tmp->p_data);
         GEN_FREE(tmp);
         del_cnt++;
      }
      else
      {
         /* Match failed */
         break;
      }
      tmp=prev=list->head;
   }  while (list->head);

      tmp=prev->next;

   while ( tmp )
   {
      if ( tmp->p_data->id == id )
      {
         /* match found */
         prev->next=tmp->next;
         GEN_FREE(tmp->p_data);
         GEN_FREE(tmp); 
         tmp=prev->next;      /* point to the next element */
         del_cnt++;
      }
      else
      {
         prev=tmp;
         tmp=tmp->next;
      }
   };

   if ( del_cnt == 0 )
   {
      return   NICHE_DEL_NOT_FOUND;
   }
   else
   {
      return   del_cnt;
   }
}


/* FUNCTION: niche_del_name()
 * 
 * Delete an element from the list.
 *
 * PARAM1: NICHELIST list - Pointer to the list in reference. (IN/OUT)
 * PARAM2: char *name - Name of the element (IN)
 *
 * REMARKS: Memory is FREE'ED to for the element, and then the element 
 * is removed from the list
 *
 * RETURNS: SUCCESS / error code
 */

int        
niche_del_name(NICHELIST list, char * name)
{
   NICHE_ELE   tmp,prev;
   int   del_cnt=0;

   if ( list == NULL )
      return   NICHE_LISTPTR_INVALID;

   tmp=prev=list->head;

   /* Check if list is empty */
   if ( tmp == NULL )
   {
      return   NICHE_DEL_LIST_EMPTY ;
   }

   /* Delete all the HEAD elements that match our condition */
   do
   {
      /* Check if the element is the first one in the list */
      if ( strcmp(tmp->p_data->name, name) == 0 )
      {
         /* match found */
         list->head=tmp->next;
         GEN_FREE(tmp->p_data);
         GEN_FREE(tmp);
         del_cnt++;
      }
      else
      {
         /* Match failed */
         break;
      }
      tmp=prev=list->head;
   }  while (list->head);

      tmp=prev->next;

   while ( tmp )
   {
      if ( strcmp(tmp->p_data->name, name) == 0 ) 
      {
         /* match found */
         prev->next=tmp->next;
         GEN_FREE(tmp->p_data);
         GEN_FREE(tmp);
         del_cnt++;
      }
      else
      {
         prev=tmp;
         tmp=tmp->next;
      }
   };

   if ( del_cnt == 0 )
   {
      return   NICHE_DEL_NOT_FOUND;
   }
   else
   {
      return   del_cnt;
   }
}


/* FUNCTION: niche_del_id_and_name()
 * 
 * Delete an element from the list.
 *
 * PARAM1: NICHELIST list - Pointer to the list in reference. (IN/OUT)
 * PARAM2: long id - ID of the element (IN)
 * PARAM3: char *name - Name of the element (IN)
 *
 * REMARKS: Memory is FREE'ED to for the element, and then the element 
 * is removed from the list
 *
 * RETURNS: SUCCESS / error code
 */

int        
niche_del_id_and_name(NICHELIST list, long id, char * name)
{
   GEN_STRUCT  tmp_ele;
   int         ret_code;

   if ( list == NULL )
      return   NICHE_LISTPTR_INVALID;

   tmp_ele  = (GEN_STRUCT) GEN_ALLOC (list->len_of_element);
   if ( tmp_ele == NULL )
   {
      ret_code =  NICHE_DEL_NOT_ENOUGH_MEMORY;
   }
   else
   {
      tmp_ele->id =  id;
      strcpy(tmp_ele->name, name);
      ret_code = niche_del(list, tmp_ele);
      GEN_FREE(tmp_ele);
   }

   return   ret_code;
}

#endif /* ifndef of V3_STATIC_TABLES */

/* FUNCTION: niche_lookup_id()
 * 
 * Lookup for the first element which has this ID.
 *
 * PARAM1: NICHELIST list - Pointer to the list in reference. (IN/OUT)
 * PARAM2: long    id - ID of the element (IN)
 *
 * RETURNS: Pointer to the element or NULL (if not found)
 */

GEN_STRUCT  
niche_lookup_id(NICHELIST list, long    id)
{
   NICHE_ELE   tmp;

   if ( list == NULL )
      return   NULL;

   tmp=list->head;
   while ( tmp )
   {
      if (tmp->p_data->id == id )
      {
         return (GEN_STRUCT)(tmp->p_data);
      }
      else
      {
         tmp=tmp->next;
      }
   }

   return   NULL;
}


/* FUNCTION: niche_lookup_name()
 * 
 * Lookup for the first element which has this name.
 *
 * PARAM1: NICHELIST list - Pointer to the list in reference. (IN/OUT)
 * PARAM2: char *name - name of the element (IN)
 *
 * RETURNS: Pointer to the element or NULL (if not found)
 */

GEN_STRUCT  
niche_lookup_name(NICHELIST list, char * name)
{
   NICHE_ELE   tmp;

   if ( list == NULL )
      return   NULL;

   tmp=list->head;
   while ( tmp )
   {
      if (strcmp(tmp->p_data->name, name) == 0 )
      {
         return (GEN_STRUCT)(tmp->p_data);
      }
      else
      {
         tmp=tmp->next;
      }
   }

   return   NULL;
}


/* FUNCTION: niche_lookup_id_and_name()
 * 
 * Lookup for the first element which has this ID,name.
 *
 * PARAM1: NICHELIST list - Pointer to the list in reference. (IN/OUT)
 * PARAM2: long    id - ID of the element (IN)
 * PARAM3: char *name - name of the element (IN)
 *
 * RETURNS: Pointer to the element or NULL (if not found)
 */

GEN_STRUCT  
niche_lookup_id_and_name(NICHELIST list, long    id, char * name)
{
   NICHE_ELE   tmp;

   if ( list == NULL )
      return   NULL;

   tmp=list->head;
   while ( tmp )
   {
      if ( ( tmp->p_data->id == id ) &&
( strcmp(tmp->p_data->name, name) == 0 ) )
      {
         return (GEN_STRUCT)(tmp->p_data);
      }
      else
      {
         tmp=tmp->next;
      }
   }

   return   NULL;
}


/* FUNCTION: niche_list_show()
 * 
 * List all elements in the list
 *
 * PARAM1: NICHELIST list - Pointer to the list in reference. (IN/OUT)
 *
 * RETURNS: SUCCESS / error code
 */

int niche_list_show(NICHELIST list)
{
   NICHE_ELE   tmp;
   int   cnt=0;

   if ( list == NULL )
      return   NICHE_LISTPTR_INVALID;

   tmp=list->head;
   while ( tmp )
   {
      niche_element_show(tmp->p_data);
      tmp=tmp->next;
      cnt++;
   }

   if ( cnt == 0 )
      Printu_Net("List is empty.\n");
   else
      Printu_Net("List length =%d.\n", cnt);

   return   SUCCESS;
}


/* FUNCTION: niche_list_len()
 * 
 * 
 *
 * PARAM1: NICHELIST list - Pointer to the list in reference. (IN/OUT)
 *
 * RETURNS: SUCCESS / error code
 */

int niche_list_len(NICHELIST list)
{
   NICHE_ELE   tmp;
   int   cnt=0;

   if ( list == NULL )
      return   NICHE_LISTPTR_INVALID;

   tmp=list->head;
   while ( tmp )
   {
      cnt++;
      tmp=tmp->next;
   }

   return   cnt;
}


/* FUNCTION: niche_list_getat()
 * 
 * 
 *
 * PARAM1: NICHELIST list - Pointer to the list in reference. (IN/OUT)
 * PARAM2: int index - Position of the item to be found. (0 based)
 *
 * RETURNS: Pointer to data (element at INDEX) or NULL
 */

GEN_STRUCT niche_list_getat(NICHELIST list, int index)
{
   NICHE_ELE   tmp;
   int   cnt=0;

   if ( list == NULL )
      return   NULL;

   tmp=list->head;
   while ( tmp )
   {
      if ( cnt == index )
      {
         return   tmp->p_data;
      }
      cnt++;
      tmp=tmp->next;
   }

   /* We don't have so many elements in the list */
   return   NULL;
}


/* FUNCTION: niche_element_show()
 * 
 * Display the values of an element.
 *
 * PARAM1: GEN_STRUCT ptr_data - Pointer to the element. (IN)
 *
 * RETURNS: SUCCESS / error code
 */

int niche_element_show(GEN_STRUCT ptr_data)
{
   if ( ptr_data == NULL )
      return   FAILURE;

   Printu_Net("Id= %ld, name=%s\n", ptr_data->id,ptr_data->name);

   return   SUCCESS;
}

int         niche_lookup_multi_match(NICHELIST  list, long id, char * name, 
             GEN_STRUCT *   matches);
/* FUNCTION: niche_lookup_multi_match()
 * 
 * Lookup for the first element which has this ID,name.
 * Check the whole list , and return all the entries 
 * where this condition is matched.
 *
 * PARAM1: NICHELIST list - Pointer to the list in reference. (IN/OUT)
 * PARAM2: long id - ID of the element (IN)
 * PARAM3: char *name - name of the element (IN)
 * PARAM4: GEN_STRUCT matches[]
 * An array of pointers to hold the MATCHED entries
 * Maximum size of this array is assumed  to GEN_MAX_ARRAY
 *
 * RETURNS: Number of entries matched.
 */

int  
niche_lookup_multi_match(NICHELIST list, long id, char * name, 
   GEN_STRUCT  matches[])
{
   NICHE_ELE   tmp;
   int   cnt=0;

   if ( list == NULL )
      return   0;

   if ( matches == NULL )
      return   0;

   tmp=list->head;
   while ( tmp )
   {
      if ( ( tmp->p_data->id == id ) &&
         ( strcmp(tmp->p_data->name, name) == 0 ) )
      {
         matches[cnt]=tmp->p_data   ;
         cnt++;
      }
      tmp=tmp->next;
   }

   return   cnt;
}

#endif   /* USE_GENLIST */

⌨️ 快捷键说明

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