-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommon.c
executable file
·126 lines (99 loc) · 2.65 KB
/
common.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
#include "common.h"
#include "download.h"
char *FileTypes[]={".flv",".mp3",".ogg",".aac",".mp4",".mov",".wma",".m4a",".m4v",".wmv",".webm",".avi",".3gp","m3u8",NULL};
char *Username=NULL, *Password=NULL;
char *ProgName=NULL, *CmdLine=NULL;
STREAM *StdIn=NULL;
int Flags=0;
char *BuildURL(char *RetStr, const char *Parent, const char *SubItem)
{
char *Proto=NULL, *Host=NULL, *Port=NULL, *Path=NULL;
char *BasePath=NULL;
ParseURL(Parent,&Proto,&Host,&Port,NULL,NULL,&Path,NULL);
if (StrValid(Port)) BasePath=FormatStr(BasePath, "%s://%s:%s/", Proto,Host,Port);
else BasePath=FormatStr(BasePath, "%s://%s/", Proto,Host);
//if it starts with '/' then we don't append to existing path
if (*SubItem=='/') RetStr=MCopyStr(RetStr, BasePath, SubItem, NULL);
else RetStr=MCopyStr(RetStr, BasePath, Path, "/", SubItem, NULL);
Destroy(Proto);
Destroy(Host);
Destroy(Port);
Destroy(Path);
Destroy(BasePath);
return(RetStr);
}
char *FileTypeFromURL(char *URL)
{
static char *Tempstr=NULL;
char *ptr;
Tempstr=CopyStr(Tempstr,URL);
ptr=strrchr(Tempstr,'?');
if (ptr) *ptr='\0';
ptr=strrchr(Tempstr,'.');
if (ptr) ptr++;
return(ptr);
}
char *ItemCodeFromFileExtension(char *RetBuf, const char *Default, const char *URL)
{
char *Tempstr=NULL, *Path=NULL, *RetStr=NULL, *start, *ptr;
Path=CopyStr(Path, URL);
ptr=strrchr(Path, '?');
if (ptr) *ptr='\0';
//we don't want to wind up treating part of a server name or an ip address
//as a file extension, so here we sure that we're beyond any '/' or ':'
start=Path;
ptr=strchr(start, ':');
while (ptr)
{
start=ptr+1;
ptr=strchr(start, ':');
}
ptr=strchr(start, '/');
while (ptr)
{
start=ptr+1;
ptr=strchr(start, '/');
}
ptr=strrchr(start,'.');
if (ptr)
{
ptr++;
RetStr=MCopyStr(RetBuf,"item:",ptr,NULL);
}
else RetStr=CopyStr(RetBuf,Default);
Destroy(Tempstr);
return(RetStr);
}
void VarsAddDownloadItem(const char *ItemName, const char *URL, ListNode *Vars, int AddFlags)
{
const char *ptr;
char *Token=NULL;
//Do this without disturbing ptr, as we must return ptr
ptr=ItemName;
if (AddFlags & EXTRACT_GUESSTYPE)
{
Token=ItemCodeFromFileExtension(Token, ItemName, URL);
if (StrValid(Token)) ptr=Token;
}
SetVar(Vars,ptr,URL);
if (Flags & FLAG_DEBUG2) fprintf(stderr,"Extracted Item: [%s] [%s]\n",ptr,URL);
Destroy(Token);
}
int CheckForKeyboardInput()
{
char *Tempstr=NULL;
int result=FALSE;
if (STREAMCheckForBytes(StdIn))
{
Tempstr=STREAMReadLine(Tempstr,StdIn);
StripTrailingWhitespace(Tempstr);
if (StrValid(Tempstr))
{
ListAddItem(DownloadQueue,CopyStr(NULL,Tempstr));
if (! (Flags & FLAG_QUIET)) fprintf(stderr,"\r\nQUEUED: %s\n",Tempstr);
result=TRUE;
}
}
Destroy(Tempstr);
return(result);
}