diff --git a/c/bits/bit1/flags b/c/bits/bit1/flags new file mode 100755 index 0000000..c06eb1c Binary files /dev/null and b/c/bits/bit1/flags differ diff --git a/c/bits/bit1/main.c b/c/bits/bit1/main.c new file mode 100644 index 0000000..ebd9505 --- /dev/null +++ b/c/bits/bit1/main.c @@ -0,0 +1,25 @@ +#include + +int main(void) { + + unsigned char flags = 0b0000010; + + unsigned char FLAG_A = 1u << 0; + unsigned char FLAG_B = 1u << 1; + + printf("El FLAG_A es: %08b\n", FLAG_A); + printf("El FLAG_B es: %08b\n", FLAG_B); + + // encender ambos. + flags = (FLAG_A | FLAG_B); + + if (flags & (FLAG_A)) { + printf("El flag A esta encendido\n"); + } + + if (flags & (FLAG_B)) { + printf("El flag B esta encendido\n"); + } + + return 0; +} diff --git a/c/files/files3/main.c b/c/files/files3/main.c index ef5102e..ebabb57 100644 --- a/c/files/files3/main.c +++ b/c/files/files3/main.c @@ -1,8 +1,18 @@ +#include +#include +#include +#include +#include +#include +#include #include #include #include #include +void render_image(SDL_Renderer *ref_renderer, unsigned char *data, + unsigned long w, unsigned long h); + int main(int argc, char *argv[]) { if (argc < 2) { fprintf(stderr, "Argumentos invalidos\n"); @@ -79,7 +89,78 @@ int main(int argc, char *argv[]) { fclose(pfile); } + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + perror("SLD_Init"); + free(data); + fclose(pfile); + return 1; + } + + SDL_Window *window = + SDL_CreateWindow("Visor de imagenes", SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, w, h, SDL_WINDOW_SHOWN); + + if (window == NULL) { + SDL_Log("SLD_WINDOW: %s", SDL_GetError()); + SDL_Quit(); + free(data); + fclose(pfile); + return 1; + } + + SDL_Renderer *renderer = SDL_CreateRenderer( + window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + + if (!renderer) { + SDL_Log("No se pudo crear el renderizador: %s", SDL_GetError()); + SDL_DestroyWindow(window); + SDL_Quit(); + free(data); + fclose(pfile); + return 1; + } + + bool running = true; + SDL_Event ev; + while (running) { + while (SDL_PollEvent(&ev)) { + if (ev.type == SDL_QUIT) { + running = 0; // Si hacen clic en la "X", salimos + } + if (ev.type == SDL_KEYDOWN) { + // Si presionan Escape, salimos + if (ev.key.keysym.sym == SDLK_ESCAPE) { + running = 0; + } + } + } + + SDL_RenderClear(renderer); + render_image(renderer, data, w, h); + SDL_RenderPresent(renderer); + } + + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_Quit(); + free(data); fclose(pfile); return 0; } + +void render_image(SDL_Renderer *ref_renderer, unsigned char *rdata, + unsigned long int w, unsigned long h) { + + unsigned long offset = 0; + for (unsigned long cury = 0; cury < h; cury++) { + for (unsigned long curx = 0; curx < w; curx++) { + uint8_t r = *(rdata + offset++); + uint8_t g = *(rdata + offset++); + uint8_t b = *(rdata + offset++); + + SDL_SetRenderDrawColor(ref_renderer, r, g, b, 255); + SDL_RenderDrawPoint(ref_renderer, curx, cury); + } + } +} diff --git a/c/files/files3/ppdviewer b/c/files/files3/ppdviewer index 727d431..bf887ea 100755 Binary files a/c/files/files3/ppdviewer and b/c/files/files3/ppdviewer differ diff --git a/c/stringview/main.c b/c/stringview/main.c new file mode 100644 index 0000000..57f3f78 --- /dev/null +++ b/c/stringview/main.c @@ -0,0 +1,13 @@ +#include "stringv.h" +#include +int main(void) { + StringView s1 = sv_from_cstr("Hola mundo con string view"); + StringView s2 = sv_from_cstr("😊"); + StringView last = sv_right(&s1, 4); + + printf("El string view contiene: %s\n", s2.text); + printf("El length es: %zu\n", s2.lenght); + + printf("last tiene:%.*s\n", (int)last.lenght, last.text); + return 0; +} diff --git a/c/stringview/string b/c/stringview/string new file mode 100755 index 0000000..169823a Binary files /dev/null and b/c/stringview/string differ diff --git a/c/stringview/stringv.c b/c/stringview/stringv.c new file mode 100644 index 0000000..aeb5f48 --- /dev/null +++ b/c/stringview/stringv.c @@ -0,0 +1,60 @@ +#include "stringv.h" +#include +#include +#include + +/* + * @brief Create a string view from a const char* and set their lenght + * + * @details This string view provides a view of a string. It does not allocate + * memory, therefore, it lives only while the source is alive. The + * lenght is a bytes number of the string. It does not represents the number of + * characters. + * + * @param str pointer from the original string. It can be NULL. + * + * @return a StringView with the view of original string with corresponding + * lenght. + */ + +#define STRING_VIEW_NULL (StringView){.text = NULL, .lenght = 0} + +StringView sv_from_cstr(const char *str) { + if (str == NULL) { + return STRING_VIEW_NULL; + } + return (StringView){.text = (char *)str, .lenght = strlen(str)}; +} + +StringView sv_left(const StringView *sv, size_t l) { + if (sv == NULL) { + perror("sv_left: pointer is NULL\n"); + return STRING_VIEW_NULL; + } + if (sv->text == NULL) { + perror("sv_left: text pointer is NULL\n"); + return STRING_VIEW_NULL; + } + if (sv->lenght < l) { + perror("sv_left: text length is less than specified length\n"); + return STRING_VIEW_NULL; + } + return (StringView){.text = sv->text, .lenght = l}; +} + +StringView sv_right(const StringView *sv, size_t r) { + if (sv == NULL) { + perror("sv_right: pointer is NULL\n"); + return STRING_VIEW_NULL; + } + if (sv->text == NULL) { + perror("sv_right: text pointer is NULL\n"); + return STRING_VIEW_NULL; + } + if (sv->lenght < r) { + perror("sv_right: text length is less than specified length\n"); + return STRING_VIEW_NULL; + } + size_t nlength = sv->lenght - r; + return (StringView){.text = sv->text + nlength, .lenght = r}; +} diff --git a/c/stringview/stringv.h b/c/stringview/stringv.h new file mode 100644 index 0000000..e48caf2 --- /dev/null +++ b/c/stringview/stringv.h @@ -0,0 +1,14 @@ +#ifndef STRING_VIEW_H +#define STRING_VIEW_H + +#include + +typedef struct StringView { + char *text; + size_t lenght; +} StringView; + +StringView sv_from_cstr(const char *); +StringView sv_left(const StringView *sv, size_t l); +StringView sv_right(const StringView *sv, size_t l); +#endif