htex

simple incorrect html parser
git clone git://git.relim.de/htex.git
Log | Files | Refs | README

misc.c (2373B)


      1 #include <stdio.h>
      2 #include <stdint.h>
      3 #include <stdlib.h>
      4 #include <stdbool.h>
      5 #include <string.h>
      6 #include <grapheme.h>
      7 #include "misc.h"
      8 
      9 char *cp_to_string(uint_least32_t cp, size_t len)
     10 {
     11 	char *str = malloc((len+1) * sizeof(char));
     12 	grapheme_encode_utf8(cp, str, len);
     13 	str[len] = 0;
     14 	return str;
     15 }
     16 
     17 char *string_concat(char *str1, char *str2)
     18 {
     19     size_t len2 = strlen(str2);
     20     if (str1 == NULL) {
     21         str1 = realloc(str1, (len2+1) * sizeof(char));
     22         strcpy(str1, str2);
     23     } else {
     24         str1 = realloc(str1, (strlen(str1)+len2+1) * sizeof(char));
     25         strcat(str1, str2);
     26     }
     27     free(str2);
     28     return str1;
     29 }
     30 
     31 char *string_trim(char *text)
     32 {
     33 	char *trimmed_text = NULL;
     34 	int begin = 0;
     35 	int end = 0;
     36     size_t len = strlen(text);
     37 	for (int i=0; i<len; i++) {
     38 		if (
     39 			text[i] == ' ' ||
     40             text[i] == '\n' ||
     41             text[i] == '\t' ||
     42             text[i] == '\r'
     43 		)
     44 			begin++;
     45 		else
     46 			break;
     47 	}
     48 	for (int i=len-1; i>=0; i--) {
     49 		if (
     50 			text[i] == ' '||
     51 			text[i] == '\n' ||
     52 			text[i] == '\t' ||
     53 			text[i] == '\r'
     54 		)
     55 			end++;
     56 		else
     57 			break;
     58 	}
     59 	int k = 0;
     60 	for (int i=0; i<len; i++) {
     61 		if (i >= begin && i < len - end) {
     62 			trimmed_text = realloc(trimmed_text, (k+1) * sizeof(char));
     63 			trimmed_text[k] = text[i];
     64 			k++;
     65 		}
     66 	}
     67 	trimmed_text = realloc(trimmed_text, (k+1) * sizeof(char));
     68 	trimmed_text[k] = 0;
     69 	return trimmed_text;
     70 }
     71 
     72 bool string_starts_with(const char *string, const char *part)
     73 {
     74     size_t part_len = strlen(part);
     75     if (part_len > strlen(string))
     76         return false;
     77     for (int i=0; i<part_len; i++) {
     78         if (string[i] != part[i])
     79             return false;
     80     }
     81     return true;
     82 }
     83 
     84 bool string_is_empty(char *string)
     85 {
     86     if (string && string[0] != 0) {
     87         return false;
     88     }
     89     return true;
     90 }
     91 
     92 // Do not use for reading from a socket fd
     93 bool file_try_read(char *buf, FILE *fp)
     94 {
     95 	size_t bytes_read = fread(buf, 1, 1, fp);
     96 	if (feof(fp) != 0)
     97 		return false;
     98 	if (ferror(fp) != 0)
     99 		file_try_read(buf, fp);
    100 	if (bytes_read != 1)
    101 		file_try_read(buf, fp);
    102 	return true;
    103 }
    104 
    105 char *file_read(FILE *fp)
    106 {
    107 	char *text = NULL;
    108 	int i = 0;
    109 	char buf;
    110 	while (1) {
    111 		if (file_try_read(&buf, fp)) {
    112 			text = realloc(text, (i+1) * sizeof(char));
    113 			text[i] = buf;
    114 			i++;
    115 		} else {
    116 			break;
    117         }
    118 	}
    119 	text = realloc(text, (i+1) * sizeof(char));
    120 	text[i] = 0;
    121 	return text;
    122 }