In the process of writing a regex to obtain the browser and version. This is what I have so far. Can anyone advise a better process? The regex will check for IE/Firefox/Safari/Chrome from this I can check the version number.
import flashx.textLayout.formats.BackgroundColor;
var inputField:TextField = new TextField();
var displayField:TextField = new TextField();
// RegExp
var reg:RegExp = /(MSIE|(?!Gecko.+)Firefox|(?!AppleWebKit.+Chrome.+)Safari|(?!AppleWebKit.+)Chrome|AppleWebKit(?!.+Chrome|.+Safari)|Gecko(?!.+Firefox) (SeaMonkey))(?: |\/)([\d]+)/i;
inputField.border = true;
inputField.width = 500;
inputField.height = 50;
inputField.x = 75;
inputField.y = 50;
inputField.type = "input";
inputField.multiline = true;
addChild(inputField);
displayField.border = true;
displayField.width = 500;
displayField.height = 200;
displayField.backgroundColor = 0x666666;
displayField.x = 75;
displayField.y = inputField.y + 50 + inputField.height;
displayField.multiline = true;
addChild(displayField);
inputField.addEventListener(Event.CHANGE, changeListener, false, 0, true);
function changeListener(event:Event):void
{
var strToTest:String = inputField.text;
var textToDisplay:String = "";
if(reg.test(strToTest))
{
var browser:String = strToTest.match(reg)[1];
var version:int = strToTest.match(reg)[2];
var chromeVersion:int = 29;
var firefoxVersion:int = 23;
var ieVersion:int = 7;
var safariVersion:int = 6;
textToDisplay += "BROWSER IS " + browser;
textToDisplay += "\nVersion " + version;
if(browser == "Chrome")
{
if(version <= chromeVersion)
{
textToDisplay += "\nUPDATE CHROME - VERSION BELOW " + chromeVersion;
}
else
{
textToDisplay += "\nVERSION GREATER THAN " + chromeVersion;
}
}
else if(browser == "MSIE")
{
if(version <= ieVersion)
{
textToDisplay += "\nUPDATE CHROME - VERSION BELOW " + ieVersion;
}
else
{
textToDisplay += "\nVERSION GREATER THAN " + ieVersion;
}
}
else if (browser == "Firefox")
{
if(version <= firefoxVersion)
{
textToDisplay += "\nUPDATE CHROME - VERSION BELOW " + firefoxVersion;
}
else
{
textToDisplay += "\nVERSION GREATER THAN " + firefoxVersion;
}
}
else if (browser == "Safari")
{
if(version <= safariVersion)
{
textToDisplay += "\nUPDATE CHROME - VERSION BELOW " + safariVersion;
}
else
{
textToDisplay += "\nVERSION GREATER THAN " + safariVersion;
}
}
}
else
{
textToDisplay = "Browser is NOT IE/FIREFOX/SAFARI/CHROME"
}
displayField.text = textToDisplay;
}
(MSIE(?!.+Opera)|Firefox(?!.+SeaMonkey)|Version(?=.+Safari)(?!.+Chrome)|Chrome)(?: |/)([\d]+)
I Simplified the Regex to follow these checks https://developer.mozilla.org/en-US/docs/Browser_detection_using_the_user_agent.
I don't want to use a third party lib as I would like to understand as I may need to make changes. The following code appears to be returning correctly for what I want.
(MSIE(?!.+Opera)|Firefox(?!.+SeaMonkey)|Version(?=.+Safari)(?!.+Chrome)|Chrome)(?: |/)([\d]+)
Related
I am receiving data in TCP in C++ using Qt library. I store the received packet in a QByteArray, but after reading the whole data, I face this error in debug. At the end, I try to clear the buffer, but I face this problem while trying to clear it too.
Here is my code :
void AvaNPortTester::scoket_readyRead()
{
ui.lineEdit_Sending_Status_->setText("Sent");
ui.lineEdit_Sending_Status_->setStyleSheet("QLineEdit { background: rgb(50, 255, 50); }");
tcpSocket_data_buffer_.append(tcpSocket_->readAll());
//qDebug() << serialport_data_buffer_.size();
//auto ddd = QString::number(tcpSocket_data_buffer_.size());// +" : " + tcpSocket_data_buffer_.toHex();
//ui.lableSocketRead->setText(ddd);
bool read_aain = false;
QByteArray dummy(int(1446), Qt::Initialization::Uninitialized);
int reminded_data = 0;
int dummy_size = 0;
int frame_size = 0;
int l_size = 0;
int total_size_rcvd = tcpSocket_data_buffer_.size();
//int total_size_rcvd_b = total_size_rcvd_b;
int temp = 0;
while (total_size_rcvd != 0)
{
if(total_size_rcvd != 0){
auto packet = tcpSocket_data_buffer_.mid(0, 1446);
auto rem = tcpSocket_data_buffer_.mid(1446);//****1146
tcpSocket_data_buffer_ = rem;
QDataStream streamdata(packet);
uint8_t Sync_Header[3];
auto ss = streamdata.readRawData((char*)&Sync_Header, 3);
uint8_t Total_size[2];
ss = streamdata.readRawData((char*)&Total_size, 2);
int t_size = Total_size[0] * 256 + Total_size[1];
uint8_t Reserved[2];
ss = streamdata.readRawData((char*)&Reserved, 2);
frame_size = t_size - 2;
reminded_data = t_size - 2;
while (frame_size != 0)
{
uint8_t portid;
ss = streamdata.readRawData((char*)&portid, 1);
//ui.lineEdit_FileSize->setText(QString::number(fileSend_2Ser->size()));
uint8_t ProtocolID;
ss = streamdata.readRawData((char*)&ProtocolID, 1);
uint8_t MoreFragmentFlag;
ss = streamdata.readRawData((char*)&MoreFragmentFlag, 1);
uint8_t Seq;
ss = streamdata.readRawData((char*)&Seq, 1);
uint8_t size[2];
ss = streamdata.readRawData((char*)&size, 2);
l_size = size[0] * 256 + size[1];
if (packet_flags.Ser2Eth.packet_started[portid] == false) {
uint8_t DDCMP_Header[14];
ss = streamdata.readRawData((char*)&DDCMP_Header, 14);
packet_flags.Ser2Eth.protocol_payload_size[portid] = DDCMP_Header[7] + 256 * DDCMP_Header[8];
temp = packet_flags.Ser2Eth.protocol_payload_size[portid];
packet_flags.Ser2Eth.packet_started[portid] = true;
}
QByteArray ddcmp_datap(int(l_size), Qt::Initialization::Uninitialized);
streamdata.readRawData(ddcmp_datap.data(), l_size - 14);
if ((pre_more_frag == 0) && (MoreFragmentFlag == 0)) {
packet_flags.Ser2Eth.packet_ended[portid] = true;
packet_flags.Ser2Eth.protocol_payload_size[portid] = l_size;
temp = packet_flags.Ser2Eth.protocol_payload_size[portid];
}
else if ((pre_more_frag == 0) && (MoreFragmentFlag == 1)) {
packet_flags.Ser2Eth.packet_ended[portid] = false;
packet_flags.Ser2Eth.protocol_payload_size[portid] = l_size + 16;
temp = packet_flags.Ser2Eth.protocol_payload_size[portid];
}
else if ((pre_more_frag == 1) && (MoreFragmentFlag == 1)) {
packet_flags.Ser2Eth.packet_ended[portid] = false;
packet_flags.Ser2Eth.protocol_payload_size[portid] = packet_flags.Ser2Eth.protocol_payload_size[portid] + l_size;
temp = packet_flags.Ser2Eth.protocol_payload_size[portid];
}
else if ((pre_more_frag == 1) && (MoreFragmentFlag == 0)) {
packet_flags.Ser2Eth.packet_ended[portid] = true;
packet_flags.Ser2Eth.protocol_payload_size[portid] = packet_flags.Ser2Eth.protocol_payload_size[portid] + l_size;
temp = packet_flags.Ser2Eth.protocol_payload_size[portid];
}
if (MoreFragmentFlag == 1) {
pre_more_frag = 1;
}
else {
pre_more_frag = 0;
}
int ff = 0;
if (packet_flags.Ser2Eth.packet_ended[portid] == true) {
packet_flags.Ser2Eth.packet_started[portid] = false;
packet_flags.Ser2Eth.packet_started[portid] = false;
set_port_id_flag(portid, packet_flags.Ser2Eth.protocol_payload_size[portid], ProtocolID);
pre_more_frag = 0;
}
reminded_data = reminded_data - 6 - l_size;
//ui.lableSocketRead->setText(ddcmp_datap.toHex());
frame_size = frame_size - l_size - 6;
}//end of while (frame_size != 0)
uint8_t sync_footer[3];
streamdata.readRawData((char *)&sync_footer, 3);
dummy_size = 1446 - t_size - 8;
uint8_t dummy_data[1000];
streamdata.readRawData((char *)&dummy_data, dummy_size);
total_size_rcvd = total_size_rcvd - 1446;
if (total_size_rcvd == 0) {
tcpSocket_data_buffer_.clear();
}
} //end of if
}//end of while()
}
I'm extracting numbers from web page, and i want compare them, if
number is more than [previous one, then show it in iimDispaly(), otherwise if is it lower then skip, i trued here to do but cannot find a solution.
for(var i = 1; i <= 13; i++) {
for(var j = 3; j<=33; j+=3 ) {
iimPlayCode('TAG POS='+j+' TYPE=DIV ATTR=CLASS:"group_row_labeled" EXTRACT=TXT')
var res = iimGetLastExtract();
var result = res.replace(/[а-z]/g, '');
if(j==3) {
var firstRes = result;
}
if(result => firstRes) {
iimDisplay("Highest Number: " + result)
}
}
}
Copy & paste the following code and try to play it:
for(var i = 1; i <= 13; i++) {
for(var j = 3; j<=33; j+=3 ) {
iimPlayCode('TAG POS='+j+' TYPE=DIV ATTR=CLASS:"group_row_labeled" EXTRACT=TXT')
var res = iimGetLastExtract();
var result = parseFloat(res.replace(/[a-z]/g, ''));
if(j==3) {
var firstRes = result;
}
if(result >= firstRes) {
iimDisplay("Highest Number: " + result)
}
}
}
I'm using ASGALLANT'S Hide/Show method shown here:
http://jsfiddle.net/asgallant/6gz2Q/
Except I have fourth data series called average. When a series is hidden or shown, I recalculate the average... by adjusting the fiddle above:
function showHideSeries () {
var sel = chart.getChart().getSelection();
var view = chart.getView() || {};
// if selection length is 0, we deselected an element
if (sel.length > 0) {
// if row is undefined, we clicked on the legend
if (sel[0].row == null) {
var col = sel[0].column;
if (typeof(columns[col]) == 'number') {
var src = columns[col];
// hide the data series
columns[col] = {
label: datatable.getColumnLabel(src),
type: datatable.getColumnType(src),
sourceColumn: src,
calc: function () {
return null;
}
};
//record as hidden
hiddenSeries[col] = true;
// grey out the legend entry
series[columnsMap[src]].color = '#CCCCCC';
chart.setOption('series', series);
//If Exists a columnLabel called "Average", check if last column (average column is currently shown), recalculate average
if ((datatable.getColumnLabel(datatable.getNumberOfColumns() - 1) == 'Average') && (hiddenSeries[datatable.getNumberOfColumns() - 1] == false)){
for(var r = 0; r < datatable.getNumberOfRows(); ++r) {
sum = 0;
k = 0
for(var c = 1; c < datatable.getNumberOfColumns()-1; ++c) {
if(hiddenSeries[c] == false){
if(datatable.getValue(r, c) > 0){
sum = sum + datatable.getValue(r, c);
k = k + 1;
}
}
}
if (k == 0) k = 1;
datatable.setValue(r, datatable.getNumberOfColumns() - 1, sum/k);
}
}
var tmpColumn = new Array();
// Add each data value to the array with push()
for(var i = 0; i < datatable.getNumberOfRows(); ++i) {
tmpColumn.push(datatable.getValue(i, col));
}
}
else {
var src = columns[col].sourceColumn;
// show the data series
columns[col] = src;
series[columnsMap[src]].color = null;
//record as shown
hiddenSeries[col] = false;
//If Exists a columnLabel called "Average", check if last column (average column is currently shown), recalculate average
if ((datatable.getColumnLabel(datatable.getNumberOfColumns() - 1) == 'Average') && (hiddenSeries[datatable.getNumberOfColumns() - 1] == false)){
for(var r = 0; r < datatable.getNumberOfRows(); ++r) {
sum = 0;
k = 0;
for(var c = 1; c < datatable.getNumberOfColumns() - 1; ++c) {
if(hiddenSeries[c] == false){
if(datatable.getValue(r, c) > 0){
sum = sum + datatable.getValue(r, c);
k = k + 1;
}
}
}
if (k == 0) k = 1;
datatable.setValue(r, datatable.getNumberOfColumns() - 1, sum/k);
}
}
//chart.setOption('series.' + i + '.type', 'bars');
if(datatable.getColumnLabel(src) == 'Average' || datatable.getColumnLabel(src) == 'Peak Demand [kVA]'){
chart.getOptions().series[columnsMap[src]].type = 'line';
if(c.second_axis == true){
chart.getOptions().series[columnsMap[src]].targetAxisIndex = 1;
}
}
}
chart.setDataTable(datatable);
view.columns = columns;
chart.setView(view);
google.visualization.events.addListener(chart, 'ready', function() {
document.getElementById('chartImg').href = chart.getChart().getImageURI();
});
chart.draw();
}
}
}
It works great, except that the Standard tooltip is not being updated on the re-draw of the chart. I would like to avoid using custom tooltips as a solution. I would think that the ToolTip values should automatically update on each draw of the chart... if not, there must be a way to force it?
The issue here is that I was using formatted numbers as shown below:
var formatter = new google.visualization.NumberFormat(
{negativeColor: 'red', negativeParens: true, pattern: '###,###'});
formatter.format(datatable, i);
Therefore, not only did I have to use datatable.setValue(row, column, value) but I also have to use datatable.setFormattedValue(row, column, *String* value).
Referring to Syntax Highlighter Example provided by QT official website, I tried to implement (actually you can call copy paste) the same logic for multiline comment in my application. For reference this is the code for multiline comment highlighting:
Inside the constructor:
quotationFormat.setForeground(QColor(164, 14, 14));
rule.pattern = QRegExp("\".*\"");
rule.format = quotationFormat;
highlightingRules.append(rule);
charFormat.setForeground(QColor(164, 14, 14));
rule.pattern = QRegExp("\'.*\'");
rule.format = charFormat;
highlightingRules.append(rule);
singleLineCommentFormat.setForeground(Qt::darkGreen);
rule.pattern = QRegExp("//[^\n]*");
rule.format = singleLineCommentFormat;
highlightingRules.append(rule);
multiLineCommentFormat.setForeground(Qt::darkGreen);
commentStartExpression = QRegExp("/\\*");
commentEndExpression = QRegExp("\\*/");
Inside the highlightBlock() function:
foreach (const HighlightingRule &rule, highlightingRules) {
QRegExp expression(rule.pattern);
int index = expression.indexIn(text);
while (index >= 0) {
int length = expression.matchedLength();
setFormat(index, length, rule.format);
index = expression.indexIn(text, index + length);
}
}
setCurrentBlockState(0);
int startIndex = 0;
if (previousBlockState() != 1)
startIndex = commentStartExpression.indexIn(text);
while (startIndex >= 0) {
int endIndex = commentEndExpression.indexIn(text, startIndex);
int commentLength;
if (endIndex == -1) {
setCurrentBlockState(1);
commentLength = text.length() - startIndex;
} else {
commentLength = endIndex - startIndex
+ commentEndExpression.matchedLength();
}
setFormat(startIndex, commentLength, multiLineCommentFormat);
startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
}
But still there is problem I am facing when /* appears inside quotes. It appears with the multiline comment color (green color) till the end ( /*.png" ] } ) See the following sample document content for reference:
{
"code": [
"./Code/Main.js"
],
"textures": [
"./Content/*.png"
]
}
I am not master of Regular Expression, but I guess there is something wrong with the Regex.
With some luck and effort I have found a workaround (still it'll not cover all the cases)
int MySyntaxHighlighter::getCommentStartIndex(const QString &text, const int offset)
{
int startIndex = -1;
qDebug() << "offset: " << offset;
if(text.length() > 1 && offset < (text.length() - 1)) {
int commentStartIndex = commentStartExpression.indexIn(text, offset);
qDebug() << "commentStartIndex: " << commentStartIndex;
QRegExp quotationExpression(quotationRule.pattern);
int quotationStartIndex = quotationExpression.indexIn(text, offset);
qDebug() << "quotationStartIndex: " << quotationStartIndex;
if (quotationStartIndex >= 0) {
int quotationLength = quotationExpression.matchedLength();
qDebug() << "quotationLength: " << quotationLength;
if(commentStartIndex > quotationStartIndex &&
commentStartIndex < (quotationStartIndex + quotationLength)) {
startIndex = getCommentStartIndex(text, (commentStartIndex + 2));
} else {
startIndex = commentStartIndex;
}
} else if(commentStartIndex >= 0) {
startIndex = commentStartIndex;
} else {
startIndex = -1;
}
}
qDebug() << "startIndex: " << startIndex;
return startIndex;
}
void MySyntaxHighlighter::highlightBlock(const QString &text)
{
setCurrentBlockState(0);
qDebug() << "text: " << text;
qDebug() << "previousBlockState(): " << previousBlockState();
int startIndex = 0;
if (previousBlockState() != 1) {
// startIndex = commentEndExpression.indexIn(text);
startIndex = getCommentStartIndex(text, 0);
}
while (startIndex >= 0) {
int endIndex = commentEndExpression.indexIn(text, startIndex);
int commentLength;
if (endIndex == -1) {
setCurrentBlockState(1);
commentLength = text.length() - startIndex;
} else {
commentLength = endIndex - startIndex
+ commentEndExpression.matchedLength();
}
setFormat(startIndex, commentLength, multiLineCommentFormat);
// startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
startIndex = getCommentStartIndex(text, startIndex + commentLength);
}
}
If someone finds a better solution please share it with me.
h value can be between 0<400 or even more, the below coding practise looks tedious...any better thought?
if (h<=40) {
nOfRound = 1;
} else if (40<h<=80) {
nOfRound = 2;
} else if (80<h<=120) {
nOfRound = 3;
} else if (120<h<=160) {
nOfRound = 4;
} else if (160<h<=200) {
nOfRound = 5;
} else {
qDebug() <<"too big";
nOfRound = 6;
}
Like this:
if (h <= 200) { nOfRound = h / 40 + 1; }
else { nOfRound = 6; qDebug << "too big"; }
(Note that division by a constant is actually pretty efficient, since it is typically implemented by a (fast) multiplication rather than a dynamic division.)