Difference between revisions of "Format specifiers"

From AMule Project FAQ
Jump to: navigation, search
(under construction)
 
Line 1: Line 1:
 
== Introduction ==
 
== Introduction ==
  
 +
[[aMule]] is developped in the [http://www.icce.rug.nl/documents/cplusplus C++] programming language. This language alows to handle strings very easily but sometimes it needs a little tweaking in those strings.
  
 +
When [[translations|translating aMule]], you might encounter with strange things. Sometimes this will be just typos, but sometimes they are there on purpose.
 +
 +
This document is a '''must read''' for anyone willing or actually [[translations|translating aMule]]. It describes all the cases of groups of characters which '''should not''' ever be modified.
 +
 +
So, the following is a description of all the groups of characters which are '''not''' supposed to be modified and what they actually mean.
  
 
== Escape codes ==
 
== Escape codes ==
  
\a alert (causes an audible or visual alert when printed)
+
=== Non-representable [http://www.ecma-international.org/publications/standards/Ecma-006.htm ASCII] codes ===
\b backspace
+
 
\f formfeed (may clear the screen on some systems, don't rely on this)
+
This are codes which represent characters of the [http://www.ecma-international.org/publications/standards/Ecma-006.htm ASCII] codeset which aren't representable with the keyboard.
\n newline (goes to the beginning of the next line; will be automatically
+
*''\a'' -> This will normally cause an audible alert (sometimes visual) like a beep
            translated to/from \r\n or another (if any) end-of-line
+
*''\b'' -> Will go back one character
            sequence on systems where this is necessary)
+
*''\f'' -> On most systems this will clean the screen
\r carriage return (goes to the beginning of the current line)
+
*''\n'' -> Ends the current line and starts a new one, palcing the cursor at the begining
\t horizontal tab
+
*''\r'' -> Goes to the beggining of the current line
\v vertical tab
+
*''\t'' -> Torizontal tabulation
\\ backslash (be careful when using backslashes in literals!)
+
*''\v'' -> Vertical tabulation
\' single quote (useful inside character literals)
+
*''\<octal digits>'' -> Will display the value ''octal digits'' in octal
\" double quote (useful inside string literals)
+
*''\x<hex digits>'' -> Will display the value ''hex digits'' in hexadecimal
\? question mark (useful to avoid unwanted trigraph translation when two
+
 
                  question marks are followed by certain characters)
+
=== Disambiguation escape codes ===
\<octal digits> character value in octal
+
 
\x<hex digits> character value in hexadecimal
+
The following are not characters non-representable on the keyboard, but due to limitations in the [http://www.icce.rug.nl/documents/cplusplus C++] programming language, are needed to be used this way:
 +
*''\?'' -> Displays a question mark ( ''?'' ) to avoid trigraph translations (not all compilers support trigraph translating, so it's not allways necessary)
 +
*''\\'' -> Displays a backslash ( ''\'' )
 +
*''\'' -> Displays a single quote ('' ' '')
 +
*''\"'' -> Displays a double quote ('' " '')
 +
 
 +
=== Examples ===
 +
 
 +
The following are some examples for the above escape codes. They are listed as couples of code-line + output. So, the first line represents the line in the way it is written into the [http://www.icce.rug.nl/documents/cplusplus C++] code and the second line (or group of lines if it needs more than one line) represents how that [http://www.icce.rug.nl/documents/cplusplus C++] code line is displayed on execution:
 +
 
 +
**Code line: ''I am an angel\aOh, true, I am not.''
 +
**Output: ''I am an angelOh, true, I am not'' (A beep will be heard right after displaying the word ''angel'' and the next word (''Oh'') will not be displayed untill the beep finishes.
 +
 
 +
**Code line: ''I have 6\b5 fingers in my right hand''
 +
**Output: ''I have 5 fingers in my right hand''
 +
 
 +
**Code line: ''Where is the <RETURN> key???\nAh, here it is!''
 +
**Output: ''Where is the <RETURN> key???''<br>''Ah, here it is!''
 +
 
 +
**Code line: ''I am a BIG lier\rI'm married with Marilyn Monroe''
 +
**Output: ''I'm married with Marilyn Monroe''
 +
 
 +
**Code line: ''\141\115\165\x6C\x65''
 +
**Output: ''aMule'' (Notice that the octal value of ''a'' in the [http://www.ecma-international.org/publications/standards/Ecma-006.htm ASCII] codeset is 141, the ctal value of ''M'' is 115, the octal value of ''u'' is 165, the hexadecimal value of ''l'' is 6C and the hexadecimal value of ''e'' is 65)
 +
 
 +
**Code line: ''Isn\'t it complicated to use the \" and \\ characters\?''
 +
**Output: ''Isn't it complicated to use the " and \ characters?''
  
 
== Format specifiers ==
 
== Format specifiers ==
 +
 +
=== Basic format specifiers ===
  
 
%d  signed int variable, decimal representation (equivalent to %i)
 
%d  signed int variable, decimal representation (equivalent to %i)
Line 38: Line 72:
 
%n  number of characters written upto now will be written to int
 
%n  number of characters written upto now will be written to int
 
     that the corresponding argument points to
 
     that the corresponding argument points to
You can change the type of the printed variable by inserting one of the following characters between the % sign and the type character (for example: %ld for long int instead of an int).
 
  
 +
=== Type extensions ===
 +
 +
You can change the type of the printed variable by inserting one of the following characters between the % sign and the type character (for example: %ld for long int instead of an int).
 
h    for d,i,o,u,x,X: short int instead of int
 
h    for d,i,o,u,x,X: short int instead of int
 
       (the short int will be promoted to int when passed anyway)
 
       (the short int will be promoted to int when passed anyway)
Line 48: Line 84:
 
L    for e,E,f,F,g,G: long double instead of float/double
 
L    for e,E,f,F,g,G: long double instead of float/double
  
There are some flags and modifiers that can be put between the % and the type character:
+
=== Output tweaks ===
  
 +
There are some flags and modifiers that can be put between the % and the type character:
 
-    left alignment, pad on the right with spaces (default=right alignment)
 
-    left alignment, pad on the right with spaces (default=right alignment)
 
+    print plus sign if positive (default=only print minus sign if negative)
 
+    print plus sign if positive (default=only print minus sign if negative)
Line 68: Line 105:
 
.*    precision will be passed as int parameter before the actual argument
 
.*    precision will be passed as int parameter before the actual argument
  
== Other stuff ==
+
=== Example ===
  
 +
== Other stuff ==
  
 +
=== Leading and ending blank spaces ===
  
 
== Overall example ==
 
== Overall example ==

Revision as of 08:30, 20 November 2004

Introduction

aMule is developped in the C++ programming language. This language alows to handle strings very easily but sometimes it needs a little tweaking in those strings.

When translating aMule, you might encounter with strange things. Sometimes this will be just typos, but sometimes they are there on purpose.

This document is a must read for anyone willing or actually translating aMule. It describes all the cases of groups of characters which should not ever be modified.

So, the following is a description of all the groups of characters which are not supposed to be modified and what they actually mean.

Escape codes

Non-representable ASCII codes

This are codes which represent characters of the ASCII codeset which aren't representable with the keyboard.

  • \a -> This will normally cause an audible alert (sometimes visual) like a beep
  • \b -> Will go back one character
  • \f -> On most systems this will clean the screen
  • \n -> Ends the current line and starts a new one, palcing the cursor at the begining
  • \r -> Goes to the beggining of the current line
  • \t -> Torizontal tabulation
  • \v -> Vertical tabulation
  • \<octal digits> -> Will display the value octal digits in octal
  • \x<hex digits> -> Will display the value hex digits in hexadecimal

Disambiguation escape codes

The following are not characters non-representable on the keyboard, but due to limitations in the C++ programming language, are needed to be used this way:

  • \? -> Displays a question mark ( ? ) to avoid trigraph translations (not all compilers support trigraph translating, so it's not allways necessary)
  • \\ -> Displays a backslash ( \ )
  • \ -> Displays a single quote ( ' )
  • \" -> Displays a double quote ( " )

Examples

The following are some examples for the above escape codes. They are listed as couples of code-line + output. So, the first line represents the line in the way it is written into the C++ code and the second line (or group of lines if it needs more than one line) represents how that C++ code line is displayed on execution:

    • Code line: I am an angel\aOh, true, I am not.
    • Output: I am an angelOh, true, I am not (A beep will be heard right after displaying the word angel and the next word (Oh) will not be displayed untill the beep finishes.
    • Code line: I have 6\b5 fingers in my right hand
    • Output: I have 5 fingers in my right hand
    • Code line: Where is the <RETURN> key???\nAh, here it is!
    • Output: Where is the <RETURN> key???
      Ah, here it is!
    • Code line: I am a BIG lier\rI'm married with Marilyn Monroe
    • Output: I'm married with Marilyn Monroe
    • Code line: \141\115\165\x6C\x65
    • Output: aMule (Notice that the octal value of a in the ASCII codeset is 141, the ctal value of M is 115, the octal value of u is 165, the hexadecimal value of l is 6C and the hexadecimal value of e is 65)
    • Code line: Isn\'t it complicated to use the \" and \\ characters\?
    • Output: Isn't it complicated to use the " and \ characters?

Format specifiers

Basic format specifiers

%d signed int variable, decimal representation (equivalent to %i) %u unsigned int variable, decimal representation %x unsigned int variable, lowercase hexadecimal representation %X unsigned int variable, uppercase hexadecimal representation %o unsigned int variable, octal representation

    (there are no format specifiers for binary representation)

%f float/double, normal notation %e float/double, exponential notation (%E uses E instead of e) %g float/double, notation %f or %e chosen depending on value (%E if %G) %c character (passed as int), text representation %s string (see 10. Arrays, strings and pointers) %p pointer (see 10. Arrays, strings and pointers) %n number of characters written upto now will be written to int

    that the corresponding argument points to

Type extensions

You can change the type of the printed variable by inserting one of the following characters between the % sign and the type character (for example: %ld for long int instead of an int). h for d,i,o,u,x,X: short int instead of int

     (the short int will be promoted to int when passed anyway)
     for n: store result in short int instead of int

l for d,i,o,u,x,X: long int instead of int

     for n: store result in long int instead of int
     Do NOT use for e,E,f,F,g,G for e.g. printing doubles.

L for e,E,f,F,g,G: long double instead of float/double

Output tweaks

There are some flags and modifiers that can be put between the % and the type character: - left alignment, pad on the right with spaces (default=right alignment) + print plus sign if positive (default=only print minus sign if negative)

     (for signed numbers only)

space print space if positive (default=only print minus sign if negative)

     (for signed numbers only)

0 pad with zeros instead of with spaces (for numbers only)

  1. "alternate form": - o: 0 will be prepended to a non-zero result
                       - x/X: prepends 0x/0X to result
                       - f/F,e/E,g/G: decimal point even if no decimals
                       - g/G: trailing zeros are not removed

<nonzero decimal value> specify field width to which result will be padded

     (this can be used together with the 0 flag)
  • field width will be passed as int parameter before the actual argument

.<nonzero decimal value> specify precision (default for f/F,e/E = 6)

     (for s, precision will limit the number of printed characters)

.0 no decimal point is printed for f/F,e/E .* precision will be passed as int parameter before the actual argument

Example

Other stuff

Leading and ending blank spaces

Overall example

Example:

Untranslated:
msgid="I am %s and I a %d-year old\nand I'm a happy \"aMule\" user "
msgstr=""
Would become (translation to spanish):
msgid="I am %s, %d years old\nand &I'm a happy \"rabitty-aMule\" user "
msgstr="Soy %s y tengo %d años\ny soy un fel&iz usuario del \"conejillo-aMule\" "
Explanation:
  1. %s and %d must be copied literally since they will be substituted in the program with some string or number. General rule: anything between a character % and the next letter-character (that is, a, b, 'c, etc...) or percentage character (%) must be copied literally.
  2. \n must be copied literally too since it brakes the line into a new line. The general rule is: \ and it's very next character (can be \, a, n, t, ,f, ", ', etc) must be copied literally.
  3. & must be placed 'before the very same letter in the translation since it indicates the combination ALT+letter that will select that option.
  4. The ending space is left since in the original message it was there. Never remove startng or ending spaces, even if they look ugly. They are there for some reason. Nomally this will be because either before or after that string comes another string. For example: "Opening file " has a blank space at the end, so you can expect that right after it the name of a file will be displayed. Something like Opening file server.met