mardi 28 juin 2016

Windows GDI Printer error StartDocPrinter

Actually I am working with Qt printing and need to send the RAW command (ESCP command) to printer. After did some search and I found that I need to use the Windows API to do it.

Refer to this thread : win32-c-print-string-to-printer

I have created the code like below :

const QString pName = "EPSON LX-300+ /II";
LPBYTE lpData;
BOOL bStatus = FALSE;
HANDLE hPrinter = NULL;
DOC_INFO_1 DocInfo;
DWORD dwPrtJob = 0L;
DWORD dwBytesWritten = 0L;
LPTSTR printerName = new wchar_t[pName.length() + 1];
pName.toWCharArray(printerName);
printerName[pName.length()] = '�';
QString so = "x1b@Is it works?";
QByteArray ba = so.toUtf8();
lpData = (unsigned char*)(ba.data());
DWORD dwCount = ba.length();
qDebug() << so;

bStatus = OpenPrinter(printerName, &hPrinter, NULL);
if(bStatus) {
    DocInfo.pDocName = (LPTSTR)_T("My Document");
    DocInfo.pOutputFile = NULL;
    DocInfo.pDatatype = (LPTSTR)_T("RAW");
    dwPrtJob = StartDocPrinter (
                    hPrinter,
                    1,
                    (LPBYTE)&DocInfo);
    qDebug() << GetLastError();
    if (dwPrtJob > 0) {
            qDebug() << "COMMAND";
            // Send the data to the printer.
            bStatus = WritePrinter (
            hPrinter,
            lpData,
            dwCount,
            &dwBytesWritten);
    }
    qDebug() << dwCount;
    qDebug() << dwBytesWritten;

    EndDocPrinter (hPrinter);

    // Close the printer handle.
    bStatus = ClosePrinter(hPrinter);
    qDebug() << bStatus;
}

if (!bStatus || (dwCount != dwBytesWritten)) {
    bStatus = FALSE;
} else {
    bStatus = TRUE;
}

delete printerName;

And the code failed on StartDocPrinter, it returning 0 which mean failed. And using the GetLastError(), the function return 1804. And refer to this, the error is ERROR_INVALID_DATATYPE. I am not sure what error is it. And i try to use different DocInfo.pDatatype to "RAW", "TEXT", and "XPS_PASS", the result is the same.

Is there anything I can do how to fix it?

my c++ program is working ambigously.number is automatically changing into hexadecimal..pleease healp

my c++ program is working ambiguously.number is automatically changing into hexadecimal..please help

int main() {
    int t=0;
    cin>>t;
    while(t--)
        {
        int Q;
        string A,B,C;
        cin>>Q;
        cin>>hex>>A;
        cin>>hex>>B;
        cin>>hex>>C;

        string bit_A,bit_B,bit_C;
        bit_A=con(A);
        bit_B=con(B);
        bit_C=con(C);
        int k=0;
       for(unsigned i = 0; i != bit_B.length(); ++i)
               {

           if(bit_B[i]=='0' && bit_C[i]=='1' && bit_A[i]=='0'  )
               {

               bit_B[i]='1';
               k++;
               continue;
           }
           if(bit_C[i]=='0')
               {
               if(bit_B[i]=='1')
                   {
                   bit_B[i]='0';
               k++;
               }
               if(bit_A[i]=='1')
                   {
                   bit_A[i]='0';
                   k++;
               }
               continue;
           }
       }
        if(k>Q)
            {
            cout<<"-1";
            continue;
                }

       cout<<"value 0f k is"<<k<<endl;
         for(unsigned i = 0; k<Q && i != bit_A.length(); ++i)
               {
             if(bit_A[i]=='1' && bit_B[i]=='1')
                 {
                 bit_A[i]='0';
                 k++;
             }
             if(bit_A[i]=='1' && bit_B[i]=='0' && (Q-k)>=2)
                 {
                 bit_A[i]='0';
                 bit_B[i]='1';
                 k++;
                 k++;
             }
         }


         }



        cout<<std::uppercase<<hex<<todeci(bit_A)<<endl;
        cout<<std::uppercase<<hex<<todeci(bit_B)<<endl;
    }
    return 0;
}

here k value hacve changed automatically to hexa decimal which i cant figure out y?can anyone please help? 1 12 CAF7028FD 59B5AC1CE CAF1B7B7F for this input you will see k showing as B in output

Page Indicator out of sync swift

I have the following PageViewController class :

