/*****************************************************************************/ /* */ /* UNIT: NTL1_Sort_Plain (Level 1 library routine) */ /* */ /* Author: Nikola Stojanovic */ /* */ /* Revision: 07 APR 97 Version 1.0 */ /* */ /* Function: */ /* */ /* Procedure receives several lists of records loaded from plain form */ /* landmark files and integrates them into a single list, sorted by */ /* increasing starting positions; returns the error structure if there were */ /* any errors encountered, NULL if everything was OK */ /* */ /* NOTE: Procedure is destructive for the received list of lists! */ /* */ /*****************************************************************************/ #include #include #include #include "ntl1.h" /*****************************************************************************/ /* */ /* Definitions section */ /* */ /*****************************************************************************/ /*****************************************************************************/ /* Definitions of local constants of the unit */ /*****************************************************************************/ /* Limit for the length of an error message that can be assembled */ #define ERR_MSGLIMIT 128 /*****************************************************************************/ /* Prototypes of all locally used functions of this unit */ /*****************************************************************************/ errind NTL1_SP_Insert_List (plain_ptr source, plain_ptr *target); plain_ptr NTL1_SP_Reverse_List (plain_ptr list); errind NTL1_SP_Assemble_Error (int severity, int code, char *comment, int description); /*****************************************************************************/ /* Definitions of global (static) variables of the unit */ /*****************************************************************************/ static char Error_Message [ERR_MSGLIMIT]; /* Temporary buffer, error passing */ /*****************************************************************************/ /* */ /* Code section */ /* */ /*****************************************************************************/ /*****************************************************************************/ /* */ /* Procedure: NTL1_Sort_Plain */ /* */ /* Main (interface) procedure of this unit */ errind NTL1_Sort_Plain (plist_ptr *lists, plain_ptr *sorted) { errind erret; plist_ptr current_list; /* Initialize the list of sorted records to return to "empty" first */ *sorted = NULL; /* Loop for all lists provided in the input to insert them to sorted list */ while (*lists != NULL) { current_list = *lists; *lists = (*lists) -> next; if ((erret = NTL1_SP_Insert_List (current_list -> lines, sorted)) != NULL) { *sorted = NTL1_Destroy_Plain_List (*sorted); return erret; } free (current_list); } *sorted = NTL1_SP_Reverse_List (*sorted); return NULL; } /*****************************************************************************/ /* */ /* Procedure: NTL1_SP_Insert_List */ /* */ errind NTL1_SP_Insert_List (plain_ptr source, plain_ptr *target) { plain_ptr item, scan; /* Loop for each record in the new list, to place it at the right spot */ while (source != NULL) { item = source; source = source -> next; item -> next = NULL; if (*target == NULL) *target = item; else if ((*target) -> start <= item -> start) { item -> next = *target; *target = item; } else { scan = *target; while ((scan -> next != NULL) && ((scan -> next) -> start > item -> start)) scan = scan -> next; item -> next = scan -> next; scan -> next = item; } } return NULL; } /*****************************************************************************/ /* */ /* Procedure: NTL1_SP_Reverse_List */ /* */ plain_ptr NTL1_SP_Reverse_List (plain_ptr list) { plain_ptr item, result; result = NULL; while (list != NULL) { item = list; list = list -> next; item -> next = result; result = item; } return result; } /*****************************************************************************/ /* */ /* Procedure: NTL1_SP_Assemble_Error */ /* */ /* Service procedure for assembling and returnning an error report, based on */ /* the values of the input parameters; returns the record with the report */ errind NTL1_SP_Assemble_Error (int severity, int code, char *comment, int description) { char *report; errind assembled; report = (char *) NTL0_ckalloc ( (strlen (comment) + strlen ("_Sort_Plain_List: ") + 1) * sizeof (char)); sprintf (report, "_Sort_Plain_List: %s", comment); assembled = NTL1_Error_Record (severity, code, report, description); free (report); return assembled; }