Hi,

I am just playing with C code in the console under Linux.

In the old times a fair bit of code was written for a console;
programs were operating in text mode, creating menues was quite a fun.

I coded very simple c routine to print a strings of characters on
the console but the characters, each is in its own single box and the whole
set of strings is in a large complette box.

I thought it could be intersting for someone to use it as an excersise.

This is nothing complex as for the 'C' code. :-)

I am using Code Blocks IDE for editing (under Ubuntu 12.04) and GNU GCC
Compiler.

You should be able to just copy/paste this snippet and compile it on any OS.

Edited 2 Years Ago by pritaeas: Changed to code snippet.

zegarnek
Deleted Member
/*
            Print boxed characters of string in the terminal
            this is just an excersize in C manipulation
            of the few remaining box drawing single line ASCII
            characters available in Unix like OS.
                   
            Edited using Code Blocks IDE Kubuntu 12.04
            and compiled ucing GCC 
        */
        
        #include <stdio.h>
        #include <stdlib.h>
        #include <wchar.h> // for future trials with wide chars
        #include <locale.h> // to set Unicode locale 
        #include <string.h>
        
        
        #define RB "\e(0\x6a\e(B" // 188 Right Bottom corner
        #define RT "\e(0\x6b\e(B" // 187 Right Top corner
        #define LT "\e(0\x6c\e(B" // 201 Left Top cornet
        #define LB "\e(0\x6d\e(B" // 200 Left Bottom corner
        #define MC "\e(0\x6e\e(B" // 206 Midle Cross
        #define HL "\e(0\x71\e(B" // 205 Horizontal Line
        #define LC "\e(0\x74\e(B" // 204 Left Cross
        #define RC "\e(0\x75\e(B" // 185 Right Cross
        #define BC "\e(0\x76\e(B" // 202 Bottom Cross
        #define TC "\e(0\x77\e(B" // 203 Top Cross
        #define VL "\e(0\x78\e(B" // 186 Vertical Line
        #define SP " " 		  // space string
        
        #define TRUE 1
        #define FALSE 0
        
        // Move the whole box to the right 'RIGHT' number of spaces
        #define RIGHT 8
        
        // IF DBL_BOX TRUE draw a second box around
        #define DBL_BOX TRUE
        
        /*
        
        */
        //print number of box chars - two string variables
        // if different able to print mixed box chars
        // eg. first right cross, horizontal, right cross ect
        // 'i' times.
        void box_print(char *def_str1, char *def_str2, int i);
        
        void print_nl(int i); // print 'i' number of newlines
        
        // print string cgars within box characters
        int box_letters(char *letters);
        
        int top_out_line(int bars); //if DBL_BOX true draw top outline
        int top_line(int bars); //draw top line of a box
        int mid_line(int bars); //draw middle line ob a box
        int bot_line(int bars); //draw bottol line of a box
        int bot_out_line(int bars); // if DBL_BOX true draw second bottom line
        
        int main(int n_args, char* args[])
        {
           // setlocale(LC_ALL, "pl_PL.utf8"); //local na utf8-Polish type
        
           int ltrs;
           char *str1, *str2, *str3, *str4, *str5, *str6;
           
           // Sample strings for boxed printing
           str1 = "abggdfrtfdxzX\0";
           str2 = "BAGIHMTKBDXDe\0";
           str3 = "^%#*%^$)v^>jy\0";
           str4 = ",./';|@8&5~12\0";
           str5 = "+=+()8(7>>54B\0";
           str6 = "hfH*Ongf^^r_-\0";
        
            print_nl(3); // print 3 newlines
        
            top_line(strlen(str1)); // print top line of str1 length
        
            ltrs = box_letters(str1); // print boxed letters
        
            mid_line(strlen(str1)); // print middle box line
        
            ltrs = box_letters(str2);
        
            mid_line(strlen(str2));
        
            ltrs = box_letters(str3);
        
            mid_line(strlen(str3));
        
            ltrs = box_letters(str4);
        
            mid_line(strlen(str6));
        
            ltrs = box_letters(str5);
        
            mid_line(strlen(str5));
        
            ltrs = box_letters(str6);
            
            bot_line(strlen(str4)); // print the bottom line of the box
        
            print_nl(5);
        
        
        exit(0);
        }
        // ===============================================
        void box_print(char *gr1, char *gr2, int repeat)
        {
            int i;
            i = 0;
            if (repeat > 1)
            {
                for (i = 1; i < repeat; i++)
                {
                    printf("%s", gr1); printf("%s", gr2);
                }
            }
            printf("%s", gr1);
            return;
        }
        //==================
        void print_nl(count)
        {
            int i;
            if (count > 1)
            {
            for (i = 0; i < count; i++)
                printf("\n");
            }
            else
                printf("\n");
            return;
        }
        //=============================
        int box_letters(char *letters)
        {
            int i = 0;
            int j = 0;
            print_nl(1);
            if (RIGHT)
            {
                if(DBL_BOX)
                {
                for( j = 1; j < RIGHT; j++) putchar(' '); box_print(VL, VL, 1);
                }
                else
                {
                for (j = 0; j < RIGHT; j++) putchar(' ');
                }
            }
            box_print(VL, VL, 1);
            while (letters[i])
                {
                printf("%c", letters[i++]);
                box_print(VL, VL, 1);
                }
            if (DBL_BOX) box_print(VL, VL, 1);
            print_nl(1);
            return(i);
        }
        //================
        int top_line(bars)
        {
            int i;
            if (DBL_BOX)
            {
            top_out_line(bars + 1);
            print_nl(1);
            }
            if (RIGHT)
                {
                if (DBL_BOX)
                    {
                    for( i = 1; i < RIGHT; i++) putchar(' ');
                    box_print(VL, VL, 1);
                    }
                else
                    {
                    for( i = 0; i < RIGHT; i++) putchar(' ');
                    }
                }
            box_print(LT, LT, 1);
            box_print(HL, TC, bars);
            box_print(RT, RT,1);
            if (DBL_BOX) box_print(VL, VL, 1);
            return(0);
        }
        //=================
        int bot_line(bars)
        {
            int i;
            if (RIGHT)
                {
                if (DBL_BOX)
                    {
                    for( i = 1; i < RIGHT; i++) putchar(' ');
                    box_print(VL, VL, 1);
                    }
                else
                    {
                    for( i = 0; i < RIGHT; i++) putchar(' ');
                    }
                }
            box_print(LB, LB, 1);
            box_print(HL, BC, bars);
            box_print(RB, RB,1);
            if (DBL_BOX)
            {
            box_print(VL, VL, 1);
            print_nl(1);
            bot_out_line(bars + 1);
            }
            return(0);
        }
        //================
        int mid_line(bars)
        {
            int i;
            if (RIGHT)
            {
                if(DBL_BOX)
                {
                for( i = 1; i < RIGHT; i++) putchar(' '); box_print(VL, VL, 1);
                }
                else
                {
                for (i = 0; i < RIGHT; i++) putchar(' ');
                }
            }
            box_print(LC, LC, 1);
            box_print(HL, MC, bars);
            box_print(RC, RC,1);
            if (DBL_BOX) box_print(VL, VL, 1);
            return(0);
        }
        //====================
        int top_out_line(bars)
        {
            int i;
            if (RIGHT)
            { for( i = 0; i < RIGHT - 1; i++) putchar(' '); }
            box_print(LT, LT, 1);
            box_print(HL, HL, bars);
            box_print(RT, RT,1);
            return(0);
        }
        //====================
        int bot_out_line(bars)
        {
            int i;
            if (RIGHT)
            { for( i = 0; i < RIGHT - 1; i++) putchar(' '); }
            box_print(LB, LB, 1);
            box_print(HL, HL, bars);
            box_print(RB, RB,1);
            return(0);
        }