class ProjectorPageViewController : UIPageViewController, UIPageViewControllerDataSource {

var randUsed : [String]?

var pageViewMatches : [SingleMatch]? {
    didSet {
       //irrelevant code

        }
        let initialcontroller = viewControlerAtIndex(0)

        let viewControllers = [initialcontroller!]
        setViewControllers(viewControllers, direction: .Forward, animated: true, completion: nil)
    }
}



override func viewDidLoad() {
    super.viewDidLoad()
    dataSource = self
    let pageControl = UIPageControl.appearance()
    pageControl.currentPageIndicatorTintColor = UIColor.whiteColor()
    pageControl.pageIndicatorTintColor = UIColor(red: (69/255.0), green: (209/255.0), blue: (153/255.0), alpha: 1.0)
}


func viewControlerAtIndex(index : Int) -> PageViewContentViewController? {

    if (self.pageUsers!.count == 0 || index >= self.pageUsers!.count) {
        return nil
    }

    let controller = PageViewContentViewController()
    controller.location = pageLocations![index]
    controller.image_name = pageImages![index]
    controller.user_name = pageUsers![index]
    controller.pageIndex = index
    return controller

}


func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
    let curr_index = (viewController as! PageViewContentViewController).pageIndex
    print ("attempting after with index : " + String(curr_index))

    if (curr_index! < pageViewMatches!.count - 1) {
        return viewControlerAtIndex(curr_index! + 1)        }
    else {
        return nil
    }
}

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
    let curr_index = (viewController as! PageViewContentViewController).pageIndex
    print ("attempting before with index : " + String(curr_index))
    if (curr_index! > 0 ) {
        return viewControlerAtIndex(curr_index! - 1)
    }
    else {
        return nil
    }


}



func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
    if let matches = pageViewMatches {

    return matches.count
}
    else {
        return 0
    }
}

func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
    return pageViewController.viewControllers!.indexOf((pageViewController.viewControllers?.first)!)!
}



}

The problem is that if I swipe fast enough between the pages, the page indicator becomes out of sync, and points to the wrong page. If I swipe at a moderate pace this doesn't occur. I have seen a similar post which said the solution was to implement the presentationIndexForPageViewController method, however translating this from Objective-C (unless I made a mistake in translation) did not solve the problem. Here is the post I am referring to : UIPageViewController setViewControllers, UIPageControl not showing right current number

Set a childByAutoId value in Firebase data service class to be retrieved in another class

Hopefully I can make this more clear than my title. I created a FirebaseService class. In it I will create all of the methods for saving and retrieving data. I don't want to have to make a firebase reference in any other class. I simply want to reference this class to do that. If this isn't possible, please let me know so I don't move forward with it. Below is my FirebaseService class...

import UIKit
import Firebase

class FirebaseService: NSObject {

    let ref = FIRDatabase.database().reference()
    var id = String()


    func savePost(post:[String:AnyObject]) {
        let postRef = ref.child("posts").childByAutoId()
        id = postRef.key
        postRef.setValue(post)

    }



}

