Thursday, March 31, 2011

How to design error reporting in PHP

How should I write error reporting modules in PHP? Say, I want to write a function in PHP: 'bool isDuplicateEmail($email)'. In that function, I want to check if the $email is already present in the database. It will return 'true', if exists. Else 'false'.

Now, the query execution can also fail, In that time I want to report 'Internal Error' to the user.

The function should not die with typical mysql error: die(mysql_error(). My web app has two interfaces: browser and email(You can perform certain actions by sending an email). In both cases it should report error in good aesthetic. Do I really have to use exception handling for this?

Can anyone point me to some good PHP project where I can learn how to design robust PHP web-app?

From stackoverflow
  • Using MVC, i always use some sort of default error/exception handler, where actions with exceptions (and no own error-/exceptionhandling) will be caught.
    There you could decide to answer via email or browser-response, and it will always have the same look :)

    Richard Levasseur : +1, each interface to the business logic should be handling the display and response of the errors. Have it throw "DuplicateException", the Email/Web ui catches it, and changes it into HTML or a Mime message, as appropriate.
  • I'd use a framework like Zend Framework that has a thorough exception handling mechanism built all through it.

  • Look into exception handling and error handling in the php manual. Also read the comments at the bottom, very useful.

    There's aslo a method explained in those page how to convert PHP errors into exceptions, so you only deal with exceptions (for the most part).

    Karim : There are those who believe that exceptions are glorified goto statements. While I wouldn't go that far, I would say that it makes it can make it difficult to follow code. I suppose it's pretty subjective, though.
    Mario : Everything is a goto statement then.
    Karim : Um. I guess you're saying that every expression leads to the next expression? I think that kind of misses my point. Consider that a thrown exception can send you not only to another part of the same function but to another area of the application entirely if not handled correctly.
    Mario : Exceptions aren't idiot proof. Bad architecture is something else.
  • In my PHP projects, I have tried several different tacts. I've come to the following solution which seems to work well for me:

    First, any major PHP application I write has some sort of central singleton that manages application-level data and behaviors. The "Application" object. I mention that here because I use this object to collect generated feedback from every other module. The rendering module can query the application object for the feedback it deems should be displayed to the user.

    On a lower-level, every class is derived from some base class that contains error management methods. For example an "AddError(code,string,global)" and "GetErrors()" and "ClearErrors". The "AddError" method does two things: stores a local copy of that error in an instance-specific array for that object and (optionally) notifies the application object of this error ("global" is a boolean) which then stores that error for future use in rendering.

    So now here's how it works in practice:

    Note that 'Object' defines the following methods: AddError ClearErrors GetErrorCodes GetErrorsAsStrings GetErrorCount and maybe HasError for convenience

    // $GLOBALS['app'] = new Application();
    
    class MyObject extends Object
    {
         /**
          * @return bool Returns false if failed
          */
         public function DoThing()
         {
              $this->ClearErrors();
              if ([something succeeded])
              {
                  return true;
              }
              else 
              {
                  $this->AddError(ERR_OP_FAILED,"Thing could not be done");
                  return false;
              }              
         }
    }
    
    $ob = new MyObject();
    if ($ob->DoThing()) 
    {
       echo 'Success.';
    }
    else 
    {
       // Right now, i may not really care *why* it didn't work (the user
       // may want to know about the problem, though (see below).
       $ob->TrySomethingElse();
    }
    
    // ...LATER ON IN THE RENDERING MODULE
    echo implode('<br/>',$GLOBALS['app']->GetErrorsAsStrings());
    

    The reason I like this is because:

    1. I hate exceptions because I personally believe they make code more convoluted that it needs to be
    2. Sometimes you just need to know that a function succeeded or failed and not exactly what went wrong
    3. A lot of times you don't need a specific error code but you need a specific error string and you don't want to create an error code for every single possible error condition. Sometimes you really just want to use an "opfailed" code but go into some detail for the user's sake in the string itself. This allows for that flexibility
    4. Having two error collection locations (the local level for use by the calling algorithm and global level for use by rendering modules for telling the user about them) has really worked for me to give each functional area exactly what it needs to get things done.
    Mario : Fail fast, crash hard. Exceptions are your friends. They get rid of these massive error checkups that riddles your main app. Use a global catcher if you must to hide errors from users and display a nice page.
    Karim : I'm not sure which massive error "checkups" you're referring to, but the truth is you can make really bad code and really good code using both methods
    Mario : Having to worry about FALSE or NULL or -1 returned on every functions you call, and handling it appropriately. Then there's all those places in your code where you do forget to check for them, leading to either silent errors or an app crashing 20 lines later, hard to debug.
    Karim : Actually, it's just false or true and the flexibility in handling it or not is exactly the benefit. And if i'm understanding right, exception are much more complicated in that you can throw any number of types of exceptions and may or may not even handle it in the current function. very messy IMHO

WinAPI - how to draw dotted line?

I create HPEN using WinAPI GDI method:

HPEN hPen = CreatePen(PS_DOT, 1, color);

Then draw line using the methods MoveToEx and LineTo.

In fact drawn line is dashed. 3 pixels empty, 3 pixels with color -- dashed line.

Why PS_DOT style doesn't draw dotted line? How to draw dotten line using WinAPI?

