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
|
/*
Copyright (C) 2006 by Jonas Kramer
Published under the terms of the GNU General Public License (GPL).
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
#include "settings.h"
#include "bookmark.h"
#include "getln.h"
#include "util.h"
#include "strary.h"
#include "globals.h"
int cmpdigit(const void *, const void *);
/* Save a stream URL with the given number as bookmark. */
void setmark(const char * streamURL, int n) {
FILE * fd;
char * bookmarks[10] = { NULL, };
if(!streamURL || n < 0 || n > 9)
return;
if((fd = fopen(rcpath("bookmarks"), "r"))) {
while(!feof(fd)) {
char * line = NULL;
unsigned size = 0, length;
int x = 0;
length = getln(& line, & size, fd);
if(line && length > 4) {
if(sscanf(line, "%d = ", & x) == 1 && !bookmarks[x]) {
bookmarks[x] = strdup(line + 4);
assert(bookmarks[x] != NULL);
}
free(line);
}
}
fclose(fd);
}
bookmarks[n] = strdup(streamURL);
assert(bookmarks[n] != NULL);
if((fd = fopen(rcpath("bookmarks"), "w"))) {
int i;
for(i = 0; i < 10; ++i)
if(bookmarks[i]) {
char * ptr = strchr(bookmarks[i], 10);
if(ptr != NULL)
* ptr = 0;
fprintf(fd, "%d = %s\n", i, bookmarks[i]);
free(bookmarks[i]);
}
fclose(fd);
if(!enabled(QUIET))
puts("Bookmark saved.");
}
}
/* Get bookmarked stream. */
char * getmark(int n) {
FILE * fd = fopen(rcpath("bookmarks"), "r");
char * streamURL = NULL;
if(!fd)
return NULL;
while(!streamURL && !feof(fd)) {
char * line = NULL;
unsigned size = 0, length;
int x;
length = getln(& line, & size, fd);
if(line && length > 4)
if(sscanf(line, "%d = ", & x) == 1 && x == n) {
char * ptr = strchr(line, 10);
if(ptr != NULL)
* ptr = 0;
streamURL = strdup(line + 4);
assert(streamURL != NULL);
}
if(size && line)
free(line);
}
fclose(fd);
return streamURL;
}
void printmarks(void) {
char ** list = slurp(rcpath("bookmarks"));
unsigned n;
if(list == NULL) {
puts("No bookmarks.");
return;
}
qsort(list, count(list), sizeof(char *), cmpdigit);
for(n = 0; list[n] != NULL; ++n)
puts(list[n]);
purge(list);
}
int cmpdigit(const void * a, const void * b) {
return (** (char **) a) - (** (char **) b);
}
|