forked from YogeshJangir/token-bucket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken-helper.c
More file actions
70 lines (66 loc) · 2.02 KB
/
Copy pathtoken-helper.c
File metadata and controls
70 lines (66 loc) · 2.02 KB
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
//
// warmup2-helper.c
// Warmup2-new
//
// Created by Abhishek Trigunayat on 6/12/14.
// Copyright (c) 2014 Abhishek Trigunayat. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "mylist.h"
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>
#include "token.h"
float calculate_time_difference(struct timeval time1, struct timeval time2){
long seconds = time1.tv_sec - time2.tv_sec;
long useconds = time1.tv_usec - time2.tv_usec;
if(useconds < 0){
seconds = seconds - 1;
useconds = useconds + 1000000;
}
long total = (seconds * 1000000) + useconds;
float f = total / 1000.00;
//printf("\n%ld", total);
//printf("\n%.3fms",f);
return f;
}
void digest_file(FILE * fp){
char buf[1026];
long num_of_packets = 0;
if(fgets(buf, sizeof(buf), fp) != NULL){
num_of_packets = atol((buf));
}
total_number_of_packets = num_of_packets;
tsfile_data = (long **)malloc(num_of_packets * sizeof(long *));
long packet_count = 0;
while(fgets(buf, sizeof(buf), fp) != NULL) {
if(buf[0] == ' ' || buf[0] == '\t' || buf[strlen(buf)-2] == ' ' || buf[strlen(buf)-2] == '\t'){
fprintf(stderr,"Trailing space or tab found in a line of the file\n");
exit(1);
}
long * record = NULL;
record = (long *) malloc(3 * sizeof(long));
int j=0;
char *start_ptr = buf,*tab_ptr = NULL;
int i=0;
for(i=0;i<2;i++){
tab_ptr = strchr(start_ptr, '\t');
if(tab_ptr == NULL){
tab_ptr = strchr(start_ptr, ' ');
}
if (tab_ptr != NULL) {
*tab_ptr++ = '\0';
while(*tab_ptr == ' ' || *tab_ptr == '\t')tab_ptr++;
*(record + j) = atol(start_ptr);
j++;
}
start_ptr = tab_ptr;
}
*(record + j) = atol(tab_ptr);
*(tsfile_data + packet_count) = record;
packet_count++;
}
}