read a file of commands and execute them

This is a discussion on read a file of commands and execute them within the C Programming forums, part of the General Programming Boards category; Hi all, I have read the FAQ on running a program from within a program, but I don't think that ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    13

    read a file of commands and execute them

    Hi all,

    I have read the FAQ on running a program from within a program, but I don't think that that's my answer. From google, I only got C++ stuff.
    I'd appreciate any input, or ideas on where to start looking (functions, links, etc.)

    I basically have the following:
    - a file of data (matrices), [cameraFile]
    - a file of commands, [commFile]: for example:
    <commFile>
    # comment
    # comment
    datafile cameraFile

    function1 arg1 arg2 arg3
    function1 arg1 arg2
    function2 arg1 arg2 arg3 arg4
    ..... ,,,</commFile>

    Now, the user is supposed to type:
    myFile commFile

    where myFile is my C file with the main.
    This will access the command file, fetch the data file name from there, read the data file, store it, and them apply the commands on the data. The commands are functions that I will write (foo(), bar()...).

    I wrote the piece of code that reads the commands and stores the data. I intended to put foo(), bar() and the rest of them in the same file as the one called by main, but I'm not sure it's the best idea.
    My main problem is that I don't know how to go from the stored commands and args as strings (after I read the command file, e.g. "foo", "bar", "args1", "args2"), to actually calling them: foo(args1), bar(args2). I'm also not sure if I should put them in another file or the same one.

    My code (with collapsed functions):

    main:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char *argv[]){
      
      [... var declarations ... ] 	
      
      readCommands(argv[1]);
    
      return 0;
    
    }
    the other file:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #include "helperFunctions.h"
    
    
    // returns the tokens in a file, and the number of tokens
    int getWords(char *fptr, char* words[]){
    
    }
    
    //store variables in camera file (tokens)
    void storeVarsCam(int nbWords, char* words[]){
    	
    }
    
    //store variables in command file (tokens)
    void storeVarsComm(int nbWords, char* words[], char* cameraFile){
    	
    }
    
    //reads data from the camera files and stores it
    void readCameras(char* fptr, char* words[]){
    
    }
    
    void readCommands(char* fptr){
    	
    	int i;	
    	int nbTokens = 0;
    	char* camFile;
    	char* tokens[500];    			//500 words per file, change to dynamic allocation
    
    	camFile = malloc(7*sizeof(char));   		//"camera"
    	for(i=0; i<500; i++){
    		tokens[i] = malloc(30*sizeof(char)); 	 // 29 characters + '\0' 
    	}
    
    	nbTokens = getWords(fptr, tokens);
    	storeVarsComm(nbTokens, tokens, camFile);
    	readCameras(camFile, tokens);
    
    }

    Thanks.

  2. #2
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    I would place functions dealing with each command into a separate file. This might seem like overkill at first, but will pay off if your commands become more numerous or complex.

    You need a function to read the first word of a line (the command) in the command file and delegate reading the rest of the line (the args) to a setup function specific to the command. The setup function will read the rest of the args, change them from a C string to the required datatype (int, float, whatever), and call the actual function handling the command. This gives you flexibility of executing your commands by placing function calls in main, or by placing commands in the command file.

    For example:

    In Command1.c
    Code:
    Command1(arg1, arg2)
    {
    }
    
    Command1_SetupAndRun()
    {
        Read args and translate to appropriate type using atio, atof, if required.
        Call Command1 with args.
    }
    In CommandReader.c
    Code:
    commandReader()
    {
         Read command
         If(command is "command1") 
         {
              Command1SetupAndRun()
         }
         else if (command is "command2")
         {
               Command2SetupAndRun()
         }
    }
    I hope that helps you in organizing your program. Do you have any problems with the mechanics of reading files?
    Last edited by Zlatko; 08-19-2009 at 09:48 AM.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    13
    Hi,
    and thanks.
    That's what I was thinking too, but it seemed a bit un-elegant and repetitive. I was wondering if there was a way of "translating" the string "command1" into command1() directly, instead of having a huge if-then-else statement. I guess not though, and it's fine, I just didn't want to miss any easier way of doing it.
    I'm okay with reading and storing the files, I'll put the command functions into a different file, like you suggested.

    Thanks for your time.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,035
    Nope. That's the easiest way. If you have a lot of commands, a better solutions would be to have a hash table, so that you don't have to do string comparison, but that's not simple.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by surlyTomato View Post
    Hi,
    and thanks.
    That's what I was thinking too, but it seemed a bit un-elegant and repetitive. I was wondering if there was a way of "translating" the string "command1" into command1() directly, instead of having a huge if-then-else statement. I guess not though, and it's fine, I just didn't want to miss any easier way of doing it.
    I'm okay with reading and storing the files, I'll put the command functions into a different file, like you suggested.

    Thanks for your time.
    Instead of having a huge if-then-else statement you can try switch.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,035
    Quote Originally Posted by msh View Post
    Instead of having a huge if-then-else statement you can try switch.
    Not with strings you can't.

    Not if you don't hash the string up into a number.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-27-2007, 12:48 PM
  2. Read from file and make few things
    By xxxrugby in forum C Programming
    Replies: 12
    Last Post: 07-15-2006, 10:13 AM
  3. Lost in C
    By David670 in forum C Programming
    Replies: 8
    Last Post: 10-31-2005, 10:19 PM
  4. Program to execute shell commands.
    By LiquidLithium in forum C++ Programming
    Replies: 6
    Last Post: 09-01-2004, 12:22 PM
  5. How to execute file commands?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 12-23-2001, 12:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21