File size: 2,596 Bytes
4559903 | 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 128 129 130 131 132 133 134 135 136 137 138 139 | /***************************************************************************//**
@file string.c
@author Stephen Brennan
@date Created Friday, 10 July 2015
@brief Libstephen string utilities!
@copyright Copyright (c) 2015-2016, Stephen Brennan. Released under the
Revised BSD License. See LICENSE.txt for details.
*******************************************************************************/
#include <stdio.h>
#include <wchar.h>
#include "libstephen/ll.h"
#include "libstephen/cb.h"
char *read_file(FILE *f)
{
cbuf cb;
int c;
cb_init(&cb, 1024);
while ((c = fgetc(f)) != EOF) {
cb_append(&cb, c);
}
cb_trim(&cb);
return cb.buf;
}
wchar_t *read_filew(FILE *f)
{
wcbuf wcb;
wint_t wc;
wcb_init(&wcb, 1024);
while ((wc = fgetwc(f)) != WEOF) {
wcb_append(&wcb, wc);
}
wcb_trim(&wcb);
return wcb.buf;
}
char *read_line(FILE *file)
{
cbuf cb;
int c;
cb_init(&cb, 256);
while ((c = fgetc(file)) != EOF && c != '\n') {
cb_append(&cb, c);
}
cb_trim(&cb);
return cb.buf;
}
wchar_t *read_linew(FILE *file)
{
wcbuf wcb;
wint_t wc;
wcb_init(&wcb, 256);
while ((wc = fgetwc(file)) != WEOF && wc != L'\n') {
wcb_append(&wcb, wc);
}
wcb_trim(&wcb);
return wcb.buf;
}
smb_ll *split_lines(char *source)
{
char *start;
smb_ll *list;
DATA d;
/*
We go through `source` looking for every newline, replace it with NUL, and
add the beginnig of the line to the list.
*/
start = source;
list = ll_create();
while (*source != '\0') {
if (*source == '\n') {
// Add line to list.
d.data_ptr = start;
ll_append(list, d);
// Null-terminate the line.
*source = '\0';
// Next string starts at the next character.
start = source + 1;
}
source++;
}
if (start != source) {
d.data_ptr = start;
ll_append(list, d);
}
return list;
}
smb_ll *split_linesw(wchar_t *source)
{
wchar_t *start;
smb_ll *list;
DATA d;
/*
We go through `source` looking for every newline, replace it with NUL, and
add the beginnig of the line to the list.
*/
start = source;
list = ll_create();
while (*source != L'\0') {
if (*source == L'\n') {
// Add line to list.
d.data_ptr = start;
ll_append(list, d);
// Null-terminate the line.
*source = L'\0';
// Next string starts at the next character.
start = source + 1;
}
source++;
}
if (start != source) {
d.data_ptr = start;
ll_append(list, d);
}
return list;
}
|