Skip to content

Instantly share code, notes, and snippets.

@sasairc
Created February 14, 2017 08:42
Show Gist options
  • Save sasairc/9c3f9dd2f22cd53f291bc680fd13e27f to your computer and use it in GitHub Desktop.
Save sasairc/9c3f9dd2f22cd53f291bc680fd13e27f to your computer and use it in GitHub Desktop.
わったしにもー!超能力が!ある気がすーるーのー!

わったしにもー!超能力が!ある気がすーるーのー!

目覚めよ念力!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static void esp_allocate(char* msg);
static void esp_watashi(void);
static void esp_esp(char* skill);
static void esp_suruno(void);
static void esp_put(void);

typedef struct {
    char*   msg;
    void    (*watashi)(void);
    void    (*esp)(char* skill);
    void    (*suruno)(void);
    void    (*put)(void);
} esp_t;

static esp_t    es = {
    NULL,
    esp_watashi,
    esp_esp,
    esp_suruno,
    esp_put,
};

static esp_t*   esp = &es;

static void esp_allocate(char* msg)
{
    if (esp->msg == NULL) {
        esp->msg = (char*)
            malloc(sizeof(char) * strlen(msg) + 1);
    } else {
        esp->msg = (char*)
            realloc(esp->msg, strlen(esp->msg) + strlen(msg) + 1);
    }

    return;
}

static void esp_watashi(void)
{
    char*   msg = "わったしにもー!\0";

    esp_allocate(msg);
    memcpy(esp->msg + strlen(esp->msg), msg, strlen(msg) + 1);

    return;
}

static void esp_esp(char* skill)
{
    if (!skill)
        return;

    char    tmp[strlen(skill) + strlen("が!") + 1];

    memcpy(tmp, skill, strlen(skill));
    memcpy(tmp + strlen(skill), "が!\0", strlen("が!") + 1);
    esp_allocate(tmp);
    memcpy(esp->msg + strlen(esp->msg), tmp, strlen(tmp) + 1);

    return;
}

static void esp_suruno(void)
{
    char*   msg = "ある気がすーるーのー!\0";

    esp_allocate(msg);
    memcpy(esp->msg + strlen(esp->msg), msg, strlen(msg) + 1);

    return;
}

static void esp_put(void)
{
    if (esp->msg != NULL) {
        fprintf(stdout, "%s\n", esp->msg);
        free(esp->msg);
        esp->msg = NULL;
    } else {
        fprintf(stdout, "超能力使えたら格好いいぞー!\n", esp->msg);
    }

    return;
}

int main(void)
{
    esp->put();

    esp->watashi();
    esp->esp("Psychic Ability Power");
    esp->suruno();
    esp->put();

    return 0;
}

超能力使えたら格好いいぞー! わったしにもー!Psychic Ability Powerが!ある気がすーるーのー!

遠回りな元ネタ - わーい、すごーい - Qiita

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment