/
SubTitles.c
142 lines (118 loc) · 4.01 KB
/
SubTitles.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include "hashmap.h"
void free_matrix(char **c, int n);
char* substring(const char* str, size_t begin, size_t len)
{
return strndup(str + begin, len);
}
HashMap *InsertSubsToHashTable(char *fullFileNamePath, int allSubsLinesLength)
{
int i, endOfLinePos = 0, line_number = 0, firstTextLineIndex = 0;
char line_buffer[BUFSIZ]; /* BUFSIZ is defined if you include stdio.h */
FILE *infile;
char endOfLine[] = "\n";
HashMap *hm;
char *shours, *sminutes, *sseconds, *smiliSeconds, scurTotlaTime[100];
char *ehours, *eminutes, *eseconds, *emiliSeconds, ecurTotlaTime[100];
scurTotlaTime[0] = 0;
ecurTotlaTime[0] = 0;
char subLines[(BUFSIZ * 2) + 50], **strArr;
infile = fopen(fullFileNamePath, "r");
if(!infile)
{
g_error("Couldn't' open file %s for reading", fullFileNamePath);
}
//Count number of subtitles in .srt files
while(fgets(line_buffer, sizeof(line_buffer), infile))
{
++line_number;
}
fclose(infile);
hm = hm_new(line_number);
if (hm == NULL)
{
g_error("Could'nt allocate hash table");
}
int lineIndex = 0;
infile = fopen(fullFileNamePath, "r");
if(!infile)
{
g_error("Couldn't' open file %s for reading", fullFileNamePath);
}
strArr = (char**)malloc((line_number + 1) * sizeof(char*));
for(i = 0; i < line_number + 1; i++)
{
strArr[i] = (char*)malloc((BUFSIZ + 1) * sizeof(char));
}
while(fgets(line_buffer, sizeof(line_buffer), infile))
{
strcpy(strArr[lineIndex], line_buffer);
lineIndex++;
}
fclose(infile);
for(i = 0; i < lineIndex; i++)
{
endOfLinePos = strcspn(strArr[i], endOfLine) + 1;
if(endOfLinePos > 8)
{
if(strcmp(substring(strArr[i], 2, 1), ":") == 0)
{
shours = substring(strArr[i], 0, 2);
sminutes = substring(strArr[i], 3, 2);
sseconds = substring(strArr[i], 6, 2);
smiliSeconds = substring(strArr[i], 9, 2);
firstTextLineIndex = i;
strcat(scurTotlaTime, shours);
strcat(scurTotlaTime, ":");
strcat(scurTotlaTime, sminutes);
strcat(scurTotlaTime, ":");
strcat(scurTotlaTime, sseconds);
strcat(scurTotlaTime, ":");
strcat(scurTotlaTime, smiliSeconds);
if(strcmp(substring(strArr[i], 13, 3), "-->") == 0)
{
ehours = substring(strArr[i], 13, 6);
eminutes = substring(strArr[i], 20, 2);
eseconds = substring(strArr[i], 23, 2);
emiliSeconds = substring(strArr[i], 26, 2);
strcat(ecurTotlaTime, ehours);
strcat(ecurTotlaTime, ":");
strcat(ecurTotlaTime, eminutes);
strcat(ecurTotlaTime, ":");
strcat(ecurTotlaTime, eseconds);
strcat(ecurTotlaTime, ":");
strcat(ecurTotlaTime, emiliSeconds);
hm_put(hm, ecurTotlaTime, ""); //insert end time
}
strcat(subLines, strArr[firstTextLineIndex + 1]);
strcat(subLines, strArr[firstTextLineIndex + 2]);
hm_put(hm, scurTotlaTime, subLines); //insert start time
strcpy(scurTotlaTime, ""); //clean string
strcpy(ecurTotlaTime, ""); //clean string
strcpy(subLines, ""); //clean string
}
}
}
free(shours);
free(sminutes);
free(sseconds);
free(smiliSeconds);
free(ehours);
free(eminutes);
free(eseconds);
free(emiliSeconds);
free_matrix(strArr, line_number - 1);
return hm;
}
void free_matrix(char **c, int n)
{
int i;
for(i = 0; i < n; i++)
{
free(c[i]);
}
free(c);
}