why my PCRE only matches the first result - pcre

I want to match all 'abc' in the input string. But I got the following result when input "first abc, second abc, third abc". I also output the ovector:
src: first abc, second abc, third abc
Matches 1
ovector: 6|9|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|
My code:
#include <stdio.h>
#include <string.h>
#include "pcre.h"
static const char my_pattern[] = "abc";
static pcre* my_pcre = NULL;
static pcre_extra* my_pcre_extra = NULL;
void my_match(const char* src)
{
printf("src: %s\n", src);
int ovector[30]={0};
int ret = pcre_exec(my_pcre, NULL, src, strlen(src), 0, 0, ovector, 30);
if (ret == PCRE_ERROR_NOMATCH){
printf("None match.\n");
}
else{
printf("Matches %d\n",ret);
}
printf("ovector: ");
for(int i=0;i<sizeof(ovector)/sizeof(int);i++){
printf("%d|",ovector[i]);
}
printf("\n");
return;
}
int main()
{
const char* err;
int erroffset;
my_pcre = pcre_compile(my_pattern, PCRE_CASELESS, &err, &erroffset, NULL);
my_pcre_extra = pcre_study(my_pcre, 0, &err);
my_match("first abc, second abc, third abc");
return 0;
}
How can I get all the 'abc's, thanks.

pcre_exec only finds one match at a time. ovector is for substring matches. int ovector[30]={0}; will give you up to 10 matches (the last third (20-29) is not used), the first pair of numbers is for the whole pattern, the next pair is for the first capturing parentheses and so on. E.g. if you change your pattern to:
`static const char my_pattern[] = "(a(b)c)";`
then in your output you should see
Matches 3
ovector: 6|9|6|9|7|8|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|
The function returns the number of captures that matched, in this case three, one for the whole pattern and two subpattern captures. The whole pattern matches at 6-9, the first parentheses match 6-9 too and the second parentheses match 7-8. To get more whole matches (global) you have to use a loop, passing in the offset of the previous match (ovector[1]) each time.
See http://www.pcre.org/pcre.txt and search for How pcre_exec() returns captured substrings

Related

Pass String array as input into external C function

