|
Hey all,
I asked this question before, but (surprisingly) did'nt get any replies.
Let me rephrase it in a bit different way..
I want to implement a SMTP server in C++, but dont know where to start from.
I looked up in internet for any tutorials or any already existing SMTP server classes,
but couldnt find any of them. The only thing that I found were classes for SMTP
clients, i.e. for sending emails, but that is not what I need as you can see.
I would really appreciate any helpful comments regarding this matter.
Thanks.
|
|
|
|
|
Hello everyone,
I have a long array (char*) and I need to check which bit is the first bit whose value is 1.
I have two ways to implement,
1. Iterate each byte, then iterate each bit in each byte one by one;
2. Iterate each byte, and check whether the value of the byte itself is non-zero, if yes, then iterate each bit in the byte to find which bit is the first bit which is set to 1, or else skip this byte and continue to iterate next byte.
I think (2) is always faster, right? But I do not know why (2) is faster since it is just my personal estimation without any concrete basis analysis.
thanks in advance,
George
|
|
|
|
|
George_George wrote: I think (2) is always faster, right?
Only because it skips bytes with a value of 0 . If your input set had no such values, there would be no difference.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Thanks DavidCrow,
I agree. If the character buffer array does not have a long heading 0-value bytes, there should not be any differences.
regards,
George
|
|
|
|
|
2 will be much faster, for data which is likely to have a lot of 0 bytes.
and, as long as you're checking for 0, you might as well test for 255 (all bit set), too.
and, that brings up a question: is there a way to test all the other bit patterns a byte can have without having to shift and mask each one ?
the answer is Yes.
int look_up_table[256];
look_up_table[0] = -1; // no bits set
look_up_table[1] = 0; // first bit set is bit 0
look_up_table[2] = 1; // first bit set is bit 1
look_up_table[3] = 1; // first bit set is bit 1
look_up_table[4] = 2; // first bit set is bit 2
look_up_table[5] = 2; // first bit set is bit 2
look_up_table[6] = 2; // first bit set is bit 2
look_up_table[7] = 2; // first bit set is bit 2
look_up_table[8] = 3; // first bit set is bit 3
...
look_up_table[255] = 7; // first bit set is bit 7
then, to see which bit is set:
int first_bit_set = look_up_table[byte_to_test];
that will outperform any shift-and-mask algorithm probably by an order f magnitude.
|
|
|
|
|
Thanks Chris Losinger,
Looks like you have smarter ways to solve my question. I am always interested to learn better ideas. I have studied your idea for a while and I do not quite understand why you have two value 1? four value 2?
Especially I do not quite understand this,
int first_bit_set = look_up_table[byte_to_test];
Could you show me some samples about how it works? Sorry for my stupid and confusion.
Chris Losinger wrote: int look_up_table[256];
look_up_table[0] = -1; // no bits set
look_up_table[1] = 0; // first bit set is bit 0
look_up_table[2] = 1; // first bit set is bit 1
look_up_table[3] = 1; // first bit set is bit 1
look_up_table[4] = 2; // first bit set is bit 2
look_up_table[5] = 2; // first bit set is bit 2
look_up_table[6] = 2; // first bit set is bit 2
look_up_table[7] = 2; // first bit set is bit 2
look_up_table[8] = 3; // first bit set is bit 3
regards,
George
|
|
|
|
|
binary 0 = 00000000
binary 1 = 00000001 : first bit set is bit 0
binary 2 = 00000010 : first bit set is bit 1
binary 3 = 00000011 : first bit set is bit 1
binary 4 = 00000100 : first bit set is bit 2
binary 5 = 00000101
binary 6 = 00000110
binary 7 = 00000111
binary 8 = 00001000 : first bit set is bit 3
binary 9 = 00001001 : first bit set is bit 3
binary 10= 00001010 : first bit set is bit 3
etc.,
up to 255 = 11111111 : first bit set is bit 7
so, look_up_table is an array of values that track the first bit set for each value a BYTE can hold.
if you want to know the first bit set in 10, for example:
int first_bit_set = look_up_table[10];
that gives you...3
|
|
|
|
|
Cool, Chris Losinger!
I am clear now.
regards,
George
|
|
|
|
|
See here.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
You always have good stuff, DavidCrow! Cool!
regards,
George
|
|
|
|
|
Hello, I've been searching on the net but I just cannot find the right thing to do to get that working.
What I want is, to basically create an image from a folder, containing the folder's contents.
Then, I'd just Mount the file in my program, and read all the images / Music files / Etc, from there, instead of from a folder.
It's hard to explain, pretty much, because I don't really know how this is called.
I'd love to hear some reply on this one
Thanks in advance.
|
|
|
|
|
I imagine you are trying a type of "backup".
Take a look into the articles, search for Compression.
I can imagine to do it like:
Supposing a list of structs, each of them with a CString for path/name, other parameters you want to save, and a BYTE* pBuffer with variable length
1) Going to Folder and get the first file complete name (or path)
2) Save path and name into a CString
3) Open the file in binary, get the length, allocate a buffer with that size, and read the contents into a buffer
4) Save it in one struct
5) Get to next file in folder
6) ....
At the end... you can use the CArchive to serialize the data into a file with the extension you want to use, compressing or not the datas.
Afterwards you would be able to read your file, extract the first parameter (the string with name/path) and list them in your app, if you want to extract one, you select the complete object, and create a new file with the binary contents you previously saved.
Hope it helps
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
|
|
|
|
|
hi all..
need help again!
i have a service running using a user which is part of the administrator group... i am doing DCOM and the service encounteres access denied error when trying to connect to the remote machine. however when i run the service using local system account everything works fine. can someone please explain to me the difference between an administrator and the local system account and how to resolve this problem. thanks for the help!
not so newbie
|
|
|
|
|
Dear All
How can i get all process which is connecting to internet and geting its download & upload rates for my PC not any remote PC
then how i put a control to such rates
for example let IE max. download rate is 20K ,preventing it to download with a rate more that
NOTE: without using SNMP
Thanks
bye
|
|
|
|
|
How to write DLL function for export sring to visual basic program ?
I try to use this code
return str.AllocSysString //where str is CString object
but can not read in VB for example I export "1234" but in VB become to "1-2-3-4-" where "-" is un-readable character.
Please give me some idea for solve this problem
|
|
|
|
|
I need to be able to tyrack a mouse's mmovement within a dialog that has several (10) EditBoxes. Since it would not be practical for the User to have to tab several EditBoxes to get to the one he/she wants to enter data into; the EditBoxes are not Tabed. I need to know where the User is (in what EditBox) so that the proper function call is active for the users input. This is actually a Wizard-style property page so there is no OK button to poll for; and the User is not required to use any of the EditBoxes on any given page. Hence the need to know when ther User has Started entering data in what EditBox.
Help with usable code examples would be appreciated.
A C++ programming language novice, but striving to learn
|
|
|
|
|
Check out OnSetFocus () , does it helps?
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
|
|
|
|
|
Before I could "SetFocus" to a particular EditBox I would have to know which one the User had selected; or intended to select. Remember, this is a property page with 10 different EditBoxes on it(there are 7 other pages to this Wizard and they each have from 8 to 12 editboxes on them. If the User starts inputing data into a EditBox I need to find out which one he/she is typing into. Hence the need for tracking the mouse movement until a editbox is selected for entering data(left click).
A C++ programming language novice, but striving to learn
|
|
|
|
|
I didn't say SetFocus (), I did say On SetFocus () or said in another form EN_SETFOCUS
This message is sent every time a edit box gets the focus by selection, before/while the carpet appears in it.
So the user klick in the control, the message is sent.
I use EN_SETFOCUS to know when the user has the input focus in an edit box by setting a bool and EN_KILLFOCUS to reset the bool because the user has clicked outside giving the focus to another control or the parent window.
So I can differenciate some things and call one function or another when a key (i.e. enter) is pressed.
In example:
afx_msg void OnSetFocusMyEdit1 ();
afx_msg void OnKillFocusMyEdit1 ();
BEGIN_MESSAGE_MAP(CMyFormView, CFormView)
ON_EN_SETFOCUS(IDC_SIG_MYEDIT1, OnSetFocusMyEdit1)
ON_EN_KILLFOCUS(IDC_SIG_MYEDIT1, OnKillFocusMyEdit1)
END_MESSAGE_MAP()
void CMyFormView::OnSetFocusMyEdit1 ()
{
m_bEdit1Focus = TRUE;
return;
}
void CMyFormView::OnKillFocusMyEdit1 ()
{
m_bEdit1Focus = FALSE;
return;
}
void CMyFormView::OnHotKeyEnterPressed ()
{
if (m_bEdit1Focus)
{ CalculateSomeStuffWithEdit1Value ();
return;
}
if (m_bEdit2Focus)
{ ValidateTheEdit2Value ();
DoAnotherStuff ();
return;
}
DestroyWindow ();
}
So if Enter is pressed while an edit has the focus, I get the data, validate it, make whatever I need and wait to next command. If the enter is pressed when nothing is focused, I close my window.
EDIT: As I saw the other messages, I have added the bolded code.
NOTE: I use VC++ 6, but I guess it will work in VS2005
-- modified at 2:37 Tuesday 6th November, 2007
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
|
|
|
|
|
Larry Mills Sr wrote: need to know when ther User has Started entering data in what EditBox.
The parent window of the edit controls will receive an EN_CHANGE notification
each time the user changes the text in an edit control.
Mouse messages will go to the edit controls (or whatever other window the cursor
is over) so you'd have to track mouse messages in the controls.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Mark, I'm sorry, but I simply don't know how to do what you said. Could you provide me with a code snipped of how to do it. Please.
Let's say he/she selects IDC_TIRES (EditBox) also, the parent window would be (The Page or the Wizard Sheet?) and how do I get that in MFC I nappologize for my ignorance in thia.
A C++ programming language novice, but striving to learn
|
|
|
|
|
Larry Mills Sr wrote: I'm sorry, but I simply don't know how to do what you said.
No need to apologize - that's why we're here
Most (if not all) controls send notification messages to their
parent window when certain events occur.
For edit controls, there's two handy notifications that can help in your situation.
One is EN_CHANGE, which is sent to the control's parent whenever the user makes a
change in the edit box. The other is EN_SETFOCUS, which is sent to the control's
parent whenever an edit control receives keyboard/input focus.
To catch these in MFC, you can do something like this:
afx_msg void OnTiresEditChange();
afx_msg void OnTiresEditSetFocus();
...
ON_EN_CHANGE(IDC_TIRES, &CMyDialog::OnTiresEditChange)
ON_EN_SETFOCUS(IDC_TIRES, &CMyDialog::OnTiresEditSetFocus)
...
void CMyDialog::OnTiresEditChange()
{
}
void CMyDialog::OnTiresEditSetFocus()
{
} Operations with control notifications like this are pretty much essential
when doing UI coding, so I'd recommend practicing and familiarizing yourself with
them.
The Win32 Control Library[^] is one of the areas of the SDK I use most. For each
common control, there's lists of messages for manipulating the controls and notification
messages sent by the controls. Very handy for this stuff
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I knew there had to be something already in place for this kind of thing; but I just didn't know what it was. Thanks. By-the-way, Do you know how to use Visual Studio 2005 C++? I can figure how to create a program (no understandable Wizard like VS 6.0 What a "CLR" and what is "General").
Question: Couldn't I, thru the Class Wizard, do a WM_COMMAND adding the function "On_Command" to each property page; and list the EditBox's Resource ID# (ie, IDC_TIRE) as a case( ie, case IDC_TIRE: OnTireUsed(); break;)
would that work if any activity occured in the EditBox?
-- modified at 15:34 Monday 5th November, 2007
A C++ programming language novice, but striving to learn ditBox ID# (ie, IDC_TIRE
|
|
|
|
|
Larry Mills Sr wrote: Do you know how to use Visual Studio 2005 C++? I can figure how to create a program (no understandable Wizard like VS 6.0
Yes. The missing app wizard is a common complaint. Its replacement is
split into two windows, both accessible from the View menu.
One is the "Class View" window, the other is the "Properties Window".
Example: If you right click a dialog class in the Class View window and choose properties,
the properties window will open and/or switch to the context of that class. There
you'll find tabs for events, messages and overrides. Select the appropriate tab, scroll
to the item you want to add a method for, use the drop down arrow to the right of it
to chose "<add> ...".
Larry Mills Sr wrote: Couldn't I, thru the Class Wizard, do a WM_COMMAND adding the function "On_Command" to each property page; and list the EditBox's Resource ID# (ie, IDC_TIRE) as a case( ie, case IDC_TIRE: OnTireUsed(); break;)
would that work if any activity occured in the EditBox?
Sure. You won't find a WM_COMMAND handler (at least not in later VS versions)
because MFC already catches the WM_COMMAND message and dispatches it via a
virtual call to CWnd::OnCommand(). All you have to do is override OnCommand() in your
dialog class, filter out command messages you want to process or are interested in, and
pass the rest on to the base class.
Larry Mills Sr wrote: What a "CLR" and what is "General").
For CLR, see common Language Runtime Wiki[^]. You only need to worry about
compiling for CLR if you intend to use the .NET framework or other .NET
assemblies from your C++ code. It's definitely an advanced topic!
I'm not sure what "General" you're referring to.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
If I understand correctly, if someone begins using, let's say IDC_TIRE EditBox and I had the Class Wizard give me a function for that page called "OnCommand" and simply had case IDC_TIRE: OnTireUsed(); break; the program would automatically go to OnTireUsed() function without any further intervention from me. Correct?
A C++ programming language novice, but striving to learn
|
|
|
|
|