From stackoverflow
  • I haven't tried this, but it might be worth checking the results from

    HPEN hPen = CreatePen(PS_DOT, 0, color);

    A pen width of zero causes GDI to always make the pen one pixel wide, regardless of the scaling associated with the device context. This may be enough to get the dots you want.

    : :( The same result - dashed line (3x3).
    DavidK : Drat, I hoped that that would be a quick fix. I suspect you'll need to go with SAMills answer, in that case.
  • I too had this problem in the past. I resorted to using LineDDA and a callback proc.

    struct LineData{
        CDC* pDC;
        COLORREF crForegroundColor;
        COLORREF crBackgroundColor;
    };
    .
    .
    .
    LineData* pData = new LineData;
    pData->crForegroundColor = crForegroundColor;
    pData->crBackgroundColor = crBackgroundColor;
    pData->pDC = pdc;
    
    LineDDA(nStartx, nStarty, nEndPointX, nEndPointY, LineDDAProc, (LPARAM) pData);
    delete pData;
    .
    .
    .
    
    void 
    LineDDAProc(int x, int y, LPARAM lpData)
    {
       static short nTemp = 0;
    
       LineData* pData = (LineData*) lpData;
    
       if (nTemp == 1)
        pData->pDC->SetPixel(x, y, pData->crForegroundColor);
       else
        pData->pDC->SetPixel(x, y, pData->crBackgroundColor);
       nTemp = (nTemp + 1) % 2;
    }
    

    Might not be the most efficient drawing algorithm, but you're now in complete control of dot spacing as well. I went with this approach because there were other non-native pen styles I was using for line rendering which used a bit pattern. I then walked the bit and used setpixel for the 'on' bits. It worked well and increased the useful linestyles.

  • Here is wonderful solution by MaxHacher that I've found on CodeProject
    (http://www.codeproject.com/KB/GDI/DOTTED_PEN.aspx)

    LOGBRUSH LogBrush;
    LogBrush.lbColor = color;
    LogBrush.lbStyle = PS_SOLID;
    HPEN hPen = ExtCreatePen( PS_COSMETIC | PS_ALTERNATE, 1, &LogBrush, 0, NULL );

    It works well!

Efficiently retrieving and filtering files.

This earlier SO question talks about how to retrieve all files in a directory tree that match one of multiple extensions.

eg. Retrieve all files within C:\ and all subdirectories, matching *.log, *.txt, *.dat.

The accepted answer was this:

var files = Directory.GetFiles("C:\\path", "*.*", SearchOption.AllDirectories)
            .Where(s => s.EndsWith(".mp3") || s.EndsWith(".jpg"));

This strikes me as being quite inefficient. If you were searching on a directory tree that contains thousands of files (it uses SearchOption.AllDirectories), every single file in the specified directory tree is loaded into memory, and only then are mismatches removed. (Reminds me of the "paging" offered by ASP.NET datagrids.)

Unfortunately the standard System.IO.DirectoryInfo.GetFiles method only accepts one filter at a time.

It could be just my lack of Linq knowledge, is it actually inefficient in the way I mention?

Secondly, is there a more efficient way to do it both with and without Linq (without resorting to multiple calls to GetFiles)?

From stackoverflow
  • You are right about the memory consumption. However, I think that's a fairly premature optimization. Loading an array of a few thousand strings is no problem at all, neither for performance nor for memory consumption. Reading a directoy containing that many files, however, is – no matter how you store/filter the file names: it will always be relatively slow.

    Ash : For enormous numbers of files, the ideal would be GetFiles() accepts multiple filters and then walks the entire directory tree, file by file (it has to anyway), calling back to a supplied method for each match. Good points anyway.
  • What about creating your own directory traversal function and using the C# yield operator?

    EDIT: I've made a simple test, I don't know if it's exactly what you need.

    class Program
    {
        static string PATH = "F:\\users\\llopez\\media\\photos";
    
        static Func<string, bool> WHERE = s => s.EndsWith(".CR2") || s.EndsWith(".html");
    
        static void Main(string[] args)
        {
            using (new Profiler())
            {
                var accepted = Directory.GetFiles(PATH, "*.*", SearchOption.AllDirectories)
                    .Where(WHERE);
    
                foreach (string f in accepted) { }
            }
    
            using (new Profiler())
            {
                var files = traverse(PATH, WHERE);
    
                foreach (string f in files) { }
            }
    
            Console.ReadLine();
        }
    
        static IEnumerable<string> traverse(string path, Func<string, bool> filter)
        {
            foreach (string f in Directory.GetFiles(path).Where(filter))
            {
                yield return f;
            }
    
            foreach (string d in Directory.GetDirectories(path))
            {
                foreach (string f in traverse(d, filter))
                {
                    yield return f;
                }
            }
        }
    }
    
    class Profiler : IDisposable
    {
        private Stopwatch stopwatch;
    
        public Profiler()
        {
            this.stopwatch = new Stopwatch();
            this.stopwatch.Start();
        }
    
        public void Dispose()
        {
            stopwatch.Stop();
            Console.WriteLine("Runing time: {0}ms", this.stopwatch.ElapsedMilliseconds);
            Console.WriteLine("GC.GetTotalMemory(false): {0}", GC.GetTotalMemory(false));
        }
    }
    

    I know that you cannot rely to much on GC.GetTotalMemory for memory profiling, but in all my test runs display a little less memory consumption around(100K).

    Runing time: 605ms
    GC.GetTotalMemory(false): 3444684
    Runing time: 577ms
    GC.GetTotalMemory(false): 3293368
    Ash : I'll look into it.
    Leandro López : I think it might help you to avoid loading all the file names and only retrieve those values when needed.
  • The GetFiles method only reads the file names, not the file contents, so while reading all the names may be wasteful I don't think this is anything to worry about.

    The only alternative as far as I know would be to do multiple GetFiles calls and add the results to a collection, but that gets clumsy and will require you to scan the folder several times, so I suspect it will be slower too.

  • I shared your problem and I found the solution in Matthew Podwysocki's excellent post at codebetter.com.

    He implemented a solution using native methods that allows you to provide a predicate into his GetFiles implementation. Additionally he implemented his solution using yield statements effectively reducing the memory utilization per file to an absolute minimum.

    With his code you can write something like the following:

    var allowedExtensions = new HashSet<string> { ".jpg", ".mp3" };
    
    var files = GetFiles(
        "C:\\path", 
        SearchOption.AllDirectories, 
        fn => allowedExtensions.Contains(Path.GetExtension(fn))
    );
    

    And the files variable will point to an enumerator that returns the files matched (delayed execution style).

Pad left or right with string.format (not padleft or padright) with arbitrary string

Can I use String.Format() to pad a certain string with arbitrary characters?

Console.WriteLine("->{0,18}<-", "hello");
Console.WriteLine("->{0,-18}<-", "hello");

returns 

->             hello<-
->hello             <-

I now want the spaces to be an arbitrary character. The reason I cannot do it with padLeft or padRight is because I want to be able to construct the format string at a different place/time then the formatting is actually executed.

--EDIT--
Seen that there doesn't seem to be an existing solution to my problem I came up with this (after Think Before Coding's suggestion)
--EDIT2--
I needed some more complex scenarios so I went for Think Before Coding's second suggestion

[TestMethod]
public void PaddedStringShouldPadLeft() {
    string result = string.Format(new PaddedStringFormatInfo(), "->{0:20:x} {1}<-", "Hello", "World");
    string expected = "->xxxxxxxxxxxxxxxHello World<-";
    Assert.AreEqual(result, expected);
}
[TestMethod]
public void PaddedStringShouldPadRight()
{
    string result = string.Format(new PaddedStringFormatInfo(), "->{0} {1:-20:x}<-", "Hello", "World");
    string expected = "->Hello Worldxxxxxxxxxxxxxxx<-";
    Assert.AreEqual(result, expected);
}
[TestMethod]
public void ShouldPadLeftThenRight()
{
    string result = string.Format(new PaddedStringFormatInfo(), "->{0:10:L} {1:-10:R}<-", "Hello", "World");
    string expected = "->LLLLLHello WorldRRRRR<-";
    Assert.AreEqual(result, expected);
}
[TestMethod]
public void ShouldFormatRegular()
{
    string result = string.Format(new PaddedStringFormatInfo(), "->{0} {1:-10}<-", "Hello", "World");
    string expected = string.Format("->{0} {1,-10}<-", "Hello", "World");
    Assert.AreEqual(expected, result);
}

Because the code was a bit too much to put in a post, I moved it to github as a gist:
http://gist.github.com/533905#file_padded_string_format_info

There people can easily branch it and whatever :)

