ppdfviewer, basic, bits and string view

This commit is contained in:
2026-11-14 00:15:02 -03:00
parent c693541103
commit 04be717bc7
8 changed files with 193 additions and 0 deletions
BIN
View File
Binary file not shown.
+25
View File
@@ -0,0 +1,25 @@
#include <stdio.h>
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;
}
+81
View File
@@ -1,8 +1,18 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_error.h>
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_log.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_video.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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);
}
}
}
Binary file not shown.
+13
View File
@@ -0,0 +1,13 @@
#include "stringv.h"
#include <stdio.h>
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;
}
BIN
View File
Binary file not shown.
+60
View File
@@ -0,0 +1,60 @@
#include "stringv.h"
#include <stddef.h>
#include <stdio.h>
#include <string.h>
/*
* @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};
}
+14
View File
@@ -0,0 +1,14 @@
#ifndef STRING_VIEW_H
#define STRING_VIEW_H
#include <stddef.h>
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