C++ using Boost with unsigned char*
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: C++ using Boost with unsigned char*

  1. #1
    Join Date
    Jul 2009
    Posts
    5

    Angry C++ using Boost with unsigned char*

    Hello Everyone,

    This is my first post as I generally try to never ask for help and am a firm believer of figuring out problems myself as I generally learn a crap ton in the process, but this is one problem that I just can't seem to get past. My C++ knowledge is very limited as I have just started with the language, and I am actually trying to take existing code developed by a previous developer for the client and get it working... So enough rambling onto my problem.

    My application is basically a File IO hog that it's sole purpose is to move a file inside one directory and move it into another. The way that I retrieve the resting place for that file is by reading a Data Matrix Barcode which has the folder name encoded within.

    Here's kinda the process of how the application is supposed to work.

    1) Read the directory /tmp/orders and see if any results come back (in the form of a PDF).
    2) Loop over the results and read the barcode that is on the bottom right of the PDF.
    3) Check to see if the folder name exists that has been decoded from the PDF.
    if(boost::filesystem::exists(/orders/{encoded_folder_name}))
    4) Move the file from it's current resting place to inside of that folder.

    The problem I am having is that when I decode the barcode, the result I'm getting is an unsigned char*, from there I'm trying to recast that into a string, concatenate it onto the end of a base folder path, and check to see if that folder exists.

    Here's my code illustrating that.
    std::string baseContainer = "/orders";
    std::string tempOrder = "order1";
    decItem = dParse.decode((char *) codedItem.str().c_str());

    boost::filesystem:ath fullPath = baseContainer + "/" + decItem;

    if(boost::filesystem::exists(fullPath)){
    std::cout << "Order Folder exists: " << fullPath << std::endl;
    } else {
    std::cout << "Order Folder doesn't exist: " << fullPath << std::endl;
    }

    fullPath = baseContainer + "/" + tempOrder;

    if(boost::filesystem::exists(fullPath)){
    std::cout << "Order Folder exists: " << fullPath << std::endl;
    } else {
    std::cout << "Order Folder doesn't exist: " << fullPath << std::endl;
    }

    Now for the decode method all that is doing is the following which at the end of the method I return the result variable.
    msg = dmtxDecodeMatrixRegion(dec, reg, DmtxUndefined);
    if(msg != NULL) {
    std::cout << "\t\t\t\tBarcode Found..." << std::endl;
    result = std::string(reinterpret_cast<const char*>(msg->output));
    dmtxMessageDestroy(&msg);
    }

    return result;

    Now running that application, no matter what I always get the else statement hit saying the folder doesn't exist on the first boost exists check using the decoded item. However using the statically set tempOrder variable boost says the folder does exist. However they put output the exact same path. I know this has to be something to do with the way that the decoded item is being converted from an unsigned char* to a string and being used for a path but I cannot figure out any other way to form that path and get boost to recognize it as a path. This is probably going to be something really stupid for a lot of you but for me it's killing me and I can't move on until I figure out why this just doesn't work... Any help would be much appreciated.

    I'm currently using the following if it helps.
    boost-1.38.0
    libdmtx-0.7.0 <-- used to read the barcode data
    CentOS 5.2

    If there is any information you need that I didn't provide I'd be more then glad to provide that if it helps resolve this issue. Thanks again.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: C++ using Boost with unsigned char*

    Casting the result of c_str() to a char* (essentially casting away const-ness) is a bit dangerous. If you're absolutely certain that the decode() method doesn't change its argument, you should make it accept a const char*; otherwise, you need to pass a modifiable char array, which the result of c_str() is not.

    (This will change in the next standard version, but holds for now.)

    However, that probably isn't your problem here. One thing you could try is try outputting fullPath.length() in your debug statements, to catch if any whitespace characters have gotten in there somehow.
    Last edited by Lindley; July 20th, 2009 at 03:55 PM.

  3. #3
    Join Date
    Jul 2009
    Posts
    5

    Re: C++ using Boost with unsigned char*

    Well you are correct on the length being different, the decoded item ends up with a length of 85, where as the statically set item has a length of 84. So there's something within that string that's different. I tried implementing a trim method to essentially remove any extra white space with the following method, but the lengths are still 85 & 84 after the trim.

    void trim(std::string& str) {
    std::string::size_type pos = str.find_last_not_of(' ');
    if(pos != std::string::npos) {
    str.erase(pos + 1);
    pos = str.find_first_not_of(' ');
    if(pos != std::string::npos) str.erase(0, pos);
    }
    else str.erase(str.begin(), str.end());
    }

    Any ideas of what else could be hidden in there that I might need to remove?

  4. #4
    Join Date
    Jul 2009
    Posts
    5

    Re: C++ using Boost with unsigned char*

    As a side note, I've also tried using a trim by doing the following which has produced the same results...
    std::remove(fullPath.begin(), fullPath.end(), ' ');

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: C++ using Boost with unsigned char*

    Try boost::trim in boost/algorithm/string.hpp.

    Do you have control over the decode() method?

  6. #6
    Join Date
    Jul 2009
    Posts
    5

    Re: C++ using Boost with unsigned char*

    Thank you Lindley, The boost::trim did the trick; I don't think I would have ever thought of checking the length to see if the items were the same, I was trusting my eyes that visually they were...

    To answer your question yes I do have control over the decode method, that was written by the previous developer which a lot of it actually came from the libdmtx website within there documentation. However I don't really fully understand C++ enough to change the decode call of

    dParse.decode((char *) codedItem.str().c_str());

    to how you were explaining it in your first post. I'm interested in bringing the code up to better standards if you have the time to explain it in layman's terms, if not I appreciate all your help so far, thank you once again.

    Currently the method definition looks like this for the decode method.

    std::string dmtxRead::decode(char *inFile){
    //.... etc

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: C++ using Boost with unsigned char*

    Well, the first think you should test is whether or not you can change it to
    Code:
    std::string dmtxRead::decode(const char *inFile)
    without creating any unresolvable compile errors. By which I mean, if you need to scatter the const word around a few more places in the function to make it work it's fine; if it's resisting that change in a more fundamental way, then you may have an issue.

    The reason is that a std::string makes no guarantees about its internal buffer. If the particular implementation of std::string you're working with uses copy-on-write semantics, then the buffer that c_str() gives you may not even point inside the current object---it could be pointing at another (so far identical) std::string elsewhere. If you cast that c_str() pointer to non-const and then modify it....badness could result.

  8. #8
    Join Date
    Jul 2009
    Posts
    5

    Re: C++ using Boost with unsigned char*

    Yep that worked without having to change anything other then the function definitions. Thank you again.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center