Can I use pattern matching in C program? [duplicate] - c

This question already has answers here:
Regular expressions in C: examples?
(5 answers)
Closed 6 years ago.
I am novice to C programming. Is it possible to match pattern inside the C string, like any built in functions?
I am using Red Hat Linux and I want to check if a string starts with abc: or def: followed by 10 digit numbers # chars. Something like : (abc|def):([0-9]{10})#([A-Za-z0-9]*).
Is there any C built in function which I can use to check this pattern matching.
Thanks for your help.

You can use POSIX regex matching in linux. See man 3 regex for more details.
If you are looking for a fast, safe and threaded library you can use re2 library from Google with support like pre-compiled regex.
(https://github.com/google/re2)

Linux and POSIX have regex(3) functions. You'll need to "compile" -that is prepare by transforming- your regular expression string using regcomp first.
In some simple cases, you might also use other string functions, like strstr, strchr, sscanf(3) (the %n format specifier might be handy, to know how much bytes you have scanned).
You could also consider some lexing utility, perhaps using flex to generate a tokenizer able to recognize your regexp.

Related

how to analyze a C header file? [duplicate]

This question already exists:
Is it possible to have gcc analyze my code and give me info about it [closed]
Closed 2 years ago.
I want to write a script that analyzes a C header file.
The header contains a few declarations of functions
The scrip needs to understand how many arguments each function has, and what are their types.
can gcc (or other tool) do this?
It comes down to how accurate you want to be when parsing a C header file. You could implement the C grammar for a function declaration and be guaranteed to parse it 100% correctly, but it will be a lot of work.
Conversely, a simple regex matcher is the easiest approach, but could have false-positive matches. You can implement a regex parser using C++, python or even other scripting languages such as bash or powershell. I would suggest python as its beginner friendly.
If you don't know what regex is, it's a simple language used to parse strings and extract data from strings. So you could create a regex expression to find the c function declaration then use that expression to pull out all of the arguments. Should be straight forward.
Create a regex expression on https://regex101.com/ and test it against teat strings, then implement that expression in python to do the work.

C Language. How to use a string value as delimiter in SSCANF

Is there a way to use a string as a delimiter?
We can use characters as delimiters using sscanf();
Example
I have
char url[]="username=jack&pwd=jack123&email=jack#example.com"
i can use.
char username[100],pwd[100],email[100];
sscanf(url, "username=%[^&]&pwd=%[^&]&email=%[^\n]", username,pwd,email);
it works fine for this string. but for
url="username=jack&jill&pwd=jack&123&email=jack#example.com"
it cant be used...its to remove SQL injection...but i want learn a trick to use
&pwd,&email as delimiters..not necessarily with sscanf.
Update: Solution doesnt necessarily need to be in C language. I only want to know of a way to use string as a delimiter
Just code your own parsing. In many cases, representing in memory the AST you have parsed is useful. But do specify and document your input language (perhaps using EBNF notation).
Your input language (which you have not defined in your question) seems to be similar to the MIME type application/x-www-form-urlencoded used in HTTP POST requests. So you might look, at least for inspiration, into the source code of free software libraries related to HTTP server processing (like libonion) and HTTP client processing (like libcurl).
You could read an entire line with getline (or perhaps fgets) then parse it appropriately. sscanf with %n, or strtok might be useful, but you can also parse the line "manually" (consider using e.g. your recursive descent parser). You might use strchr or strstr also.
BTW, in many cases, using common textual representations like JSON, YAML, XML can be helpful, and you can easily find many libraries to handle them.
Notice also that strings can be processed as FILE* by using fmemopen and/or open_memstream.
You could use parser generators such as bison (with flex).
In some cases, regular expressions could be useful. See regcomp and friends.
So what you want to achieve is quite easy to do and standard practice. But you need more that just sscanf and you may want to combine several things.
Many external libraries (e.g. glib from GTK) provide some parsing. And you should care about UTF-8 (today, you have UTF-8 everywhere).
On Linux, if permitted to do so, you might use GNU readline instead of getline when you want interactive input (with editing abilities and autocompletion). Then take inspiration from the source code of GNU bash (or of RefPerSys, if interested by C++).
If you are unfamiliar with usual parsing techniques, read a good book such as the Dragon Book. Most large programs deal somewhere with parsing, so you need to know how that can be done.

Concatenating a string using Win32 API

What's the best way to concatenate a string using Win32? If Understand correctly, the normal C approach would be to use strcat, but since Win32 now deals with Unicode strings (aka LPWSTR), I can't think of a way for strcat to work with this.
Is there a function for this, or should I just write my own?
lstrcat comes in ANSI and Unicode variants. Actually lstrcat is simply a macro defined as either lstrcatA or lstrcatW.
These functions are available by importing kernel32.dll. Useful if you're trying to completely avoid the C runtime library. In most cases you can just use wcscat or _tcscat as roy commented.
Also consider the strsafe.h functions, such as StringCchCat These come in ANSI and Unicode variants as well, but they help protect against buffer overflow.

Can I use 2 languages to make 1 program? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Writing a program in 2 languages?
Note: I've seen Writing a program in 2 languages? but found no good answer IMO.
Can I use 2, 3, or even 4 languages to make a single program? What do I need to do to accomplish this (probably in Perl and C)? I am using Windows 7 64-bit.
As people have already mentioned, yes you can as long as there are bindings. Inline::C provides easy access to C from Perl, although I find XS to be almost as easy once you start to get the hang of it.
For now:
#!/usr/bin/env perl
use strict;
use warnings;
use Inline C => <<'END_C';
char* name () {
return "Joel";
}
END_C
print name() . "\n";
So that begs the question, what are you trying to do? If you need something more in-depth, you can look at the source for my (work in progress) Math::GSLx::ODEIV2.
The relevant Perl documentation is perlembed if you want to execute Perl code from C and perlxstut if you want to execute C code from Perl.
One approach is to use some of the Perl modules in the
Inline::* namespace. For example, Inline::C allows you to write C subroutines directly in your Perl source file. The module transparently handles compiling the C routines to a shared library and dynamically loading the library. There are 'Inline' modules for a variety of languages - in varying states of completeness and support.
Sure, you can use as many languages as you want. You just need the proper tools to bind the units together. Each language has its own builtin support to interoperate with other languages, and often libraries that do that as well. The exact tools you need would depend on the languages you use, but every language I know supports at least binding to C functions.
Check out chapter 21.3. Extending Perl (Using C from Perl) in Programming Perl.
If you're looking for Perl binding for C, take a look at swig.

What does printf (_("hello, world\n")) mean? [duplicate]

This question already has answers here:
Underscore `_` before the format string
(3 answers)
Closed 3 years ago.
What's the role of the _("hello, world\n") argument to printf/puts etc ? I often find it while reading GNU source code.
See GNU Gettext -- it (_(...)) is used as a "binding site" for translation.
This is the gettext internationalization system.
it is a macro that replaces the gettext translation function. For a thorough explanation on gettext, check out this write-up: http://oriya.sarovar.org/docs/gettext_single.html
The underscore function (or macro) is a custom function defined by whatever project you're looking at. By convention, it's defined to send the string to GNU Gettext to fetch a translated version of the string for the user's current language.
This use of the _ macro is documented in the overview found in the GNU Gettext manual.

Resources