From stackoverflow
  • Edit: I misunderstood your question, I thought you were asking how to pad with spaces.

    What you are asking is not possible using the string.Format alignment component; string.Format always pads with whitespace. See the Alignment Component section of MSDN: Composite Formatting.

    According to Reflector, this is the code that runs inside StringBuilder.AppendFormat(IFormatProvider, string, object[]) which is called by string.Format:

    int repeatCount = num6 - str2.Length;
    if (!flag && (repeatCount > 0))
    {
        this.Append(' ', repeatCount);
    }
    this.Append(str2);
    if (flag && (repeatCount > 0))
    {
        this.Append(' ', repeatCount);
    }
    

    As you can see, blanks are hard coded to be filled with whitespace.

    configurator : I misunderstood the question...
    borisCallens : It is possible with numbers .. I would think it realy stupid if it is possible with numbers and not with strings.
    configurator : How is it possible with numbers?
    borisCallens : I was kind of lying there. But you can do the following: Console.WriteLine("->{0:00000}<-", 12); That would give you ->00012<-
    borisCallens : I see your edit there ;) Thanks for going the extra mile to check the internal code for me. Please have look at my solution in OP. Looks rather solid to me.
  • You could encapsulate the string in a struct that implements IFormattable

    public struct PaddedString : IFormattable
    {
       private string value;
       public PaddedString(string value) { this.value = value; }
    
       public string ToString(string format, IFormatProvider formatProvider)
       { 
          //... use the format to pad value
       }
    
       public static explicit operator PaddedString(string value)
       {
         return new PaddedString(value);
       }
    }
    

    Then use this like that :

     string.Format("->{0:x20}<-", (PaddedString)"Hello");
    

    result:

    "->xxxxxxxxxxxxxxxHello<-"
    
  • There is another solution.

    Implement IFormatProvider to return a ICustomFormatter that will be passed to string.Format :

    public class StringPadder : ICustomFormatProvider
    {
      public string Format(string format, object arg,
           IFormatProvider formatProvider)
      {
         // do padding for string arguments
         // use default for others
      }
    }
    
    public class StringPadderFormatProvider : IFormatProvider
    {
      public ICustomFormatProvider GetFormat(Type type)
      { 
         if (formatType == typeof(ICustomFormatProvider))
            return new StringPadder();
    
         return null;
      }
      public static readonly IFormatProvider Default =
         new StringPadderFormatProvider();
    }
    

    Then you can use it like this :

    string.Format(StringPadderFormatProvider.Default, "->{0:x20}<-", "Hello");
    
    borisCallens : This one allows for multiple arguments to be formatted. Updated OP.
    demokritos : parameter for GetFormat should be (Type formatType) rather than (Type type) I reckon.

remove a chunk of a string

Anyone know a better way to do this?

Dim Result1,Result2,FinalResult As String

Result1 = Left(sXMLResponse, sXMLResponse.IndexOf("<start>"))
Result2 = Mid(sXMLResponse, sXMLResponse.IndexOf("<end>"))

FinalResult =Result1 & Result2

Surely there is a built in String.Remove(StringOne, StringTwo) Method or something more graceful?

