Thursday, May 5, 2011

Can I use full text indexing on a view?

I am using SQL Server 2000 and I am using full text indexing on a table. Can I use full text indexing on a view of this table? If yes, then how can I use? If no, then what should I do?

Actually, I am doing refinement of the results obtained from a table and for this refinement I am using a view of this table...

From stackoverflow

Scaffolding for PHP

Is there any thing in PHP to create basic scaffold, like in Rails?

EDIT: I need something to prototype quickly..

From stackoverflow
  • If you use CakePHP as the web framework it supports scafalding. See this link for more info. http://book.cakephp.org/view/105/Scaffolding

    Akshay : i looked at cakePHP but it seemed you still have to do a bunch of stuff. (i maybe wrong!)
    troelskn : So do you with Rails' scaffolding. It's really just a startingpoint.
  • Some frameworks like Symfony, CakePHP, Akelos, CodeIgniter and others have support for scaffolding.

    However if you don't want to use a framework you can try phpScaffold which generates CRUD scaffold pages based on phpMyAdmin table exports...

    Akshay : thanks.. phpScaffold is almost what i was looking for.
  • QCodo is another great option. And since it uses reflection to do Code Generation instead of reflection at runtime, you'll likely see better performance.

    Alex : I too use QCodo, it's awesome. The fork of QCodo that's currently active in development is QCubed
  • First, Rails is a framework. PHP is a language. PHP does not have built-in scaffolding support, just as Ruby--the language Rails is build on--does not. A framework like CakePHP, however, does support scaffolding.

    Second, I see that you raised an objection to CakePHP because "you still have to do a bunch of stuff." That's true--with any framework, you're going to have to learn new conventions, configurations etc.

    Akshay : Maybe the language of question was not perfect, but basic intent was just to find something that will generate basic scaffold _in_ php code; just like rails scaffold generator does. Not that i'm opposed to learning a new framework, but currently i just needed that just generates basic scaffold. Thx.
  • For myslef I Use CodeIniter for development, sure they have scaffolding, but only in terms of a "simple scaffolding" which mean you're not gonna use it in live product (i dunno about everyone but i'm only using it as some tools) .

    but if you need some like CRUD generator you can use SparkPlug , or Ignition both of them can be used on Codeigniter

  • Another approach to scaffolding is using action delegates... a pretty good article on that can be found here http://iam.mrburly.com/content/display/show/slug/action-delegates.

  • For anyone else reading this thread, I've released a powerful standalone scaffold class for PHP: http://www.aaronharp.com/dev/php-scaffold-class/

  • what about aaronharp.com scaffold class? is $29 but its worthit.

Postfix / Spamassassin: Undelivered Mail Returned to Sender

I'm having an issue with a newly configured mailserver where spam emails that are spoofed to come from the local domain are actually accepted by the mailserver, the mail isnt delivered as is though, the spamassassin tags it as spam and then send an email "Undelivered Mail Returned to Sender" to the spoofed local user.

I know there is a way of fixing this in the configuration but i have no idea where, i'm hoping someone can point me in the right direction.

To be clear, the mailserver is not relaying, this is only a local user issue. I want postfix to reject any emails supposedly from local users that aren't sent internally. It would stop this problem.

Here is an email to show you whats happening. I've changed the domain to example.com.au.

###############################################


This is the mail system at host example.com.au.

I'm sorry to have to inform you that your message could not
be delivered to one or more recipients. It's attached below.

For further assistance, please send mail to postmaster.

If you do so, please include this problem report. You can
delete your own text from the attached returned message.

The mail system

dan@example.com.au: host 127.0.0.1[127.0.0.1] said: 554 5.7.0 Reject, id=11887-07 - SPAM (in reply to end of DATA command)
?
Reporting-MTA:dns; example.com.au
X-Postfix-Queue-ID: 661DC5D1DE
X-Postfix-Sender: rfc822; dan@example.com.au
Arrival-Date: Tue, 5 May 2009 06:21:38 +1000 (EST)

Final-Recipient: rfc822;dan@example.com.au
Original-Recipient:rfc822;dan@example.com.au
Action: failed Status: 5.7.0
Remote-MTA: dns; 127.0.0.1
Diagnostic-Code: smtp; 554 5.7.0 Reject, id=11887-07 - SPAM ?

From: Berenice Penez dan@example.com.au
Date: Mon, 4
May 2009 22:21:41 +0200
To: dan@example.com.au Subject: Were it you, on forum?

Reliable quality and no delays with
delivery! Super online store for
disease treating
http://www.xopfekec.cn/

###############################################

Postfix main.cf (the important parts, not complete)

readme_directory = /usr/share/doc/postfix
mydomain_fallback = localhost
message_size_limit = 0
mailbox_size_limit = 0
myhostname = example.com.au
mailbox_transport = cyrus
mydomain = example.com.au
inet_interfaces = all
enable_server_options = yes
mydestination = $myhostname,localhost.$mydomain,localhost,example.com.au
smtpd_sasl_auth_enable = yes
smtpd_use_pw_server = yes
smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination,reject_unknown_recipient_domain,reject_unknown_sender_domain,reject_invalid_hostname
smtpd_pw_server_security_options = plain,login
content_filter = smtp-amavis:[127.0.0.1]:10024
mynetworks = 127.0.0.0/8, 10.0.1.0/24
smtpd_client_restrictions = permit_sasl_authenticated,reject_rbl_client dnsbl.sorbs.net