I want to be able to retrieve id from the FirebaseService class and have it actually use the value of childByAutoId (the post's randomly generated id in Firebase) when I save a post so I can save it as a value in post called "id". However, right now when I save it, I get an intended random id generated for the post as a whole but I don't get that value in my "id" key.

func postPost() {
    post.id = FirebaseService().id
    post.textContent = textView.text
    post.name = "Michael Williams"
    post.profileImageName = "cool"
    post.imageContentName = "coffee"
    post.createdDate = convertCurrentTimeToString()
    let postArray:[String:AnyObject] = ["name":post.name!, "profileImageName":post.profileImageName!, "textContent":post.textContent!, "imageContentName":post.imageContentName!, "createdDate":post.createdDate!, "id":post.id!]
    dismissViewControllerAnimated(true) {
        FirebaseService().savePost(postArray)
    }
}

Firebase database scheme:

  • Posts "randomly generated id"

    • name: "John Doe"

    • id: same randomly generated id as above

    • postText: "Blah Blah"

Again, all I am getting for id is an empty string when I run the postPost() function.

Does the C++ standard guarantee that a function return value has a constant address?

Consider this program:

#include <stdio.h>
struct S {
  S() { print(); }
  void print() { printf("%pn", (void *) this); }
};
S f() { return {}; }
int main() { f().print(); }

As far as I can tell, there is exactly one S object constructed here. There is no copy elision taking place: there is no copy to be elided in the first place, and indeed, if I explicitly delete the copy and/or move constructor, compilers continue to accept the program.

However, I see two different pointer values printed. This happens because my platform's ABI returns trivially copyable types such as this one in CPU registers, so there is no way with that ABI of avoiding a copy. clang preserves this behaviour even when optimising away the function call altogether. If I give S a non-trivial copy constructor, even if it's inaccessible, then I do see the same value printed twice.

The initial call to print() happens during construction, which is before the start of the object's lifetime, but using this inside a constructor is normally valid so long as it isn't used in a way that requires the construction to have finished -- no casting to a derived class, for instance -- and as far as I know, printing or storing its value doesn't require the construction to have finished.

Does the standard allow this program to print two different pointer values?

Note: I'm aware that the standard allows this program to print two different representations of the same pointer value, and technically, I haven't ruled that out. I could create a different program that avoids comparing pointer representations, but it would be more difficult to understand, so I would like to avoid that if possible.

C++ Text adventure game - Make words upper case

Basically, i'm following this tutorial : http://cplussplussatplay.blogspot.com.cy/2012/11/text-adventure-games-c-part-1.html

I have understood everything up to now except from this:

// Make words upper case
// Right here is where the functions from cctype are used
    for(i = 0; i < words.size(); i++)
    {
        for(j = 0; j < words.at(i).size(); j++)
        {
            if(islower(words.at(i).at(j)))
            {
                words.at(i).at(j) = toupper(words.at(i).at(j));
            }
        }
    }

At this point, we have a words vector which is full of characters. I don't understand the need of two for loops, nor this words.at(i).at(j)). Is it a 2D vector or something?

Thanks in advance.

EDIT : Thank you all very much for your help! I have understood it now! This is the first time using Stack Overflow and i'm loving it so far! :)

One more thing, another question has arised!

string sub_str;
vector words;
char search = ' ';



    // Clear out any blanks
    // I work backwords through the vectors here as a cheat not to       invalidate the iterator
    for(i = words.size() - 1; i > 0; i--)
    {
        if(words.at(i) == "")
        {
            words.erase(words.begin() + i);
        }

1. What does the second comment mean?

2.How can there be blanks in the vector? The first loops according to the creator clears out any blanks. This is the code previously :

for(i = 0; i < Cmd.size(); i++)
    {
        if(Cmd.at(i) != search)
        {
            sub_str.insert(sub_str.end(), Cmd.at(i));
        }
        if(i == Cmd.size() - 1)
        {
            words.push_back(sub_str);
            sub_str.clear();
        }
        if(Cmd.at(i) == search)
        {
            words.push_back(sub_str);
            sub_str.clear();
        }
    }

Thanks again!

RGBA to ABGR: Inline arm neon asm for iOS/XCode

This code(very similar code, haven't tried exactly this code) compiles using Android NDK, but not with XCode/armv7+arm64/iOS

Errors in comments:

uint32_t *src;
uint32_t *dst;

#ifdef __ARM_NEON
__asm__ volatile(
    "vld1.32 {d0, d1}, [%[src]] n" // error: Vector register expected
    "vrev32.8 q0, q0            n" // error: Unrecognized instruction mnemonic
    "vst1.32 {d0, d1}, [%[dst]] n" // error: Vector register expected
    :
    : [src]"r"(src), [dst]"r"(dst)
    : "d0", "d1"
    );
#endif

What's wrong with this code?

EDIT1:

I rewrote the code using intrinsics:

uint8x16_t x = vreinterpretq_u8_u32(vld1q_u32(src));
uint8x16_t y = vrev32q_u8(x);
vst1q_u32(dst, vreinterpretq_u32_u8(y));

After disassembling, I get the following, which is a variation I have already tried:

vld1.32 {d16, d17}, [r0]!
vrev32.8    q8, q8
vst1.32 {d16, d17}, [r1]!

So my code looks like this now, but gives the exact same errors:

__asm__ volatile("vld1.32 {d0, d1}, [%0]! n"
                 "vrev32.8 q0, q0         n"
                 "vst1.32 {d0, d1}, [%1]! n"
                 :
                 : "r"(src), "r"(dst)
                 : "d0", "d1"
                 );

EDIT2:

Reading through the disassembly, I actually found a second version of the function. It turns out that arm64 uses a slightly different instruction set. For example, the arm64 assembly uses rev32.16b v0, v0 instead. The whole function listing(which I can't make heads or tails of) is below:

_My_Function:
cmp     w2, #0
add w9, w2, #3
csel    w8, w9, w2, lt
cmp     w9, #7
b.lo    0x3f4
asr w9, w8, #2
ldr     x8, [x0]
mov  w9, w9
lsl x9, x9, #2
ldr q0, [x8], #16
rev32.16b   v0, v0
str q0, [x1], #16
sub x9, x9, #16
cbnz    x9, 0x3e0
ret