How To Make A marquee In C




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

#define MAX_STR_LEN 40

typedef enum { false = 0, true } bool;
typedef enum { LEFT, RIGHT, LEFT_CIRCULAR, RIGHT_CIRCULAR } Direction;

void marquee(char *, size_t count, Direction);
void delay(int);
void shift(char *, size_t distance, Direction);
bool isCircularShift(Direction);
bool isAllSpaces(const char *);

char msg[MAX_STR_LEN];
const size_t repeatCount = 2;
const int delayCount = 1e6;
const int shiftLen = 1;

int main(int argc, char *argv[]) {

    while (1) {
        printf("> ");
        fgets(msg,MAX_STR_LEN,stdin);
        *(strchr(msg,'\n')) = '\0';
        marquee(msg, repeatCount, LEFT_CIRCULAR);
        marquee(msg, repeatCount, RIGHT_CIRCULAR);
    }
    return 0;
}

void marquee(char *s0, size_t count, Direction dir) {
    size_t len = strlen(s0) * 3, i, j;
    char *s;

    if (len > 0) {
        s = calloc(len+1,sizeof(char));
        memset(s, ' ', len);
        memcpy(s, s0, strlen(s0));

        /* Scroll it. */
        for (i = 0; i < count; i++) {
            for (j = 0; j < len; j++) {
                printf("\r%s",s); fflush(stdout);
                shift(s, shiftLen, dir);
                delay(delayCount);
            }
        }

        /* Clear it. */
        dir = (dir == RIGHT_CIRCULAR) ? RIGHT : LEFT;
        while (isAllSpaces(s) == false) {
            shift(s, shiftLen, dir);
            printf("\r%s",s); fflush(stdout);
            delay(delayCount);
        }
        printf("\r"); fflush(stdout);
        free(s);
    }
}

void shift(char *a, size_t distance, Direction dir) {
    char *source, *dest, *bumpFrom, *rotateTo, temp;
    size_t n, aLen = strlen(a);

    if (distance > 0) {
        if ((dir == RIGHT) || (dir == RIGHT_CIRCULAR)) {
            source = a; dest = a+1;
            bumpFrom = &a[aLen-1]; rotateTo = &a[0];
        } else {
            source = a+1; dest = a;
            bumpFrom = &a[0]; rotateTo = &a[aLen-1];
        }
        for (n = 0; n < distance; n++) {
            temp = *bumpFrom;
            memmove(dest,source,aLen-1);
            if (isCircularShift(dir) == true) {
                *rotateTo = temp;
            } else {
                *rotateTo = ' ';
            }
        }
    }
}

bool isCircularShift(Direction dir) {
    return (dir == LEFT_CIRCULAR) || (dir == RIGHT_CIRCULAR);
}

bool isAllSpaces(const char *s) {
    bool allSpaces = true;
    const char *p = s;

    while ((*p != '\0') && (allSpaces == true)) {
        allSpaces = (*p++ == ' ');
    }
    return allSpaces;
}

double factorial(unsigned n) {
    if (n == 1) return n;
    return n * factorial(n-1);
}

void delay(int n) {
    int i;

    for (i = 0; i < n; i++) {
        factorial(10);
    }
}

No comments:

Post a Comment