Tuesday, 10 September 2013

Regular expression to match colon

Regular expression to match colon

I need to add an interface to an class in c# and need to match if has
already been declared:
public partial class Client
Needs to be
public partial class Client: IEntity
And
public partial class Client: IEntity
Must remain
I have reached this "public partial class (\w+)" into "public partial
class $1: IEntity"
Thanks

Multiple displays in SDL2

Multiple displays in SDL2

I am writing a program that displays an animation that is dependent on the
size of the display. In order to get this to work with multiple displays,
I have an array of display_data objects:
struct display_data
{
SDL_Rect bounds;
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *texture;
//...
};
and initialize these for each display:
int numdisplays = SDL_GetNumVideoDisplays();
for( int i = 0 ; i < numdisplays ; ++i )
{
SDL_GetDisplayBounds( i, &( screens[ i ].bounds ) );
screens[ i ].window
= SDL_CreateWindow( "Display", screens[ i ].bounds.x,
screens[ i ].bounds.y, screens[ i ].bounds.w,
screens[ i ].bounds.h, SDL_WINDOW_FULLSCREEN );
screens[ i ].renderer = SDL_CreateRenderer( screens[ i ].window, -1, 0 );
SDL_SetRenderDrawColor( screens[ i ].renderer, 0, 0, 0, 255 );
SDL_RenderClear( screens[ i ].renderer );
SDL_RenderPresent( screens[ i ].renderer );
screens[ i ].texture = SDL_CreateTexture( screens[ i ].renderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
screens[ i ].bounds.w,
screens[ i ].bounds.h );
//...
}
This works fine as long as my mouse cursor is in the primary display, but
if I start the program with the cursor in the secondary display, it will
draw both windows in the secondary display, resulting in only the second
window being visible. This behavior seems to depend only on the location
of the cursor and not the terminal window from which I run the program.
I have verified that the same display numbers and bounds are found
regardless of the cursor location, so I am perplexed by the variation in
the program behavior. Is this the intended behavior of SDL2, or a bug? In
either case, could anyone suggest a workaround?
I am using XFCE4 on Debian Sid, in case that makes a difference. I have
not had the chance to test this on any other systems or desktop
environments, or with more than 2 monitors.

Get image url google cse JSON

Get image url google cse JSON

I am trying to retrieve images from JSON CSEand no success, I am able to
retrieve title and description by
for (var i = 0; i < response.items.length; i++)
{
var item = response.items[i];
var title = item.htmlTitle;
...
But I am not able to query image url from JSON, here is JSON schema:
"items": [
{
"kind": "",
"title": "",
"htmlTitle": "",
"link": "",
"displayLink": "",
"snippet": "",
"htmlSnippet": "",
"cacheId": "",
"formattedUrl": "",
"htmlFormattedUrl": "",
"pagemap": {
"product": [
{
"name": "",
"image": "",
"description": ""
}
],
"cse_image": [
{
"src": ""
}
],
I tried:
item.pagemap.product.image
item.[pagemap][product].image
item.product.image
etc
Please help.

C++: Passing classes as argument to methods of other classes

C++: Passing classes as argument to methods of other classes

I want to pass a class pointer to a function which is a member of another
class and it won't work. If the function fill_packet is not a member of
any class, the code compiles without problems:
// WORKS FINE:
class Packet{
private:
char *contents;
public:
void pack();
};
void fill_packet(Packet *a){
a->pack();
};
However, if the function fill_packet is inside another class (here
Worker), it suddenly does no longer compile as soon as I try to access a
method of a.
// WON'T COMPILE:
// error: 'Packet' has not been declared
class Packet{
private:
char *contents;
public:
void pack();
};
class Worker{
public:
void fill_packet(Packet *a){
a->pack();
};
};
Can someone give me insight? thanks in advance

When should I write my functions as higher order functions

When should I write my functions as higher order functions

I've recently started learning F# and come across higher order functions
for simple examples such as the following:
Consider a function that calculates sales by multiplying price p by number
of units sold n.
let sales (p,n) = p * (float n);;
The type of this function is given as
val sales : p:float * n:int -> float
i.e. take a pair of float and int and returns a float.
We can instead write this as a higher order function
let salesHi p n = p * (float n);;
The type of this higher order function is given as
val salesHi : p:float -> n:int -> float
i.e. takes a float and returns a function of int to float.
In simple cases this seems to make no difference
sales (0.99, 100);;
salesHi 0.99 100;;
Both give
val it : float = 99.0
However with the higher order function I can feed in the price for
particular items to get new functions. E.g.
let salesBeer = salesHi 5.99;;
let salesWine = salesHi 14.99;;
Then salesBeer 2 gives 11.98 and salesWine 2 gives 29.98.
Also, I've noticed that built-in operators such as + are defined as higher
order functions, so I can write, for example:
let plus2 = (+) 2;
List.map plus2 [1;3;-1];;
and get
val it : int list = [3; 5; 1]
This seems like a good thing. So when I want to implement a function in an
imperative language that would have taken n > 1 arguments, should I for
example always use higher order function in F# (so long as the arguments
are independent)? Or should I take the simple route and use regular
function with an n-tuple and curry later on if necessary? Or something
else?
How do F# programmers decide when to make a function higher order or use a
regular function with a tuple?

Monodroid in app purchases

Monodroid in app purchases

I'm trying to add In app Purchases in my Monodroid application. Has anyone
successfully implemented this in their app?
I found this project
enter link description here
but I'm not sure how to integrated in my app
I also found this
enter link description here
Which mentions in app purchases, but I kind find any example of how to do it
Thanks

Monday, 9 September 2013

Push string into vector and send vector through socket

Push string into vector and send vector through socket

Here I am tring to push into vector. And then want to send it via socket
How can I correctly push, print each word and then send entire vector to
socket?
:
int main(int, char**)
{
string text = "token test string";
char* word;
char *str =NULL;
int i=0,j=0;
word = strtok(& text[0], " ");
std::vector<std::string> vec;
while(word!=NULL)
{
cout << word << " : " << text.length() << endl;
word = strtok(NULL, " ");
vec.push_back(word);
str[j]=*word;
j++;
}
for (int i=0; i<vec.size();i++){
cout << vec[i] << endl; ---> Segmentation fault after printing
forst word "tocken"
}
send(*csock, vec, vec.size, 0)) == -1) --> is this correct? give
correction please if wrong
}
Please show me some help with existing code only. Taking another data
structure is not possible me as it encorporate other code of existing
project.