From stackoverflow
  • You seem to be doing things in the old VB6 way. It is generally recommended to use the .NET functions in VB.NET, i.e. string.Substring instead of the Left/Right/Mid/etc. functions. Now it looks like you're simply trying to remove the text between and in a string, which can be done quite easily in one line.

    Dim result = sXMLResponse.Substring(0, sXMLResponse.IndexOf("<start>")) & sXMLResponse.Substring(sXMLResponse.IndexOf("<end>") + 5)
    

    Really it's not different to what you did, except it removes the need for temporary variables. Also note that there is a string.Remove method but would require a temporary variable for calculating the count parameter.

    Of course you could equivalently use RegEx to remove the chunk of the string in one line, but it would be rather unnecessary for such a simple task, and certainly a good deal slower.

    Paul : Ok great, just making sure there's nothing in the framework already for this. Thought I was missing an overload somewhere. Might use a combination of the solutions. Thanks
    Noldorin : No prob. You could write your own extension method if you're using VB.NET 9.0 I suppose, but only bother if you're doing this regularly in code.
  • For a start, I think Remove() would remove one string from another which is not really what you want.

    You want to be able to extract a string that starts with one string and ends with another. In which case, why not write your own? Something like:

    function extractStr (str as string, startStr as string, endStr as string) as string
        dim sPos as integer
        dim ePos as integer
    
        sPos = instr (str,startStr);
        ePos = instr (str,endStr);
    
        extractStr = str
        if ePos > 0 then
            extractStr = left (extractStr, ePos + len(endStr))
        end if
        if sPos > 0 then
            extractStr = mid (extractStr, sPos)
        end if
    end
    
    Noldorin : Correct me if I'm wrong, but I believe the OP is using VB.NET (albeit in a VB6 style). Although your code may compile and work in VB.NET (it's been a long time since I wrote any VB6), it is not the recommended way to perform the task.
    paxdiablo : Why do you think OP is using .NET?
    Noldorin : Oh, I meant to say that the IndexOf function only exists in .NET (as part of the System.String class). The VB6 equivalent is the InStr function, which you use in your post.
    Paul : I am. BTW. using .NET
  • You can try this solution :

    Dim stringOne as String="ae234-asd-dddddffdfdff";
    Dim stringTwo as String="-asd-";
    
    stringOne.Replace(stringTwo, String.Empty);
    

    But if there are more than one stringTwo in stringOne, they are also removed in this solution

    Kris : He's probably not removing a known text though.

Can a Crystal Report be edited in a .Net Windows interface?

Please could you provide some guidance for me?

I am currently evaluating Crystal Reports 2008 for use within a major enterprise project. I have successfully used Crystal Reports Basic within Visual Studio, but we want a bit more functionality this time.

Reports will be produced based on ADO.NET Xml datasets and will be saved to a SQL Server db as blobs of the rpt files. We will be retrieving these rpt files for viewing within a .Net Windows application coded using Visual Studio 2008 in C#.

I need to produce letters that hide and show sections/paragraphs based on formulae, but the users want to be able to edit the text.

Once a report has been created and is being displayed within the .Net CrystalReportViewer control (inside a .Net Windows application), is there any way I could permit the user to alter the displayed text and re-save the rpt file?

I know that I can use parameters, but it's not ideal for large paragraphs of text which may include some words in bold for example. The users are only likely to be changing a few words, such as the addressee of the letter. They have insisted that they need to be able to change anything on the letter.

