Work in progress.
This commit is contained in:
parent
b2435720c1
commit
d0697da15e
46
inplace_url.h
Normal file
46
inplace_url.h
Normal file
@ -0,0 +1,46 @@
|
||||
// Written by retoor@molodetz.nl
|
||||
|
||||
// This source code extracts URLs from an input string and replaces them inline with their respective content in Markdown style.
|
||||
|
||||
// Imports used: regex.h for regular expression operations, http.h and url.h for HTTP and URL-related utility functions.
|
||||
|
||||
// MIT License
|
||||
|
||||
|
||||
#include <regex.h>
|
||||
#include "http.h"
|
||||
#include "url.h"
|
||||
|
||||
void extract_urls(const char *input, char **urls, int *url_count) {
|
||||
const char *pattern = "https?://[^ ]+";
|
||||
regex_t regex;
|
||||
regcomp(®ex, pattern, REG_EXTENDED);
|
||||
regmatch_t match;
|
||||
const char *cursor = input;
|
||||
|
||||
*url_count = 0;
|
||||
while (!regexec(®ex, cursor, 1, &match, 0)) {
|
||||
int length = match.rm_eo - match.rm_so;
|
||||
urls[*url_count] = strndup(cursor + match.rm_so, length);
|
||||
cursor += match.rm_eo;
|
||||
(*url_count)++;
|
||||
}
|
||||
|
||||
regfree(®ex);
|
||||
}
|
||||
|
||||
void inplace_urls_markdown_style(char *input, char **urls, char **contents, int url_count) {
|
||||
for (int i = 0; i < url_count; i++) {
|
||||
char *found = strstr(input, urls[i]);
|
||||
if (found) {
|
||||
char *new_text = (char *)malloc(strlen(input) + strlen(contents[i]) - strlen(urls[i]) + 1);
|
||||
strncpy(new_text, input, found - input);
|
||||
new_text[found - input] = '\0';
|
||||
strcat(new_text, contents[i]);
|
||||
strcat(new_text, found + strlen(urls[i]));
|
||||
|
||||
free(input);
|
||||
input = new_text;
|
||||
}
|
||||
}
|
||||
}
|
85
url.h
Normal file
85
url.h
Normal file
@ -0,0 +1,85 @@
|
||||
// Written by retoor@molodetz.nl
|
||||
|
||||
// This code defines a URL parser in C that extracts components such as scheme, hostname, port, path, and query from a given URL
|
||||
|
||||
// Includes:
|
||||
// - <stdio.h> for standard I/O operations
|
||||
// - <string.h> for string manipulation functions
|
||||
// - <stdlib.h> for memory allocation and management
|
||||
|
||||
// MIT License
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
#ifndef R_URL_H
|
||||
#define R_URL_H
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
char scheme[16];
|
||||
char hostname[256];
|
||||
char port[8];
|
||||
char path[512];
|
||||
char query[512];
|
||||
} url_t;
|
||||
|
||||
int parse_url(const char *url, url_t *parsed_url) {
|
||||
memset(parsed_url, 0, sizeof(url_t));
|
||||
|
||||
const char *scheme_end = strstr(url, "://");
|
||||
if (!scheme_end) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
strncpy(parsed_url->scheme, url, scheme_end - url);
|
||||
parsed_url->scheme[scheme_end - url] = '\0';
|
||||
|
||||
const char *hostname_start = scheme_end + 3;
|
||||
const char *port_start = strchr(hostname_start, ':');
|
||||
const char *path_start = strchr(hostname_start, '/');
|
||||
const char *query_start = strchr(hostname_start, '?');
|
||||
|
||||
if (port_start && (!path_start || port_start < path_start)) {
|
||||
size_t hostname_length = port_start - hostname_start;
|
||||
if (hostname_length >= sizeof(parsed_url->hostname)) {
|
||||
fprintf(stderr, "Hostname is too long\n");
|
||||
return -1;
|
||||
}
|
||||
strncpy(parsed_url->hostname, hostname_start, hostname_length);
|
||||
parsed_url->hostname[hostname_length] = '\0';
|
||||
|
||||
size_t port_length = query_start ? (size_t)(query_start - port_start - 1) : strlen(port_start + 1);
|
||||
if (port_length >= sizeof(parsed_url->port)) {
|
||||
fprintf(stderr, "Port value is too long\n");
|
||||
return -1;
|
||||
}
|
||||
strncpy(parsed_url->port, port_start + 1, port_length);
|
||||
parsed_url->port[port_length] = '\0';
|
||||
} else {
|
||||
size_t hostname_length = path_start ? (size_t)(path_start - hostname_start) :
|
||||
(query_start ? (size_t)(query_start - hostname_start) : strlen(hostname_start));
|
||||
if (hostname_length >= sizeof(parsed_url->hostname)) {
|
||||
fprintf(stderr, "Hostname is too long\n");
|
||||
return -1;
|
||||
}
|
||||
strncpy(parsed_url->hostname, hostname_start, hostname_length);
|
||||
parsed_url->hostname[hostname_length] = '\0';
|
||||
}
|
||||
|
||||
if (path_start) {
|
||||
if (query_start) {
|
||||
strncpy(parsed_url->path, path_start, query_start - path_start);
|
||||
parsed_url->path[query_start - path_start] = '\0';
|
||||
} else {
|
||||
strcpy(parsed_url->path, path_start);
|
||||
}
|
||||
}
|
||||
|
||||
if (query_start) {
|
||||
strcpy(parsed_url->query, query_start + 1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user