AppCenter: Find and replace string in a file during build - visual-studio-app-center

Is it possible to edit a .mm file before it gets compiled in AppCenter?
In an attempt to fix a build error, I want to find and replace a string in ../node_modules/react-native/React/CxxBridge/RCTCxxBridge.mm.
I tried using sed -i 's/oldString/newString/g' ../node_modules/react-native/React/CxxBridge/RCTCxxBridge.mm inside appcenter-pre-build.sh but it does not work.
Any help will be appreciated,
Thanks.

Not sure if this is your case, but I needed to update a version number on a complex project. To replace the counter of the current version with a new one, I considered updating the file with each build. After some versions of bash scripts, I realized that it's easier for me to write a console application in C with a couple of parameters to solve this problem. Works almost perfect for me. If you need I can share the simple code of this program.

Here is the C code that looks for a string in the file passed as a parameter and replaces the version number in it.
int main(int argc, char* argv[])
{
cout << "Version changer start\n";
if (argc < 2) {
cout << "There is no path argument. Version was no changed.";
return 1;
}
string sourcePath = argv[1];
string targetPath = sourcePath + ".tmp";
bool firstLine = true;
cout << sourcePath << endl;
ifstream sourceFile(sourcePath); // open file for input
ofstream targetFile(targetPath); // open file for output
string line;
while (getline(sourceFile, line)) // for each line read from the file
{
// looking for the desired line
if (line.find("public static String VER") != std::string::npos) { // replace "public static String VER" to your string definition code
line.replace(0, 32, "");
line.replace(line.length() - 2, 2, "");
int number = atoi(line.c_str());
number++; // In my case, I get an integer and add one to it
string v = to_string(number);
line = " public static String VER = \"" + v + "\";";
cout << v;
}
if (firstLine) {
targetFile << line;
firstLine = false;
}
else
targetFile << endl << line;
}
sourceFile.close();
targetFile.close();
remove(sourcePath.c_str());
if (rename(targetPath.c_str(), sourcePath.c_str()) != 0)
perror("Error renaming file");
else
cout << endl << "----------- done -----------\n";
}

Related

std::string::find returns npos when the char is present in the string

In a small utility I'm writing, I want to read a file record and:
look for the presence of an XOR checksum in the form *XX, where XX are hex digits
replace it if it's incorrect
add one if it's not present
So far, I'm only to the point of reading the file and looking for the checksum. The problem I'm up against is that std::string::find is not finding the * I know to be present; it returns npos every time.
The find() is on line 37. The first line read into strInput is:
$GPGGA,14240.99,2732.581,S,15301.947,E,1,06,3,65,M,37,M,-1.0,0006*6E\n
Here's the code:
#include <iostream>
#include <string>
int main()
{
std::cout << "Enter input file name:\n";
std::string strFileName = "";
std::getline(std::cin, strFileName);
std::cout << "Filename entered: " << strFileName << '\n';
FILE* fp;
int nErrCode = EXIT_FAILURE;
fopen_s(&fp, strFileName.c_str(), "r+");
if (!fp)
{
std::string strErr = "Failed to open " + strFileName;
perror(strErr.c_str());
return nErrCode;
}
rewind(fp);
std::string strInput;
strInput.reserve(100);
std::string::size_type n;
do
{
fgets(&strInput[0], 99, fp);
std::cout << strInput.c_str();
//n = 0;
n = strInput.find('*');
if (n != std::string::npos)
std::cout << "Found checksum at position " << n;
else
std::cout << "Did not find checksum";
} while (!feof(fp));
}
Thanks in advance.
I've debugged this and all is well up to the find(). At that point, I can see that the return value is npos, even though I can see the value of strInput and verify that it contains an asterisk.

No suitable constructor exists to convert from "char" to "std::string"

