commit fe1cd8ff532510dfe16619027b0d57d0dc12ee57
parent 295b9667dc877082fcb85db9fa3b4f71c967ac32
Author: klewer-martin <martin.cachari@gmail.com>
Date: Mon, 28 Jun 2021 20:48:17 -0300
Update
Diffstat:
9 files changed, 272 insertions(+), 9 deletions(-)
diff --git a/guia06/ex01.c b/guia06/ex01.c
@@ -1,20 +1,22 @@
#include <stdio.h>
#include <stdlib.h>
-#define ERR_ARG "Error: No arguments"
+#define MSG_ERR_NO_ARG "Error: No arguments"
+
+#define STR_ARG_POS 1
int main (int argc, char *argv[]) {
- size_t i;
+
if(argc == 1) {
- fprintf(stderr, ERR_ARG"\n");
+ fprintf(stderr, "%s\n", MSG_ERR_NO_ARG);
return 1;
}
- printf("argc: %d \n", argc);
+ size_t i = 0;
+ while((*(argv + STR_ARG_POS))[++i])
+ ;
- for (i = 0; i < argc; i++) {
- printf("argv[%ld] = %s \n", i, argv[i]);
- }
+ printf("%ld\n", i);
return EXIT_SUCCESS;
}
diff --git a/guia06/ex02.c b/guia06/ex02.c
@@ -0,0 +1,32 @@
+#include <stdio.h>
+
+#define ARGS
+
+typedef enum {
+ OK,
+ ERROR_NO_ARGUMENT,
+ ERROR_NULL_POINTER,
+} status_t;
+
+status_t validate_arguments(int argc, char *argv[]);
+
+int main (int argc, char *argv[])
+{
+ status_t st;
+ int cmp;
+
+ if((st = validate_arguments(argc, argv))) return st;
+
+ cmp = 0;
+ for(size_t i = 0; argv[1][i] && argv[2][i]; i++) {
+ cmp += ((argv[1][i]) - (argv[2][i]));
+ }
+
+ printf("%s\n", cmp ? "Son distintas" : "Son iguales");
+ return 0;
+}
+
+status_t validate_arguments(int argc, char *argv[])
+{
+ return OK;
+}
diff --git a/guia07/ex08.c b/guia07/ex08.c
@@ -1,3 +1,8 @@
+/* guia07/ej08.c
+ * por Martin J. Klöckner
+ * github.com/klewer-martin
+ */
+
#include <stdio.h>
#define MASK_RED 0xFF0000
@@ -58,7 +63,7 @@ uchar get_blue(uint color)
return (uchar)((color & MASK_BLUE) >> SHIFT_BLUE);
}
-/* This implementation its independent from other functions */
+/* This implementation its independendent from other functions */
status_t rgb_components(uint color, uchar *red, uchar *green, uchar *blue)
{
if(!red || !green || !blue) return ERROR_NULL_POINTER;
@@ -70,7 +75,7 @@ status_t rgb_components(uint color, uchar *red, uchar *green, uchar *blue)
return OK;
}
-/* This implementation insted its dependent from other functions */
+/* This implementation insted depends from other functions */
status_t rgb_components2(uint color, uchar *red, uchar *green, uchar *blue)
{
if(!red || !green || !blue) return ERROR_NULL_POINTER;
diff --git a/guia07/ex11.c b/guia07/ex11.c
@@ -60,6 +60,7 @@ void getCOMControl(uchar control, uchar *prescalingFactor, uchar *divisionFactor
{
if(!prescalingFactor || !divisionFactor) return;
+ /* static because otherwise it would have to load the array into memmory everytime the function is called */
static uchar divFactor[4] = { 2, 4, 16, 32 };
*prescalingFactor = ((control & (MASK_SPR0 + MASK_SPR1)) >> SHIFT_SPR0);
diff --git a/guia08/ex11.c b/guia08/ex11.c
@@ -0,0 +1,96 @@
+/* guia08/ej11.c
+ * por Martin J. Klöckner
+ * github.com/klewer-martin
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#define ARR_INIT_SIZE 3
+#define STR_INIT_SIZE 10
+
+#define GROWTH_FACTOR 2
+
+char **split(const char *s, char delim, size_t *fields);
+
+int main (void)
+{
+ char csv[] = "Hola,Hello,Hallo,Aloha";
+ char **arr;
+ size_t i, len;
+
+ if(!(arr = split(csv, ',', &len))) return 1;
+
+ for(i = 0; i <= len; i++)
+ puts(arr[i]);
+
+ while(len + 1) free(arr[len--]);
+
+ free(arr);
+
+ return 0;
+}
+
+char **split(const char *s, char delim, size_t *fields)
+{
+ if(!s || !fields) return NULL;
+
+ char **arr;
+ size_t i, arr_size, str_size;
+
+ arr_size = ARR_INIT_SIZE;
+ str_size = STR_INIT_SIZE;
+
+ if(!(arr = (char **)calloc(sizeof(char *), arr_size))) return NULL;
+
+ if(!(*arr = (char *)calloc(sizeof(char), str_size))) {
+ free(arr)
+ return NULL;
+ }
+
+ for(*fields = i = 0; *s; i++, s++) {
+ /* If the array doesn't have any memory left then it gets more memory */
+ if(*fields == (arr_size - 1)) {
+ /* Exponential growth of the array */
+ arr_size *= GROWTH_FACTOR;
+
+ if(!(arr = (char **)realloc(arr, arr_size * sizeof(char *)))) {
+ /* If it can't get more memory then all the previous allocations gets freed */
+ while((*fields) + 1) free(arr[(*fields)--]);
+ free(arr);
+ return NULL;
+ }
+ }
+
+ if(*s == delim) {
+ /* Gets memory for a new string and increments arr index by one */
+ if(!(arr[++(*fields)] = (char *)calloc(sizeof(char), str_size))) {
+ /* If it can't get more memory then all the previous allocations gets freed */
+ while(*fields)
+ free(arr[(*fields)--]);
+
+ free(arr);
+ return NULL;
+ }
+ i = 0;
+ s++;
+ }
+ /* This makes sure that every string has available memory, if not then it gets more */
+ if(i == (str_size - 2))
+ {
+ str_size *= GROWTH_FACTOR;
+ if(!(arr[*fields] = (char *)realloc(arr[*fields], str_size))) {
+ /* If it can't get more memory all the previous used memmory gets freed */
+ while((*fields) + 1)
+ free(arr[(*fields)--]);
+
+ free(arr);
+ return NULL;
+ }
+ /* Sets all the new memory to the null character */
+ for(size_t j = i; j < str_size; j++) (*arr)[j] = '\0';
+ }
+ arr[*fields][i] = (*s);
+ }
+ return arr;
+}
diff --git a/guia08/ex11_imp.c b/guia08/ex11_imp.c
@@ -0,0 +1,58 @@
+/* guia08/ej11.c
+ * por Martin J. Klöckner
+ * github.com/klewer-martin
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define ARR_INIT_SIZE 3
+#define STR_INIT_SIZE 10
+
+#define GROWTH_FACTOR 2
+
+char **split(const char *s, char delim, size_t *fields);
+
+int main (void)
+{
+ char csv[] = "Hola,Hello,Hallo,Aloha";
+ char **arr, d[1];
+ size_t i, len;
+
+ d[0] = delim;
+
+ if(!(arr = split(csv, ',', &len))) return 1;
+
+ for(i = 0; i <= len; i++)
+ puts(arr[i]);
+
+ while(len + 1) free(arr[len--]);
+
+ free(arr);
+
+ return 0;
+}
+
+char **split(const char *s, char delim, size_t *fields)
+{
+ if(!s || !fields) return NULL;
+
+ char **arr, *p;
+ size_t i, arr_size, str_size;
+
+ arr_size = ARR_INIT_SIZE;
+ str_size = STR_INIT_SIZE;
+
+ if(!(arr = (char **)calloc(sizeof(char *), arr_size))) return NULL;
+
+ if(!(*arr = (char *)calloc(sizeof(char), str_size))) {
+ free(arr);
+ return NULL;
+ }
+
+ *fields = i = 0;
+ while(*p = strtok(s, delim));
+
+ return arr;
+}
diff --git a/guia08/ex15.c b/guia08/ex15.c
@@ -0,0 +1,31 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+char *strclone(char *);
+
+int main (void) {
+ char str[] = "Hello, world!";
+ char *cpy;
+
+ if(!(cpy = strclone(str))) return 1;
+
+ printf("%s\n", cpy);
+
+ free(cpy);
+ return 0;
+}
+
+char *strclone(char *s)
+{
+ if(!s) return NULL;
+
+ char *c;
+
+ /* +1 for the null character */
+ if(!(c = (char *)calloc(sizeof(char), (strlen(s) + 1)))) return NULL;
+
+ for(size_t i = 0; *s; s++, i++) c[i] = *s;
+
+ return c;
+}
diff --git a/guia08/ex16.c b/guia08/ex16.c
@@ -0,0 +1,36 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+typedef enum {
+ OK,
+ ERROR_MEMORY_ALLOC,
+ ERROR_NULL_POINTER
+} status_t;
+
+status_t strclone(const char *, char **);
+
+int main (void) {
+ char str[] = "Hello, world!";
+ char *cpy;
+ status_t st;
+
+ if((st = strclone(str, &cpy))) return st;
+
+ printf("%s\n", cpy);
+
+ free(cpy);
+ return 0;
+}
+
+status_t strclone(const char *s, char **d)
+{
+ if(!s || !d) return ERROR_NULL_POINTER;
+
+ /* +1 for the null character */
+ if(!(*d = (char *)calloc(sizeof(char), (strlen(s) + 1)))) return ERROR_MEMORY_ALLOC;
+
+ for(size_t i = 0; *s; s++, i++) (*d)[i] = *s;
+
+ return OK;
+}
diff --git a/guia08/ex17.c b/guia08/ex17.c
@@ -47,6 +47,8 @@ char *fread_line(FILE *fp)
int aux;
size_t i, alloc_size;
+ if(!fp) return NULL;
+
alloc_size = INIT_SIZE;
if(!(dst = (char *)calloc(alloc_size, sizeof(char)))) return NULL;