LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Converting VI to C code

Hello all,

 

I'd like to convert the "Search and Replace Pattern VI" to an equivalent C code. Enclosed please find the requirements for the search and replace pattern vi and also its match regular expression.  I don't have the C code generator nor how to use it.

 

Below is what I've so far for the search and replace pattern. I'd appreciated any help to complete it.

 

void Search_Replace_Pattern (int replace_all, char string[], char regular_Expression[], char replace_String[],
                int offset, char result_String[], int *offset_Past_Match)
    
    INT_DEFAULT = 0xFFFF;

    int i = 0, j = 0, matched = 0, matchPosition = 0, matchLength = 0, caseSenitive = 1;

    char searchString[512] = "";

    if (replace_all = 1){            //REPLACING ALL SUBSTR IN STR THAT MATCH REGULAR EXPRESSION

        strcpy(searchString, string);

        if((offset == INT_DEFAULT) || (offset < 0)){

            offset = 0;
        }

        else{
            strcpy(searchString, string);
        }

    
    RegExpr_FindPatternInText(regular_Expression,
                              caseSensitive,
                              searchString,
                              strlen(string),
                              RegExpr_SearchForwards,
                              RegExpr_MatchLargestNumChars,
                              &matched,
                              &matchPosition,
                              &matchLength);
    if(matched == 1){

        for(i = 0, j = (matchPosition + offset); j < (matchPosition + matchLength + offset); i++, j++){
        
        result_String[i] = string[j];

        *offset_Past_Match = matchPosition + matchLength + offset;
        }        
        
    }

    else{

        strcpy(result_string, string);
        *offset_Past_Match = -1;

    }

0 Kudos
Message 1 of 3
(2,823 Views)

Since the people who frequent the LabVIEW forums are programming in LabVIEW, they probably aren't the experts in how to write something in C code.

 

I'd suggest you find a forum that specializes in C code and ask them how to use regular expressions in C code.  My bet is that there are already libraries out there to do it.

0 Kudos
Message 2 of 3
(2,776 Views)

To implement regular expression support in C by hand is basically a sure way to insanity. Believe me!!

 

There are really just two approaches that are feasable for this. Either using one of the preexisting regular expression libraries like pcre, regex, or if you already use Boost++ anyhow in your C code, use their regex parser.

 

Second approach is to use an expression parser code generator. Here you define an expression syntax first in some formalized expression description and then let the parser generator do the work of generating the actual C code.  There are many different expression parser code generator approaches, some specifically for mathematic formulas, others more generic for programming languages etc.

 

Doing it all from hand may be an interesting academic experiment but not something you want to do if you need to come up with a working and bug free implementation for a project.

 

Even if we assume that your RegExpr_FindPatternInText() already exists and is working bug free, you have many troubles in your existing code.

 

-> Allocating a fixed size buffer on the stack for searchString, then using an unbounded strcpy() to copy the buffer into it!!!!

-> copying into resultString inside the function without any way of checking if that buffer is large enough

-> quite a few other potential buffer overflow situations that make your code an ideal attack angle for DOS and even more serious attackers

Rolf Kalbermatter
My Blog
0 Kudos
Message 3 of 3
(2,731 Views)