-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbookmarks.c
114 lines (93 loc) · 3.01 KB
/
bookmarks.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
/*
Copyright (c) 2019 Colum Paget <colums.projects@googlemail.com>
* SPDX-License-Identifier: GPL-3.0
*/
#include "bookmarks.h"
#define MAX_LINE 1024
void SaveBookmark(const char *url, xine_stream_t *stream)
{
FILE *outf, *inf;
char *Line=NULL, *InPath=NULL, *OutPath=NULL, *Tempstr=NULL;
const char *ptr;
int val, pos, size;
//if no url get the hell out of here!
if (! url) return;
if (stream)
{
//if it's not currently playing then we either got to the end, or haven't started
//either way there's no point saving a bookmark
if (! (Config->state & STATE_PLAYING)) return;
//if it's not a seekable stream, don't save a bookmark
if (! xine_get_stream_info(Config->stream, XINE_STREAM_INFO_SEEKABLE)) return;
}
OutPath=rstrcpy(OutPath, xine_get_homedir());
OutPath=rstrcat(OutPath, "/.cxine/cxine.bookmarks+");
outf=fopen(OutPath, "w");
//first copy all bookmarks that *aren't* our target bookmark. This ensures there
//should only be one entry for a given url
if (outf)
{
InPath=rstrcpy(InPath, xine_get_homedir());
InPath=rstrcat(InPath, "/.cxine/cxine.bookmarks");
inf=fopen(InPath, "r");
if (inf)
{
Line=(char *) calloc(MAX_LINE, sizeof(char));
while (fgets(Line, MAX_LINE, inf))
{
Line=xine_chomp(Line);
//xine_chomp doesn't strip lines that are just '\n' or '\r\n', so
//we have to operate on a minium size that a line can be, and that's
//3 bytes
if (StrLen(Line) > 2)
{
ptr=rstrtok(Line, " ", &Tempstr);
if (ptr && strcmp(ptr, url) !=0) fprintf(outf, "%s\n", Line);
}
}
fclose(inf);
}
//if stream is NULL we don't write a bookmark to the file. This can be used to delete
//an existing bookmark for a stream
if (stream)
{
xine_get_pos_length (stream, &val, &pos, &size);
if ((size - pos) > 20000) fprintf(outf, "%d %s\n", pos, url);
}
fclose(outf);
rename(OutPath, InPath);
}
free(Tempstr);
free(OutPath);
free(InPath);
free(Line);
}
int LoadBookmark(const char *url)
{
FILE *inf;
char *Line=NULL, *Tempstr=NULL;
const char *ptr;
int RetVal=0;
Tempstr=rstrcpy(Tempstr, xine_get_homedir());
Tempstr=rstrcat(Tempstr, "/.cxine/cxine.bookmarks");
inf=fopen(Tempstr, "r");
if (inf)
{
Line=calloc(MAX_LINE, sizeof(char));
while (fgets(Line, MAX_LINE, inf))
{
Line=xine_chomp(Line);
if (StrLen(Line))
{
ptr=rstrtok(Line, " ", &Tempstr);
if (strcmp(ptr, url) ==0) RetVal=atoi(Tempstr);
}
}
fclose(inf);
}
//delete bookmark now we've used it
SaveBookmark(url, NULL);
destroy(Tempstr);
destroy(Line);
return(RetVal);
}