Tuesday, April 24, 2012

std::string tips


1. Links
http://notfaq.wordpress.com/2007/08/04/cc-convert-string-to-upperlower-case/

1.1 Conversion

#include <algorithm>
#include <string>

std::string data = “Abc”;
std::transform(data.begin(), data.end(), data.begin(), ::toupper);

For converting to lower case, you need to replace ::toupper with ::tolower.



Monday, April 16, 2012

C/C++ Visual Leak Detector for Visual C++ 2008/2010



Visual Leak Detector for Visual C++ 2008/2010

1. Links

http://vld.codeplex.com/
http://sites.google.com/site/dmoulding/vld


http://blog.codecentric.de/en/2012/02/find-memory-leaks-at-runtime-addendum/

Battery charger


1. Simple circuits

http://www.simple-electronics.com/2011/09/simple-universal-battery-charger-low.html

This charger can charge batteries 1.2V,1.5V,3V,4.5V,6V,9V and 12V with different charging currents ranging from 50mA up to 700mA.

charger schematic
Part List:
C- 220uF electrolytic capacitor 25V
D1- 2.7V zener 0.5W rated
D2- 1N4001 or any diode 1A rated
R2- 560 ohms 1W
Transistor- TIP41C,TIP31C or any similar NPN transistor. see  pinouts below
R1- value is dependent on the charging current. See table below.
R1 vs charging current
TIP41C/TIP31C pinout

To protect the battery from damage, dc input voltage is specified. This tabulated input voltage also protects the charger from overheating (may result to damage) and minimize the charger loss. See table below.
Input voltage vs battery voltage
If you are planning build a more "universal charger", you can use the schematic below. It is just the derived version of the charger above. You add only the selector switches where you can turn one R1 at a time to select the magnitude of current you need.

Universal charger with current selector
Note: This charger is not automatic and you need to track the time when will the battery fully charge to avoid damage.
How long will it be fully charged?
Time = battery capacity / charging current; battery capacity is usually in mAh or Ah. (Ah=1000mAh)


C/C++ static variables


1. Links

http://en.wikipedia.org/wiki/Static_variable


2. Notes

- Static global variables: variables declared as static at the top level of a source file (outside any function definitions) are only visible throughout that file ("file scope", also known as "internal linkage").

- Static local variables: variables declared as static inside a function are statically allocated while having the same scope as automatic local variables. Hence whatever values the function puts into its static local variables during one call will still be present when the function is called again.

- Static member variables: in C++, member variables declared as static inside class definitions are class variables (shared between all class instances, as opposed to instance variables).



Sunday, April 15, 2012

Delphi useful links


Useful links
http://delphi.about.com/

http://www.delphibasics.co.uk/

www.delphiforums.com

 

Friday, April 13, 2012

C/C++ Visual C++ stuff



INFO: Which Visual C++ Files to Add to Source-Code Control
http://support.microsoft.com/kb/156513


C/C++ useful shortcuts



1. Visual C++ 6 or later

http://msdn.microsoft.com/en-us/library/35yce1f2%28v=vs.80%29.aspx

Alt + F8: Code adjustment




C/C++ Debug: useful tips


1. Useful link

http://msdn.microsoft.com/en-us/library/w2fhc9a3%28v=vs.90%29.aspx
http://www.gameengineer.net/articles/Debugging/Debugging%20in%20Visual%20Studio.pdf


2. CRT Memory Leak Detection and Isolation (Microsoft tool)

To enable the debug heap functions, we need to include the following statements in our program:

#define _CRTDBG_MAP_ALLOC  // a debug info trigger
#include <stdlib.h>
#include <crtdbg.h>


- Detect leakage memory & dumping objects function

if(_CrtDumpMemoryLeaks() !=0)
...


The memory allocation number (inside the curly braces).

- Breakpoints on a Memory Allocation: memory allocation number
http://msdn.microsoft.com/en-us/library/w2fhc9a3%28v=vs.90%29.aspx
_crtBreakAlloc

After finding break point, Shift+F11 may be used several times to get out of memory allocation function up to allocation command in the source code.


2. VLD Visual Leak Detector - Enhanced Memory Leak Detection for Visual C++

http://vld.codeplex.com/
http://sites.google.com/site/dmoulding/vld
http://www.codeproject.com/Articles/9815/Visual-Leak-Detector-Enhanced-Memory-Leak-Detectio

Really nice tool



C/C++ useful links, libs & apps


1. Useful links

Code project:  www.codeproject.com


2. Free zip / unzip

http://www.codeproject.com/Articles/7530/Zip-Utils-clean-elegant-simple-C-Win32


3. Drag & drop from Windows Explorer to CListCtrl

http://www.codeproject.com/Articles/840/How-to-Implement-Drag-and-Drop-Between-Your-Progra


Thursday, April 12, 2012

C/C++ parsing XML using Xerces

