I am writing a script that can create proxy rule from a csv file.
#!/usr/bin/env bash
awk -F', ' '
function head (soft, keyword) {
if (soft == "clash") {
switch (keyword) {
case "HOST":
rule_head = "- DOMAIN";
break;
case "KEYWORD":
rule_head = "- DOMAIN-KEYWORD";
break;
case "SUFFIX":
rule_head = "- DOMAIN-SUFFIX";
break;
case "IP-CIDR":
rule_head = "- IP-CIDR";
break;
}
} else
if (soft == "quant") {
switch (keyword) {
case "HOST":
rule_head = "HOST";
break;
case "KEYWORD":
rule_head = "HOST-KEYWORD";
break;
case "SUFFIX":
rule_head = "HOST-SUFFIX";
break;
case "IP-CIDR":
rule_head = "IP-CIDR";
break;
}
}
ruturn rule_head;
}
function tail (soft, keyword) {
if (soft == "clash") {
switch (keyword) {
case "REJECT":
rule_tail = "REJECT";
break;
case "DIRECT":
rule_tail = "DIRECT";
break;
case "jp_proxy":
rule_tail = "Japan_Tokyo";
break;
case "us_proxy":
rule_tail = "USA_Oregon";
break;
case "kr_proxy":
rule_tail = "Korea_Seoul";
break;
}
} else
if (soft == "quant") {
switch (keyword) {
case "REJECT":
rule_tail = "REJECT";
break;
case "DIRECT":
rule_tail = "DIRECT,no-resolve";
break;
case "jp_proxy":
rule_tail = "Japan - Tokyo";
break;
case "us_proxy":
rule_tail = "USA - Oregon";
break;
case "kr_proxy":
rule_tail = "Korea - Seoul";
break;
}
}
return rule_tail;
}
{
if (NR>2) {
printf "%s,%s,%s\n", head(clash, $2), $3 ,tail(clash, $4);
}
}
' Resources/exception.csv
and the file Resources/exception.csv
# group, type, basis, action, comment
china_list, KEYWORD, 360buy, DIRECT, No_comment
china_list, KEYWORD, baidu, DIRECT, No_comment
china_list, KEYWORD, bdstatic, DIRECT, No_comment
china_list, KEYWORD, bcebos, DIRECT, No_comment
proxy_list, KEYWORD, fbcdn, us_proxy, No_comment
proxy_list, KEYWORD, twitter, us_proxy, No_comment
proxy_list, KEYWORD, twimg, us_proxy, No_comment
and the shell output is
,360buy,
,baidu,
,bdstatic,
,bcebos,
,fbcdn,
,twitter,
,twimg,
The rule_head and rule_tail seems not working at all, I already tried to print the keyword inside function head or tail > if before switch/case, it can print the keyword correctly.
But when I tried to print the rule_head or rule_tail after the switch/case, it output noting.
Could you please try changing either(I am driving so haven't test it but should work if your code is not having any syntax issues)
head(clash, $2), $3 ,tail(clash, $4); ----> head("clash", $2), $3 ,tail("clash", $4);.
OR you could create a variable with awk -v value="clash" to keep it in variable format only. Then mention:
head(value, $2), $3 ,tail(value, $4);
#JoshuaLee. mentioning anything inside " double quotes means we are mentioniong value, if you want to have a variable and assign value to it use var="bla" or you want to pass directly anything without variable any value then use print "bla" for example I am mentioning here.
Related
I'm running a switch/case that looks at the Vuetify breakpoint, but instead of just taking the name and giving me the int I want, it always ends up taking whatever the highest number is for the "limitSize" variable.
This is for a news slider I'm working on where, depending on the breakpoint, it shows either one, two, or three elements in the slider. I've tried giving the variable a default value, but that didn't work. I'd preferably like it to go SM & down is 1, MD is 2 and LG & Up is 3, but I'm not sure what the right way to achieve that is. Any help would be greatly appreciated.
The following is the Switch/Case which sits inside a computed property. I've also attached an image of the current results of the code (Image is on MD window when I'd want 2)
VueJS
pageOfWhatsNews() {
var limitSize;
switch (this.$vuetify.breakpoint.name) {
case "xs":
limitSize = 1;
case "sm":
limitSize = 1;
case "md":
limitSize = 2;
case "lg":
limitSize = 3;
case "xl":
limitSize = 3;
}
var start = (this.currentPage - 1) * limitSize;
var end = (this.currentPage - 1) * limitSize + limitSize;
return this.whatsNews.slice(start, end);
}
Well, it's because you are missing break statements in between your switch cases. Look at the correct syntax of a switch-case block below:
Switch-Case Syntax
const expr = 'Papayas';
switch (expr) {
case 'Oranges':
console.log('Oranges are $0.59 a pound.');
break;
case 'Mangoes':
case 'Papayas':
console.log('Mangoes and papayas are $2.79 a pound.');
// expected output: "Mangoes and papayas are $2.79 a pound."
break;
default:
console.log(`Sorry, we are out of ${expr}.`);
}
Solution
function pageOfWhatsNews() {
var limitSize;
switch (this.$vuetify.breakpoint.name) {
case "xs":
limitSize = 1;
break;
case "sm":
limitSize = 1;
break;
case "md":
limitSize = 2;
break;
case "lg":
limitSize = 3;
break;
case "xl":
limitSize = 3;
break;
}
var start = (this.currentPage - 1) * limitSize;
var end = (this.currentPage - 1) * limitSize + limitSize;
console.log(limitSize);
// return this.whatsNews.slice(start, end);
}
// Just for demo
this.$vuetify = {breakpoint: {name: 'md'}};
pageOfWhatsNews();
Optimized Solution
I would also suggest you put cases of md, lg & xl breakpoints and rest of the breakpoints case fallback to the default case.
function pageOfWhatsNews() {
var limitSize;
switch (this.$vuetify.breakpoint.name) {
case "md":
limitSize = 2;
break;
case "lg":
limitSize = 3;
break;
case "xl":
limitSize = 3;
break;
default:
limitSize = 1;
}
var start = (this.currentPage - 1) * limitSize;
var end = (this.currentPage - 1) * limitSize + limitSize;
console.log(limitSize);
// return this.whatsNews.slice(start, end);
}
// Just for demo
this.$vuetify = {breakpoint: {name: 'sm'}};
pageOfWhatsNews();
I want to add a select list to to/from fields of range_number so user can pick from set amounts to set range.
best option would be to use the custom function filtering filter_type: 'custom_func'
see showcase, first column , code sample and everything can be found on that page
{
column_number: 0,
filter_type: 'custom_func',
custom_func: myCustomFilterFunction,
...
(possible) custom func implementation
function myCustomFilterFunction(filterVal, columnVal) {
var found;
if (columnVal === '') {
return true;
}
switch (filterVal) {
case 'happy':
found = columnVal.search(/:-\]|:\)|Happy|JOY|:D/g);
break;
case 'sad':
found = columnVal.search(/:\(|Sad|:'\(/g);
break;
case 'angry':
found = columnVal.search(/!!!|Arr\.\.\./g);
break;
case 'lucky':
found = columnVal.search(/777|Bingo/g);
break;
case 'january':
found = columnVal.search(/01|Jan/g);
break;
default:
found = 1;
break;
}
if (found !== -1) {
return true;
}
return false;
}
How could I shorter this part of the code and instead of putting all options append weather icons for all possible situations?
if (desc == "clear sky")
{
$('div.clear').removeClass('hide');
} else if (desc == "broken clouds")
{
$('div.cloudy').removeClass('hide');
}
else if (desc == "few clouds")
{
$('div.cloudy').removeClass('hide');
}
and so on...
else {
$('#desc').text("now it's ");
}
You can use a switch/case statement which would look like this
switch(desc)
{
case "clear sky":
$('div.clear').removeClass('hide');
break;
case "broken clouds":
$('div.cloudy').removeClass('hide');
break;
case "few clouds":
$('div.cloudy').removeClass('hide');
break;
case default:
$('#desc').text("now it's ");
break;
}
An other option would be to make a dictionary where the key is the case and the value is a function to execute.
I use Gstreamer as base for playing videos and audio and so far its working fine. Now I want to know when media is stopped, paused, loaded or started playing and notify the GUI. Now the Messages keep repeating itself (see the attached image). Now in that situation, it will be flooding my GUI. I want to receive event only once (for example when media is stopped or pause or loaded).
The only Message that I get consistently (once) is End of Stream.
Is there a way to get the said message only once, when they happen?
Here is my current code
extern "C"
{
static GstBusSyncReply
bus_sync(GstBus* bus, GstMessage* message, gpointer user_data)
{
CustomData* data = reinterpret_cast<CustomData*>(user_data);
GstState old_state, new_state, pending;
gst_message_parse_state_changed(message, &old_state, &new_state, &pending);
g_print ("State set from %s to %s --- %s\n", gst_element_state_get_name (old_state), gst_element_state_get_name (new_state), gst_element_state_get_name (pending));
switch(GST_MESSAGE_TYPE(message))
{
case GST_MESSAGE_STATE_CHANGED:
{
wxGStreamer* win = dynamic_cast<wxGStreamer*>(data->parent);
if(win)
{
win->SendMessage(wxMEDIASTATE_CHANGED);
}
if(data->state!=new_state)
{
//state changed, save it
data->state = new_state;
if(old_state==GST_STATE_NULL && new_state == GST_STATE_READY && pending== GST_STATE_PLAYING)
{
g_print ("***Loaded***\n");
}
else if(old_state==GST_STATE_PLAYING && new_state == GST_STATE_PAUSED && pending== GST_STATE_VOID_PENDING)
{
g_print ("***Paused***\n");
}
else if(old_state==GST_STATE_READY && new_state == GST_STATE_NULL && pending== GST_STATE_VOID_PENDING)
{
g_print ("***Stopped***\n");
}
else if(new_state == GST_STATE_PLAYING &&(old_state==GST_STATE_READY||old_state==GST_STATE_PAUSED) )
{
g_print ("***Playing***\n");
}
}
break;
}
}
}
}
Simply answered: you get more messages from GstBus other than play/stop states.
Implement a switch in your code and your if/else statements are called only once:
switch (GST_MESSAGE_TYPE (msg)) {
case GST_MESSAGE_ERROR:
break;
case GST_MESSAGE_WARNING:
break;
case GST_MESSAGE_INFO:
break;
case GST_MESSAGE_EOS:
break;
case GST_MESSAGE_BUFFERING:
break;
case GST_MESSAGE_STATE_CHANGED: {
GstState old_state, new_state, pending_state;
gst_message_parse_state_changed (msg, &old_state,
&new_state, &pending_state);
if (GST_MESSAGE_SRC (msg) == GST_OBJECT (__YOUR_PIPELINE_POINTER__)) {
// your code here
}
}
break;
}
// DON'T FORGET TO RETURN
return GST_BUS_DROP;
Hope, that helps ;-)
I am trying to add new custom error in my login, if user_status is '0' then it should give error, I am proceeding as follow :
UserIdentity.php
const ERROR_USERNAME_INACTIVE=68;
public function authenticate()
{
$user = Users::model()->findByAttributes(array('email'=>$this->username));
if ($user===null)
{
$this->errorCode=self::ERROR_USERNAME_INVALID;
}
else if ($user->password !== md5($this->password))
{
$this->errorCode=self::ERROR_PASSWORD_INVALID;
}
else if($user->user_status !='1')
{
$this->errorCode=self::ERROR_USERNAME_INACTIVE;
}
else
{
$this->errorCode=self::ERROR_NONE;
$this->_id = $user->id;
}
return !$this->errorCode;
}
In login.php
case UserIdentity::ERROR_NONE:
Yii::app()->user->login($identity);
break;
case UserIdentity::ERROR_USERNAME_INVALID:
$this->addError('username','No Such user is associated with us .');
break;
case UserIdentity::ERROR_USERNAME_INACTIVE:
$this->addError('user_status','Sorry, this user is not activated yet');
default: // UserIdentity::ERROR_PASSWORD_INVALID
$this->addError('password','Password is incorrect.');
break;
Now I am getting problem that when the user_Status is !=1 then it is giving the error correctly but also giving, password incorrect while the password is correct
You forgot to add a break to the last case statement. Without a break the default will be executed.
...
break;
case UserIdentity::ERROR_USERNAME_INACTIVE:
$this->addError('user_status','Sorry, this user is not activated yet');
break;
default: // UserIdentity::ERROR_PASSWORD_INVALID
...
Sorry, I was making a silly mistake, after switch case, i forget to put break statement so the next default statement was also executing.
case UserIdentity::ERROR_USERNAME_INACTIVE:
$this->addError('user_status','Sorry, this user is not activated yet');
**break;**
default: // UserIdentity::ERROR_PASSWORD_INVALID
$this->addError('password','Password is incorrect.');
break;