I also know that (with Crystal XI or 2008) I can export to EditableRTF which does not put the text in frames like the standard RichTextFormat export option. The .Net RichTextBox component does not show headers or footers, which is a pain. I can show the RTFs in Word (even though they miss out lines and boxes from the report, but that's another matter) but quite frankly I'm terrified of the stories of deploying Office interop components in .Net apps.

When Crystal displays a report in preview mode you can click on pararaphs and it KNOWS that there is a 'field' there because it highlights the row(s) with a box. Is there any way we can just edit this text and save the report again?

Do you have any suggestions for me? I'm under pressure to produce an estimate for this area of work and need to know if it is possible with Crystal.

Thanks in advance

From stackoverflow
  • You've got a really good handle on the capabilities of Crystal, and you're right - the idea of editing big chunks of report text "live" is going to be tough.

    The "export to RTF" option might be workable, provided you can live with one-way generation (after you use Crystal to generate the report and start editing the output, you can't re-generate without losing your edits).

    Have you considered something like OneNote or other XSLT-based solutions? It seems like your users want a lot of control over the generated output, so your design's going to have to factor that in. Maybe even generate output and then shoot it straight into a document management system so users' changes are tracked and controlled?

    JamesW : Thanks for the suggestions. We won't need to regenerate the reports from scratch, so it looks like the RTF export is going to be our best option. A colleague did some more research since my post a reckons he's nailed the MS interop deployment. Cheers

Migrating applications from Dev to QA to Prod

I have a number of webservices and clients (click-once deployment) that I am wondering how to deploy efficiently. We have a QA department that reviews and tests releases as well as an operations group that currently does the 'deployment' which basically consists of copying tested releases from QA boxes to Prod boxes. The process is quite error prone as config files are not copied correctly and feedback on if/when/how deployment was done is not being given and therefore I consider this method of deploying a not-so-best-practice method.

We are using a build machine for doing our builds and I don't want to use publish from my dev machine.

I have been wondering if scripted deployment is anything I should look into and if there are any standard ways of doing this. I have experimented a little with TFSDeployer which is a utility that lives on boxes to be deployed to and pick up events from team foundation server that can be then be acted upon via powershell scripts. I can see something like that working on dev/test machines but for production servers I don't know.

How do you deploy your webservices/clients?

From stackoverflow
  • I use TFSDeployer for the push to production as well, as you can set TFSDeployer up to push so you don't have to install it on the production boxes (it just means that you need to configure permissions for you build account/machine to access production).

    One useful tip is to restrict permissions on who can change build qualities and only allow the push to production script to fire once the build quality is at the UAT passed level (or whatever the equivalent is for your organistion).

    Fadeproof : Do you have several instances of tfs deployer up and running or do you use the same technique for pushing to staging/uat?

Calling .Net Class Library (dll) from VB

Hi All,

I've used a tutorial (http://support.microsoft.com/kb/317535) to create a VB.NET Class that exports a COM visible method for calculating MD5 checksums.

The problem is that I need to call this .dll from a language similar to VB (Extra Basic) and it only supports COM calls. On my machine everything works like a charm, but on the destination server it doesn’t work at all. I get "Object creation failed" from the "VB like" application.

According to the tutorial the only thing one need to do is to use regasm to register the .dll. This doesn't work. I have obviously made something wrong when I registered the .dll.

What do VS2005 do to make this .dll visible for COM calls?

I have tried to uese regsvr32, but it faild to register the .dll with the following message: "xxx.dll was loaded, but the DllRegisterServer entry point was not found. This file can not be registered"

Best regards Ausgar

From stackoverflow
  • Have you tried registering the COM DLL using regsvr32? I don't know if regasm does this automatically but apparently it doesn't.

    Ausgar : HI, Yes I have tried to use regsvr32. I get the following error message: "xxx.dll was loaded, but the DllRegisterServer entry point was not found. This file can not be registered"
    Konrad Rudolph : Ouch. :-( Unfortunately, I've never worked with COM dlls created in .NET
  • It sounds like you don't have all the items that your application requires installed or working on the server. Some things to look at are:

    1. Is the .NET Framework installed?
    2. Make sure COM is working. Some fundamental things to try are:
      • Inserting an object in a WordPad document.
      • If you can run WSCript, run a simple vbscript that creates an object.
      • Run Dependency Walker on your EXE and make sure you have everything you need.
    3. If the above doesn't get you going you can use Sysinternals ProcMon to watch what files and registry entries your application is accessing (and maybe not finding). This should give you clues as to what is going on.
  • You can't use Regsvr32.exe, Regasm.exe is required. You must either run it with the /codebase command line argument or install the assembly in the GAC. I assume that's your problem.

    These kind of registration issues are always fugly, not in the least because they require an install program instead of Xcopy.exe. And invoke DLL Hell if you don't version correctly. Which you won't if you don't use the GAC. Consider using a manifest instead that contains the <clrClass> element. Take the first Google hit.

    Ausgar : Hi! "Take the first Google hit" of what?
    Hans Passant : Crud, the post editor ate my word. It is element.
    Ausgar : Thanks al lot! /Codebase solved all my problems!
    Euro Micelli : I fixed the angle brackets in the response body. Cheers!

jQuery - suggestions for a "nextWhile" traversion?

jQuery currently has .next(filter) and .nextAll(filter) but I need something that fits in the middle of these - effectively, a .nextWhile(filter) that repeatedly does next until the filter is no longer true, then stops (rather than continuing to the end).

To demonstrate this, the following is some simplified HTML - (in reality, it is dynamically generated, random order/data, more columns, proper class names, and so on).

<table>
 <thead>
  <tr>
   <th>...</th>
  </tr>
 </thead>
 <tbody>

  <tr class="x"><td>a <button>Show/Hide</button></td></tr>
   <tr class="x y"><td>a1</td></tr>
   <tr class="x y"><td>a2</td></tr>

  <tr class="z"><td>b</td></tr>

  <tr class="z"><td>c</td></tr>

  <tr class="x"><td>d <button>Show/Hide</button></td></tr>
   <tr class="x y"><td>d1</td></tr>
   <tr class="x y"><td>d2</td></tr>
   <tr class="x y"><td>d3</td></tr>

  <tr class="z"><td>e</td></tr>

  <tr class="x"><td>f</td></tr>

  <tr class="x"><td>g <button>Show/Hide</button></td></tr>
   <tr class="x y"><td>g1</td></tr>
   <tr class="x y"><td>g2</td></tr>

 </tbody>
</table>

And against this some JavaScript is run:

<script type="text/javascript">
 var $j = jQuery.noConflict();

 $j().ready(init);

 function init()
 {
  $j('tr.y').hide();
  $j('tr.x button').click( toggleRelated );
 }

 function toggleRelated()
 {
  // Only toggles one row
  // $j(this).parents('tr').next('.y').toggle();

  // Toggles unrelated ones also
  $j(this).parents('tr').nextAll('.y').toggle();

  // Not currently a jQuery construct
  // $j(this).parents('tr').nextWhile('.y').toggle();
 }

</script>

Is there an easy way to implement this nextWhile construct?

Ideally this needs to be achieved without modifying the current HTML.

From stackoverflow
  • UPDATE: Gave up on the recursion idea and decided to count and slice (see last example).

    Initial Attempt:
    This code is buggy - it only returns the last two items, so doesn't work for 3+ items.

    jQuery.fn.nextWhile = function(f)
    {
     if( this.next(f).html() )
     {
      return this.next(f).nextWhile(f).andSelf();
     }
     else
     {
      return this;
     }
    };
    


    Current Version:

    jQuery.fn.nextWhile = function(f)
    {
     var Next = this.next(f);
     var Pos = 0;
    
     while( Next.length > 0 )
     {
      Pos++;
      Next = Next.next(f);
     }
    
     return this.nextAll(f).slice(0,Pos);
    }
    

    This appears to work fine, but I'm not sure if there are any performance penalties to selecting everything and then slicing only a handful from it?

    Matthew Crumley : what about just checking this.next(f).length > 0?
    Peter Boughton : Yes, that makes much more sense; I want to kick myself.
    Peter Boughton : Wait, this is only returning the last two items - so doesn't work if 3 or more items
    grammar31 : If performance degrades try tweaking the selector to do the slicing for you: this.nextAll(f + ':lt(' + Pos + ')')

C#: Comparing two Linq2Sql entities not working?

Could anyone explain to my why this is not working?

In the database we have Employees with an IdEmployee column. We also have some tables with foreign keys to that one.

Say we have something like this:

var employee = dataContext.Employees.First(<some predicate>);
var someEntity = dataContext.SomeEntities.First(<some predicate>);

Why does this not work:

var something = someEntity.SomeThings.First(t => t.Employee == employee);

while this does:

var something = someEntity.SomeThings.First(t => t.IdEmployee == employee.IdEmployee);

I don't get it... Isn't the first version supposed to work? The Employee entity is from the same datacontext...

From stackoverflow
  • Edit : the preceding explanation is wrong since 'it works on my machine' :

    var contract = dataContext.Contracts.First();
    var room = dataContext.Rooms.First(r => r.Contract == contract);
    

    something must be broken in your association between your classes.

    You can get the executed command by using

      var command = dataContext.GetCommand(
             dataContext.Rooms.Where(r => r.Contract == contract))
         .CommandText;
    

    The resulting command text is :

    SELECT ...    FROM [Rooms] AS [t0]
    WHERE [t0].[ContractId] = @p0
    

    So we can see that the primary key is found from the entity type...


    Isn't the first version supposed to work? The Employee entity is from the same datacontext...

    Yes but the lambda expression is converted to SQL script and it seems that the linq Sql script builder doesn't try to find the entity key to generate the request.

    The linq to Sql command builder use the lambda content as an Expression<> tree to build the query text. To perform this operation it expect the lambda to contain expressions referencing entities properties.

    The problem here seems to be that it references the entities themselve and not there properties.

    The following query

    var something = someEntity.SomeThings
        .First(t => t.IdEmployee == employee.IdEmployee);
    

    produces Sql that will be something like

    SELECT ... FROM SomThings
    WHERE IdEmployee = @idEmployee
    

    The table columns are found using the properties names in the lambda.

    In the other case, there is no property name...

    Svish : but isnt that what it is supposed to do? or have I missunderstood something?
    Think Before Coding : Actually the comparison will not happen in the C# code, but in the Sql command. It seems that linq to sql doesn't generate the right sql command from this lambda.
    Svish : hm, thats a bit scary? Is there a way I can make it generate the right sql? Is there something wrong with my dbml file? Or something in the database?
    Think Before Coding : The fact is that there are a lot of things that can be written in C# that cannot be translated in Sql by linq... I'm digging further to see if I can find the reason in this particular case
    Svish : thank you :) looking forward to an explanation if you find one, cause this just doesn't make sense to me :p
    Svish : So comparing a foreign key entity with another entity is a no-go then? If so I must say that is a bit annoying... but something I will have to live with I guess. Weird thing is that the code doesn't crash. If I gave it something linq2sql couldn't use, it should crash imho...
    Svish : Tried using that GetCommand funciton now, and here actually both versions give the exact same sql... could there be something with deffered loading?
    Think Before Coding : Do you use specific options on your dataContext ?
    Svish : I do now, cause if not, even using IdEmployee doesn't work for some reason. I really don't want to though. Setting DeferredLoadingEnabled to false doesnt seem to do any good either.
    Think Before Coding : Yes, I think the probleme is in the configuration of you entities and association in the dbml file...
    Svish : Hm, but those should be correct. Using http://www.huagati.com/dbmltools/ to sync it. And as far as I can see, all is like it should be...
  • I think the first one does not work because the types are different, but in the second they are not (you're dealing with integers in the second one).

    You should override the Equals (and GetHashCode) method in order to implement your own equality comparission.


    Edit

    As you have pointed out they have to be same type, cause if not it would fail on compile-time, but I would insist that it might be convenient to implement your own Equals and GetHashCode functions. I think your problem lies there. I can recall the link now, but I've read somewhere that it's a bit tricky the equality comparission in .NET.

    Check this example:

    using System;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Employee a = new Employee(1);
                Employee b = new Employee(1);
    
                Console.WriteLine("a == b := {0}", a == b);
                Console.ReadLine();
            }
        }
    
        class Employee
        {
            private int id;
    
            public Employee(int id) { this.id = id; }
        }
    }
    

    The output is:

    a == b := False
    

    That's because .NET internally uses GetHashCode and Equals methods from Object.

    Svish : No, they have to be same type, cause if not it would fail on compile-time.
    Leandro López : You are right, check the edited notes.
    Think Before Coding : But linq to sql ensure instance equality through an identity map... so that's not the problem.
  • It is a little bit tricky to know for sure when you have used names like SomeThings and someEntity, but what I believe is happening is that when you're looking at someEntity.SomeThings, SomeThings is a collection of Employee entities. Thus, the t in your lambda expression will refer to an Employee object, giving you the ability to compare their properties.

    Try the following

    var something = someEntity.SomeThings.First(t => t.Equals(employee));
    

    and see if that works better.

    EDIT: Actually, the .Equals() syntax is better than the == I first proposed...


    EDIT2: As Sessiz Saas proposes, it is probably because of lazy loading. An alternate way of loading the employee objects is provided below:

    var myThings = dataContext.Things.Include("employee");
    

    However, remember that this could cause extra (and unnecessary, as you are able to do the job anyway) data calls.

    Svish : No, that fails on compile-time, since employee is not of type SomeThing.
    Tomas Lycken : Well, I made the attempt since SomeThing seems to have the property IdEmployee. If you're not querying for employees but for other things linked to employees, I'd recommend a reference to the ID (which, anyway, should be a unique primary key...) as you did it in your second expression.
    Svish : .Equals does not work either.
    Tomas Lycken : Well, it won't, since as you said they're not the same type - they won't be equal. What kind of relationship do you have between the Employees table and the SomeThings table? One-to-Many, or Many-to-Many? Which direction? (I'd actually just compare the Id properties - it does the job, right?)
    Svish : yeah, it does the job. I just find it annoying :p It is a One-to-Many. One employee can have many things.
    Tomas Lycken : If you necessarily wants it comparing the employee, and not the employeeID, you have to load the employee objects (as Sessiz Saat proposed) when you get the things. I have provided a code example of how to do this in an edit to my post.
  • Because of lasy loading. someEntity.Employee property isn't loaded from DB. if you want to load it also, you should use "DataLoadOptions" before retriving data from DB,syntax is like:

    DataLoadOptions options = new DataLoadOptions();
      options.LoadWith<SomeThing>(s=>SomeProperty);
      dataContext.LoadOptions = options;
    
    Tomas Lycken : You could also do this by using the .Include(string EntityName) syntax.

