Computer Science Canada

Input and output at same time

Author:  echeese [ Thu Jan 29, 2004 8:24 pm ]
Post subject:  Input and output at same time

I'm trying to finish my chat program, and I need a way to get input without interrupting the chat's output. Can anyone give me an example of a way to put output to the screen and get input at the same time?

They must be able to watch the chat happen as they type.

Author:  shorthair [ Thu Jan 29, 2004 8:32 pm ]
Post subject: 

process , one word thats all

Author:  echeese [ Thu Jan 29, 2004 8:40 pm ]
Post subject: 

That doesn't work Sad
Can anyone come up with an idea using getch?

Author:  shorthair [ Thu Jan 29, 2004 8:42 pm ]
Post subject: 

it has to work , thats how i did mine , and mine works awsome , let me see some code so i can figure out why it wont work Very Happy Very Happy

Author:  Paul [ Thu Jan 29, 2004 8:42 pm ]
Post subject: 

Im curious, how does getch help? doesn't it only take one character at a time? So, if you use it in a chat program and send one character at a time?

Author:  echeese [ Thu Jan 29, 2004 8:43 pm ]
Post subject: 

code:
var debug : boolean := false
var irc, ran : int
var tmp, data, svrname : string := ""
var fnum : int
open : fnum, "file.dat", put
irc := Net.OpenConnection ("irc.avalonworks.ca", 6667)
put : irc, "USER echeese echeese echeese :echeese"
randint (ran, 1000, 9999)
put : irc, "NICK bot" + intstr (ran)

proc setcolor (col : int)
    if col = 0 then
        color (0)
    elsif col = 1 then
        color (7)
    elsif col = 2 then
        color (1)
    elsif col = 3 then
        color (2)
    elsif col = 4 then
        color (12)
    elsif col = 5 then
        color (4)
    elsif col = 6 then
        color (5)
    elsif col = 7 then
        color (6)
    elsif col = 8 then
        color (14)
    elsif col = 9 then
        color (10)
    elsif col = 11 then
        color (11)
    elsif col = 12 then
        color (9)
    elsif col = 13 then
        color (13)
    elsif col = 14 then
        color (15)
    elsif col = 15 then
        color (8)
    else
        color (7)
    end if
end setcolor

proc colorize (data : string)
    var j : int := 1
    loop
        exit when j = length (data) + 1
        if (ord (data (j)) = 3) then
            if j < length (data) then
                if (ord (data (j + 1)) > 47) and (ord (data (j + 1)) < 58) and (ord (data (j + 2)) > 47) and (ord (data (j + 2)) < 58) then
                    setcolor (strint (data (j + 1)) * 10 + (strint (data (j + 2))))
                    j += 3
                elsif (ord (data (j + 1)) > 47) and (ord (data (j + 1)) < 58) then
                    setcolor (strint (data (j + 1)))
                    j += 2
                else
                    color (7)
                    j += 1
                end if
            else
                j += 1
            end if
        else
            put data (j) ..
            j += 1
        end if
    end loop
    color (7)
end colorize

function getw (s : string, separator : char, n : int) : string
    var wi := 0
    var s2 := ""
    for i : 1 .. length (s)
        if wi >= n then
            if s (i) = separator then
                result s2
            end if
            s2 += s (i)
        end if
        if s (i) = separator then
            wi += 1
        end if
    end for
    if s2 not= "" then
        result s2
    else
        result ""
    end if
end getw

function getws (s : string, separator : char, n : int) : string
    var wi := 0
    var s2 := ""
    for i : 1 .. length (s)
        if wi >= n then
            s2 += s (i)
        end if
        if s (i) = separator then
            wi += 1
        end if
    end for
    if s2 not= "" then
        result s2
    else
        result ""
    end if
end getws