I'm new to coding in C and C++, and I have a program with
an issue. When I (try) to run it, it gives me this error:
"No suitable constructor exists to convert from "char" to "std::string".
I'm not sure what it means. My code is an example of a simple
substitution cipher covered in the book "Cracking Codes with Python" by Al Sweigart.
I just want to replicate it in C++. Here's my code:
#include <iostream> // for communicating with user
#include <string>
using namespace std;
string symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // all symbols
string encrypt(string msg, string key, string mode) {
string ca = symbols;
string cb = key;
string translated;
if (mode == "decrypt") {
ca, cb = cb, ca;
}
int index = 0;
for (index = 0; index < msg.length(); index++) {
cout << "index is " << index << endl;
int sindex = ca.find(msg[index]); // it works here
cout << "sindex is " << sindex << endl;
string cl = cb[sindex]; // the problem
translated += cl;
}
return translated;
}
int main() {
string msg = "";
string key = "";
string mode = "";
string ciphertext = ""; // our variables
cout << "Enter message: (no spaces please)\n";
cin >> msg;
cout << "Enter key (or \"none\" for using default):\n";
cin >> key;
if (key == "none") {
key = "QWERTYUIOPASDFGHJKLZXCVBNM";
}
cout << "Enter mode: (\"encrypt\" or \"decrypt\")\n";
cin >> mode;
ciphertext = encrypt(msg, key, mode);
cout << "The ciphertext is\n" << ciphertext;
}
For some reason it works with msg on line 17 but not with cb on line 19, even though
they're both std::string. The actual error is on line 19 with string cl = cb[sindex];.
Not even sure what's wrong. It works on line 17 int sindex = ca.find(/*The thing here*/msg[index]);.
(Maybe my Visual Studio 2019 has gone nuts.) If I replace cb with msg it still gives me the
same error. Maybe line 17 is a lucky line? Who knows? But please help, I'm so
confused!

Program To Input/Output File And Find String

Beginner C++ student here, first ever programming class. Currently learning string functions and input/output files. Trying to put a program together which will look at an existing file for the name 'john' or 'JOHN' in both upper and lower case. Then output the results into another file.
We were told we could convert all instances of the name to upper or lower case (I chose upper) so the the program would output instances of the name regardless what case it is in.
I noted below where I am having one of my problems and I may have more somewhere else which I can't see yet. Wondering if any of you kind folks can help me out with this.
Below is what I have so far and I noted the errors being returned as well.
Thank you so very much for your time and help!!!
#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
bool die(const string & msg);
bool input(string & s, const string & prompt);
bool open(ifstream & fin, const string & fileName);
bool open(ofstream & fout, const string & fileName);
//bool name(const string & line);
bool convert(string & str, string & converted);
int main() {
string inName, outName;
ifstream fin;
ofstream fout;
if (!input(inName, "Name of input file: "))
die("I can't read the name of the input file");
if (!open(fin, inName))
die("I can't open " + inName + " for output");
if (!input(outName, "Name of output file: "))
die("I can't read the name of the input file");
if (!open(fout, outName))
die("I can't open " + outName + " for output");
for (string converted; getline(fin, converted);) {
if (convert(converted)) //<---***HAVING AN ISSUE HERE***
fout << converted << endl;
}
if (fin.rdstate() != (ios::failbit | ios::eofbit))
die("Input file " + inName + " terminated input incorrectly");
fout.close();
if (!fout)
die("Output file " + outName + "had a problem with writing or closing");
fin.close();
cout << "read from " << inName << ", wrote to " << outName << ", ok" << endl;
}// main
bool die(const string & msg){
cout << "Fatal error: " << msg << endl;
exit(EXIT_FAILURE);
}
bool input(string & s, const string & prompt) {
cout << prompt;
return getline(cin, s) ? true : false;
}
bool open(ifstream & fin, const string & fileName){
fin.open(fileName);
return fin ? true : false;
}
bool open(ofstream & fout, const string & fileName){
ifstream tin(fileName);
if (tin) return false;
fout.open(fileName);
return fout ? true : false;
}
//bool name(const string & line){
//return line.find("john") != UINT_MAX;
//}
bool convert(string & str, string & converted)
{
for (short i = 0; i < str.size(); ++i)
converted += toupper(str[i]);
return converted.find("john") != UINT_MAX;
}
Errors I am getting:
Error 1 error C2660: 'convert' : function does not take 1 arguments Line 40
Warning 2 warning C4018: '<' : signed/unsigned mismatch Line 93
3 IntelliSense: too few arguments in function call Line 40
Well, the error message pretty much says it all. The convert function is declared to take two arguments, but you're only passing in one argument.
bool convert(string & str, string & converted)
You need to pass another string reference that takes the converted string.
Off topic: Also, for the sake of security (and maybe you later wanting to switch to other programming languages): Please don't start learning the bad habit of considering everything not 0 to be true.
Things like the following are bound to cause trouble sooner or later:
return fin ? true : false;

