QString Remove numbers not associated with letters - qstring

I am looking for a way to remove all numbers and letters in brackets, as well as numbers not associated with a letter (i.e. I want to keep 'v2' or 'vol.2').
For instance:
"My Notes v02 003 (2009) (My sillyness)"
would become:
"My Notes v02".
I have found ways to remove the data in the braces and the braces themselves, however the issue I have now is removing the numbers not associated with a volume identifier.
Currently I have:
QString myItem = "My Notes v02 003 (2009) (My sillyness)";
myItem = myItem.remove( QRegExp( "\\[.*\\]|\\(.*\\)" ) );
Do I need to break the strings up into individual words and check manually? Or is there a better solution?

first i want recommend you to use boost library to manipulate your string data easily
http://www.boost.org/
so if your QString myItem is always struct data, it is easy to get what you want using split your string every time you find blank
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost;
/..
QString myItem = "My Notes v02 003 (2009) (My sillyness)";
vector< string > newItem;
split( newItem, myItem.tostdstring, is_any_of(" "));
cout <<newItem.at(0) <<" "<<newItem.at(1) <<" "<<newItem.at(2) <<endl;

Related

Batch building the same code multiple times with different values for a variable in Visual Studio

I need to build my code with a variable MyVar. I need to build it eg. 10 times but for each time, MyVar would be different, eg.
First build:
static unsigned char MyVar[] = "ABC";
Second build:
static unsigned char MyVar[] = "XYZ";
Is there an option to do this in a batch style? Eg. the command line? I just don't want to change MyVar manually, press "build", rename the built file etc.
Thanks a lot
PS: I'm not a professional coder, this could be bad practice. At the same time I'd just like to get the job done and don't want to change the code too much.
PPS: Already looked into several things (property sheets, custom macros, per-/post build actions, environment variables) but didn't find anything suitable.
Here is a sample C++ program utilizing a macro named MY_MACRO that you'll set in the CL environment variable using the /D option, before invoking MSBuild:
#include <iostream>
#include <string>
#ifndef MY_MACRO
#define MY_MACRO "unknown"
#endif
std::string greeting(const std::string& their_name)
{
static constexpr char my_name[] = MY_MACRO;
return "Hello, " + their_name + ", nice to meet you!\nMy name is " + my_name + '.';
}
int main()
{
std::cout << "Please enter your name: ";
std::string their_name;
std::cin >> their_name;
std::cout << greeting(their_name) << '\n';
}
As described in the docs mentioned above, when dealing with environment variables, you'll use the number sign (#) instead of the equal sign (=) to define a preprocessor constant with an explicit value. Also, the double-quotes (") need to be escaped (like \") when defining a string constant via command line.
To sum it up, here is a sample PowerShell script to build and collect ten different executables, each of which will be using a different string literal for the value of MY_MACRO, selected from a list of names, in a loop (note that in the script, \" needs to be expressed as \`" using a backtick before double-quotes, so that PowerShell will interpret literally those double-quotes inside a double-quoted string):
$MSBuildExe = 'C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\MSBuild.exe'
$MSBuildCmdLineArgs = #('MySolution.sln', '/property:Configuration=Release;Platform=x64', '/target:Rebuild', '/maxCpuCount')
New-Item -Force -ItemType Directory -Name 'CollectedExecutables'
$Names = #('Anni', 'Frida', 'Kadi', 'Mari', 'Piia', 'Pille', 'Piret', 'Reet', 'Siret', 'Triinu')
foreach ($MyNameString in $Names)
{
$env:CL = "/DMY_MACRO#\`"$MyNameString\`""
& $MSBuildExe $MSBuildCmdLineArgs "/property:TargetName=executable_named_$MyNameString"
Copy-Item -LiteralPath "x64\Release\executable_named_${MyNameString}.exe" -Destination 'CollectedExecutables'
}
In this script, I'm assuming you're using Visual Studio 2022; otherwise just change the value of $MSBuildExe to the appropriate path. Also, I'm assuming you want to build a Release (not Debug) configuration of your C++ project, and that you want to build for the x64 platform. If not, change those strings in the script accordingly. And of course, put the actual name of your C++ solution file (instead of MySolution.sln) into the first element of the MSBuild command-line arguments array in the script.

string size limit input cin.get() and getline()

In this project the user can type in a text(maximum 140 characters).
so for this limitation I once used getline():
string text;
getline(cin, text);
text = text.substr(1, 140);
but in this case the result of cout << text << endl; is an empty string.
so I used cin.get() like:
cin.get(text, 140);
this time I get this error: no matching function for call to ‘std::basic_istream::get(std::__cxx11::string&, int)’
note that I have included <iostream>
so the question is how can I fix this why is this happening?
Your first approach is sound with one correction - you need to use
text = text.substr(0, 140);
instead of text = text.substr(1, 140);. Containers (which includes a string) in C/C++ start with index 0 and you are requesting the string to be trimmed from position 1. This is perfectly fine, but if the string happens to be only one character long, calling text.substr(1, 140); will not necessarily cause the program to crash, but will not end up in the desired output either.
According to this source, substr will throw an out of range exception if called with starting position larger than string length. In case of a one character string, position 1 would be equal to string length, but the return value is not meaningful (in fact, it may even be an undefined behavior but I cannot find a confirmation of this statement - in yours and my case, calling it returns an empty string). I recommend you test it yourself in the interactive coding section following the link above.
Your second approach tried to pass a string to a function that expected C-style character arrays. Again, more can be found here. Like the error said, the compiler couldn't find a matching function because the argument was a string and not the char array. Some functions will perform a conversion of string to char, but this is not the case here. You could convert the string to char array yourself, as for instance described in this post, but the first approach is much more in line with C++ practices.
Last note - currently you're only reading a single line of input, I assume you will want to change that.

Beyond Compare 4: How to show only lines containing a specific string?

I am trying to compare two log files containing a list of transactions. The fields of these transactions are defined within the line itself. eg:
transactionID: 1, transactionType: 6, transactionData: 123456
transactionID: 2, transactionType: 6, transactionData: 654321
In one log file, transactionType 6 transactions may come consecutively, while in the other file they may be interlaced with other transaction types. So while transactionID may be different, they would still be in the same order and contain the same data.
How can I filter or otherwise only show the lines in both files which contain the string "transactionType: 6"? This would filter out all the other transactions and allow me to see only the ones with Type 6.
Thank you.
What you are asking is not possible in Beyond Compare 4.1.1 (current version).
The closest you can get to what you're describing is to only display differences within text matching a specific string.
Define a regular expression grammar element that matches on ".transactionType: 6." using the Define Unimportant Text in Beyond Compare instructions.
After you've defined the grammar element, click the Rules toolbar button (referee icon). In the Importance tab, check the box next to your new grammar element and uncheck all other grammar elements to make them unimportant. This will only highlight differences in lines that match the grammar element you defined.
Here's the way I was able to accomplish the desired behavior in BC4.
BC supports running a "pre-processor" application as it opens a file for comparison.
So what I did was make a simple executable which takes 3 arguments (argv[]):
The path to the original file
The path to the processed file (which will ultimately be opened for comparison by BC)
The path to a txt file containing line-delimited search substrings (more on this below)
Item number 3 above could contain only one entry (to use the same example as the original question) such as "transactionType: 6". The executable then searches each line of the original file (item 1 above) for the search substrings defined in item 3 above. Every time there is a hit, the whole line is copied (appended) into the output file (item 2 above).
Next, you need to define a File Format in BC (on a Mac you go to Beyond Compare menu and click on File Formats...). Select your file's extension and click on the Conversion tab. Use the screenshot below as example.
Note: %s is defined by BC to refer to the path of the original file (item 1) and %t refers to the path of the processed file (item 2).
screenshot
So, when you open File1.txt and File2.txt for comparison, BC will invoke your executable (once for each) and then open the resulting files (item 2). This will effectively show a "filtered" version of both files showing only lines containing the search substring.
Also note that the %t argument will be some sort of temp path generated internally by BC.
Below is a quick-and-dirty implementation of the executable described above:
#include <iostream>
#include <fstream>
#include <list>
#include <string>
using namespace std;
int main(int argc, const char * argv[])
{
ifstream inputFile (argv[1]);
ifstream substringFile (argv[3]);
ofstream outputFile;
outputFile.open(argv[2]);
//build the list of substrings from the substring input file
list<string> substringList;
string line;
//TODO: make this safer
while (getline(substringFile, line))
{
substringList.push_back(line);
}
//for each substring in the list
for (list<string>::const_iterator iter = substringList.begin(); iter != substringList.end(); iter++)
{
if (inputFile.is_open())
{
//for all the lines in the file
while (getline(inputFile, line))
{
//Find the current substring
if (line.find(*iter) != string::npos)
{
outputFile << line << "\n";
}
}
}
//go back to the beginning of the file
inputFile.clear();
inputFile.seekg(0, ios::beg);
}
inputFile.close();
outputFile.close();
return 0;
}
Hope this helps!

How to make a syntax manipulator?

Firstly, sorry for the question, I know I've heard something that could help, but I just can't remember.
Basically I would like to create my own syntax for a programming language. For example this code:
WRITE OUT 'Hello World!'
NEW LINE
would turn into this Java code:
System.out.print("Hello World!");
System.out.println();
How could I achieve this? Is there a method?
Olá.
There are techniques and proper algorithms to do that.
Search for "compiler techniques" and "Interpreter pattern".
An initial approach could be a basic pattern interpreter.
Assuming simple sentences and only one sentence per line, you could read the input file line by line and search for defined patterns (regular expressions).
The patterns describe the structure of the commands in your invented language.
If you get a match then you do the translation.
In particular, we use the regex.h library in c to perform the regular expression search.
Of course regex is also available in java.
Ex. NEW LINE match the pattern " *NEW +LINE *"
The * means that the preceding character occurs 0 or more times.
The + means that the preceding character occurs 1 or more times.
Thus, this pattern can match the command " NEW LINE " with arbitrary spaces between the words.
Ex. WRITE OUT 'Hello World!' match the pattern "WRITE OUT '([[:print:]]*)'"
or if you want to allow spaces " *WRITE +OUT +'([[:print:]]*)' *"
[[:print:]] means: match one printable character (ex. 'a' or 'Z' or '0' or '+')
Thus, [[:print:]]* match a sequence of 0, 1 or more printable characters
If a line of your input file matched the pattern of some command then you can do the translation, but in most cases you will need to retrieve some information before,
ex. the arbitrary text after WRITE OUT. Thats why you need to put parenthesis around [[:print:]]*. That will indicate to the function that perform the search that you want retrieve that particular part of your pattern.
A nice coincidence is that I recently assisted a friend with an college project similar to the problem you want to solve: a translator from c to basic. I reused that code to make an example for you.
I tested the code and it works.
It can translate:
WRITE OUT 'some text'
WRITE OUT variable
NEW LINE
#include <stdio.h>
#include <stdlib.h>
#include <regex.h>
#include <string.h>
#define STR_SHORT 100
#define MATCHES_SIZE 10
/**************************************************************
Returns the string of a match
**************************************************************/
char * GetExp(char *Source, char *Destination, regmatch_t Matches) {
//Source The string that was searched
//Destination Will contains the matched string
//Matches One element of the vector passed to regexec
int Length = Matches.rm_eo - Matches.rm_so;
strncpy(Destination, Source+Matches.rm_so, Length);
Destination[Length]=0;
return Destination;
}
/**************************************************************
MAIN
**************************************************************/
int main(int argc, char *argv[]) {
//Usage
if (argc==1) {
printf("Usage:\n");
printf("interpreter source_file\n");
printf("\n");
printf("Implements a very basic interpreter\n");
return 0;
}
//Open the source file
FILE *SourceFile;
if ( (SourceFile=fopen(argv[1], "r"))==NULL )
return 1;
//This variable is used to get the strings that matched the pattern
//Matches[0] -> the whole string being searched
//Matches[1] -> first parenthetical
//Matches[2] -> second parenthetical
regmatch_t Matches[MATCHES_SIZE];
char MatchedStr[STR_SHORT];
//Regular expression for NEW LINE
regex_t Regex_NewLine;
regcomp(&Regex_NewLine, " *NEW +LINE *", REG_EXTENDED);
//Regular expression for WRITE OUT 'some text'
regex_t Regex_WriteOutStr;
regcomp(&Regex_WriteOutStr, " *WRITE +OUT +'([[:print:]]*)' *", REG_EXTENDED);
//Regular expresion for WRITE OUT variable
regex_t Regex_WriteOutVar;
regcomp(&Regex_WriteOutVar, " *WRITE +OUT +([_[:alpha:]][[:alnum:]]*) *", REG_EXTENDED);
//Regular expression for an empty line'
regex_t Regex_EmptyLine;
regcomp(&Regex_EmptyLine, "^([[:space:]]+)$", REG_EXTENDED);
//Now we read the file line by line
char Buffer[STR_SHORT];
while( fgets(Buffer, STR_SHORT, SourceFile)!=NULL ) {
//printf("%s", Buffer);
//Shorcut for an empty line
if ( regexec(&Regex_EmptyLine, Buffer, MATCHES_SIZE, Matches, 0)==0 ) {
printf("\n");
continue;
}
//NEW LINE
if ( regexec(&Regex_NewLine, Buffer, MATCHES_SIZE, Matches, 0)==0 ) {
printf("System.out.println();\n");
continue;
}
//WRITE OUT 'some text'
if ( regexec(&Regex_WriteOutStr, Buffer, MATCHES_SIZE, Matches, 0)==0 ) {
printf("System.out.print(\"%s\");\n", GetExp(Buffer, MatchedStr, Matches[1]));
continue;
}
//WRITE OUT variable
//Assumes variable is a string variable
if ( regexec(&Regex_WriteOutVar, Buffer, MATCHES_SIZE, Matches, 0)==0 ) {
printf("System.out.print(\"%%s\", %s);\n", GetExp(Buffer, MatchedStr, Matches[1]));
continue;
}
//Unknown command
printf("Unknown command: %s", Buffer);
}
return 0;
}
Proper solution for this question requires the following steps:
Parse the original syntax code and create a syntax tree.
That is commonly done with tools like ANTLR.
Go through the syntax tree and either convert it to Java code, or to a Java syntax tree.
Both of those steps have their own complexity, so it would be better to ask separate questions about specific issues you encounter while implementing them.
Strictly speaking you can skip step 2 and generate Java directly when parsing, but unless your language is very simple renaming of Java concepts, you wouldn't be able to do that easily.

Ignoring commas and dots using fscanf()

I'm trying to read strings without commas and dots using fscanf().
Example input:
"Mr. and Mrs. Dursley, of number four, Privet Drive, were proud to say that they were perfectly normal, thank you very much."
I want to read "Mr", "Mrs" and "Dursley" each time e.g
I tried several ways to do this using optional arguments, but I failed. How can I ignore the commas and dots using fscanf()?
You can use the regex feature of sscanf to do this.
#include <stdio.h>
int main()
{
char *str = "Mr. Fiddle Tim went to the mall. Mr. Kurdt was there. Mrs. Love was there also. "
"They said hi and ate ice cream together." ;
char res[800] = { 0 };
sscanf( str, "%800[^.,]", res ) ;
puts( str ) ;
}
To keep going, you will need to use the return value of sscanf (or fscanf) to identify how many characters were matched. I leave that to you.

Resources