Looping problem (revisited for better reading)

Discussion in 'C Programming' started by Yodai, Feb 11, 2004.

  1. Yodai

    Yodai Guest

    Hi all.... I am trying to reduce the code of this function and I presume
    there's a way to do it through a loop, buit I can't figure out how. This is
    what I got: an embedded processor with a mini webserver running on it, Then
    I have a function that substitutes dinamicaly some special codes that I've
    spread through the webpage with information that is being written onto my
    RAM memory by an external processor. So what I do is: I define the storage
    positions:

    #define R1DIA ((unsigned char*)0xC000)
    #define R1TIPUS ((unsigned char*)0xC001)
    #define R1ANY ((unsigned char*)0xC002)
    #define R1MES ((unsigned char*)0xC003)
    #define R1HORA ((unsigned char*)0xC004)
    #define R1MIN ((unsigned char*)0xC005)
    #define R1VALOR ((uint16_t*)0xC006)
    #define R2DIA ((unsigned char*)0xC008)
    #define R2TIPUS ((unsigned char*)0xC009)
    #define R2ANY ((unsigned char*)0xC00A)
    #define R2MES ((unsigned char*)0xC00B)
    #define R2HORA ((unsigned char*)0xC00C)
    #define R2MIN ((unsigned char*)0xC00D)
    #define R2VALOR ((uint16_t*)0xC00E)
    #define R3DIA ((unsigned char*)0xC010)
    #define R3TIPUS ((unsigned char*)0xC011)
    #define R3ANY ((unsigned char*)0xC012)
    #define R3MES ((unsigned char*)0xC013)
    #define R3HORA ((unsigned char*)0xC014)
    #define R3MIN ((unsigned char*)0xC015)
    #define R3VALOR ((uint16_t*)0xC016)


    // Then I do all of the stuff my webserver needs and when a request is made
    I jump to a loop something like this: (I've omited some of the code so it's
    not so long, but you'll get the picture)



    void InsertDynamicValues(void)
    {

    if (TCPTxDataCount < 4) return;
    Key = TCP_TX_BUF;

    for (i = 0; i < (TCPTxDataCount - 2); i++)
    {
    if (*Key == '¿') //every time I find a '¿' in my code I search for
    next character
    {
    if (*(Key + 1) == '%')
    {
    switch (*(Key + 2))
    {
    case '?':
    {
    switch (*(Key + 3)) // So I have found the whole
    string, no go for the last caractes and make a substitution
    {
    case 'a': // from what will be written
    on the webpage, two of the vars have to go
    //through a small
    external calculation (Get_value) that will return a float
    //number
    {
    sprintf(NewKey, "%02x/%02x/%02x
    %02x:%02x %s%04.2f ", *R1DIA, *R1MES, *R1ANY, *R1HORA, *R1MIN, "(",
    Get_value(*R1VALOR,*R1TIPUS));
    memcpy(Key, NewKey, 23);
    break;
    }
    case 'b':
    {
    sprintf(NewKey, "%02x/%02x/%02x
    %02x:%02x %s%04.2f ", *R2DIA, *R2MES, *R2ANY, *R2HORA, *R2MIN, "(",
    Get_value(*R2VALOR,*R2TIPUS));
    memcpy(Key, NewKey, 23);
    break;
    }
    case 'c':
    {
    sprintf(NewKey, "%02x/%02x/%02x
    %02x:%02x %s%04.2f ", *R3DIA, *R3MES, *R3ANY, *R3HORA, *R3MIN, "(",
    Get_value(*R3VALOR,*R3TIPUS));
    memcpy(Key, NewKey, 23);
    break;
    }
    }
    }
    break;
    } //and when all of my letters are done (from 'a' to 'Z') I
    switch the preceeding character and go around again
    case '%' :
    {
    switch (*(Key + 3))
    {
    case 'a':
    {
    sprintf(NewKey, "%s",
    Detect_type(*R1TIPUS,*R1VALOR));
    memcpy(Key, NewKey, 15);
    break;
    }
    case 'b':
    {
    sprintf(NewKey, "%s",
    Detect_type(*R2TIPUS,*R2VALOR));
    memcpy(Key, NewKey, 15);
    break;
    }
    case 'c':
    {
    sprintf(NewKey, "%s",
    Detect_type(*R3TIPUS,*R3VALOR));
    memcpy(Key, NewKey, 15);
    break;
    }
    }
    }
    break;
    }
    }
    }
    Key++;
    }
    }



    /* So far, so good. This works smoothly, but since I have to set 300
    variables on the system now, the code has become extremely large, and I
    think I could do it some other way. I'm going to try to outline it, but I'm
    not sure I can do it... see if you guys can give me some guide lines as
    where to go...*/

    void InsertDynamicValues(void)
    {
    unsigned char *Key;
    unsigned char NewKey[5];
    unsigned int e;
    unsigned char leter = a ;
    *Dia = (unsigned char *)0xc000; //Inicialize pointers on the first
    memory address where they will start
    *Tipus = (unsigned char *)0xC001;
    *Any = (unsigned char *)0xC002;
    *Mes = (unsigned char *)0xC003;
    *Hora = (unsigned char *)0xC004;
    *Minut = (unsigned char *)0xC005;
    *Valor = (unsigned int *)0xC006; // note that the last value is 2 words
    length instead of one

    if (TCPTxDataCount < 4) return;
    Key = TCP_TX_BUF;

    for (i = 0; i < (TCPTxDataCount - 2); i++)
    {
    if (*Key == '¿')
    {
    if (*(Key + 1) == '%')
    {
    switch (*(Key + 2))
    {
    case '?':
    {
    for (leter = a; leter < Z ; leter++)
    {
    if (*(Key + 3) == leter)
    {
    sprintf(NewKey, "%02x/%02x/%02x
    %02x:%02x %s%04.2f ", *DIA, *MES, *ANY, *HORA, *MIN, "(",
    Get_value(*VALOR,*TIPUS));
    memcpy(Key, NewKey, 23);
    break;
    }
    else
    {
    leter = leter+1;
    *DIA = *DIA +8;
    *MES = *MES +8;
    *ANY = *ANY +8;
    *HORA = *HORA +8;
    *MIN = *MIN +8;
    *VALOR = *VALOR+ 8;
    *TIPUS = *TIPUS+ 8;
    if (LETRA >'Z') break;
    }
    Key++;
    }
    }

    /*Now, I feel there are some big gaps that go beyond my level of C and I
    really don't know if something like this should ever work, but, it seems
    pretty logical to me, though at times finding the sintaxis to do what I want
    to do it's a hard task.....

    Thanks in advance

    Yodai */
     
    Yodai, Feb 11, 2004
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.