$npx -y skills add totvs/engpro-advpl-tlpp-skills --skill utf8-to-cp1252-conversionConvert AdvPL/TLPP source files from UTF-8 encoding to Windows-1252 (CP1252) after code generation. The Protheus compiler only supports CP1252 encoded files — any source created or modified by an AI agent will be in UTF-8 and must be converted before compilation. Use when user sa
| 1 | # UTF-8 to CP1252 Encoding Conversion |
| 2 | |
| 3 | ## CRITICAL — Agent Execution Rules |
| 4 | |
| 5 | > **These rules are MANDATORY. Any violation causes compilation failures and file corruption.** |
| 6 | |
| 7 | 1. **ALWAYS use the provided scripts** — NEVER attempt manual encoding conversion (e.g., Python, Node.js, or copying content to a new file). |
| 8 | 2. **Conversion is IN-PLACE** — The script replaces the original file. The filename and extension do NOT change. A `.tlpp` file stays `.tlpp`, a `.prw` stays `.prw`. |
| 9 | 3. **NEVER create files with `.cp1252` extension** — There is no such convention. Files keep their original extension (`.prw`, `.prg`, `.tlpp`, `.prx`, `.ch`, `.th`). |
| 10 | 4. **NEVER create backup copies or duplicate files** — No `.bak`, `.orig`, `.utf8`, `.old`, or any other copy. Only the converted file must exist. |
| 11 | 5. **NEVER rename files during conversion** — The filename must remain exactly the same before and after conversion. |
| 12 | 6. **VERIFY after conversion** — Run `file --mime-encoding <file>` (Linux/macOS) to confirm the result is `iso-8859-1` or `unknown-8bit`, NOT `utf-8`. |
| 13 | 7. **ONE final state** — After this skill runs, only the original file (now CP1252-encoded) must exist. Zero temporary files, zero copies, zero artifacts. |
| 14 | |
| 15 | ## Overview |
| 16 | |
| 17 | Convert AdvPL and TLPP source files from UTF-8 encoding to Windows-1252 (CP1252). The Protheus compiler (AdvPL, AdvPL ASP, 4GL, and 4GLP) **only supports files encoded in CP1252** (Windows code page 1252). AI agents and modern editors create files in UTF-8 by default, which causes compilation errors and corrupted characters (mojibake) when compiled by TDS/AppServer. |
| 18 | |
| 19 | This skill provides **two native scripts** (zero external dependencies) that safely convert file encoding with BOM detection and batch processing support: |
| 20 | |
| 21 | - **Bash + `iconv`** → Linux and macOS (both pre-installed) |
| 22 | - **PowerShell + .NET Encoding** → Windows (both pre-installed) |
| 23 | |
| 24 | > **Source:** TDN Documentation — "Os compiladores Protheus (AdvPL, AdvPL Asp, 4GL e 4GLP) suportam apenas os arquivos com código de página CP1252." ([Informações sobre a gravação de arquivos-fontes](https://tdn.totvs.com/pages/viewpage.action?pageId=56132455)) |
| 25 | |
| 26 | ## When to Use |
| 27 | |
| 28 | Use this skill when: |
| 29 | |
| 30 | - **After any code generation** — any skill that creates `.prw`, `.prg`, `.tlpp`, or `.prx` files (e.g., `mvc-generator`, `tlpp-rest-endpoint-generator`, `entry-point-designer`) |
| 31 | - **After code migration** — `advpl-to-tlpp-migration` |
| 32 | - **After code refactoring** — `refactor`, `refactor-method-complexity-reduce` |
| 33 | - **When compilation fails** with encoding-related errors (garbled characters, invalid syntax from bad encoding) |
| 34 | - **When SonarQube flags rule CA0000** with "wrong charset" as the cause |
| 35 | - **When converting files received from external sources** in UTF-8 |
| 36 | |
| 37 | **Do NOT use when:** |
| 38 | |
| 39 | - Files are already in CP1252 encoding |
| 40 | - Working with non-Protheus source files (Python, JavaScript, etc.) |
| 41 | - Working with REST API response encoding (use Charset configuration instead) |
| 42 | |
| 43 | --- |
| 44 | |
| 45 | ## Why CP1252? |
| 46 | |
| 47 | The TOTVS Protheus platform was designed around the Windows-1252 code page: |
| 48 | |
| 49 | 1. **Compiler requirement** — TDS and AppServer compilers parse source files assuming CP1252 byte sequences |
| 50 | 2. **String literals** — accented characters in Portuguese (à, é, ç, ã, õ) have different byte representations in UTF-8 vs CP1252 |
| 51 | 3. **Runtime behavior** — functions like `EncodeUTF8()` and `DecodeUTF8()` explicitly expect CP1252 as the source/target encoding |
| 52 | 4. **SonarQube compliance** — rule CA0000 flags files with incorrect charset as MAJOR severity |
| 53 | |
| 54 | ### Character Encoding Differences |
| 55 | |
| 56 | | Character | UTF-8 Bytes | CP1252 Byte | Issue | |
| 57 | | --- | --- | --- | --- | |
| 58 | | `é` | `0xC3 0xA9` | `0xE9` | 2 bytes vs 1 byte | |
| 59 | | `ç` | `0xC3 0xA7` | `0xE7` | Compiler reads wrong byte sequence | |
| 60 | | `ã` | `0xC3 0xA3` | `0xE3` | String length mismatch at runtime | |
| 61 | | `ü` | `0xC3 0xBC` | `0xFC` | Breaks `Len()`, `SubStr()`, etc. | |
| 62 | |
| 63 | --- |
| 64 | |
| 65 | ## Conversion Scripts |
| 66 | |
| 67 | This skill includes two native scripts — use the one matching your operating system: |
| 68 | |
| 69 | | OS | Script | Runtime | Dependencies | |
| 70 | | --- | --- | --- | --- | |
| 71 | | **Linux / macOS** | [scripts/convert-encoding.sh](scripts/convert-encoding.sh) | Bash + `iconv` | None (pre-installed) | |
| 72 | | **Windows** | [scripts/convert-encoding.bat](scripts/convert-encoding.bat) | CMD + PowerShell inline | None |