What's the numerical value of the Windows CE constant IOCTL_DISK_READ? - pinvoke

I'm trying to do direct disk access on Win CE (5.0), and I need to p/invoke a DeviceIoControl call with IOCTL_DISK_READ.
But I can't find which header file that's defined in (I'm using VS2008 rather than a CE SDK), so I can't find the value of the IOCTL.
If anyone knows the value (or where I can get it) I'd be very grateful.

from diskio.h
#define IOCTL_DISK_BASE FILE_DEVICE_DISK
...
#define IOCTL_DISK_READ \
CTL_CODE(IOCTL_DISK_BASE, 0x702, METHOD_BUFFERED, FILE_READ_ACCESS)
EDIT
For completeness sake so you don't have to backtrack the other values
from winioctl.h
#define CTL_CODE ( DeviceType, Function, Method, Access ) ( \
((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method) \
)
...
#define METHOD_BUFFERED 0
...
#define FILE_READ_ACCESS (0x0001)
...
#define FILE_DEVICE_DISK 0x00000007
EDIT 2
And for those who are lazy (like me) it unfolds like this:
(7 << 16) | (1 << 14) | (0x702 << 2) | (0)
which is
(0x70000) | (0x4000) | (0x1C08) | (0)
Which would be (in C#)
public const int IOCTL_DISK_READ = 0x75C08;

Related

gcov - how to reset the coverage statistics in the C/C++ code

This question is related to googletest - command line option to execute "the first, the second, etc"
I want to be able to attribute given coverage results to each googletest test case.
QUESTION
In my C++ code, is it possible to reset the statistics?
Every time a test-case is done executing, I would save the accumulated gcov statistics and reset them.
From within your C++ code you could try it with __gcov_dump and __gcov_reset. They are mentioned in the GNU documentation although there is surprisingly limited info on them.
Below is a C++ program main.cpp that calls both.
1 #include <iostream>
2 #include <signal.h>
3
4 extern "C" void __gcov_dump(void);
5 extern "C" void __gcov_reset(void);
6
7
8 static void handler(int signum)
9 {
10 std::cout<<std::endl<<"Signal "<<signum<<" received!"<<std::endl;
11 __gcov_dump(); // Dump coverage upon interupt
12 std::cout<<"Coverage data dumped!"<<std::endl;
13 exit(0);
14 }
15
16
17 int main()
18 {
19 // Initialize signal handling
20 struct sigaction sa;
21
22 sa.sa_handler = handler;
23 sigemptyset(&sa.sa_mask);
24 sa.sa_flags = SA_RESTART;
25
26 if (sigaction(SIGINT, &sa, NULL) == -1)
27 std::cerr<<"ERROR: Could not register handler"<<std::endl;
28
29 // Main loop
30 while(true) {
31 int n;
32 std::cout<<"Type a number: ";
33 std::cin>>n;
34
35 if (n % 2 == 0) {
36 std::cout<<"Your number is even!"<<std::endl;
37 }
38 else {
39 std::cout<<"Your number is odd!"<<std::endl;
40 }
41 __gcov_reset(); // Reset coverage at the end of every iteration
42 }
43 }
If I compile it with g++ main.cpp --coverage -O0 and then run it as follows:
gomfy:gcov$ ./a.out
Type a number: 1
Your number is odd!
Type a number: 2
Your number is even!
Type a number: 3
Your number is odd!
Type a number: 4
Your number is even!
Type a number: ^C
Signal 2 received!
Coverage data dumped!
And then call gcovr in the build directory, I get:
gomfy:gcov$ gcovr
------------------------------------------------------------------------------
GCC Code Coverage Report
Directory: .
------------------------------------------------------------------------------
File Lines Exec Cover Missing
------------------------------------------------------------------------------
main.cpp 18 6 33% 12-13,17,22-24,26-27,35-36,39,41
------------------------------------------------------------------------------
TOTAL 18 6 33%
------------------------------------------------------------------------------
This shows that the coverage has been successfully reset at the end of the while loop.

If (someVar And 8) = 8 Then -- what does this mean?

I'm working in some legacy VB code (.aspx page) and all over the place I see conditionals of this format:
If (someVar And {integer_x}) = {integer_x} Then
And I've seen all kinds of integers, but they all seem to be powers of 2 (8, 64, 256, 16384, etc.).
What does this code do?
This is a bitwise check. This article says it all.
(But link-only answers are frowned upon, so...)
When you do bitwise operations, you look for a bit that's "on", or has the value 1. If you look for someVar And 8, that's saying "does someVar have its 4th bit on from the right at the on position".
8 translates in binary as 0001000. If someVar has a 1 in that position, the logical operator And will return 0001000, or 8. And then you check for equality. Note that someVar can still have other bits on. It could be worth 9 (0001001), and that would still mean the condition is true.
Now, unless this is for an interface with an electronic system or parsing raw data, I'd say that's a really outdated way of doing what Enum with the Flags attribute can do.
Let's fast-forward to today:
<Flags()> _
Enum PizzaToppings
Sauce = 1
Pepperoni = 2
Mushrooms = 4
Peppers = 8
Bacon = 16
Ham = 32
Cheese = 64
Pineapple = 128
End Enum
'...
Dim myToppings As PizzaToppings = PizzaToppings.Sauce Or PizzaToppings.Cheese 'Use Or to combine or +
'...
If myToppings.HasFlag(PizzaToppings.Sauce) Then
addSauce()
End If
If you don't want to bother with powers, you can also do a bit shift:
1 << 0 is 2^0 is 1
1 << 1 is 2^1 is 2
1 << 2 is 2^2 is 4
1 << 3 is 2^3 is 8
Thus:
Enum PizzaToppings
Sauce = 1 << 0
Pepperoni = 1 << 1
'...

CDT : IASTBinaryExpression calculating the value

I'm working on the development of the eclipse plugin based on CDT.
The plugin parses the C++ code and generates another C++ code based on the data from parsed code.
Suppose the original code is
enum SOMEENUM
{
ONE = 1 << 1 // Bit 2 2
,TWO = 1 << 2 // Bit 3 4
,THREE = 1 << 3 // Bit 4 8
,FOUR = 1 << 4 // Bit 5 16
}
CDT recognizes the 1 << 1, 1 << 2, etc as IASTBinaryExpression.
Question : Does anybody know how is it possible to evaluate the value of each binary expression by means of CDT ?
Otherwise the only option remain the calculation by manual parsing of all operands.

Why my rules of bison don't work

Every time I run my parser, it will appear "syntax error in line 1 near <>" (Because there is a subroutine yyerror(char *s)). I think that's because there is something wrong with my rules in bison.
The file (c17.isc) I want to parse.
*c17 iscas example (to test conversion program only)
*---------------------------------------------------
*
*
* total number of lines in the netlist .............. 17
* simplistically reduced equivalent fault set size = 22
* lines from primary input gates ....... 5
* lines from primary output gates ....... 2
* lines from interior gate outputs ...... 4
* lines from ** 3 ** fanout stems ... 6
*
* avg_fanin = 2.00, max_fanin = 2
* avg_fanout = 2.00, max_fanout = 2
*
*
*
*
*
1 1gat inpt 1 0 >sa1
2 2gat inpt 1 0 >sa1
3 3gat inpt 2 0 >sa0 >sa1
8 8fan from 3gat >sa1
9 9fan from 3gat >sa1
6 6gat inpt 1 0 >sa1
7 7gat inpt 1 0 >sa1
10 10gat nand 1 2 >sa1
1 8
11 11gat nand 2 2 >sa0 >sa1
9 6
14 14fan from 11gat >sa1
15 15fan from 11gat >sa1
16 16gat nand 2 2 >sa0 >sa1
2 14
20 20fan from 16gat >sa1
21 21fan from 16gat >sa1
19 19gat nand 1 2 >sa1
15 7
22 22gat nand 0 2 >sa0 >sa1
10 20
23 23gat nand 0 2 >sa0 >sa1
21 19
My flex file is as follows and it is right. You can find some information about how my scanner work here.
Error in the output of my flex file
declare.h
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# define INPT 1
# define NOR 2
# define NAND 3
# define NOT 4
# define XOR 5
# define AND 6
# define BUFF 7
# define FROM 8
flex file is
%{
# include "declare.h"
# include "parse.tab.h"
/*gi=1,it's input;gi=8,it's fanout;otherwise,it's gate*/
static int gi=-1;
static int inum=0;
struct{
char *symbol;
int val;
} symtab[]={
{"inpt", INPT},
{"nor", NOR},
{"nand", NAND},
{"not", NOT},
{"xor", XOR},
{"and", AND},
{"buff", BUFF},
{"from",FROM},
{"0",0}
};
extern FILE *yyin;
extern int yylval;
%}
%start A B C D E
DIGITS [0-9]+
BLANK [ \t\n\r\f\v\b]+
ALPHA [a-z]+
%%
"*".*\n {BEGIN A; return(COMMENT);}
<A>{DIGITS} {yylval=atoi(yytext); BEGIN B; return(NUM);}
<B>{DIGITS}{ALPHA} {yylval=atoi(yytext); BEGIN C; return(GNAME);}
<C>{DIGITS} {yylval=atoi(yytext); BEGIN D; return(OPNUM);}
<C>{DIGITS}{ALPHA} {yylval=atoi(yytext); BEGIN A; return(FR);}
<D>{DIGITS} {inum=atoi(yytext);
yylval=inum;
if(gi==1)
{BEGIN A;}
if(gi!=1)
{BEGIN E;}
return(IPNUM);
}
<E>{DIGITS} {inum--;
yylval=atoi(yytext);
if(inum<0)
{BEGIN B; return(NUM);}
else
{BEGIN E; return(ILIST);}
}
{ALPHA} {yylval=lookup(yytext);
return(GTYPE);
}
">sa"[0-1] {yylval=atoi(&yytext[yyleng-1]);return(FAULT);}
{BLANK} ;
. ;
%%
int lookup(const char *s)
{
int i;
for (i = 0; symtab[i].val != 0; i++)
{
if (strcmp(symtab[i].symbol, s) == 0)
break;
}
return(symtab[i].val);
}
The right rules in bison file are as follows
parto:
| parto COMMENT
| parto parti
;
parti: NUM
{...}
GNAME
{...}
GTYPE
{...}
| parti partii
| parti partiii
;
partii:OPNUM
{...}
IPNUM
{...}
partiv
partv
;
partiii: FR
{...}
partiv
;
partiv:
| partiv FAULT
{...}
;
partv:
| partv ILIST
{...}
;
Transferring the key comments into an answer.
The first edition of the code had a couple of problems. In the scanner code, there were lines like this:
<A>{DIGITS} { yylval=atoi(yytext); return(NUM); BEGIN B; }
You should be getting warnings about unreachable code from the BEGIN operations appearing after return. The BEGIN operations have to be executed. They aren't being executed, so you're not switching into your start states.
Michael commented:
There is no warning. I've modified it as you say and edit my codes in the question. Now I put return after BEGIN. Still, "syntax error in line 1 near <�>".
This probably means you aren't compiling the C code with enough warnings. Assuming you're using GCC, add -Wall to the compilation options for starters. There's a chance the warning requires optimization too.
Have you printed the tokens as they're returned (in the Flex scanner)? Have you compiled the Bison grammar with -DYYDEBUG? You also need to turn the debug on: yydebug = 1; in the main() program. You're probably not getting the tokens you expect when you expect them. I've not tried compiling this code yet. Tracking the tokens is key (in my experience) to getting grammars to work. Otherwise, you're running blind.
The other problem (closely related) is that you need to generate the symbolic names for FAULT etc from the grammar (bison -d grammar.y generates grammar.tab.h). You'll find that COMMENT is assigned the value 258, for example. Your scanner, though, is returning other numbers altogether because they're in declare.h. You'll have to fix this mismatch. One option is to #include "grammar.tab.h" in your scanner; this is more or less normal.
In retrospect, I think this is probably the most important observation; things seemed to revert to normal C debugging after this was resolved.
(People often include 'grammar.h' and only update 'grammar.h' if the content of 'grammar.tab.h' changes, so you don't recompile the scanner all the time).
The significance of this is that the set of tokens used by a grammar tends to be fairly stable while the actions associated with the rules change all the time as the implementation of the grammar evolves. So, if it takes enough time to be worth worrying about, you can create file grammar.h that is a copy of grammar.tab.h, but only update grammar.h when the content of grammar.tab.h changes.
cmp -s grammar.tab.h grammar.h || cp grammar.tab.h grammar.h
You'd include this in the makefile rule that converts that grammar into a C file (or an object file).
If the scanner is small enough and your machine fast enough, it may be simpler not to bother with this refinement; it mattered more in the days of 50 MHz machines with a few MiB of RAM than it does in these days of multiple cores running at 2+ GHz with a few GiB of RAM.

combine/merge two bytes into one...?

I working with serial frames. I'm receiving a 16-bit value as two separate 8-bit values. How can I merge buffer[0] with buffer[1]? I don't want 0b01+0b10 = 12 (base 10). I want it to equal 258.
How can I accomplish this?
uint16_t value = (highByte << 8) | lowByte ;