Code porting from windows to Linux

Source: Internet
Author: User
Tags usleep

To port the gettickcount API function in Windows to Linux, you can use the followingCode:

 
Long gettickcount () {tms tm; return times (& TM );}

2,Transplantation of ITOA in Windows and Linux systems

As you know, since STL in Linux does not implement the default ITOA function, it does not work normally in Linux when the STL code of Windows is transplanted to Linux. If STL is disabled on the GCC command line, STL cannot be used in the code, leading to loss of portability. Here is a simple and feasible solution to facilitate migration from windows to Linux:

# If defined (_ Linux _) # DEFINE _ ITOA itoachar * ITOA (INT value, char * STR, int Radix) {int REM = 0; int Pos = 0; char CH = ''! ''; Do {REM = Value % Radix; value/= Radix; If (16 = Radix) {If (REM> = 10 & REM <= 15) {Switch (REM) {case 10: CH = 'a'; break; Case 11: CH = 'B'; break; Case 12: ch = 'C'; break; Case 13: CH = ''d'; break; Case 14: CH = ''e''; break; Case 15: ch = ''f''; break ;}} if (''! ''= CH) {STR [POS ++] = (char) (REM + 0x30);} else {STR [POS ++] = CH ;}} while (value! = 0); STR [POS] = ''\ 0''; return strrev (STR);} # endif

3. Porting _ strrev from windows to Linux

Because the _ strrev function is not available in Linux, porting windows code to Linux may cause problems. This article describes the following tips, in Linux, provide a method to replace the _ strrev function. Here two independent implementations are provided: one is the standard implementation of the common char * C function using _ strrev, and the other is the implementation of STL. Both input and output are still char *.

 
/// Strrev Standard Edition // # If! Defined (_ Linux _) # DEFINE _ strrev # endifchar * strrev (char * Szt) {If (! Szt) // process the passed empty string. Return ""; int I = strlen (Szt); int T =! (I % 2 )? 1: 0; // check the string length. for (Int J = I-1, K = 0; j> (I/2-T); j --) {char CH = Szt [J]; szt [J] = Szt [k]; Szt [k ++] = CH;} return Szt;} // strrev STL version. // char * strrev (char * Szt) {string S (Szt); reverse (S. begin (), S. end (); strncpy (Szt, S. c_str (), S. size (); Szt [S. size () + 1] = ''\ 0''; return Szt;

4. Porting sleep functions from windows to Linux

Suppose you have some code written in the Windows environment. If you want them to run in the Linux environment, the condition is that you must keep the original API signature called. For example, if sleep exists in Windows and the corresponding function in Linux is usleep, how can we keep the original function name called? The following code is used as an example:

 
Void sleep (unsigned int useconds) {// 1 Millisecond (milisecond) = 1000 microseconds (microsecond ). // windows sleep uses miliseconds in milliseconds // Linux usleep uses microsecond (microsecond) // since the original code is used in windows, therefore, the parameter must be converted in milliseconds to microseconds. Usleep (useconds * 1000 );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.