-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstopwatch.c
109 lines (99 loc) · 2.18 KB
/
stopwatch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "stopwatch.h"
#include "logging.h"
typedef struct STOPWATCH_INFO_TAG* STOPWATCH_HANDLE;
typedef struct STOPWATCH_INFO_TAG
{
clock_t start_time;
clock_t stop_time;
int started;
} STOPWATCH_INFO;
STOPWATCH_HANDLE stopwatch_create()
{
STOPWATCH_INFO* result = (STOPWATCH_INFO*)malloc(sizeof(STOPWATCH_INFO));
if (result == NULL)
{
LogError("FAILURE: unable to allocate stopwatch info");
result = NULL;
}
else
{
memset(result, 0, sizeof(STOPWATCH_INFO));
}
return result;
}
void stopwatch_destroy(STOPWATCH_HANDLE handle)
{
if (handle != NULL)
{
free(handle);
}
}
int stopwatch_start(STOPWATCH_HANDLE handle)
{
int result;
if (handle == NULL)
{
LogError("FAILURE: Invalid handle specified on start");
result = __LINE__;
}
else
{
if (handle->started != 0)
{
LogError("FAILURE: Cannot start a inprogress timer");
result = __LINE__;
}
else
{
handle->started = 1;
handle->start_time = clock();
handle->stop_time = clock();
}
result = 0;
}
return result;
}
void stopwatch_stop(STOPWATCH_HANDLE handle)
{
if (handle != NULL)
{
handle->started = 0;
handle->stop_time = clock();
}
}
void stopwatch_reset(STOPWATCH_HANDLE handle)
{
if (handle != NULL)
{
handle->start_time = clock();
handle->stop_time = clock();
}
}
clock_t stopwatch_get_elapsed(STOPWATCH_HANDLE handle)
{
clock_t result;
if (handle == NULL)
{
LogError("FAILURE: Invalid handle specified on start");
result = __LINE__;
}
else
{
// If still in progress
if (handle->started != 0)
{
result = clock() - handle->start_time;
}
else
{
result = handle->stop_time - handle->start_time;
}
}
return result;
}