Multiple timers can be implemented.
The followingCodeIs the interface of the timer Library:Timer. h
# Ifndef timer_h
# Define Timer_h
# Include <time. h>
Typedef Struct Mytimer;
Typedef Struct Mytimer * timer;
Struct Mytimer
{
Clock_t base_tm;
Float Di1__second;
};
Typedef Int Timerid;
Typedef Enum Timer_errors;
Enum Timer_errors
{
Timerok,
Timerbadid,
Timeroutofmem,
Timeravoiddistm,
Timernosucherrcode
};
/* ***************************
* Initlize a new timer.
* Input: distm: The timer's limitation.
* Output: timer's ID.
* Used:
***************************** */
Timerid initlize_timer ( Float Distm );
/* ***************************
* Judge the timer is over or not.
* Input: ID: the timer.
* Output: 1 if the timer is overtimer Otheiwise 0.
* Used:
***************************** */
Int Is_timer_over (timerid ID );
/* ***************************
* Reset the timer, the timer begin to time again.
* Input: ID: the timer.
* Output: Nothing.
* Used :...
***************************** */
Void Reset_timer (timerid ID );
/* ***************************
* Get the timer's di1__second.
* Input: ID: the timer.
* Output: The timer's ditm_second.
* Used :...
***************************** */
Float Get_timer_distm (timerid ID );
/* ***************************
* Set the timer's di1__second.
* Input: ID: the timer.
* Output: Nothing.
* Used :...
***************************** */
Void Set_timer_distm (timerid ID, Float Newdistm );
/* ***************************
* Ger the recent timer's result.
* Input: Nothing.
* Output: timer_errors's code.
* Used :...
***************************** */
Timer_errors timer_result (Void );
/* ***************************
* Get the recent timer's message.
* Input: Nothing
* Output: a char poniter to a const string.
* Used :....
***************************** */
Char * Timer_msg (timer_errors ERR );
# Endif
Library implementation timer. c
# Include " Timer. h "
# Include <stdlib. h>
Static Int Timercount = 0 ;
Static Timer * alltimer = NULL;
Static Int Nowpos =- 1 ;
Static Const Int Increase = 10 ;
Static Timer_errors timer_error = timerok;
Static Const Char * Error_msg [] =
{
" Timer is OK. " ,
" No such timer, please check it. " ,
" Initlize timer fail, beacuse of not enough memory. " ,
" A avoid distm_second, it shoshould be a postive float. " ,
" No such error message "
};
Timerid initlize_timer ( Float Distm)
{
Timer * TMP;
Timer newtimer;
If (Distm < 0 )
{
Timer_error = timeravoiddistm;
Return - 1 ;
}
Newtimer = (timer) malloc ( Sizeof (Mytimer ));
If (Newtimer = NULL)
{
Timer_error = timeroutofmem;
Return - 1 ;
}
Newtimer-> di1__second = distm;
Newtimer-> base_tm = clock ();
Nowpos ++;
If (Nowpos> = timercount)
{
TMP = (timer *) realloc (( Void *) Alltimer, (unsigned Int ) (Timercount + increase ));
If (TMP = NULL)
{
Timer_error = timeroutofmem;
Free (newtimer );
Return - 1 ;
}
Alltimer = TMP;
Timercount = timercount + increase;
}
Alltimer [nowpos] = newtimer;
Timer_error = timerok;
Return (Timerid) nowpos;
}
Int Is_timer_over (timerid ID)
{
Timer P;
Clock_t nowclock = clock ();
If (ID < 0 | ID> nowpos)
{
Timer_error = timerbadid;
Return - 1 ;
}
P = alltimer [ID];
If (Nowclock-p-> base_tm)> (P-> di1__second) * clocks_per_sec ))
{
P-> base_tm = nowclock;
Return 1 ;
}
Else
Return 0 ;
}
Void Reset_timer (timerid ID)
{
Timer P;
Clock_t nowclock;
If (ID < 0 | ID> nowpos)
{
Timer_error = timerbadid;
Return ;
}
P = alltimer [ID];
Nowclock = clock ();
P-> base_tm = nowclock;
}
Float Get_timer_distm (timerid ID)
{
Timer P;
If (ID < 0 | ID> nowpos)
{
Timer_error = timerbadid;
Return - 1 ;
}
P = alltimer [ID];
Return P-> di1__second;
}
Void Set_timer_distm (timerid ID, Float Newdistm)
{
Timer P;
If (Newdistm < 0 )
{
Timer_error = timeravoiddistm;
Return ;
}
If (ID < 0 | ID> nowpos)
{
Timer_error = timerbadid;
Return ;
}
P = alltimer [ID];
P-> di1__second = newdistm;
}
Timer_errors timer_result ( Void )
{
Return Timer_error;
}
Char * Timer_msg (timer_errors ERR)
{
If (( Int ) Err < 0 | ( Int ) Err> 3 )
Err = timernosucherrcode;
Return ( Char *) Error_msg [( Int ) Err];
}