I would like to pass a String vector into an external C function.
In a minimal example I just want to pass the String vectors (or 1D array) through the C function.
My Modelica function looks like:
function testreadstri
input String instri[2];
output String outstri[2];
external "C" test_stri(instri,, size(instri, 1), outstri);
annotation (Include="#include <ebcmysql.cpp>", Library="libmysql");
end testreadstri;
My C fucntion looks like:
void test_stri(const char* thestring, size_t nLines, const char **testresult)
{
//bout = 12.3;
size_t iLines;
//size_t nLines;
iLines = 0;
//nLines = 1;
while ( iLines <= nLines ) {
<LINE_OF_INTEREST>
iLines++;
}
}
I tried for <LINE_OF_INTEREST> the following lines:
testresult[iLines] = thestring[iLines];
strcpy(testresult[iLines], thestring[iLines]);
What works, but of course does not pass the input through as an output, is:
testresult[iLines] = "aTestString";
Is there any possibility to handle Modelica input String vectors in the external C function?
Thanks in advance!
Here's a short, self-contained and compilable example demonstrating both input string and output string handling of a pure external function in Modelica
model Model
function testreadstri
input String instri[2];
output String outstri[2];
external "C" test_stri(instri, size(instri, 1), outstri, size(outstri, 1));
annotation(Include="
#include \"ModelicaUtilities.h\"
#include <stdlib.h>
#include <string.h>
void test_stri(const char** thestring, size_t nLinesIn, const char** testresult, size_t nLinesOut)
{
size_t iLines;
// example for input string handling
for (iLines = 0; iLines < nLinesIn; iLines++) {
ModelicaFormatMessage(\"%s\\n\", thestring[iLines]);
}
// example for output string handling
for (iLines = 0; iLines < nLinesOut; iLines++) {
char* line = ModelicaAllocateStringWithErrorReturn(6);
if (line != NULL) {
strcpy(line, \"result\");
testresult[iLines] = line;
}
}
}");
end testreadstri;
String s[:] = testreadstri({"first", "second"});
end Model;
Yes, this is supported by the Modelica specification, see https://specification.modelica.org/v3.4/Ch12.html#argument-type-mapping.

How to display the result of function called using object reference in c++

#include "pch.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <stdio.h>
using namespace std;
class LetterDistribution
{
public: char district, trace;
public: int random_num;
public : LetterDistribution(){}
public: LetterDistribution(char dis)
{
district = dis;
trace = 'Z';
}
public: string LetterNumbers()
{
random_num = rand();
string letter_no ( district + " " + random_num);
return letter_no;
}
};
int main()
{
srand(time(0));
cout << "Enter district\n"<<endl;
char dis ;
cin >> dis;
LetterDistribution ld(dis);
cout << ld.LetterNumbers();
return 0;}
I am getting error in second last line inside main "cout << ld.LetterNumbers();". I am new to c++ , I have been working on C# earlier. I shall be thankful if someone could help me .
You have 2 issues in LetterNumbers function:
You can't add to string a number, you should convert the number to string first. you can do so by std::to_string(random_num)
You can't start concatenate string with a character, since character is like number in c++, and adding anything to number is a number. You should start from a string, even an empty one.
So the whole function can be something like:
string LetterNumbers()
{
random_num = rand();
string letter_no ( std::string("") + district + " " + std::to_string(random_num));
return letter_no;
}
Another issues: (but not errors!)
in c++ you can specify public: once, and everything after it is still public, until you change it. same thing for private and protected.
instead of <stdio.h> you should use <cstdio> which is the c++ wrapper for the c header.

fscanf function not working

I'm currently writing a program that reads numbers from sets of 2 numbers from a text file and prints them out. I'd like to use the numbers to determine a GCD later, but I have to be able to scan them from the file first. The text file looks like this:
24 72
25 50
31 89
...
Tab is pressed between each number in the first row and each number in the second.
I've come up with this so far (commented out section is to be used for determining the GCD):
/*
File name: euclid.cpp
This program find the largest common multiple of two numbers using the Euclid method.
*/
#include <stdio.h>
#include "genlib.h"
#include "simpio.h"
int main()
{
FILE *input;
long num1=0, num2=0, orinum2=0, rem=0, gcd=0;
int i=0, size=0;
char temp;
input=fopen("Euclid.txt", "r");
while((temp=getc(input))!=EOF)
{
if(temp=='\n') size++;
}
size++;
while(i<size)
{
fscanf(input, "%d\t%d%[^\n]", &num1, &num2);
printf("%d\t%d\n", num1, num2);
orinum2=num2;
/* while (true)
{
rem=num1%num2;
if (rem==0)
{
gcd=num2; break;
}
else
{
num1=num2;
num2=rem;
}
}
printf("The GCD of %d and %d is %d.\n", num1, orinum2, gcd);
*/ i++;
}
fclose(input);
}
Every single webpage and resource I have checked dictates that this should work, but for some reason it just isn't.
fscanf will "return the number of input items successfully matched and assigned":
#include <stdio.h>
int main()
{
FILE *input = fopen("input.txt", "r"); ;
int num1, num2;
while(fscanf(input, "%d %d", &num1, &num2) > 0)
printf("%d\t%d\n", num1, num2);
fclose(input);
}
The pattern ("%d %d") will match and assign two integers, separated by any number of whitespace characters.
Whitespace characters include your tab (\t), and newline (\n).

QString encoding with special character

I m trying to convert QString with special character to const char but I did not succeed.
my function is:
void class::func(const QString& fileName) // fileName = "â.tmp"
{
qDebug()<< fileName; // display "â.tmp"
const char* cfileName = fileName.toAscii().data();
qDebug() << cfileName; // display "a?.tmp"
}
qDebug()<< fileName display the true value that is "â.tmp" but after converting it to const a char*, I do not succeed to have the right value.
In the second time I try to use
const char* cfileName = QString::fromUtf8(fileName.toAscii().data());
but I did not still have the right value, it display the same thing: a?.tmp.
How can I fix this?
ASCII character set does not have the character â, so what you are trying to do is impossible.
You could try this:
const char* cfileName = = fileName.toUtf8().data();

Drawing currency symbol

How to draw a currency symbol in a custom label using CGContextShowTextAtPoint method in draw rect.
Here the symbol is in string format.
Any help!!
Thanks
You have to resort to C style strings, since this is what CGContextShowTextAtPoint() requires. In order to correctly handle the locale (the currency symbol changes with the locale) you must use setlocale(), then you format your string using strfmon() and finally you pass the string created with strfmon() to CGContextShowTextAtPoint().
Documentation is available as follows from the terminal:
man 3 setlocale
man 3 strfmon
EDIT/UPDATE: For your information, strfmon() internally uses struct lconv. The structure can be retrieved with the function localeconv(). See man 3 localeconv for a detailed description of the fields available in the structure.
for instance, try the following simple C program setting different locales
#include <stdio.h>
#include <locale.h>
#include <monetary.h>
int main(void)
{
char buf[BUFSIZ];
double val = 1234.567;
/* use your current locale */
setlocale(LC_ALL, "");
/* uncomment the next line and try this to use italian locale */
/* setlocale(LC_ALL, "it_IT"); */
strfmon(buf, sizeof buf, "You owe me %n (%i)\n", val, val);
fputs(buf, stdout);
return 0;
}
The following uses localeconv():
#include <stdio.h>
#include <limits.h>
#include <locale.h>
int main(void)
{
struct lconv l;
int i;
setlocale(LC_ALL, "");
l = *localeconv();
printf("decimal_point = [%s]\n", l.decimal_point);
printf("thousands_sep = [%s]\n", l.thousands_sep);
for (i = 0; l.grouping[i] != 0 && l.grouping[i] != CHAR_MAX; i++)
printf("grouping[%d] = [%d]\n", i, l.grouping[i]);
printf("int_curr_symbol = [%s]\n", l.int_curr_symbol);
printf("currency_symbol = [%s]\n", l.currency_symbol);
printf("mon_decimal_point = [%s]\n", l.mon_decimal_point);
printf("mon_thousands_sep = [%s]\n", l.mon_thousands_sep);
printf("positive_sign = [%s]\n", l.positive_sign);
printf("negative_sign = [%s]\n", l.negative_sign);
}
I don't really get what you're asking,
checking the documentation, the method would look something like that:
CGContextRef ctx = UIGraphicsGetCurrentContext();
const char *string = "$";
CGContextShowTextAtPoint (ctx, 160, 240, string, 1);
Haven't tested it, but this should draw $ in the center of the screen.
BTW, why not use images?
~ Natanavra.