view codetools.c @ 2:ff5e493bc9cb default tip

2003-01-29
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Sat, 13 Oct 2012 21:14:50 +0200
parents
children
line wrap: on
line source

#include <stdio.h>
#include "codetools.h"


#define COMM_INDENT 60

/* Zwiększa wcięcie kolejnych linii. */
void
codeIndent(CodeText *text)
{
    ++(text->indent);
    if (text->last_line->string->len==0)
        ++(text->last_line->indent);
}

/* Zmniejsza wcięcie. */
void
codeUnindent(CodeText *text)
{
    --(text->indent);
    if (text->last_line->string->len==0)
        --(text->last_line->indent);
}

/* Przechodzi do nowej linijki. */
void
codeNewLine(CodeText *text)
{
    CodeLine *line;

    line = newCodeLine(text);
    line->indent = text->indent;
    text->last_line = line;
    text->line_list = g_list_append(text->line_list, line);
}

/* Dodaje tekst do ostatniej linijki. */
void
codePrint(CodeText *text, const char *string)
{
    if (text->last_line == NULL)
        codeNewLine(text);
    text->last_line->string = g_string_append(text->last_line->string, string);
}

/* Dodaje tekst etykiety do ostatniej linijki. */
void
codeLabel(CodeText *text, const char *string)
{
    if (text->last_line == NULL)
        codeNewLine(text);
    if (text->last_line->label==NULL) {
        text->last_line->label = g_string_new(string);
    } else {
        text->last_line->label = g_string_append(text->last_line->label, string);
    }
}

/* Dodaje tekst komentarza do ostatniej linijki. */
void
codeComment(CodeText *text, const char *string)
{
    if (text->last_line == NULL)
        codeNewLine(text);
    if (text->last_line->comment==NULL) {
        text->last_line->comment = g_string_new(string);
    } else {
        text->last_line->comment = g_string_append(text->last_line->comment, string);
    }
}

/* Wyświetla kod na ekranie. */
void
codeShow(CodeText *text)
{
    GList *iter;
    CodeLine *line;
    int ind;

    if (text==NULL)
        return;
    for (iter=text->line_list; iter!=NULL; iter=iter->next) {
        line = ((CodeLine *)iter->data);
        if (line->label!=NULL) {
            ind = line->label->len;
            printf("%s", line->label->str);
        } else {
            ind = 0;
        }
        while (ind<line->indent*4) {
            printf(" ");
            ++ind;
        }
        printf("%s", line->string->str);
        ind += line->string->len;
        if (line->comment!=NULL) {
            while (ind<COMM_INDENT) {
                printf(" ");
                ++ind;
            }
            printf("%s", line->comment->str);
        }
        printf("\n");
    }
}

/* Zaznacza początek tokena. */
void
codeBeginTag(CodeText *text, int group, void *node)
{
    CodeTag *tag;
    int kind;
    
    if (text->last_line == NULL)
        codeNewLine(text);
    tag = newCodeTag();
    tag->xstart = text->last_line->string->len;
    tag->ystart = (text->last_line->number)-1;
    text->tag_stack = g_list_prepend(text->tag_stack, tag);    
    text->last_line->tag_list = g_list_append(text->last_line->tag_list, tag);
    tag->group = group;
    tag->node = node;
}

/* Zanzacza koniec tokena. */
void
codeEndTag(CodeText *text)
{
    GList *first;
    CodeTag *tag;

    tag = text->tag_stack->data;
    tag->xend = text->last_line->string->len;
    tag->yend = (text->last_line->number)-1;
    first = text->tag_stack;
    text->tag_stack = g_list_remove_link(text->tag_stack, first);
    g_list_free(first);
}