From stackoverflow
  • A few different points:

    • This should be on serverfault.com, but since I'm not on the beta there I'll answer it here.

    • The output of postconf -n is better than including what you think are the relevant lines in main.cf. Also include relevant lines from master.cf if you have parameter overrides or other customisations in there.

    • Don't accept then bounce mail like that. If you are using SpamAssassin as an after-queue filter in Postfix (the usual way of running it), you need to either tag-and-deliver (and filter with client-side rules) or quarantine the mail without notifying the sender. From the look of your question, you are probably a backscatter source. Stop it. See for example http://www.postfix.org/BACKSCATTER_README.html. Do consider amavisd-new for integrating SpamAssassin into Postfix with all kinds of useful features.

    • Consider collapsing all of your restrictions into smtpd_recipient_restrictions. It's generally easier to manage the linear flow of restrictions like that than to deal with the interactions between smtpd_{client,helo,sender,recipient}_restrictions.

    • To prevent Postfix from accepting mail from outside, add a sender_access map that rejects mail claiming to be from your domains:

    smtpd_recipient_restrictions = 
      permit_sasl_authenticated, 
      permit_mynetworks, 
      reject_unauth_destination, 
      check_sender_access hash:$config_directory/reject_mydomains
      reject_unknown_recipient_domain,
      reject_unknown_sender_domain,
      reject_invalid_hostname
    

    And in reject_mydomains

    example.com.au REJECT you are not me
    

    This will probably be prone to false positives with mail that comes from senders with a legitimate(?) reason to use your domain as the envelope sender (E-cards, invitations, maybe some outsourced service like surveys or whatnot). You can whitelist around your you-are-not-me rules with a client_access map before your sender_access map that returns OK or an appropriate restriction class (see http://www.postfix.org/RESTRICTION_CLASS_README.html).

    You can use similar HELO checks to weed out clients HELO-ing with your own Hostname/IP or known bad HELO strings

    smtpd_recipient_restrictions = 
      permit_sasl_authenticated, 
      permit_mynetworks, 
      reject_unauth_destination, 
      check_helo_access hash:$config_directory/helo_checks
      check_sender_access hash:$config_directory/reject_mydomains
      reject_unknown_recipient_domain,
      reject_unknown_sender_domain,
      reject_invalid_hostname
    

    and in helo_checks:

    example.com.au             REJECT BAD-HELO you are not example.com.au
    mailserver.example.com.au  REJECT BAD-HELO you are not me
    localhost                  REJECT BAD-HELO you are not me
    localhost.localdomain      REJECT BAD-HELO you are not me
    # where 1.2.3.4 is the IP of your server
    1.2.3.4                    REJECT BAD-HELO you are not me
    127.0.0.1                  REJECT BAD-HELO you are not me
    

    Lastly, it's a very good idea to subscribe to a good reputation service such as an RBL. The best RBL for most purposes is zen.spamhaus.org. It's free to use for light to moderate loads, and if your usage is high enough to cross over their free/paid threshold, the cost is well worth it. To configure in Postfix, add

    reject_rbl_client zen.spamhaus.org
    

    to your smtpd_recipient_restrictions. Do that after your cheap local checks to save on DNS query load and latency, but before expensive local checks like reject_unverified_recipient (you aren't using that one and probably don't need it from your problem description).

    robc : Glad to help (please upvote or accept the answer if it helps). Note that the check_sender_access is the important part for your specific question. The HELO checks are nice to have and an easy way to weed out particular kinds of bad clients. One thing I missed in my original answer was to subscribe to a good RBL. Zen.spamhaus.org is the best default choice. I'll edit the answer to incluse that.

DISPhelper Library

Has anyone used DISPhelper library? I wanna know if it's really a better way to use this library if you're pretty new to COM technology. I'd appreciate your feedbacks. thanks..

From stackoverflow
  • It is really better for a beginner. It's easy to use and comes along with a bunch of examples. But if you're into serious COM programming, better learn it the conventional way.

What is the best way to utilize Subversive when sharing multi-module project in Eclipse?

I'm trying to share multi-module Maven project (using m2eclipse plug-in) from SpringSource Tool Suite 2.0.0 to Subversion server (Subversive plug-in for Eclipse). I've found that svn:ignore property is not propagated to submodules of the parent project: I need to right-click on the property and run "Apply recursively...". More, it won't work if root folder of submodule is not in SVN repository yet: first of, I need to check-in root folder, then right-click on the property and run "Apply recursively...".

Am I missing something? Please, describe your way of ridding of the problem.

P.S. The project has nested structure:

PARENT
+ component A
  - module AA
  - module AB
+ component B
  - module BA
+ component C
  - module CA
  - module CB
  - module CC
From stackoverflow
  • If the Eclipse plugin 'Subversive' GUI is not advanced enouggh to support the kind of property setting you want, may be a command line would be more efficient.

    cd PARENT
    ls -s | xargs svn propset -R -F ignore.txt svn:ignore
    

    That way, you apply svn:ignore only on already committed directories (and sub-directory) without having to commit first PARENT.

    If you are on Windows, a package like GnuWin32 can help (or there is multiple alternative, like PowerShell)

sql server 2005 :how to put not null constraint on a column depending upon value in other column?

in a table two of columns are billable(bit),billabledate(datetime).i want billable date to be not null if billable is not null.

From stackoverflow
  • I'd try adding a trigger to the table, on after insert and after update, to enforce that constraint. Check the billable value, and block insert/update in case it is not null and the billabledate is null.

  • Add a check constraint:

    CHECK (billable is not null and billabledate is not null) OR (billable is null)

  • You need a Check Constraint

    ALTER TABLE dbo.Table WITH NOCHECK
    ADD CONSTRAINT CK_Table_BusinessRule CHECK (Billable IS NOT NULL AND BillableDate IS NOT NULL)

    http://msdn.microsoft.com/en-us/library/ms179491(SQL.90).aspx

How does Phusion Passenger reuse threads and processes?

I am setting up an Apache2 webserver running multiple Ruby on Rails web applications with Phusion Passenger. I know that Passenger spawns Ruby processes for handling requests. I have the following questions:

  • If more than one request has to be handled at the same time, will Passenger spawn multiple processes or multiple (Ruby) threads? How do I configure it so it always spawns single-threaded processes?
  • If I have two Rails applications, imagine that a request for app A goes to process 1, then later request for app B arrives. Is it possible that process 1 will handle this request as well? When and how is this possible? In other words, is one process allowed to handle requests for multiple Rails applications?
  • I have the same Rails application exported in multiple URLs and multiple virtual hosts (such as http:// and https://). Will the same process be able to serve different virtual hosts? (The answer to this seems to be yes, I've set a global variable in answering a request to virtual host A, and I was able to retrieve the value in virtual host B.)
From stackoverflow
  • Generally speaking, Passenger spawns new processes by forking an ApplicationSpawner, which has the framework and application code pre-loaded into memory, or a FrameworkSpawner, which just has the framework code.

    Passenger, as far as I know, doesn't deal in threads. Instead, as the load increases on an application, it will fork that Application's ApplicationSpawner and initialize another instance. When load decreases, one or more application instances are killed off.

    If Passenger is configured in a certain way (I believe by choosing the "smart" spawn method), it will create a FrameworkSpawner, which loads the rails code, but no application code, which can then be forked to load and application using that version of Rails.

    So to answer your questions:

    • It will serve them sequentially, then spawn additional processes if it decides the load is high enough.

    • No. One process can only belong to a single Rails Application.

    • I'm kind of sketchy on this one, but your experiment makes sense. Passenger should be smart enough to figure out that even though it's running from different places in the server config, you're talking about the same application. It's probably based on the application's filesystem path.

    EDIT: I went and read up on this a bit. Turns out I was mostly right, but the technical details were a bit off. See the Passenger documentation

  • Yup, Burke is right. In case of the third question, Phusion Passenger recognizes applications by their application root path. So even if you have two virtual hosts, if they both point to the same DocumentRoot then Phusion Passenger will think that they're the same app.

Problem in reading Excel Sheet with Special Character in C#

Hi

I am Using OLEDB to read Excel Sheet with C#. In that Column header Name is "Sr.No." in my "Sheet1". When i fetch Excel Sheet Data into my Datatable , i am getting "Sr#No#" as a column header name instead of original one. Can you please tell me why it’s happening so? And what is the solution for the same.

Thanks.

From stackoverflow
  • Googling around suggests that this is an inherent feature of OLEDB, rather than being anything to do with Excel - it seems to simply not like periods (.) in column names.

    My suggestions would be either to cope with it - it seems to be a deterministic replacement of .s with #s - or if possible use an alternative means of reading Excel data from C# (search this site for alternatives).

Would Drupal be a viable CMS for a mobile web application?

I am evaluating open-source CMS systems to see which one(s) would be the best option to deliver rich mobile web based applications for mobile handhelds.

So far I am leaning towards Drupal because it seems to be the most extensible, flexible, best performance (cache & compression, etc) and has a good user management system.

The CMS must be able to deliver HTML content that is ideally rendered for most smart handheld phones. I have to assume most if not all would do so as you would just control the front-end (headers, CSS, HTML and javascript) to ensure it renders correctly...correct?

So, it Drupal a good option? And, is there anything else I am missing when choosing a CMS to deliver mobile web pages?

From stackoverflow
  • I think Drupal is Ok for your application but it has no built in support for mobile. I have worked with drupal but not with mobile web apps with Drupal. But Check this. It seems we can control the things as you expect.

    Update: Check this as well

  • I think it depends on what kind of "rich mobile web based application" you are talking about. As far as rendering the front end for mobile devices go (such as screen size based UI etc.), so long the CMS allows you to control the stylesheets and JS flexibly that's all that matters.

    The choice of the CMS itself depends on your own level of comfort also. Do take a look at Joomla! too

    Bamerza : I am leaning against Joomla mainly because Joomla does not have native support for multiple sites, less flexible/extensible than Drupal, and a less advanced user group permissions system.
  • One thing to watch out for with Drupal is that the theme engine tends to be quite verbose. If you take a look at pretty much any Drupal site in something like Firebug, you'll quickly notice that it's full of nested-nested-nested-nested divs.

    This makes it great for theming, since there's almost always an element, with a unique ID, to apply your CSS styles to. The downside is that it presents heavy, complex pages that could potentially cause problems for mobile browsers.

    ceejayoz : I find that Drupal's theme engine isn't what's verbose, it's a few modules - Views being a particularly notable one. It can be very helpful for someone who knows CSS but is not comfortable with PHP.

Passing references to pointers in C++

As far as I can tell, there's no reason I shouldn't be allowed to pass a reference to a pointer in C++. However, my attempts to do so are failing, and I have no idea why.

This is what I'm doing:

void myfunc(string*& val)
{
    // Do stuff to the string pointer
}

// sometime later 
{
    // ...
    string s;
    myfunc(&s);
    // ...
}

And I'm getting this error:

cannot convert parameter 1 from 'std::string *' to 'std::string *&'

I'm probably making some stupid mistake somewhere, but I can't see what I'm doing wrong for the life of me.

From stackoverflow
  • Try:

    void myfunc(string& val)
    {
        // Do stuff to the string pointer
    }
    
    // sometime later 
    {
        // ...
        string s;
        myfunc(s);
        // ...
    }
    

    or

    void myfunc(string* val)
    {
        // Do stuff to the string pointer
    }
    
    // sometime later 
    {
        // ...
        string s;
        myfunc(&s);
        // ...
    }
    
    Alex : For what I'm doing though, I need the address of the pointer as well as the pointer itself. I don't want to pass the pointer by value.
    Sake : Still not understand what you try to accomplish. You cannot have "address of the pointer" of "string s", simply because "string s" is not a pointer.
    David Rodríguez - dribeas : @Alex: I take it that you need detect whether the string is exactly the same as another one you are storing and not just whether their contents are the same. If that is the case, note that you can use the address-of operator to a reference and it will get you the address of the referenced object: void f( std::string const & s ) { std::string const *p = &s; }
  • Change it to:

      std::string s;
      std::string* pS = &s;
      myfunc(pS);
    

    EDIT:

    This is called ref-to-pointer and you cannot pass temporary address as a reference to function. ( unless it is const reference).

    Though, I have shown std::string* pS = &s; (pointer to a local variable), its typical usage would be : when you want the callee to change the pointer itself, not the object to which it points. For example, a function that allocates memory and assigns the address of the memory block it allocated to its argument must take a reference to a pointer, or a pointer to pointer:

    void myfunc(string*& val)
    {
    //val is valid even after function call
       val = new std::string("Test");
    
    }
    
  • I know that it's posible to pass references of pointers, I did it last week, but I can't remember what the syntax was, as your code looks correct to my brain right now. However another option is to use pointers of pointers:

    Myfunc(String** s)
    
  • &s produces temporary pointer to string and you can't make reference to temporary object.

    1800 INFORMATION : This is not quite true - you can make a reference to const temporary
  • You function expects a reference to an actual pointer 'object':

    string s;
    string* _s = &s;
    myfunc(_s);
    

    should compile just fine. However, this is only useful if you intend to modify the pointer you pass to the function, if you intend to modify the string itself you should use a reference to the string as Sake suggested. With that in mind it should be more obvious why the compiler complains about you original code, in your code the pointer is created 'on the fly', modifying that pointer would have no consequence and that is not what is intended, the idea of a reference (vs. a pointer) is that it always points to an actual object.

    Ofek Shilon : You probably meant myfunc(_s)...
    Chris : Indeed, thanks for correcting that!
  • The problem is that you're trying to bind a temporary to the reference, which C++ doesn't allow unless the reference is const.

    So you can do one of either the following:

    void myfunc(string*& val)
    {
        // Do stuff to the string pointer
    }
    
    
    void myfunc2(string* const& val)
    {
        // Do stuff to the string pointer
    }
    
    int main()
    // sometime later 
    {
        // ...
        string s;
        string* ps = &s;
    
        myfunc( ps);   // OK because ps is not a temporary
        myfunc2( &s);  // OK because the parameter is a const&
        // ...
    
        return 0;
    }
    
  • EDIT: I experimented some, and discovered thing are a bit subtler than I thought. Here's what I now think is an accurate answer.

    &s is not an lvalue so you cannot create a reference to it unless the type of the reference is reference to const. So for example, you cannot do

    string * &r = &s;
    

    but you can do

    string * const &r = &s;
    

    If you put a similar declaration in the function header, it will work.

    void myfunc(string * const &a) { ... }
    

    There is another issue, namely, temporaries. The rule is that you can get a reference to a temporary only if it is const. So in this case one might argue that &s is a temporary, and so must be declared const in the function prototype. From a practical point of view it makes no difference in this case. (It's either an rvalue or a temporary. Either way, the same rule applies.) However, strictly speaking, I think it is not a temporary but an rvalue. I wonder if there is a way to distinguish between the two. (Perhaps it is simply defined that all temporaries are rvalues, and all non-lvalues are temporaries. I'm not an expert on the standard.)

    That being said, your problem is probably at a higher level. Why do you want a reference to the address of s? If you want a reference to a pointer to s, you need to define a pointer as in

    string *p = &s;
    myfunc(p);
    

    If you want a reference to s or a pointer to s, do the straightforward thing.

  • myfunc("string*& val") this itself doesn't make any sense. "string*& val" implies "string val",* and & cancels each other. Finally one can not pas string variable to a function("string val"). Only basic data types can be passed to a function, for other data types need to pass as pointer or reference. You can have either string& val or string* val to a function.

    Ari : Wrong on all levels. Please refresh your var declaration syntax.

Vertical aligning in CSS?

So I know all about the problems with vertical-align: middle; and the different methods people have used to vertical align elements in CSS. But I haven't found one that works for what I need it to work for.

Basically my page has just one <div> on it, which I want positioned in the center of the page, both horizontally and vertically. Obviously the horizontal part is easy, but I'm getting hung up on the vertical part. My problem is that the height of the <div> is unknown; the content changes, so I can't specify a height for it.

Anyone have any tips for me? I'm willing to use JavaScript if necessary. Thanks!

From stackoverflow
  • Long story short, you need two divs to get CSS-only vertical centering.

    With Javascript, you can do it with a single div, though.

  • As you said you don't mind using JS, here it is... (I generally never fallback to a JS solution, but if you're cool with it, then so am I)

    If your CSS is

    #element {
        position: absolute;
        top: 50%;
    }
    

    Then you could use my friend jQuery

    $(document).ready(function() {
        var height = $('#element').height();
        $('#element').css({marginTop: '-' + (height / 2) + 'px'})
    
    {);
    

    Note, this is untested, but should be a start. Hopefully the height / 2 will work as expected, if not, try using parseInt();

linq - how do you do a query for items in one query source that are not in another one?

If I have 2 query sources how do I find ones that are in one that are not in the other?

example of join to find items in both:

var results = from item1 in qs1.Items
   join item2 in qs2 on item1.field1 equals item2.field2
   select item1;

So what would the linq code be to return the items in qs1 that are not in qs2?

From stackoverflow
  • From Marco Russo

    NorthwindDataContext dc = new NorthwindDataContext();
    dc.Log = Console.Out;
    var query =
        from c in dc.Customers
        where !(from o in dc.Orders
                select o.CustomerID)
               .Contains(c.CustomerID)
        select c;
    foreach (var c in query) Console.WriteLine( c );
    
    Graphain : Would this have O(N^2) complexity because for each customer it is checking against all orders? Is Darren Kopp's suggestion of Except better?
    Graphain : Yeah when tested it is.
  • use the Except extension method.

    var items1 = new List<string> { "Apple","Orange","Banana" };
    var items2 = new List<string> { "Grapes","Apple","Kiwi" };
    
    var excluded = items1.Except(items2);
    
    Graphain : This is the better method (see my answer below).
  • Here's a more simple version of the same thing, you don't need to nest the query:

    List<string> items1 = new List<string>();
    items1.Add("cake");
    items1.Add("cookie");
    items1.Add("pizza");
    
    List<string> items2 = new List<string>();
    items2.Add("pasta");
    items2.Add("pizza");
    
    var results = from item in items1
           where items2.Contains(item)
           select item;
    
    foreach (var item in results)
        Console.WriteLine(item); //Prints 'pizza'
    
  • Another totally different way of looking at it would be to pass a lambda expression (condition for populating the second collection) as a predicate to the first collection.

    I know this is not the exact answer to the question. I think other users already gave the correct answer.

  • Darren Kopp's answer:

    var excluded = items1.Except(items2);
    

    is the best solution from a performance perspective.

    (NB: This true for at least regular LINQ, perhaps LINQ to SQL changes things as per Marco Russo's blog post. However, I'd imagine that in the "worst case" Darren Kopp's method will return at least the speed of Russo's method even in a LINQ to SQL environment).

    As a quick example try this in LINQPad:

    void Main()
    {
       Random rand = new Random();
       int n = 100000;
       var randomSeq = Enumerable.Repeat(0, n).Select(i => rand.Next());
       var randomFilter = Enumerable.Repeat(0, n).Select(i => rand.Next());
    
       /* Method 1: Bramha Ghosh's/Marco Russo's method */
       (from el1 in randomSeq where !(from el2 in randomFilter select el2).Contains(el1) select el1).Dump("Result");
    
       /* Method 2: Darren Kopp's method */
       randomSeq.Except(randomFilter).Dump("Result");
    }
    

    Try commenting one of the two methods out at a time and try out the performance for different values of n.

    My experience (on my Core 2 Duo Laptop) seems to suggest:

    n = 100. Method 1 takes about 0.05 seconds, Method 2 takes about 0.05 seconds
    n = 1,000. Method 1 takes about 0.6 seconds, Method 2 takes about 0.4 seconds
    n = 10,000. Method 1 takes about 2.5 seconds, Method 2 takes about 0.425 seconds
    n = 100,000. Method 1 takes about 20 seconds, Method 2 takes about 0.45 seconds
    n = 1,000,000. Method 1 takes about 3 minutes 25 seconds, Method 2 takes about 1.3 seconds
    

    Method 2 (Darren Kopp's answer) is clearly faster.

    The speed decrease for Method 2 for larger n is most likely due to the creation of the random data (feel free to put in a DateTime diff to confirm this) whereas Method 1 clearly has algorithmic complexity issues (and just by looking you can see it is at least O(N^2) as for each number in the first collection it is comparing against the entire second collection).

    Conclusion: Use Darren Kopp's answer of LINQ's 'Except' method

    Darren Kopp : nice write up. i also like that Enumerable.Repeat() method. haven't seen that one until today but that is a neat little method.
    Carlton Jenke : changed accepted answer due to the greatness of this answer. It just needs to include the answer instead of just referencing it. Please edit this to include this at least (with proper credit given to Darren of course): var excluded = items1.Except(items2);

How can I return records with a MySQL query using a regular expression?

I have records in my database that are searched with user input, and initially, this was sufficient for finding the correct records.

"SELECT id FROM plants WHERE Flower LIKE '%" . $sanitizedUserInput . "%'"

This was working all well and good, but then things started to happen like, searching 'red' was getting plants with the characters 'red' in sequence in their Flower field, and not simply the whole word 'red'.

I was suggested to simply put a space either side of the user's input, but I know this will fail where the word is the first word in the field, or the last. I thought to myself, I should use a regular expression! To search the word where it has a word boundary either side.

Unfortunately, I've never used regexs in a database before. How do you construct a query to search a db with a regex? Hopefully, it's as easy as this:

"SELECT id FROM plants WHERE Flower REGEX `/\b" . $sanitizedUserInput . " \b/`"
From stackoverflow
  • Yes, it's pretty much that easy.

    MySQL Reference Manual - Regular Expressions

    The word boundary sequences in MySQL regular expressions are [[:<:]] and [[:>:]] so you're going to end up with something like:

    $query = "SELECT id ".
                "FROM plants ".
                "WHERE Flower REGEXP '[[:<:]]".$sanitizedUserInput."[[:>:]]'";
    
    alex : For some reason I find the mySQL docs confusing...
    alex : Thanks for that, Chad. +1
    Chad Birch : Really? I'm a pretty big fan of the mysql docs overall. The search function isn't great, but most pages are usually written pretty well, once I find the right one.
    alex : Yeah, I'm perhaps just a bit odd... the PHP or jQuery docs on the other hand... love 'em

Understanding empty main()'s translation into assembly

Hi, Could somebody please explain what GCC is doing for this piece of code? What is it initializing? The original code is:

#include <stdio.h>
int main()
{

}

And it was translated to:

    .file "test1.c"
    .def ___main; .scl 2; .type 32; .endef
    .text
.globl _main
    .def _main; .scl 2; .type 32; .endef
_main:
    pushl %ebp
    movl %esp, %ebp
    subl $8, %esp
    andl $-16, %esp
    movl $0, %eax
    addl $15, %eax
    addl $15, %eax
    shrl $4, %eax
    sall $4, %eax
    movl %eax, -4(%ebp)
    movl -4(%ebp), %eax
    call __alloca
    call ___main
    leave
    ret

I would be grateful if a compiler/assembly guru got me started by explaining the stack, register and the section initializations. I cant make head or tail out of the code.

EDIT: I am using gcc 3.4.5. and the command line argument is gcc -S test1.c

Thank You, kunjaan.

From stackoverflow
  • Well, dont know much about GAS, and i'm a little rusty on Intel assembly, but it looks like its initializing main's stack frame.

    if you take a look, __main is some kind of macro, must be executing initializations. Then, as main's body is empty, it calls leave instruction, to return to the function that called main.

    From http://en.wikibooks.org/wiki/X86_Assembly/GAS_Syntax#.22hello.s.22_line-by-line:

    This line declares the "_main" label, marking the place that is called from the startup code.

        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
    

    These lines save the value of EBP on the stack, then move the value of ESP into EBP, then subtract 8 from ESP. The "l" on the end of each opcode indicates that we want to use the version of the opcode that works with "long" (32-bit) operands;

        andl    $-16, %esp
    

    This code "and"s ESP with 0xFFFF0000, aligning the stack with the next lowest 16-byte boundary. (neccesary when using simd instructions, not useful here)

        movl    $0, %eax
        movl    %eax, -4(%ebp)
        movl    -4(%ebp), %eax
    

    This code moves zero into EAX, then moves EAX into the memory location EBP-4, which is in the temporary space we reserved on the stack at the beginning of the procedure. Then it moves the memory location EBP-4 back into EAX; clearly, this is not optimized code.

        call    __alloca
        call    ___main
    

    These functions are part of the C library setup. Since we are calling functions in the C library, we probably need these. The exact operations they perform vary depending on the platform and the version of the GNU tools that are installed.

    Here's a useful link.

    http://unixwiz.net/techtips/win32-callconv-asm.html

  • Here's a good step-by step breakdown of a simple main() function as compiled by GCC, with lots of detailed info: GAS Syntax (Wikipedia)

    For the code you pasted, the instructions break down as follows:

    • First four instructions (pushl through andl): set up a new stack frame
    • Next five instructions (movl through sall): generating a weird value for eax, which will become the return value (I have no idea how it decided to do this)
    • Next two instructions (both movl): store the computed return value in a temporary variable on the stack
    • Next two instructions (both call): invoke the C library init functions
    • leave instruction: tears down the stack frame
    • ret instruction: returns to caller (the outer runtime function, or perhaps the kernel function that invoked your program)
  • It looks like GCC is acting like it is ok to edit main() to include CRT initialization code. I just confirmed that I get the exact same assembly listing from MinGW GCC 3.4.5 here, with your source text.

    The command line I used is:

    gcc -S emptymain.c
    

    Interestingly, if I change the name of the function to qqq() instead of main(), I get the following assembly:

            .file   "emptymain.c"
            .text
    .globl _qqq
            .def    _qqq;      .scl    2;      .type   32;     .endef
    _qqq:
            pushl   %ebp
            movl    %esp, %ebp
            popl    %ebp
            ret
    

    which makes much more sense for an empty function with no optimizations turned on.

  • It would really help to know what gcc version you are using and what libc. It looks like you have a very old gcc version or a strange platform or both. What's going on is some strangeness with calling conventions. I can tell you a few things:

    Save the frame pointer on the stack according to convention:

    pushl       %ebp
    movl        %esp, %ebp
    

    Make room for stuff at the old end of the frame, and round the stack pointer down to a multiple of 4 (why this is needed I don't know):

    subl        $8, %esp
    andl        $-16, %esp
    

    Through an insane song and dance, get ready to return 1 from main:

    movl        $0, %eax
    addl        $15, %eax
    addl        $15, %eax
    shrl        $4, %eax
    sall        $4, %eax
    movl        %eax, -4(%ebp)
    movl        -4(%ebp), %eax
    

    Recover any memory allocated with alloca (GNU-ism):

    call        __alloca
    

    Announce to libc that main is exiting (more GNU-ism):

    call        ___main
    

    Restore the frame and stack pointers:

    leave
    

    Return:

    ret
    

    Here's what happens when I compile the very same source code with gcc 4.3 on Debian Linux:

            .file   "main.c"
            .text
            .p2align 4,,15
    .globl main
            .type   main, @function
    main:
            leal    4(%esp), %ecx
            andl    $-16, %esp
            pushl   -4(%ecx)
            pushl   %ebp
            movl    %esp, %ebp
            pushl   %ecx
            popl    %ecx
            popl    %ebp
            leal    -4(%ecx), %esp
            ret
            .size   main, .-main
            .ident  "GCC: (Debian 4.3.2-1.1) 4.3.2"
            .section        .note.GNU-stack,"",@progbits
    

    And I break it down this way:

    Tell the debugger and other tools the source file:

            .file   "main.c"
    

    Code goes in the text section:

            .text
    

    Beats me:

            .p2align 4,,15
    

    main is an exported function:

    .globl main
            .type   main, @function
    

    main's entry point:

    main:
    

    Grab the return address, align the stack on a 4-byte address, and save the return address again (why I can't say):

            leal    4(%esp), %ecx
            andl    $-16, %esp
            pushl   -4(%ecx)
    

    Save frame pointer using standard convention:

            pushl   %ebp
            movl    %esp, %ebp
    

    Inscrutable madness:

            pushl   %ecx
            popl    %ecx
    

    Restore the frame pointer and the stack pointer:

            popl    %ebp
            leal    -4(%ecx), %esp
    

    Return:

            ret
    

    More info for the debugger?:

            .size   main, .-main
            .ident  "GCC: (Debian 4.3.2-1.1) 4.3.2"
            .section        .note.GNU-stack,"",@progbits
    

    By the way, main is special and magical; when I compile

    int f(void) {
      return 17;
    }
    

    I get something slightly more sane:

            .file   "f.c"
            .text
            .p2align 4,,15
    .globl f
            .type   f, @function
    f:
            pushl   %ebp
            movl    $17, %eax
            movl    %esp, %ebp
            popl    %ebp
            ret
            .size   f, .-f
            .ident  "GCC: (Debian 4.3.2-1.1) 4.3.2"
            .section        .note.GNU-stack,"",@progbits
    

    There's still a ton of decoration, and we're still saving the frame pointer, moving it, and restoring it, which is utterly pointless, but the rest of the code make sense.

    kunjaan : I am using gcc 3.4.5. which do you recommend?
    kunjaan : what are those .scl 2; .type 32; .endef after main btw?
  • I should preface all my comments by saying, I am still learning assmebly.

    I will ignore the section initialization. A explanation for the section initialization and basically everything else I cover can be found here: http://en.wikibooks.org/wiki/X86_Assembly/GAS_Syntax

    The ebp register is the stack frame base pointer, hence the BP. It stores a pointer to the beginning of the current stack.

    The esp register is the stack pointer. It holds the memory location of the top of the stack. Each time we push something on the stack esp is updated so that it always points to an address the top of the stack.

    So ebp points to the base and esp points to the top. So the stack looks like:

    esp -----> 000a3   fa
               000a4   21
               000a5   66
               000a6   23
    esb -----> 000a7   54
    

    If you push e4 on the stack this is what happens:

    esp -----> 000a2   e4
               000a3   fa
               000a4   21
               000a5   66
               000a6   23
    esb -----> 000a7   54
    

    Notice that the stack grows toward lower addresses, this fact will be important below.

    The first two steps are known as the procedure prolog or more commonly the function prolog, they prepare the stack for use by local variables. See procedure prolog quote at the bottom.

    In step 1 we save the pointer to the old stack frame on the stack by calling, pushl %ebp. Since main is the first function called, I have no idea what the previous value of %ebp points too.

    Step 2, We are entering a new stack frame because we are entering a new function (main). Therefore, we must set a new stack frame base pointer. We use the value in esp to be the beginning of our stack frame.

    Step 3. Allocates 8 bytes of space on the stack. As we mentioned above, the stack grows toward lower addresses thus, subtracting by 8, moves the top of the stack by 8 bytes.

    Step 4; Alligns the stack, I've found different opinions on this. I'm not really sure exactly what this is done. I suspect it is done to allow large instructions (SIMD) to be allocated on the stack,

    http://gcc.gnu.org/ml/gcc/2008-01/msg00282.html

    This code "and"s ESP with 0xFFFF0000, aligning the stack with the next lowest 16-byte boundary. An examination of Mingw's source code reveals that this may be for SIMD instructions appearing in the "_main" routine, which operate only on aligned addresses. Since our routine doesn't contain SIMD instructions, this line is unnecessary.

    http://en.wikibooks.org/wiki/X86_Assembly/GAS_Syntax

    Steps 5 through 11 seem to have no purpose to me. I couldn't find any explanation on google. Could someone who really knows this stuff provide a deeper understanding. I've heard rumors that this stuff is used for C's exception handling.

    Step 5, stores the return value of main 0, in eax.

    Step 6 and 7 we add 15 in hex to eax for unknown reason. eax = 01111 + 01111 = 11110

    Step 8 we shift the bits of eax 4 bits to the right. eax = 00001 because the last bits are shift off the end 00001 | 111.

    Step 9 we shift the bits of eax 4 bits to the left, eax = 10000.

    Steps 10 and 11 moves the value in the first 4 allocated bytes on the stack into eax and then moves it from eax back.

    Steps 12 and 13 setup the c library.

    We have reached the function epilogue. That is, the part of the function which returns the stack pointers, esp and ebp to the state they were in before this function was called.

    Step 14, leave sets esp to the value of ebp, moving the top of stack to the address it was before main was called. Then it sets ebp to point to the address we saved on the top of the stack during step 1.

    Leave can just be replaced with the following instructions:

    mov  %ebp, %esp
    pop  %ebp
    

    Step 15, returns and exits the function.

    1.    pushl       %ebp
    2.    movl        %esp, %ebp
    3.    subl        $8, %esp
    4.    andl        $-16, %esp
    5.    movl        $0, %eax
    6.    addl        $15, %eax
    7.    addl        $15, %eax
    8.    shrl        $4, %eax
    9.    sall        $4, %eax
    10.   movl        %eax, -4(%ebp)
    11.   movl        -4(%ebp), %eax
    12.   call        __alloca
    13.   call        ___main
    14.   leave
    15.   ret
    

    Procedure Prolog:

    The first thing a function has to do is called the procedure prolog. It first saves the current base pointer (ebp) with the instruction pushl %ebp (remember ebp is the register used for accessing function parameters and local variables). Now it copies the stack pointer (esp) to the base pointer (ebp) with the instruction movl %esp, %ebp. This allows you to access the function parameters as indexes from the base pointer. Local variables are always a subtraction from ebp, such as -4(%ebp) or (%ebp)-4 for the first local variable, the return value is always at 4(%ebp) or (%ebp)+4, each parameter or argument is at N*4+4(%ebp) such as 8(%ebp) for the first argument while the old ebp is at (%ebp).

    http://www.milw0rm.com/papers/52

    A really great stack overflow thread exists which answers much of this question. http://stackoverflow.com/questions/499842/why-are-there-extra-instructions-in-my-gcc-output

    A good reference on x86 machine code instructions can be found here: http://programminggroundup.blogspot.com/2007/01/appendix-b-common-x86-instructions.html

    This a lecture which contains some of the ideas used below: http://csc.colstate.edu/bosworth/cpsc5155/Y2006_TheFall/MySlides/CPSC5155_L23.htm

    Here is another take on answering your question: http://www.phiral.net/linuxasmone.htm

    None of these sources explain everything.

"seekg identifier not found"

I have a program called main:

#include<iostream>
#include<fstream>
using namespace std;
#include"other.h"
int main()
{
//do stuff
}

and then other.h:

char* load_data(int begin_point,int num_characters)
{
    seekg(begin_point);
    char* return_val=new char[num_characters+1];
    mapdata.getline(return_val,num_characters);
    return return_val;
}

and I get the error:

'seekg': identifier not found

why do I get this error and how do I fix it?

From stackoverflow
  • seekg is a method from the fstream (declared in istream) class.

    You haven't instantiated any.

    Take this as an example

      ifstream is;
      is.open ("test.txt", ios::binary );
    
      // get length of file:
      is.seekg (0, ios::end);
    

    source: http://www.cplusplus.com/reference/iostream/istream/seekg/

    So, you should

    char* load_data(int begin_point,int num_characters)
    {
        ifstream is;
        is("yourfile.txt") //file is now open for reading. 
    
        seekg(begin_point);
        char* return_val=new char[num_characters+1];
        mapdata.getline(return_val,num_characters);
        return return_val;
    }
    

    Take into account what ParoXon commented in your question.

    You should create a file other.cpp containing function's load_data implementation. File other.h should contain function's load_data declaration. In that file (other.h) you should include all files neccesary for functions declared there to work. And dont forget to protect yourself against multiple includes !

    File other.h

    #ifndef __OTHER_H__
    #define  __OTHER_H__
    
    #include <iostream>
    #include <fstream>
    
    char* load_data(int,int);//no implementation
    #endif
    

    File other.cpp

    #include "other.h" //assumes other.h and other.cpp in same directory
    
    char* load_data(int begin,int amount){
          //load_data implementation
    }
    

Why I could not add mousemove event after mousedown in prototype?

I would move my former js codes to more OOP style. Here's the code.

function addEvent( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
    obj.attachEvent( 'on'+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
}

function test() {

}

test.prototype = {
    init: function () {

     addEvent(document, 'mousedown', this.displaydown);

    },

    displaydown : function(e){
     document.getElementById('mydiv').innerHTML = "down";
     addEvent(document, 'mousemove', this.displaymove);
    },

    displaymove : function(e){
     document.getElementById('mydiv').innerHTML = "move";
    }
}

var test0 = new test();

test0.init()

I could not add mousemove event after mousedown with

addEvent(document, 'mousemove', this.displaymove);

But if I write inline style like

addEvent(document, 'mousemove', function(e){
   document.getElementById('mydiv').innerHTML = "move";
});

It's OK. It looks the 2 codes do the same thing. Why is there the difference? Thanks!


edit,

After struggle for 2 nights I finally solved the problem. Thanks johnvey for your enlighting.

The erro occurs on this keyword. In the event of an object, this refers to window rather than object itself. The solution is I defined global parameter me (me = this) at the beginning. Now it's OK.

From stackoverflow
  • This is a classic Javascript stumbling block, relating to how the "this" key is scoped within a closure. To illustrate:

    redPrinter = function() {
        this.X = 'red';
        return function() {
            return this.X;
        }
    }
    
    main = function() {
        this.X = 'blue';
        var myRedPrinter = new redPrinter();
        alert("Red printer returned: " + myRedPrinter());
    }
    
    main();
    

    This code will print out:

    Red printer returned: blue
    

    because the scope of 'this' in the line:

    return this.X
    

    is actually tied to the main() object at the time of invocation.

    There are generally 2 ways to address this:

    1) Avoid using the 'this' keyword within a function closure. To fix your code this way, I would simply collect all of the event bindings in one place instead of cascading them, thus removing the 'this' references from both 'displaydown()' and 'displaymove()':

    test.prototype = {
        init: function () {
            addEvent(document, 'mousedown', this.displaydown);
            addEvent(document, 'mousemove', this.displaymove);
        },
    
        displaydown : function(e){
            document.getElementById('mydiv').innerHTML = "down";
        },
    
        displaymove : function(e){
            document.getElementById('mydiv').innerHTML = "move";
        }
    }
    

    OR

    2) Use function currying to bind the scope at definition time. I've lifted the bind() method from the Prototype library to illustrate:

    // declare the global bind() method for all functions
    Function.prototype.bind = function(obj) {
        var method = this,
        temp = function() {
            return method.apply(obj, arguments);
        };
        return temp;
    } 
    
    test.prototype = {
        init: function () {
            addEvent(document, 'mousedown', this.displaydown);
        },
    
        displaydown : function(e){
            document.getElementById('mydiv').innerHTML = "down";
    
            // note that we bind the method object here
            addEvent(document, 'mousemove', this.displaymove.bind(this));
        },
    
        displaymove : function(e){
            document.getElementById('mydiv').innerHTML = "move";
        }
    }
    

    The important change here is:

    this.displaymove.bind(this)
    

    which basically says, "when you call displaymove(), re-scope the keyword 'this' to the original scope context instead of the current Event object.

    unigogo : Hi, johnvey Thanks! I think I need to do as the 2nd method. But I could not bind(). An error comes up at this step. I'm using IE.6. Is this the reason?
    johnvey : IE6 shouldn't be the problem. What is the error that you're getting? As a larger question, what is it that you're trying to do? Calling addEvent() from inside another event handler is not good because you'll end up attaching the same handler over and over.
    unigogo : johnvey, I finally found the error on your code. addEvent(document, 'mousedown', this.displaydown); should be also binded. or this in the scope of mousedown is window. I just worked on a slider for easy loading. http://www.pagecolumn.com/webparts/sliders.htm

How can I display a status message while a file is uploading?

Can any one give a simple method using JavaScript to display a status message while file is uploading and fade away when file is uploaded?

From stackoverflow
  • This depends on your server-side language. If you're using PHP, you need something like this:

    http://martinjansen.com/2007/04/28/file-upload-progress-bars-with-php/

  • I was in a similar situation a while back and ended up using the excellent BlockUI plugin for jQuery.

  • Using Jquery plugins you can apply many effects while uploading and after uploaded.

    Check out this demo links :
    http://valums.com/wp-content/uploads/ajax-upload/demo-prototype.htm
    http://www.uploadify.com/demo/

    Jquery Example :
    http://valums.com/ajax-upload/

  •   <style type="text/css">
         #loadingMessage {
            position: absolute;
            top: WHEREVER;
            left: WHEREEVER;
            z-Index: 100;
         }
      </style>
    
        <div id="loadingMessage" style="visibility:hidden;">This page be uploading!</div>
    
        <form id="yourForm"> ... </form>
    
        <script>
           document.getElementById("yourform").onsubmit = function() {
              document.getElementById("loadingMessage").visibilty = "visible";
              return true;
           };
        </script>
    
  •   <style type="text/css">
         #loadingMessage {
            position: absolute;
            top: WHEREVER;
            left: WHEREEVER;
            z-Index: 100;
         }
      </style> 
    
    
       <div id="loadingMessage" style="visibility:hidden;">This page be uploading!</div>
    
        <form id="yourForm"> ... </form>
    
        <script>
           document.getElementById("yourform").onsubmit = function() {
              document.getElementById("loadingMessage").visibilty = "visible";
              return true;
           };
        </script>
    

What's the best IDE for DigitalMars D programming?

Digital Mars D seems to be very good programming language in a lot of ways, but my only problem with it is the amount of time wasted writing arguments to the compiler in the command line at the old fashion way. Is there any good IDE for D? Thanks in advance!

From stackoverflow
  • I haven't tried D in them, so you'll need to do your own eval, but:

    both claim full support for D.

  • Dupe: http://stackoverflow.com/questions/50179/an-ide-for-d

  • Briefly there is no best IDE for D yet. "Eclipse + Descent" or gEdit are my current choices.

  • After being looking around and testing all the IDEs recommended in this question and the one in http://stackoverflow.com/questions/50179/an-ide-for-d, I was not comfortable with any of the answers. I was begining to believe that really there is not such thing as a "best IDE" for D, but recently I found one that is in my point of view a very good solution: SkyIDE

    alt text

    It is also a Good IDE for Digital Mars C++, Free Pascal and other languages...

    BigSandwich : Does it have debugger support? The lousy debug options are my main gripe with D right now.
  • Please try my D-IDE and check out if this IDE is the best one you'll find ;)