proc parse (data : string)
    if getw (data, " ", 0) = "PING" then
        put : irc, "PONG ", getws (data, " ", 1)
    elsif getw (data, " ", 1) = "MODE" then
        color (2)
        put "* ", getw (data, " ", 0) (2 .. *), " sets mode: ", getws (data, " ", 2)
        color (7)
    elsif getw (data, " ", 1) = "JOIN" then
        color (2)
        put "* ", getw (data, "!", 0) (2 .. *), " has joined ", getws (data, " ", 2) (2 .. *)
        color (7)
    elsif getw (data, " ", 1) = "NICK" then
        color (2)
        put "* ", getw (data, "!", 0) (2 .. *), " is now known as ", getws (data, " ", 2) (2 .. *)
        color (7)
    elsif getw (data, " ", 1) = "001" then
        put getws (data, " ", 3) (2 .. *)
        svrname := getw (data, " ", 0) (2 .. *)
    elsif getw (data, " ", 1) = "002" then
        put getws (data, " ", 3) (2 .. *)
    elsif getw (data, " ", 1) = "003" then
        put getws (data, " ", 3) (2 .. *)
    elsif getw (data, " ", 1) = "251" then
        put getws (data, " ", 3) (2 .. *)
    elsif getw (data, " ", 1) = "252" then
        put getw (data, " ", 3), " " ..
        put "IRC Ops online"
    elsif getw (data, " ", 1) = "254" then
        put getw (data, " ", 3), " " ..
        put "channels formed"
    elsif getw (data, " ", 1) = "255" then
        put getws (data, " ", 3) (2 .. *)
    elsif getw (data, " ", 1) = "265" then
        put getws (data, " ", 3) (2 .. *)
    elsif getw (data, " ", 1) = "266" then
        put getws (data, " ", 3) (2 .. *)
    elsif getw (data, " ", 1) = "372" then
        put getws (data, " ", 3) (2 .. *)
    elsif getw (data, " ", 1) = "375" then
        put "Message of the day"
    elsif getw (data, " ", 1) = "376" then
        put : irc, "JOIN #bots"
    elsif getw (data, " ", 1) = "PRIVMSG" then
        put "<", getw (data, "!", 0) (2 .. *), ":", getw (data, " ", 2), "> " ..
        tmp := getws (data, " ", 3) (2 .. *)
        colorize (tmp)
        put ""
    elsif getw (data, " ", 1) = "NOTICE" then
        color (4)
        put "-", getw (data, "!", 0) (2 .. *), "- " ..
        tmp := getws (data, " ", 3) (2 .. *)
        colorize (tmp)
        put ""
    else
        if debug = true then
            put data
        end if
    end if
end parse
put : irc, "JOIN #bots"
loop
    if Net.CharAvailable (irc) then
        loop
            get : irc, tmp : 1
            exit when ord (tmp) = 10
            if length (data) = 255 then
                exit
            else
                data += tmp
            end if
        end loop
        parse (data)
        data := ""
    end if
end loop

Author:  we64 [ Thu Jan 29, 2004 9:13 pm ]
Post subject: 

pretty complicated, I guess it is beyond my knowledge...

Author:  echeese [ Fri Jan 30, 2004 10:28 am ]
Post subject: 

C'mon! Don't tell me it's IMPOSSIBLE! Can someone at least give me an idea?

Author:  echeese [ Fri Jan 30, 2004 10:49 am ]
Post subject: 

Well, I SORTA got it. I opened a new window for input. It's kinda cheap, but hey, it works. If anyone has a better idea, tell me.

Author:  sport [ Fri Jan 30, 2004 3:38 pm ]
Post subject: 

Run the input in one process and output in the other. Process is the only solution and it works!!!

Author:  Cervantes [ Fri Jan 30, 2004 3:57 pm ]
Post subject: 

process will do it...

um, just to make certain, you have to use the fork command when you use processes though.

Cheers

Author:  shorthair [ Fri Jan 30, 2004 4:14 pm ]
Post subject: 

I said process right from the start , but he didnt want t ohear it , said it didnt work , but i know thats the way to do it

Author:  Cervantes [ Fri Jan 30, 2004 5:34 pm ]
Post subject: 

yeah I know you said that, I was just stating the fact that I agree with you Smile

Author:  shorthair [ Fri Jan 30, 2004 6:27 pm ]
Post subject: 

I wasnt countering Your Comment CErvantes ,sorry it loks like i did , hte comment was for echeese that said it shouldnt be that way , were all right about hte process ,even though they suck its tehe way to go , make a process then open up task manager , your cpu use is 100% always ( well on my apps may be different for someone else) ,

Author:  echeese [ Fri Jan 30, 2004 6:53 pm ]
Post subject: 

Ok.. Listen, I used process and I was trying to type a message, and it ended up cut-up all over the place

eg

Hell[18:52] * Apage43 has left #oot
o everyb[18:52] <craig443:#oot> Hey guys
ody!

Author:  shorthair [ Fri Jan 30, 2004 6:56 pm ]
Post subject: 

turing messes up messages ,if your goint to do it , you have to learrn to add 10 spaces to the beginning of each message , so it will only cut off spaces , also its been noted that the NET commands are weak ,and were not designed for intense use , edit : I GOT YOUR MESSAGE WRONG , and about that Cervantes is right , but rewmember there is a 255 character limit to a line , turing may be interperating some values or bytes as characters , that couldbe what is splitting the message , or the message could include hidden charaters , i noticed it looks like it sbeing used for IRC , it wouldnt surprise me if there were hidden values and charaacter in the messages

Author:  Cervantes [ Fri Jan 30, 2004 7:45 pm ]
Post subject: 

adding 10 spaces.. in echeese's example all the problem wasn't that characters were missing its that the lines were getting screwed up.

I'd suggest playing around with locate and having variables inside the locate that increase with every post.

btw, in this are the characters appearing as they are typed, or only when you hit 'enter'
i'd assume you're doing it by the characters because I dont think that the lines could get screwed up like that if you used the enter style.
if you really want to do it by the characters then you should do like they did in Warcraft II 8) have a box beside each person's name that is used to input the characters that they type.

Cheers


: