>cat genRequest.c
#include <time.h>
#include <ctype.h>
#include <stdio.h>
#define YUID_LEN_DEFAULT 32
typedef int bool;
#define false 0;
#define true 1;
int getRandomChar()
{
struct timespec time1={0, 0};
int asCode;
while(1)
{
clock_gettime(CLOCK_REALTIME, &time1);
//srand((unsigned)(time1.tv_sec + time1.tv_nsec));
srand((unsigned)( time1.tv_nsec));
asCode = rand()%128;
//
//Only letters, numbers, underscores, and one dot (.) are allowed.
if ( ('A'<= asCode && asCode <= 'Z') ||
('a' <= asCode && asCode<= 'z' ) ||
('0' <= asCode && asCode <= '9') ||
asCode == '_' ||
asCode == '.' ) break;
//printf("%c:%d\n", asCode, asCode);
}
return asCode;
}
/**uage:
* if 1 argument is appended, the argument is the yuid length when it is not greater than 32, else 32 is its length
*
*/
int main(int argc, char * argv[])
{
int headerLen;
if ( argc == 2 )
{
int arg = atoi( argv[1] );
if ( arg < 32 && arg >= 8)
headerLen= arg;
else
headerLen = YUID_LEN_DEFAULT;
}
else
{
struct timespec time1={0, 0};
clock_gettime(CLOCK_REALTIME, &time1);
srand((unsigned)(time1.tv_nsec));
//Your username must be between 8 and 32 characters long
// rand()%(Y-X+1)+X;
headerLen = rand()%(YUID_LEN_DEFAULT - 7) + 8;
}
int asCode=0;
char header32B[33] = {0};
char tail2B[3]={0};
int tailLen = 2;
int i, j;
bool SingleDot=false;
bool SingleUnderScore=false;
for ( i=0; i < headerLen; ++i)
{
while( 1 )
{
char tmp = (char)getRandomChar();
if ( i == 0 ) {
if( !isalpha(tmp) ) continue;
header32B[0] = tmp; break ;
}
if ( tmp == '.' ) {
if ( SingleDot || header32B[i-1] == '_' ) continue;
header32B[i] = tmp;
SingleDot = true;
break;
}
if ( tmp == '_') {
if( SingleUnderScore || header32B[i-1] == '.') continue;
header32B[i] = tmp;
SingleUnderScore = true;
break;
}
header32B[i] = tmp;
break;
}
}
/*
header32B[i]=(char)getRandomChar();
if ( i == 0 )
{
//yuid must begin with a letter
while( !isalpha(header32B[0]) )
{
header32B[0]=(char)getRandomChar();
}
continue;
}
if( header32B[i] == '.' && !isSingleDot )
{
}
}
*/
//for gen a tail
for ( j=0; j < tailLen ; ++j )
tail2B[j] = (char)getRandomChar();
//tail character indicating the regester time should be alphanumeric
while (!isalnum(tail2B[0]))
{
tail2B[0] = (char)getRandomChar();
}
while (!isalnum(tail2B[1]))
{
tail2B[1] = (char)getRandomChar();
}
printf("%s#%s\n", header32B, tail2B);
return 0;
}
c99支持bool,用支持c99的编译器例如gcc就可以的。
C语言里面没有bool(布尔)类型 C++里面才引入bool类型 C语言里面用数值0表示假,非0整数表示真(一般是1)
c中bool类型的定义 及其他
standard c 是没有bool类型的。一般来说c可以用int来判断对错,返回0是否,其他是对。
确实需要时自己可以定义一个:用枚举类型
typedef enum {FALSE, TRUE
} bool;
c里边也没有DWORD类型,如果需要用unsigned long int 来定义
typedef unsigned long int DWORD;
一些微软c++的定义如何转到standard c 和 c++可以参考微软MSDN
http://msdn.microsoft.com/en-us/library/cc230309%28PROT.10%29.aspx
This section contains simple data types that are defined by either a C/C++ typedef or #define statement. The data types in this section are essentially aliases for C/C++ primitive data types.
A BOOL is a 32-bit field that is set to 1 to indicate TRUE, or 0 to indicate FALSE.
This type is declared as follows:
typedef int BOOL, *PBOOL, *LPBOOL;A BOOLEAN is an 8-bit field that is set to 1 to indicate TRUE, or 0 to indicate FALSE.
This type is declared as follows:
typedef BYTE BOOLEAN, *PBOOLEAN;A BSTR is a pointer to a null-terminated character string in which the string length is stored with the string. Because the length is stored with the string, BSTR variables can contain embedded null characters. For example:
[4 bytes (length prefix)], wchar_t[length], [\0]This type is declared as follows:
typedef WCHAR* BSTR;A BYTE is an 8-bit unsigned value that corresponds to a single octet in a network protocol.
This type is declared as follows:
typedef unsigned char BYTE, *PBYTE, *LPBYTE;A CHAR is an 8-bit block of data that typically contains an ANSI character, as specified in [ISO/IEC-8859-1]. For information on the char keyword, see [C706] section 4.2.9.3.
This type is declared as follows:
typedef char CHAR, *PCHAR;A DOUBLE is an 8-byte, double-precision, floating-point number that represents a double-precision, 64-bit [IEEE754] value with the approximate range: +/–5.0 x 10-324 through +/–1.7 x 10308.
The DOUBLE type can also represent not a number (NAN); positive and negative infinity; or positive and negative 0.
This type is declared as follows:
typedef double DOUBLE;A DWORD is a 32-bit unsigned integer (range: 0 through 4294967295 decimal). Because a DWORD is unsigned, its first bit (Most Significant Bit (MSB)) is not reserved for signing.
This type is declared as follows:
typedef unsigned long DWORD, *PDWORD, *LPDWORD;A DWORD_PTR is an unsigned long type used for pointer precision. It is used when casting a pointer to an unsigned long type to perform pointer arithmetic. DWORD_PTR is also commonly used for general 32-bit parameters that have been extended to 64 bits in 64-bit Windows. For more information, see ULONG_PTR.
This type is declared as follows:
typedef ULONG_PTR DWORD_PTR;A DWORD32 is a 32-bit unsigned integer.
This type is declared as follows:
typedef unsigned int DWORD32;