As was foretold, we've added advertisements to the forums! If you have questions, or if you encounter any bugs, please visit this thread: https://forums.penny-arcade.com/discussion/240191/forum-advertisement-faq-and-reports-thread/
We're funding a new Acquisitions Incorporated series on Kickstarter right now! Check it out at https://www.kickstarter.com/projects/pennyarcade/acquisitions-incorporated-the-series-2

PA Programming Thread - The Best Servers Are The Ones You Don't Use

1424345474863

Posts

  • MKRMKR Registered User regular
    edited February 2011
    Echo wrote: »
    I used to write IRC stuff in TCL back when I was a l33t d00d.

    Then I grew hair on my balls and got interested in girls.

    Still a nerd

    MKR on
  • AnteCantelopeAnteCantelope Registered User regular
    edited February 2011
    Jimmy King wrote: »
    One of my uni courses is apparently going to be about Tcl/Tk, which I've never even heard of before. What is it? More importantly, is it useful ever? I had a quick look and it talked about being designed for Internet Explorer and Netscape, which... doesn't fill me with optimism.
    Your needs are much better filled by Perl, Python, or Ruby. You will also grow to hate its bracketiness. Seriously. It's the bracketiest language I've ever seen.

    That's what I was thinking - why are they teaching us this thing I'd never heard of when other scripting languages seem popular and useful?
    And the first page of notes about it we're given has things like brackets being used for order of operations, to print variable names instead of their values, and to signify a code block, all in one segment of code. Yay curly braces, lets use them for everything!

    AnteCantelope on
  • ecco the dolphinecco the dolphin Registered User regular
    edited February 2011
    Jimmy King wrote: »
    One of my uni courses is apparently going to be about Tcl/Tk, which I've never even heard of before. What is it? More importantly, is it useful ever? I had a quick look and it talked about being designed for Internet Explorer and Netscape, which... doesn't fill me with optimism.
    Your needs are much better filled by Perl, Python, or Ruby. You will also grow to hate its bracketiness. Seriously. It's the bracketiest language I've ever seen.

    That's what I was thinking - why are they teaching us this thing I'd never heard of when other scripting languages seem popular and useful?
    And the first page of notes about it we're given has things like brackets being used for order of operations, to print variable names instead of their values, and to signify a code block, all in one segment of code. Yay curly braces, lets use them for everything!

    It does have the advantage that it exposes you to a different language, a different way of thinking.

    As you've pointed out, you're very very likely to encounter Python/Ruby/Perl in practice, so why not expose you to something different?

    ecco the dolphin on
    Penny Arcade Developers at PADev.net.
  • Jimmy KingJimmy King Registered User regular
    edited February 2011
    Jimmy King wrote: »
    One of my uni courses is apparently going to be about Tcl/Tk, which I've never even heard of before. What is it? More importantly, is it useful ever? I had a quick look and it talked about being designed for Internet Explorer and Netscape, which... doesn't fill me with optimism.
    Your needs are much better filled by Perl, Python, or Ruby. You will also grow to hate its bracketiness. Seriously. It's the bracketiest language I've ever seen.

    That's what I was thinking - why are they teaching us this thing I'd never heard of when other scripting languages seem popular and useful?
    And the first page of notes about it we're given has things like brackets being used for order of operations, to print variable names instead of their values, and to signify a code block, all in one segment of code. Yay curly braces, lets use them for everything!

    It does have the advantage that it exposes you to a different language, a different way of thinking.

    As you've pointed out, you're very very likely to encounter Python/Ruby/Perl in practice, so why not expose you to something different?
    And if you happen to live near a semiconductor fab, which there are several in the US, then it may put you at a distinct advantage if you apply for a dev job at one of them since people who have even heard of Tcl, let alone actually used it, are rare.

    Jimmy King on
  • Jimmy KingJimmy King Registered User regular
    edited February 2011
    Oh yeah, I just remembered something else totally awesome about Tcl. Tcl handles comments strangely. They are technically a command more or less and so the whole line still gets looked at by the interpreter. This causes craziness when you comment out a line with a bracket (such as the first line of a for loop so that you can test some change to it but want that kept there so you can go back to it) because it will actually complain that there's no closing bracket for the opening bracket that is in a comment.

    Talk about a mindfuck the first time that happens. I spent hours just re-running the same script over and over trying to figure out how there could possibly be an error on a line when it's a comment.

    Jimmy King on
  • JasconiusJasconius sword criminal mad onlineRegistered User regular
    edited February 2011
    The request library I am using in iOS automatically changes my request method from DELETE to POST if my post body has any data at all. Anything.

    Is this just stupid on their part, or should a DELETE request not have any post data associated with it?

    I have a method that deletes a collection of objects of any size, and I'm sure as hell not going to put their ID's in a querystring. What are my alternatives?

    *edit*

    Apparently mod_wsgi does not even pass a POST body on to the web application when the request method is DELETE. So I guess that answers my question. So is there any RESTful way to delete multiple objects with the same request? Or am I just going to have to make my own handler.

    *edit2*

    Googling tells me most people just do something like example.com/resource{id,id,id,id}

    grumble grumble grmuble

    Jasconius on
  • SenjutsuSenjutsu thot enthusiast Registered User regular
    edited February 2011
    DELETE and PUT are really poorly supported by most pieces of software; that's why rails emulates them with POST params

    Senjutsu on
  • GnomeTankGnomeTank What the what? Portland, OregonRegistered User regular
    edited February 2011
    Well, by most BROWSERS anyway. If you are writing a web service, you can pretty safely rely on PUT and DELETE because any half-way decent web service client should be able to handle PUT and DELETE, and if they can't, either talk to the service raw (straight web requests), or use a better wrapper.

    GnomeTank on
    Sagroth wrote: »
    Oh c'mon FyreWulff, no one's gonna pay to visit Uranus.
    Steam: Brainling, XBL / PSN: GnomeTank, NintendoID: Brainling, FF14: Zillius Rosh SFV: Brainling
  • JasconiusJasconius sword criminal mad onlineRegistered User regular
    edited February 2011
    Well I'm making the service.

    My concept was to send JSON data in a DELETE request that described the object I wanted to delete, specifically the primary key of the entity in question... and of course sending a collection of objects would allow multiple objects to be deleted.

    But it appears that my post body is not even being sent to my application when the request method is DELETE.. which gives me frowny faces.

    Jasconius on
  • SenjutsuSenjutsu thot enthusiast Registered User regular
    edited February 2011
    if I'm reading the standard right, DELETE requests deletion of the resource identified by the URI

    even if you could send a payload in the body it wouldn't really be RESTful per se

    Senjutsu on
  • JasconiusJasconius sword criminal mad onlineRegistered User regular
    edited February 2011
    Yeah.

    I was wrong about the webserver not passing along the POST body. It actually does, I just misinterpreted the error message I was getting.

    So I actually can do what I was describing. And I'm pretty happy with it. It's easier to POST some JSON on objects to delete than it is to construct and deconstruct a URI.

    The django plugin I am using that is supposed to be a REST plugin surprisingly doesn't really address deconstructing a URI that describes multiple objects. So. Just going to do something else.

    Jasconius on
  • GnomeTankGnomeTank What the what? Portland, OregonRegistered User regular
    edited February 2011
    It doesn't do sub-resources? Or it doesn't allow you to do DELETE /object/id,id,id?

    GnomeTank on
    Sagroth wrote: »
    Oh c'mon FyreWulff, no one's gonna pay to visit Uranus.
    Steam: Brainling, XBL / PSN: GnomeTank, NintendoID: Brainling, FF14: Zillius Rosh SFV: Brainling
  • JasconiusJasconius sword criminal mad onlineRegistered User regular
    edited February 2011
    GnomeTank wrote: »
    It doesn't do sub-resources? Or it doesn't allow you to do DELETE /object/id,id,id?

    As far as I'm aware it, it has no faculties for automatically interpreting such URL's (/objects/id,id,id)

    Jasconius on
  • ASimPersonASimPerson Cold... and hard.Registered User regular
    edited February 2011
    Echo wrote: »
    I used to write IRC stuff in TCL back when I was a l33t d00d.

    Then I grew hair on my balls and got interested in girls.

    Were you one of the 10 people besides me who actually used XiRCON?

    ASimPerson on
  • MKRMKR Registered User regular
    edited February 2011
    ASimPerson wrote: »
    Echo wrote: »
    I used to write IRC stuff in TCL back when I was a l33t d00d.

    Then I grew hair on my balls and got interested in girls.

    Were you one of the 10 people besides me who actually used XiRCON?

    I knew a few people who used it. All of them were terrifying people.

    MKR on
  • FremFrem Registered User regular
    edited February 2011
    Jimmy King wrote: »
    One of my uni courses is apparently going to be about Tcl/Tk, which I've never even heard of before. What is it? More importantly, is it useful ever? I had a quick look and it talked about being designed for Internet Explorer and Netscape, which... doesn't fill me with optimism.
    Your needs are much better filled by Perl, Python, or Ruby. You will also grow to hate its bracketiness. Seriously. It's the bracketiest language I've ever seen.

    That's what I was thinking - why are they teaching us this thing I'd never heard of when other scripting languages seem popular and useful?
    And the first page of notes about it we're given has things like brackets being used for order of operations, to print variable names instead of their values, and to signify a code block, all in one segment of code. Yay curly braces, lets use them for everything!

    You know Python? It uses TkInter as a layer on top of Tcl/Tk as the horrible default GUI library. If you use Python a lot, you'll probably encounter it at least once. Even Idle, the horrible default IDE is built on TkInter.

    Of course, Tk is missing a significant amount of widgets, so many that I've never had a GUI project simple enough to use it. If you want progress bars or dropdown lists that you can filter by typing into them, you'll probably have to write to code to display them yourself.

    But still. It's not quite a completely useless language to know.

    Frem on
  • skettiosskettios Enchanted ForestRegistered User regular
    edited February 2011
    Anyone know about php SVN API? Says experimental, but I see stuff from 08. Is it safe or should I switch languages (probably to python, as I see pysvn come up a bunch)

    skettios on
  • EvilMonkeyEvilMonkey Registered User regular
    edited February 2011
    Any WebSphere Portlet programmers in the thread?

    Shamelessly posting my stackoverflow question in hopes of more views.

    EvilMonkey on
    [PSN: SciencePiggy] [Steam]
  • SenjutsuSenjutsu thot enthusiast Registered User regular
    edited February 2011
    EvilMonkey wrote: »
    Any WebSphere Portlet programmers in the thread?

    Shamelessly posting my stackoverflow question in hopes of more views.

    I threw you an upvote. Best I can do since I know nothing about websphere

    Senjutsu on
  • DVGDVG No. 1 Honor Student Nether Institute, Evil AcademyRegistered User regular
    edited February 2011
    Any recommendations for debugging a php script that doesn't produce output and thus I can't rely on echo and var_dump?

    I made some modifications to a phpbb3 installation to make calls to Google Calendar. It does what I wanted to do, but now edit isn't working.

    DVG on
    Diablo 3 - DVG#1857
  • InfidelInfidel Heretic Registered User regular
    edited February 2011
    DVG wrote: »
    Any recommendations for debugging a php script that doesn't produce output and thus I can't rely on echo and var_dump?

    I made some modifications to a phpbb3 installation to make calls to Google Calendar. It does what I wanted to do, but now edit isn't working.

    Output to a file instead?

    Infidel on
    OrokosPA.png
  • DVGDVG No. 1 Honor Student Nether Institute, Evil AcademyRegistered User regular
    edited February 2011
    Good call.

    DVG on
    Diablo 3 - DVG#1857
  • SnowblindvictimSnowblindvictim Flying casual Registered User regular
    edited February 2011
    Is anyone here good with C?
    I just have a quick question/problem that needs solving.

    What would C code look like if I wanted a program (when compiled) to print "1" on a "little endian" machine
    and "0" on a "big/large endian" machine.

    I'm more of an up and coming Java guy not a C guy but this is something that came up.

    Snowblindvictim on
  • InfidelInfidel Heretic Registered User regular
    edited February 2011
    Is anyone here good with C?
    I just have a quick question/problem that needs solving.

    What would C code look like if I wanted a program (when compiled) to print "1" on a "little endian" machine
    and "0" on a "big/large endian" machine.

    I'm more of an up and coming Java guy not a C guy but this is something that came up.

    You would write an is_little_endian() function and printf accordingly? :)

    The real question: Do you know how to write something like is_little_endian?

    Infidel on
    OrokosPA.png
  • ecco the dolphinecco the dolphin Registered User regular
    edited February 2011
    Infidel wrote: »
    Is anyone here good with C?
    I just have a quick question/problem that needs solving.

    What would C code look like if I wanted a program (when compiled) to print "1" on a "little endian" machine
    and "0" on a "big/large endian" machine.

    I'm more of an up and coming Java guy not a C guy but this is something that came up.

    You would write an is_little_endian() function and printf accordingly? :)

    The real question: Do you know how to write something like is_little_endian?

    Experiencing flashbacks, Infidel? :P

    A couple of ways to do it - quite a few are covered here.

    ecco the dolphin on
    Penny Arcade Developers at PADev.net.
  • EndEnd Registered User regular
    edited February 2011
    skettios wrote: »
    Anyone know about php SVN API? Says experimental, but I see stuff from 08. Is it safe or should I switch languages (probably to python, as I see pysvn come up a bunch)

    I've never touched it, but it looks like a relatively low level wrapper, so the API probably wouldn't change much. However, you might have to worry about bitrot. Fortunately, subversion's api tends to be pretty stable, but unfortunately, I don't think php's is quite as stable.

    What are you trying to do exactly I wonder? Just a repository viewer?

    End on
    I wish that someway, somehow, that I could save every one of us
    zaleiria-by-lexxy-sig.jpgsteam~tinythumb.png
  • InfidelInfidel Heretic Registered User regular
    edited February 2011
    Infidel wrote: »
    Is anyone here good with C?
    I just have a quick question/problem that needs solving.

    What would C code look like if I wanted a program (when compiled) to print "1" on a "little endian" machine
    and "0" on a "big/large endian" machine.

    I'm more of an up and coming Java guy not a C guy but this is something that came up.

    You would write an is_little_endian() function and printf accordingly? :)

    The real question: Do you know how to write something like is_little_endian?

    Experiencing flashbacks, Infidel? :P

    A couple of ways to do it - quite a few are covered here.

    Someone mentioned endian so I reversed all mah bits! :^:

    Infidel on
    OrokosPA.png
  • EvilMonkeyEvilMonkey Registered User regular
    edited February 2011
    Senjutsu wrote: »
    EvilMonkey wrote: »
    Any WebSphere Portlet programmers in the thread?

    Shamelessly posting my stackoverflow question in hopes of more views.

    I threw you an upvote. Best I can do since I know nothing about websphere
    Thanks for the votes (up to 3, assuming they all came from PA'ers :))

    Time to medicate myself with beer and pretend I didn't hit this road block.

    EvilMonkey on
    [PSN: SciencePiggy] [Steam]
  • JasconiusJasconius sword criminal mad onlineRegistered User regular
    edited February 2011
    Anyone here done WebDAV servers before? Or programmed anything for WebDAV?

    Jasconius on
  • LednehLedneh shinesquawk Registered User regular
    edited February 2011
    This is gonna be an awkward question because I don't quite know how to ask it. Can Visual Studio be used to develop on another server via SSH?

    We have an application that we log into via SSH to do our work, usually in emacs or NetBeans if we really need an IDE. This is getting beyond irksome., so I thought maybe there's a way to install Visual Studio on our (Windows) machines and code/compile remotely, or something.

    Thoughts?

    Ledneh on
  • InfidelInfidel Heretic Registered User regular
    edited February 2011
    Ledneh wrote: »
    This is gonna be an awkward question because I don't quite know how to ask it. Can Visual Studio be used to develop on another server via SSH?

    We have an application that we log into via SSH to do our work, usually in emacs or NetBeans if we really need an IDE. This is getting beyond irksome., so I thought maybe there's a way to install Visual Studio on our (Windows) machines and code/compile remotely, or something.

    Thoughts?

    You can use the MS compiler remotely. What exactly do you want to do? Edit remote code in a local IDE, compile on remote machine?

    Infidel on
    OrokosPA.png
  • LednehLedneh shinesquawk Registered User regular
    edited February 2011
    Infidel wrote: »
    Ledneh wrote: »
    This is gonna be an awkward question because I don't quite know how to ask it. Can Visual Studio be used to develop on another server via SSH?

    We have an application that we log into via SSH to do our work, usually in emacs or NetBeans if we really need an IDE. This is getting beyond irksome., so I thought maybe there's a way to install Visual Studio on our (Windows) machines and code/compile remotely, or something.

    Thoughts?

    You can use the MS compiler remotely. What exactly do you want to do? Edit remote code in a local IDE, compile on remote machine?

    That, yeah. I'd also like to be able to compile the remote code locally for testing (fortunately our code is sound as far as potential cross-platform issues go).

    Ledneh on
  • InfidelInfidel Heretic Registered User regular
    edited February 2011
    Ledneh wrote: »
    Infidel wrote: »
    Ledneh wrote: »
    This is gonna be an awkward question because I don't quite know how to ask it. Can Visual Studio be used to develop on another server via SSH?

    We have an application that we log into via SSH to do our work, usually in emacs or NetBeans if we really need an IDE. This is getting beyond irksome., so I thought maybe there's a way to install Visual Studio on our (Windows) machines and code/compile remotely, or something.

    Thoughts?

    You can use the MS compiler remotely. What exactly do you want to do? Edit remote code in a local IDE, compile on remote machine?

    That, yeah. I'd also like to be able to compile the remote code locally for testing (fortunately our code is sound as far as potential cross-platform issues go).

    Use Visual Studio and MSBuild then? Work locally, send to build server in some fashion (usually TFS/source control), profit?

    Infidel on
    OrokosPA.png
  • SnowblindvictimSnowblindvictim Flying casual Registered User regular
    edited February 2011
    Infidel wrote: »
    Infidel wrote: »
    Is anyone here good with C?
    I just have a quick question/problem that needs solving.

    What would C code look like if I wanted a program (when compiled) to print "1" on a "little endian" machine
    and "0" on a "big/large endian" machine.

    I'm more of an up and coming Java guy not a C guy but this is something that came up.

    You would write an is_little_endian() function and printf accordingly? :)

    The real question: Do you know how to write something like is_little_endian?

    Experiencing flashbacks, Infidel? :P

    A couple of ways to do it - quite a few are covered here.

    Someone mentioned endian so I reversed all mah bits! :^:

    I am completely new to C.
    And my current understanding is that the code I would write would go into a .txt file
    and then have the SSH shell or whatever read the code from the txt to compile.

    That being said for my big endian/little endian 1 and 0 problem would this be proper code?
    int num =1;
    if(*(char*)&num == 1)
    {
    	printf("\nLittle-Endian\n");
    }
    
    else
    
    {
    	printf("Big-Endian\n");
    }
    

    Is that right? That was taken from that link

    Basically I need to know what would be saved as a .c file

    Snowblindvictim on
  • skettiosskettios Enchanted ForestRegistered User regular
    edited February 2011
    End wrote: »
    skettios wrote: »
    Anyone know about php SVN API? Says experimental, but I see stuff from 08. Is it safe or should I switch languages (probably to python, as I see pysvn come up a bunch)

    I've never touched it, but it looks like a relatively low level wrapper, so the API probably wouldn't change much. However, you might have to worry about bitrot. Fortunately, subversion's api tends to be pretty stable, but unfortunately, I don't think php's is quite as stable.

    What are you trying to do exactly I wonder? Just a repository viewer?

    Decided to stick with it and it's been working just fine.

    Essentially writing an SVN deployer.
    But ya, have to view/select the repo you want to deploy and where.

    skettios on
  • LednehLedneh shinesquawk Registered User regular
    edited February 2011
    Infidel wrote: »
    Ledneh wrote: »
    Infidel wrote: »
    Ledneh wrote: »
    This is gonna be an awkward question because I don't quite know how to ask it. Can Visual Studio be used to develop on another server via SSH?

    We have an application that we log into via SSH to do our work, usually in emacs or NetBeans if we really need an IDE. This is getting beyond irksome., so I thought maybe there's a way to install Visual Studio on our (Windows) machines and code/compile remotely, or something.

    Thoughts?

    You can use the MS compiler remotely. What exactly do you want to do? Edit remote code in a local IDE, compile on remote machine?

    That, yeah. I'd also like to be able to compile the remote code locally for testing (fortunately our code is sound as far as potential cross-platform issues go).

    Use Visual Studio and MSBuild then? Work locally, send to build server in some fashion (usually TFS/source control), profit?
    I'll look into MSBuild, thanks, hadn't heard of that one. Does VS integrate with Rational Clearcase in any nice way?

    Ledneh on
  • PapillonPapillon Registered User regular
    edited February 2011
    Infidel wrote: »
    Infidel wrote: »
    Is anyone here good with C?
    I just have a quick question/problem that needs solving.

    What would C code look like if I wanted a program (when compiled) to print "1" on a "little endian" machine
    and "0" on a "big/large endian" machine.

    I'm more of an up and coming Java guy not a C guy but this is something that came up.

    You would write an is_little_endian() function and printf accordingly? :)

    The real question: Do you know how to write something like is_little_endian?

    Experiencing flashbacks, Infidel? :P

    A couple of ways to do it - quite a few are covered here.

    Someone mentioned endian so I reversed all mah bits! :^:

    I am completely new to C.
    And my current understanding is that the code I would write would go into a .txt file
    and then have the SSH shell or whatever read the code from the txt to compile.

    That being said for my big endian/little endian 1 and 0 problem would this be proper code?
    int num =1;
    if(*(char*)&num == 1)
    {
    	printf("\nLittle-Endian\n");
    }
    
    else
    
    {
    	printf("Big-Endian\n");
    }
    

    Is that right? That was taken from that link

    Basically I need to know what would be saved as a .c file

    That'll work. Some compilers/OSes also define preprocessor directives for byte order e.g. gcc's __BYTE_ORDER__. Why do you need to know the endian-ness? If you need to write to a file/network in a machine independent order, you should probably use something like htonl() instead.

    Papillon on
  • Monkey Ball WarriorMonkey Ball Warrior A collection of mediocre hats Seattle, WARegistered User regular
    edited February 2011
    Here's an easier question: What is the correct way to properly do command-line interactive programs in C. I feel like the way I normally do it is incredably fragile...

    Here was I was working on tonight. It's homework, where we are simulating an ALU. The test program here isn't really the main point of the project, so I don't feel bad asking about it. scanf is such a house of cards sometimes.
    int main(void) {
        RegisterFilePtr rf = registerFile_initialize();
        RegisterPtr status = register_initialize();
        Alu alu = alu_initialize(rf, status);
        printf("%s", registerFile_toString(rf));
    
        int running = TRUE;
        int input = 0;
        int value = 0;
        int error = NO_ERROR;
    
        while (running == TRUE) {
            printf("What would you like to do?\n");
            printf("\t1: Print register file.\n");
            printf("\t2: Set a register.\n");
            printf("\t3: Perform an ALU operation.\n");
            printf("\t0: Exit.\n:");
            //read input
            if (scanf("%i", &input) == 1){
                //determine what to do
                switch (input){
                    //Exit
                    case 0: running = FALSE; break;
                    //Print register file
                    case 1: printf("%s", registerFile_toString(rf)); break;
                    //Set register
                    case 2:
                        //get register choice
                        printf("Which register to set?\n:");
                        if (scanf("%i", &input) != 1){printf("What?\n"); break;}
                        //get register value
                        printf("\nInput new value.\n:");
                        if (scanf("%i", &value) != 1){printf("What?\n"); break;}
                        //Perform register setting
                        error = registerFile_putRegValue(rf, (uchar) input,
                                                         (uchar) value);
                        //report results
                        if (error == NO_ERROR){
                            printf("\nRegister $%X set to %04X\n", input, value);
                        } else {
                            printf("\nError!\n");
                        }
                        break;
                    case 3:
                        //get operation choice
                        printf("Which ALU operation would you like to perform?\n");
                        printf("ADD:\t0x0\t(A+B->R)\n");
                        printf("SUB:\t0x1\t(A-B->R)\n");
                        printf("NEG:\t0x2\t(-A->R)\n");
                        printf("MUL:\t0x3\t(A*B->[R8,R9])\n");
                        printf("DIV:\t0x4\t(A/B->RA, A%B->RB)\n");
                        printf("AND:\t0x5\t(A&B->R)\n");
                        printf("OR:\t0x6\t(A|B->R)\n");
                        printf("XOR:\t0x7\t(A^B->R)\n");
                        printf("NOT:\t0x8\t(~A->R)\n");
                        printf("SFL:\t0x9\t(A<<1->R)\n");
                        printf("SFR:\t0xA\t(A>>1->R)\n");
                        if (scanf("%i", &input) != 1){printf("What?\n"); break;}
                        //get first operand
                        printf("Which register is first operand?\n:");
                        if (scanf("%i", &value) != 1){
                            printf("What?\n"); break;
                        } else {
                            error = alu_loadFromReg(alu, value, $RALUA);
                            if (error == NO_ERROR){
                                printf("\nLoaded ALU register A with value");
                                printf(" in register $R%X\n", value);
                            } else {
                                printf("\nError(%X)\n", error);
                            }
                        }
                        //get second operand
                        int choice;
                        printf("Load second operand from register (0)\n");
                        printf("Or load an immediate value? (1)\n:");
                        if (scanf("%i", &choice) != 1){printf("What?"); break;}
    
                        //Load B from register
                        if (choice == 0){
                            printf("Which register is second operand?\n:");
                            if (scanf("%i", &value) != 1){
                                printf("What?\n"); break;
                            } else {
                                error = alu_loadFromReg(alu, (uchar) value, $RALUB);
                                if (error == NO_ERROR){
                                    printf("\nLoaded ALU register B with value");
                                    printf(" in register $R%X\n\n", value);
                                } else {
                                    printf("\nError(%X)\n", error);
                                }
                            }
                        } else {//Load B from immediate
                            printf("What value for second operand?\n:");
                            if (scanf("%i", &value) != 1){
                                printf("What?\n"); break;
                            } else {
                                error = alu_loadFromImmediate(alu, (ushort) value);
                                if (error == NO_ERROR){
                                    printf("\nLoaded ALU register B with value");
                                    printf("%04X\n\n", value);
                                } else {
                                    printf("\nError(%X)\n", error);
                                }
                            }
                        }
                        alu_operation(alu, input);
                        printf("RegA: %04X\n", alu_getRegValue(alu, $RALUA, &error));
                        printf("RegB: %04X\n", alu_getRegValue(alu, $RALUB, &error));
                        printf("RegR: %04X\n", alu_getRegValue(alu, $RALUR, &error));
                }
            } else {
                //clear the input buffer
                while (getchar() != '\n'){};
            }
        }
    }
    

    Monkey Ball Warrior on
    "I resent the entire notion of a body as an ante and then raise you a generalized dissatisfaction with physicality itself" -- Tycho
  • ASimPersonASimPerson Cold... and hard.Registered User regular
    edited February 2011
    Here's an easier question: What is the correct way to properly do command-line interactive programs in C. I feel like the way I normally do it is incredably fragile...

    Here was I was working on tonight. It's homework, where we are simulating an ALU. The test program here isn't really the main point of the project, so I don't feel bad asking about it. scanf is such a house of cards sometimes.
    int main(void) {
        RegisterFilePtr rf = registerFile_initialize();
        RegisterPtr status = register_initialize();
        Alu alu = alu_initialize(rf, status);
        printf("%s", registerFile_toString(rf));
    
        int running = TRUE;
        int input = 0;
        int value = 0;
        int error = NO_ERROR;
    
        while (running == TRUE) {
            printf("What would you like to do?\n");
            printf("\t1: Print register file.\n");
            printf("\t2: Set a register.\n");
            printf("\t3: Perform an ALU operation.\n");
            printf("\t0: Exit.\n:");
            //read input
            if (scanf("%i", &input) == 1){
                //determine what to do
                switch (input){
                    //Exit
                    case 0: running = FALSE; break;
                    //Print register file
                    case 1: printf("%s", registerFile_toString(rf)); break;
                    //Set register
                    case 2:
                        //get register choice
                        printf("Which register to set?\n:");
                        if (scanf("%i", &input) != 1){printf("What?\n"); break;}
                        //get register value
                        printf("\nInput new value.\n:");
                        if (scanf("%i", &value) != 1){printf("What?\n"); break;}
                        //Perform register setting
                        error = registerFile_putRegValue(rf, (uchar) input,
                                                         (uchar) value);
                        //report results
                        if (error == NO_ERROR){
                            printf("\nRegister $%X set to %04X\n", input, value);
                        } else {
                            printf("\nError!\n");
                        }
                        break;
                    case 3:
                        //get operation choice
                        printf("Which ALU operation would you like to perform?\n");
                        printf("ADD:\t0x0\t(A+B->R)\n");
                        printf("SUB:\t0x1\t(A-B->R)\n");
                        printf("NEG:\t0x2\t(-A->R)\n");
                        printf("MUL:\t0x3\t(A*B->[R8,R9])\n");
                        printf("DIV:\t0x4\t(A/B->RA, A%B->RB)\n");
                        printf("AND:\t0x5\t(A&B->R)\n");
                        printf("OR:\t0x6\t(A|B->R)\n");
                        printf("XOR:\t0x7\t(A^B->R)\n");
                        printf("NOT:\t0x8\t(~A->R)\n");
                        printf("SFL:\t0x9\t(A<<1->R)\n");
                        printf("SFR:\t0xA\t(A>>1->R)\n");
                        if (scanf("%i", &input) != 1){printf("What?\n"); break;}
                        //get first operand
                        printf("Which register is first operand?\n:");
                        if (scanf("%i", &value) != 1){
                            printf("What?\n"); break;
                        } else {
                            error = alu_loadFromReg(alu, value, $RALUA);
                            if (error == NO_ERROR){
                                printf("\nLoaded ALU register A with value");
                                printf(" in register $R%X\n", value);
                            } else {
                                printf("\nError(%X)\n", error);
                            }
                        }
                        //get second operand
                        int choice;
                        printf("Load second operand from register (0)\n");
                        printf("Or load an immediate value? (1)\n:");
                        if (scanf("%i", &choice) != 1){printf("What?"); break;}
    
                        //Load B from register
                        if (choice == 0){
                            printf("Which register is second operand?\n:");
                            if (scanf("%i", &value) != 1){
                                printf("What?\n"); break;
                            } else {
                                error = alu_loadFromReg(alu, (uchar) value, $RALUB);
                                if (error == NO_ERROR){
                                    printf("\nLoaded ALU register B with value");
                                    printf(" in register $R%X\n\n", value);
                                } else {
                                    printf("\nError(%X)\n", error);
                                }
                            }
                        } else {//Load B from immediate
                            printf("What value for second operand?\n:");
                            if (scanf("%i", &value) != 1){
                                printf("What?\n"); break;
                            } else {
                                error = alu_loadFromImmediate(alu, (ushort) value);
                                if (error == NO_ERROR){
                                    printf("\nLoaded ALU register B with value");
                                    printf("%04X\n\n", value);
                                } else {
                                    printf("\nError(%X)\n", error);
                                }
                            }
                        }
                        alu_operation(alu, input);
                        printf("RegA: %04X\n", alu_getRegValue(alu, $RALUA, &error));
                        printf("RegB: %04X\n", alu_getRegValue(alu, $RALUB, &error));
                        printf("RegR: %04X\n", alu_getRegValue(alu, $RALUR, &error));
                }
            } else {
                //clear the input buffer
                while (getchar() != '\n'){};
            }
        }
    }
    

    scanf() tends to be a house of cards mainly due to its vulnerabilities more than anything else. Anyway, I don't really see anything wrong here (other than the lack of a default case in your switch statement). I would probably abstract some of those larger switch cases into functions though.

    I gotta say, though, if you actually had to write this out what a huge waste of time. The focus should be on the ALU part. Much easier not worry about interactivity and just let you parse a file or something. Geez.

    ASimPerson on
  • ASimPersonASimPerson Cold... and hard.Registered User regular
    edited February 2011
    Infidel wrote: »
    Infidel wrote: »
    Is anyone here good with C?
    I just have a quick question/problem that needs solving.

    What would C code look like if I wanted a program (when compiled) to print "1" on a "little endian" machine
    and "0" on a "big/large endian" machine.

    I'm more of an up and coming Java guy not a C guy but this is something that came up.

    You would write an is_little_endian() function and printf accordingly? :)

    The real question: Do you know how to write something like is_little_endian?

    Experiencing flashbacks, Infidel? :P

    A couple of ways to do it - quite a few are covered here.

    Someone mentioned endian so I reversed all mah bits! :^:

    I am completely new to C.
    And my current understanding is that the code I would write would go into a .txt file
    and then have the SSH shell or whatever read the code from the txt to compile.

    That being said for my big endian/little endian 1 and 0 problem would this be proper code?
    int num =1;
    if(*(char*)&num == 1)
    {
    	printf("\nLittle-Endian\n");
    }
    
    else
    
    {
    	printf("Big-Endian\n");
    }
    

    Is that right? That was taken from that link

    Basically I need to know what would be saved as a .c file

    You need to surround it in a int main() block. Here's what your whole C file would be:
    #include <stdio.h>
    
    int main (void)
    {
        int num =1;
        if(*(char*)&num == 1) {
            printf("\nLittle-Endian\n");
        } else {
            printf("Big-Endian\n");
        }
        return 0;
    }
    

    And an explanation, if you really want to know:
    You're declaring an integer called "num". This integer has a value of "1". ints are 32-bit on most (if not all) platforms, so this is 31 zeroes in front of a 1.

    Well, sort of. You might know this part, since you're asking. The actual representation of the integer in memory depends on the machine's endianness. On a big endian machine, the most-significant (i.e., left-most if you were writing it down on paper) bit has the lowest memory address, and the least significant has the highest. Little endian machines are the opposite. You could say that the number would be "backward" in memory but that's not entirely correct. However, that's the trick this example relies on.

    This example takes heavy advantage of C-pointer/casting trickery to accomplish its goals. Here's what "*(char*)&num" does:
    1. "&num" - gets the address of variable num. This means that the value of "&num" is a memory address, i.e., a pointer.
    2. "(char*)&num" - this casts the pointer that we just created to be a character pointer instead of an integer pointer.
    3. "*(char*)&num" - the "*" says to return the value of the address referenced by the pointer that comes after it. The reason we casted to char* is because while ints are 4-bytes characters are only 1-byte. So now we'll do a 1-byte read instead of a 4-byte one.

    The reason why this works is because it will simply read one byte at whatever the address of num is. If the machine is little endian, then the address will point to the least-significant byte, which means it will have a value of "1", and vice versa for a big endian machine.

    Of course, there are other assumptions that could break this. Hey eecc, want to tell that story again about the middle endian machine you had? :)

    ASimPerson on
This discussion has been closed.