Hi all,

I posted the code above but now I am not sure if it is the right place and if that is what admins/moderators mean by snippets.

I posted it also with the hope to see if there are some simpler sollutions to parts of the code, maybe more efficient, faster not necessary more difficult to read, usage of header file and placing variables ect. I did some coding many, many years ago but not very complex in nature and I am trying to revive my memories. I thought also that maybe this would help younger coders/students to grasp some aspects of 'C'.
Ciao

Failed to compile with gcc running on linux

cr@cr-Aspire-V5-122P:~/Documents/cpp$ gcc -Wall test.cc
test.cc: In function ‘int main(int, char**)’:
test.cc:68:19: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
test.cc:69:19: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
test.cc:70:19: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
test.cc:71:19: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
test.cc:72:19: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
test.cc:73:19: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
test.cc:64:16: warning: variable ‘ltrs’ set but not used [-Wunused-but-set-variable]
test.cc: At global scope:
test.cc:124:28: error: variable or field ‘print_nl’ declared void
test.cc:124:23: error: ‘count’ was not declared in this scope

You may be suprised how heavily used console interfaces are used in these modern times. I work with them and build them on a near daily basis in Linux for high end modern enterprise software.

As much as I love building things from scratch, I also consider the value of using a robust and community tested system like ncurses, cdk, and dialog which lets you get straight to the menu building. But then again you'd miss out on some really great coding practice.

The article starter is a financial contributor. Sponsored articles offer a bounty for quality replies.