x86 assember - illegal opcode 0xff /7 under Windows

Hi overflowers

I'm currently developing an x86 disassembler, and I started disassembling a win32 PE file. Most of the disassembled code looks good, however there are some occurences of the illegal 0xff /7 opcode (/7 means reg=111, 0xff is the opcode group inc/dec/call/callf/jmp/jmpf/push/illegal with operand r/m 16/32). The first guess was, that /7 is the pop instruction, but it is encoded with 0x8f /0. I've checked this against the official Intel Architecture Software Developer’s Manual Volume 2: Instruction Set Reference - so I'm not just missleaded.

Example disassembly: (S0000O0040683a is a lable being jumped to by another instruction)

S0000O0040683a: inc    edi                      ; 0000:0040683a  ff c7
                test   dword ptr [eax+0xff],edi ; 0000:0040683c  85 78 ff
                0xff/7 edi                      ; 0000:0040683f  ff ff

BTW: gdb disassembles this equally (except the bug 0xff not yielding -1 in my disassembly):

(gdb) disassemble 0x0040683a 0x00406840
Dump of assembler code from 0x40683a to 0x406840:
0x0040683a:     inc    %edi
0x0040683c:     test   %edi,0xffffffff(%eax)
0x0040683f:     (bad)  
End of assembler dump.

