This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
forked from gek169/stringutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
61 lines (50 loc) · 1.79 KB
/
test.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
#include "stringutil.h"
#ifndef NULL
#define NULL ((void*)0)
#endif
#pragma I wonder if this will cause an error.
int main(int argc, char** argv){
char* one_and_two = NULL;
if(argc > 2)
one_and_two = strcatalloc(argv[1],argv[2]);
if(one_and_two)
printf("Arguments 1 and 2 combined together are %s", one_and_two);
if(one_and_two) free(one_and_two);
puts("\nReading from stdin until terminator B or 9 characters...\n");
char* buf = malloc(10);
if(!buf)return 1;
unsigned long blen = 0; blen = read_until_terminator(stdin, buf, 10, 'B'); fgetc(stdin); //Used to handle erroneous '\n grabs.'
printf("\nYou entered a string of length %lu:\n%s\n",blen, buf);
free(buf); buf = NULL;
//Test read_until_terminator_alloc
puts("\nReading from stdin until terminator Q with infinite capacity...\n");
blen = 0;
buf = read_until_terminator_alloced(stdin, &blen, 'Q',8);
if(buf){
printf("\nYou entered a string of length %lu:\n%s\n",blen, buf); fgetc(stdin); //Used to handle erroneous '\n grabs.'
free(buf); buf = NULL;
}else {
puts("\nSome sort of error?\n");
}
FILE* f = fopen("README.MD", "rb");
if(!f) return 1;
unsigned long len;
buf = NULL; buf = read_file_into_alloced_buffer(f, &len);
if(buf){
printf("\nThe entire contents of README.MD are:\n%s\n~~~\n",buf);
free(buf);
}
if(f) fclose(f);
//Substring search.
const char* text = "Oh, what a mighty fine day it is! cccars";
long pos = strfind(text, "mighty");
long pos2 = strfind(text, "Mighty"); //-1 means it could not find it
long pos4 = strfind(text, "ccars");
printf("\n\"mighty\" is at %ld \n", pos);
printf("\nwhich is:%s\n", text+pos);
printf("\"Mighty\" is at %ld \n", pos2);
printf("\"Oh,\" is at %ld\n", strfind(text, "Oh,"));
printf("\"ccars\" is at %ld\n", pos4);
printf("\nwhich is:%s\n", text+pos4);
return 0;
}