1. Parsing an XML file

    DOMElement* root_element;

        XMLCh_transcode_ptr tempStr("LS");           
        DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(tempStr);   
        DOMLSParser_ptr parser(((DOMImplementationLS*)impl)->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 0));

        DOMConfiguration* configuration = parser->getDomConfig();

        if (configuration->canSetParameter(XMLUni::fgDOMNamespaces, true)) {
            configuration->setParameter(XMLUni::fgDOMNamespaces, true);
        }
        else {
            ostringstream oss;
            oss << "Unable to set XML parser parameter: XMLUni::fgDOMNamespaces (" << XMLUni::fgDOMNamespaces << ")";
            Logger::runtime_error(oss.str());
        }
        if (configuration->canSetParameter(XMLUni::fgDOMDatatypeNormalization, true)) {
            configuration->setParameter(XMLUni::fgDOMDatatypeNormalization, true);
        }
        else {
            ostringstream oss;
            oss << "Unable to set XML parser parameter: XMLUni::fgDOMDatatypeNormalization (" << XMLUni::fgDOMDatatypeNormalization << ")";
            Logger::runtime_error(oss.str());
        }

        boost::scoped_ptr<XML_error_handler> error_handler(new XML_error_handler());
        configuration->setParameter(XMLUni::fgDOMErrorHandler, error_handler.get());

        DOMDocument* doc = parser->parseURI(sXmlFile.c_str());

        if (error_handler->get_num_errors() != 0) {
            ostringstream oss;
            oss << "The following errors occured when parsing configuration XML file:" << endl;
            oss << error_handler->get_errors();
            Logger::runtime_error(oss.str());
        }
       
        root_element = doc->getDocumentElement();       


2. Parsing an XML buffer

char* buf;
 ...

        XMLByte* pXMLByte = reinterpret_cast<XMLByte*>(buf);
        boost::shared_ptr<MemBufInputSource> memIS;
        memIS = boost::shared_ptr<MemBufInputSource>(new MemBufInputSource((const XMLByte*)buf, strlen(buf), "test", false));
        Wrapper4InputSource wrap(memIS.get(), false);
        DOMDocument* doc = parser->parse(&wrap);



C/C++ dll


1. Important note to use FreeLibrary

http://msdn.microsoft.com/en-us/library/windows/desktop/ms683152%28v=vs.85%29.aspx


FreeLibrary does not free dll immediately if called, first it decrease dll counter, if this counter become zero, dll will be free




C/C++ tutorial



1. Useful links

http://www.tenouk.com/cncplusplustutorials.html

http://www.cprogramming.com/tutorial/c++-tutorial.html


2. 8 Common Programming Mistakes

http://www.cprogramming.com/tutorial/common.html



C/C++ Using smart pointer


1. shared pointer

Original code:
        XMLByte* pXMLByte = reinterpret_cast<XMLByte*>(buf);
/*        MemBufInputSource* memIS = new MemBufInputSource((const XMLByte*)buf, strlen(buf), "test", false);
        if(memIS == NULL)
            return 1;
        Wrapper4InputSource wrap(memIS, false);
        DOMDocument* doc = parser->parse(&wrap);

        delete memIS;

Using smart pointer

        XMLByte* pXMLByte = reinterpret_cast<XMLByte*>(buf);
        boost::shared_ptr<MemBufInputSource> memIS;
        memIS = boost::shared_ptr<MemBufInputSource>(new MemBufInputSource((const XMLByte*)buf, strlen(buf), "test", false));
        Wrapper4InputSource wrap(memIS.get(), false);
        DOMDocument* doc = parser->parse(&wrap);


set value for a smart pointer

memset(cbuf.get(), 0x00, ze.unc_size+1);



Windows Command prompt: important commands


1. Network Command-line utilities
http://www.grape-info.com/doc/win2000srv/network/netcommand.html

- Viewing configuration by using ipconfig /all

For Windows 95 and Windows 98 clients, use the winipcfg command instead of ipconfig.
Refreshing configuration by using ipconfig /renew


- Testing connections by using ping

- Troubleshooting hardware addresses by using arp

- Troubleshooting NetBIOS names by using nbtstat

- Displaying connection statistics by using netstat
If you use netstat –n, addresses and port numbers are not converted to names

- Tracing network connections by using tracert

- Testing routers by using pathping

2. Others

- Shutdown: shutdown -s -t 01
- Restart: : shutdown -r -t 01






C/C++ compiler / linker bug fix



1. VS 2008 linker

Error message when you link a Visual C++ project by using the /INCREMENTAL build option in Visual Studio 2008: "LNK1000: Internal error during IncrBuildImage"

Related links:
http://social.msdn.microsoft.com/Forums/en/vcgeneral/thread/2a29f8ec-7c71-475d-a358-f2f1b0458f8b
https://connect.microsoft.com/VisualStudio/Downloads/DownloadDetails.aspx?DownloadID=11399
http://support.microsoft.com/kb/948127



C/C++ Macros for Build Commands & Preprocessor directives



1. Macros for Build Commands and Properties
http://msdn.microsoft.com/en-us/library/c02as0cs%28v=vs.71%29.aspx

$(ConfigurationName), $(PlatformName), $(IntDir), $(OutDir), $(InputPath), $(ProjectPath), $(ProjectName), $(TargetDir), $(TargetPath), $(SolutionDir), $(SolutionPath), ...


2. Preprocessor directives

http://www.cplusplus.com/doc/tutorial/preprocessor/

http://msdn.microsoft.com/en-us/library/aewtdfs3.aspx


Wednesday, April 11, 2012

C/C++ type conversion related


1. TCHAR marcos

#include <tchar.h>

_T("Hello"), TEXT("Hello"), L("Hello")


2. Convert from std::string to std::wstring

std::wstring sFileName(s.begin(), s.end());


3. Convert from wstring to WCHAR* (or TCHAR*)

wstring sFileName;
TCHAR* pChar = (TCHAR*)sFileName.c_str()


4. Convert from string to CHAR* (or TCHAR*)

TCHAR* pChar = sPw.c_str()


5. TCHAR to string, wstring
#ifdef _UNICODE
            std::wstring sFileName(szFileName);
#else
            std::string sFileName(szFileName);
#endif


6. string, wstring and TCHAR*

std:: string s = "Hello";

std::wstring sFileName(s.begin(), s.end());
sFileName += _T(".cfg");