So the question is: Is there any default handler in the illegal opcode exception handler of Windows, which implements any functionality in this illegal opcode, and if yes: What happends there?

Regards, Bodo

From stackoverflow
  • Visual Studio disassembles this to the following:

    00417000 FF C7            inc         edi  
    00417002 85 78 FF         test        dword ptr [eax-1],edi 
    00417005 ??               db          ffh  
    00417006 FF 00            inc         dword ptr [eax]
    

    Obviously, a general protection fault happens at 00417002 because eax does not point to anything meaningful, but even if I nop it out (90 90 90) it throws an illegal opcode exception at 00417005 (it does not get handled by the kernel). I'm pretty sure that this is some sort of data and not executable code.

    Nathan Fellman : So Visual Studio conveniently sidesteps the problem. I wonder why it didn't write, "dw ffffh" and decide that 00 was the first byte of the next instruction (I believe it's some type of "add")
    Evan Teran : @Nathan, if after a one byte, it is known that the instruction is illegal, why should it assume that it is a 2 byte instruction? I would say that visual studio is handling it quite sanely.
    bothie : Most other disassemblers do it the same way as Visual Studio. On one hand, after reading the first byte 0xff, it's not clear yet if it will be an invalid opcode. On the other hand: The CPU will raise an invalid opcode exception, so it doesn't matter if one assumes 1, 2 or 20 bytes ...
  • To answer your question, Windows will close the application with the exception code 0xC000001D STATUS_ILLEGAL_INSTRUCTION. The dialog will match the dialog used for any other application crashes, whether it offers a debugger or to send an error report.

    Regarding the provided code, it would appear to have either been assembled incorrectly (encoding a greater than 8-bit displacement) or is actually data (as suggested by others already).

  • It looks like 0xFFFFFFFF has been inserted instead of 0xFF for the test instruction, probably in error?

    85 = test r/m32, and 78 is the byte for parameters [eax+disp8], edi, with the disp8 to follow which should just be 0xFF (-1) but as a 32-bit signed integer this is 0xFFFFFFFF.

    So I am assuming that you have 85 78 FF FF FF FF where it should be 85 B8 FF FF FF FF for a 32-bit displacement or 85 78 FF for the 8-bit displacement? If this is the case the next byte in the code should be 0xFF...

    Of course, as suggested already, this could just be data, and don't forget that data can be stored in PE files and there is no strong guarantee of any particular structure. You can actually insert code or user defined data into some of the MZ or PE header fields if you are agressively optimising to decrease the .exe size.

    EDIT: as per the comments below I'd also recommend using an executable where you already know exactly what the expected disassembled code should be.

    bothie : I'm writing the *dis*assembler, not the assembler ;) And the point with the data is most problably not true as well, because the mentioned opcode is reachable via JMPs, CALLs and/or JCCs from the program's main entry point.
    DrJokepu : bothie: What sort of program is this? Is it possible that the program modifies the offending instruction bytes before getting there? There is no way this set of opcodes will ever run unmodified on Windows or any other x86 OS.
    jheriko : if other disassemblers fall over with this input then I wouldn't worry too much about it. I'd recommend using an executable where you already know exactly what the expected disassembled code is...
  • After many many additional hours getting my disassembler to produce the output in the exact same syntax than gdb does, I could diff over the two versions. This revealed a rather awkward bug in my disassember: I forgot to take into account, that the 0x0f 0x8x jump instruction have a TWO byte opcode (plus the rel16/32 operand). So each 0x0f 0x8x jump target was off by one leading to code which is not reachable in reality. After fixing this bug, no 0xff/7 opcodes are disassembled any longer.

    Thanks go to everyone answering to my question (and commenting that answers as well) and thus at least trying to help me.

