initial commit

This commit is contained in:
2021-08-08 21:11:22 +02:00
commit fe7136d801
130 changed files with 6858 additions and 0 deletions

41
c/acronym/GNUmakefile Normal file
View File

@@ -0,0 +1,41 @@
# The original 'makefile' has a flaw:
# 1) it overrides CFLAGS
# 2) it does not pass extra "FLAGS" to $(CC) that could come from environment
#
# It means :
# - we need to edit 'makefile' for different builds (DEBUG, etc...), which is
# not practical at all.
# - Also, it does not allow to run all tests without editing the test source
# code.
#
# To use this makefile (GNU make only):
# "make": build with all predefined tests (without editing test source code)
# "make mem": perform memcheck with all tests enabled
# "make unit": build standalone (unit) test
# "make debug": build standalone test with debugging code
#
# Original 'makefile' targets can be used (test, memcheck, clean, ...)
.PHONY: default all mem unit debug std
default: all
# default is to build with all predefined tests
BUILD := teststall
include makefile
all: CFLAGS+=-DTESTALL -g
all: clean test
mem: CFLAGS+=-DTESTALL
mem: clean memcheck
unit: CFLAGS+=-DUNIT_TEST
unit: clean std
debug: CFLAGS+=-DUNIT_TEST -DDEBUG -g
debug: clean std
std: src/*.c src/*.h
$(CC) $(CFLAGS) src/*.c -o test.out

46
c/acronym/README.md Normal file
View File

@@ -0,0 +1,46 @@
# Acronym
Convert a phrase to its acronym.
Techies love their TLA (Three Letter Acronyms)!
Help generate some jargon by writing a program that converts a long name
like Portable Network Graphics to its acronym (PNG).
## Getting Started
Make sure you have read the "Guides" section of the
[C track][c-track] on the Exercism site. This covers
the basic information on setting up the development environment expected
by the exercises.
## Passing the Tests
Get the first test compiling, linking and passing by following the [three
rules of test-driven development][3-tdd-rules].
The included makefile can be used to create and run the tests using the `test`
task.
make test
Create just the functions you need to satisfy any compiler errors and get the
test to fail. Then write just enough code to get the test to pass. Once you've
done that, move onto the next test.
As you progress through the tests, take the time to refactor your
implementation for readability and expressiveness and then go on to the next
test.
Try to use standard C99 facilities in preference to writing your own
low-level algorithms or facilities by hand.
## Source
Julien Vanier [https://github.com/monkbroc](https://github.com/monkbroc)
## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
[c-track]: https://exercism.io/my/tracks/c
[3-tdd-rules]: http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd

37
c/acronym/makefile Normal file
View File

@@ -0,0 +1,37 @@
### If you wish to use extra libraries (math.h for instance),
### add their flags here (-lm in our case) in the "LIBS" variable.
LIBS = -lm
###
CFLAGS = -std=c99
CFLAGS += -g
CFLAGS += -Wall
CFLAGS += -Wextra
CFLAGS += -pedantic
CFLAGS += -Werror
CFLAGS += -Wmissing-declarations
CFLAGS += -DUNITY_SUPPORT_64
ASANFLAGS = -fsanitize=address
ASANFLAGS += -fno-common
ASANFLAGS += -fno-omit-frame-pointer
.PHONY: test
test: tests.out
@./tests.out
.PHONY: memcheck
memcheck: test/*.c src/*.c src/*.h
@echo Compiling $@
@$(CC) $(ASANFLAGS) $(CFLAGS) src/*.c test/vendor/unity.c test/*.c -o memcheck.out $(LIBS)
@./memcheck.out
@echo "Memory check passed"
.PHONY: clean
clean:
rm -rf *.o *.out *.out.dSYM
tests.out: test/*.c src/*.c src/*.h
@echo Compiling $@
@$(CC) $(CFLAGS) src/*.c test/vendor/unity.c test/*.c -o tests.out $(LIBS)

69
c/acronym/src/acronym.c Normal file
View File

@@ -0,0 +1,69 @@
#include <string.h>
#include <malloc.h>
#include <ctype.h>
#include "acronym.h"
/* See GNUmakefile in following link for explanation
* https://exercism.io/my/solutions/103b2f7d92db42309c1988030f5202c7
*/
#if defined UNIT_TEST || defined DEBUG
#include <stdio.h>
#include <stdlib.h>
#endif
#define SKIPWORD(p) { while (*(p) && (isalpha(*(p)) || *p=='\'')) (p)++;}
#define NEXTWORD(p) { while (*(p) && !isalpha(*(p))) (p)++;}
char *abbreviate(const char *phrase)
{
/* Yet another approach (to avoid scanning phrase twice):
* We (re)allocate a ALLOCSIZE buffer when current one is not large
* enough to accept next character + 1 ('\0')
*
* Other solutions would be to scan phrase twice (for example an initial
* strlen() to find out a maximum length), or the (bad idea) using a fixed
* size buffer.
*
* The usual choices.
*/
char *buf=NULL;
int c=0, size=0;
if (!phrase)
return NULL;
while (*phrase) {
NEXTWORD(phrase);
if (*phrase) {
/* buffer too small */
if (c>=size-1) {
size+=ALLOCSIZE;
if (!(buf=realloc(buf, size)))
return NULL;
}
*(buf+c++)=toupper(*phrase++);
SKIPWORD(phrase);
}
}
/* at least one character */
if (c)
*(buf+c)=0;
return buf;
}
#ifdef UNIT_TEST
int main(int ac, char **av)
{
int arg=1;
char *p;
for (; arg<ac; ++arg) {
p=abbreviate(*(av+arg));
printf("acronym(%s)=[%s]\n", *(av+arg), p? p: "NULL");
if (p)
free(p);
}
}
#endif

20
c/acronym/src/acronym.h Normal file
View File

@@ -0,0 +1,20 @@
#ifndef ACRONYM_H
#define ACRONYM_H
char *abbreviate(const char *phrase);
#ifdef DEBUG
#define ALLOCSIZE 2
#else
#define ALLOCSIZE 1024
#endif
/* See GNUmakefile in following link for explanation
* https://exercism.io/my/solutions/103b2f7d92db42309c1988030f5202c7
*/
#ifdef TESTALL
#undef TEST_IGNORE
#define TEST_IGNORE() {}
#endif
#endif