c++ program of sorting names

Instruction for program:
Read the list of names from “names.txt” in the format “First Last”.
Sort the names based upon typical alphabetic order of peoples names based upon last name then first name.
Write the sorted list to a file called “sortednames.txt” in the format “Last, First”.
Here's my code: file data was stored in fullname array but now I am stuck on how to flip the first and last name in the array??
int main()
{
const int MAXNAMES = 100;
int value = 0;
string fullname[MAXNAMES];
ifstream inFile;
inFile.open("names.txt"); //open the file to excess the rainfall data
if (inFile.fail()) // testing the file
{
cout << "Error opening file. Please check that the file currently `enter code here`exist" << endl;
exit(1);
}
cout << "File successfully open" << endl;
while(!inFile.eof())
{
while(value < 100)
{
getline(inFile,fullname[value]);
value++;
}
}
return 0;
}
To flip the name around you could do the following:
string myString;
int spacePosition;
value = 0;
while(value < 100) {
myString = fullname[value];
spacePosition = myString.find(" ");
fullname[value] = myString.substr(spacePostion) + " " + myString.substr(0, spacePostion -1);
}

Zlib uncompress returns Z_DATA_ERROR

I am working on a client server application, where client compresses a 2MB data sends to the server, server receives the data uncompresses it and writes it to a file.
For some packets uncompression was failing and I added MD5 sum to both client side and server side code and also debugged using uncompression at the client side after compressing the data. The same parameters that passes for uncompress function in client side is failing with Z_DATA_ERROR in the server side. The data's MD5sum seems same. Am totally clueless what I could do next.
Server Side cod looks like this:
int ret = uncompress((Bytef*)unCompressedBuffer, &dwUncompressedBytes, (const Bytef*) receivedBuffer+525, dwBlockLength);
if (ret == Z_OK)
{
}
else
{
std::cout << " Uncompression failed for Block: " << iBlock << std::endl;
std::cout << " PacketType: 4" << " Block Number:" << iBlock << " Length:" << dwBlockLength << "Error:" << ret << std::endl;
PrintMD5SumResult((PBYTE)receivedBuffer+525, compressedSize-525);
std::cout << " Uncompressed MD5 Checksum:0";
PrintMD5SumResult((PBYTE)unCompressedBuffer, dwUncompressedBytes);
}
}
Client Code Looks like this:
int ret = compress2(l_pCompressData + 4, &destLen,
(const Bytef*) pBlockData, dwBlockSize, 6);
memcpy(m_pWriteBuffer+525, l_pCompressData, destLen);
m_dwWriteBytes = destLen+525;
std::cout << " \n Compressed MD5 Sum:0";
PrintMD5SumResult(m_pWriteBuffer, m_dwWriteBytes);
PrintMD5SumResult(m_pWriteBuffer+525, m_dwWriteBytes-525);
int ret = uncompress(m_pUnCompressData, &uncomLen, (const Bytef*)m_pWriteBuffer+525, destLen);
if(ret != Z_OK)
{
std::cout << " Uncompression has failed." << std::endl;
}
else
{
//std::cout << " UnCompressed MD5 Sum:0";
//PrintMD5SumResult((PBYTE)m_pUnCompressData, md5Output, dwBlockSize);
}
// Write the 2MB to the network
WriteDataOverNetwork(m_NetworkStream, m_pWriteBuffer, m_dwWriteBytes, &dwNumBytes, TRUE);
I narrowed down the problem to the following piece of code in zlib - but have a hard time understanding it. In the inflate() function, (ZSWAP32(hold)) != state->check) this statement is failing. Can someone help me out here? MD5sum used here is from Boton C++ library.
case CHECK:
if (state->wrap) {
NEEDBITS(32);
out -= left;
strm->total_out += out;
state->total += out;
if (out)
strm->adler = state->check =
UPDATE(state->check, put - out, out);
out = left;
if ((
#ifdef GUNZIP
state->flags ? hold :
#endif
ZSWAP32(hold)) != state->check) {
strm->msg = (char *)"incorrect data check";
state->mode = BAD;
break;
}
I also met this issue recently when I used zlib to do in-memory compression/decompression. The code is as follow:
size_t size = 1048576;
void *data;
void *comp_data;
uLong comp_data_len;
void *uncomp_data;
uLong uncomp_data_len;
void *temp;
int ret;
data = calloc(1, size); // data is filled with all zeros
comp_data_len = size * 1.01 + 12;
comp_data = calloc(1, size);
ret = compress(comp_data, &comp_data_len, data, size); //here ret is Z_OK.
uncomp_data_len = size;
uncomp_data = calloc(1, uncomp_data_len);
ret = uncompress(uncomp_data, &uncomp_data_len, comp_data, comp_data_len); //here ret is Z_OK
temp = calloc(1, 496);
for (i = 0; i < 100; i++)
{
//here fill some random data to temp
memcpy((char*)data + i * 100, temp, 496);
ret = compress(comp_data, &comp_data_len, data, size); //here ret is Z_OK.
ret = uncompress(uncomp_data, &uncomp_data_len, comp_data, comp_data_len); //here ret sometimes is Z_OK, sometimes is Z_DATA_ERROR!!!
}
I also traced the code and found that it failed at the statement "inflate() function, (ZSWAP32(hold)) != state->check)" too. So I cannot believe that the function uncompress is related to the data pattern. Am I wrong?
I also noticed that compress function calls deflate to do compression, deflate processes data every 64k, so need I split it to 64k blocks, compress each block one by one, then uncompress can work well?
i don't know whether it‘s the right answer ,maybe it help !my English is so poor,hope you can understand. perhaps the parameters convert to another has bugs . when they convert the info maybe lose ! i meet the same problem , after use the source code type the problem has been solved (Bytef\uLongf\ uLong,etc). the wed is Chinese you can use Google to translate.
http://www.360doc.com/content/13/0927/18/11217914_317498849.shtml
This is my test.the arry[] can be larger,same time the sour[]/dest[]/destLen/Len will be changed.using the source code type the problem has been solved. Hope will be helpful.
my code as follow:
#include <stdio.h>
#include "zlib.h"
int main(){
//the buffer can be larger
Bytef arry[] = "中文测试 yesaaaaa bbbbb ccccc ddddd 中文测试 yesaaaaa bbbbb ccccc ddddd 中文测试yesaaaaa bbbbb ccccc ddddd 中文测试 yes 我是一名军人!";
//buffer length
int size = sizeof(arry);
//store the uncompressed data
Bytef sour[2500];
//store the compressed data
Bytef dest[2500];
//压缩后的数据可能比源数据要大
unsigned long destLen = 2500;
//解压数据时因为不知道源数据大小,设置时长度尽可能大一些。以免出错
unsigned long Len = 2500;
int ret = -1;
ret = compress(dest,&destLen,arry,size);
//dest[destLen] = '\0';
printf("ret = %d\ndest = %s\n", ret, dest);
ret = uncompress(sour,&Len,dest,destLen);
//sour[size-1] = '\0';
printf("ret = %d\nsour = %s\n", ret, sour);
return 0;
}

Resources