Gridview Column width changing.

I have a gridview whose datasource is an oracle database. I want to know how to be able to change column width (in the gridview) when it is being displayed in the web browser.

Thank you

From stackoverflow
  • Hi zohair--

    You can set the column directly with the GridView tag:

    <asp:GridView ID="myGrid" runat="server">
      <Columns>
        <asp:BoundField DataField="myColumn" ControlStyle-Width="25%" />
      </Columns>
    </asp:GridView>
    

    You could also assign the column a CSS class, and then use CSS to set the column width.

How to set MQMD ApplicationID field via JMS API?

Hello, I'm using the JMS API to send messages to a Websphere MQ server. The application that pulls the messages i create want's me to set up the ApplicationID field in the MQMD structure to constant value. I could not find a way in the JMS API to access the MQMD structure

The question: Is there a way doing this? if yes then how? If no, then is my only option is to use the MQ java API, instead of JMS API?

Thanks

From stackoverflow

How can I tell if I'm running in a VMWARE virtual machine (from linux)?

I have a VMWARE ESX server. I have Redhat VMs running on that server. I need a way of programatically testing if I'm running in a VM. Ideally, I'd like to know how to do this from Perl.

From stackoverflow
  • See the answer to Detect virtualized OS from an application?.

  • VMWare has a couple of SDK's, including an SDK for Perl.

  • I think (depending on the version of esx) you can inspect at the MAC address of the NIC. VMs running in VMWare NIC will have a manufacturer string assigned to VMWare, no the physical NIC MAC. (We were trying to spoof the MAC to VM a license server and newer versions won't let you do it.) Also, this won't guarantee you aren't running on a physical box with a NIC spoofed to look like VMWare, but that would be an odd thing to do in most circumstances anyway.

    Martin Beckett : Virtualbox lets you enter any MAC you want
    sascha : @mgb fair enough, but OP was asking about VMware ESX..
    Martin Beckett : I would assume that you can change it in ESX. It's a very useful feature if you are trying to run licenced apps in a VM
  • You shouldn't 100% depend on any method, as they are undocumented features/bugs - they work on some host OSes and some virtualization solutions, but there is no guarantee that they will continue working; indeed, the whole point of virtualization is to be as undistinguishable from real metal as possible. With this in mind, the blue pill red pill (which is mentioned in the accepted answer to this similar question) seems to work ... for now.

  • Try the Red Pill.

Unit testing inheritance

I have a question concerning unit testing. Let's say that I have several classes that inherit behaviour from a parent class. I don't want to test all the child classes for this behaviour. Instead I would test the parent class. However, I should also provide a test proving that the behaviour is available in the child classes. Do you think something like Assert.IsTrue(new ChildClass() is ParentClass) makes sense?

From stackoverflow
  • I think that writing a test that inheritance works is a waste of time. The compiler will check that the base class methods are available if you try to use them, assuming you don't catch with intellisense. I would probably test the behavior in one child class and then only in each child class that modifies the behavior (or some state the behavior depends on).

  • You want to unit test to make sure inheritance works??? If you don't trust your language or compiler, why are you programming?

  • You wouldn't want to test the type of the object, unless it's coming out of an untyped factory method. Otherwise you're writing a unit test against the C# compiler which is not what you want to do.

  • The C# compiler takes care of that kind of check for you.

    If you like you can write something like:

    ParentClass childClass = new ChildClass()
    var testOutput = childClass.ParentMethod();
    Assert.IsNotNull(testOutput);
    
  • There are two approaches you can use to test the behavior of the base class:

    1. Create a stub implementation of the base class, and unit test the stub. Only test the public methods. It's pointless to test your private and protected methods, since those will be consumed by public methods that you should test in your subclasses. This approach will not enforce that your implementations of the base class hasn't shadowed behavior incorrectly.
    2. Create test superclass for your unit tests that exercises the base class methods. Whenever you're testing a subclass of your base class, have your test class inherit from your test superclass. This approach ensures that you haven't changed the behavior of the base class inadvertently, but limits the flexability of your test.

    Don't bother to verify that inheritance actually works (the whole Assert.IsTrue(new ChildClass() is ParentClass) thing). You should only test behavior. This is a structural feature of the .Net framework (inheritance), and you should trust that it works, otherwise you'll find yourself in a downward spiral of checking framework features.

  • If you're using a state-of-the-art unit-testing framework, I don't understand the statement

    I don't want to test all the child classes for this behaviour.

    Your unit tests (written against an instance of the parent class) should work unchanged if you hand them an instance of a child class, provided that your child class hasn't overridden some aspect of the parent's behavior in a way that breaks the contract on the inherited methods. And that's exactly what you need to be testing, isn't it?

    If you're concerned about the time it takes to run the tests, I'd double-check to make sure that your tests are partitioned such that you can selectively run tests based on what you're actively working on (but still run the full portfolio periodically to catch unintended dependencies.)

    Sol : Amen. This is a very good reason to actually test that the "inheritance works" (ie conforms to the Liskov substitution principle) rather than just verify that child classes are subtypes of the parent class.
    trendl : It's not that I'm concerned about the time it takes to run the test. I just want to write the minimum number of tests possible to make rafactoring in the future easier.