1. Си / Говнокод #28292

    +2

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    85. 85
    86. 86
    87. 87
    88. 88
    89. 89
    90. 90
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    #define NUMARGS(type,...)  (sizeof((type[]){__VA_ARGS__})/sizeof(type))
    #define xDEF(type) typedef struct {size_t size; type* arr;  }  xARR(type)
    #define xARR(type) jArr_ ## type
    
    #define A(type, ...) (xARR(type)) {NUMARGS(type,__VA_ARGS__), (type[]){__VA_ARGS__}}
    
    #define _FOR(type, item, Arr) type item=Arr.arr[0]; for(size_t I = 0; I < Arr.size; item=Arr.arr[++I] ) 
    // MSVC
    #define xFOR(type, item, array) do{  _FOR(type, item, array) {
    // GCC, Clang
    #define FOR(item, array)        do{ __auto_type Arr=array; _FOR(__auto_type, item, Arr) {
        
    #define NEXT }} while(0);
    
    #define OfArray(type,arr) (xARR(type)){sizeof(arr)/sizeof(type), arr }
    
    typedef struct {
        char *name;
        int     id;
    } Entry;
    
    
    typedef struct {const char *name;} Str;
    typedef struct {int x[2]; } Pair;
    
    xDEF(Entry);
    xDEF(Str);
    xDEF(Pair);
    xDEF(int);
    
    void printEntry(xARR(Entry) entries)
    {
        xFOR(Entry, e, entries)
            printf("%s %d \n", e.name, e.id);
        NEXT
    }
    
    void printSquares(xARR(int) ints)
    {
        FOR(v, ints)
            printf("%d²=%d\n", v,(int) pow(v,2.));
        NEXT
    }
    
    int main(void)
    {
        xARR(Entry) e = A(Entry, {"one",1}, {"two",2}, {"three",3});
        printEntry(e);
        
        puts("_______________________________________");
        
        // можно передавать в метод непосредственно аргуметом
        printSquares( A(int, 3, 7, 5, 4) );
        puts("_______________________________________");    
        
        int nums[]={4,3,2,1};
        // можно использовать ранее объявленный массив
        printSquares(OfArray(int,nums));
        
        // можно итерироватьcя по ранее объявленному массиву
        FOR(i, OfArray(int, nums))
            printf("%d-",i);
        NEXT
        
        puts("\n_______________________________________");
        
        //вложенные циклы:
        for (int k=1;k<3;k++)
            FOR(i, A(Str, "kakoi", "bagor"))    
                FOR(j, A(int, 1111,2222,3333))
                    printf("%d %s %d\n", k, i.name, j);
                NEXT
            NEXT
        
        puts("_______________________________________");
        
        FOR(v, A(Pair, {1,2}, {11,12}, {20,21}))
            printf("%d,%d\n", v.x[0], v.x[1]);
        NEXT
        puts("_______________________________________");    
        //проблема пустого варарга
        FOR(j, A(int))
            printf("%d\n", j);
        NEXT    
        return(0);
    }

    https://godbolt.org/z/o9Tv9EvGx

    Довёл for~each до ума.

    3.14159265, 23 Июля 2022

    Комментарии (26)
  2. Си / Говнокод #28289

    −1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    #define BITS 8
    
    typedef union
    {
        int v;
        struct 
        {
            #define FIELD(x,_) int b##x:1;        
            EVAL(REPEAT(BITS, FIELD, ~))
            #undef FIELD
        };
    } Num;
    
    Num shl(Num n, int carry)
    {
        #define SHIFTL(x,_) CAT(n.b, CAT(x = n.b, CAT(DEC_,x)));    
        EVAL(RREPEAT(BITS, SHIFTL, ~))
        #undef SHIFTL
        n.b0 = carry;
    }
    
    Num shr(Num n, int carry)
    {
        #define SHIFTR(x,_) CAT(n.b, CAT(CAT(DEC_,x) = n.b, x));
        EVAL(REPEAT(BITS, SHIFTR, ~))
        #undef SHIFTR
        CAT(n.b, CAT(DEC_,BITS)) = carry;
    }
    
    
    int main()
    {
        for (int i=0; i<33; ++i){
            Num n   = {i};
            Num n1 = shl(n,0);
            Num n2 = shr(n,0);        
            printf("%d %d %d\n",n ,n1 , n2);
        }
    }

    https://godbolt.org/z/48h6EWacY

    Двунаправленный сдвиговый регистр на препроцессоре.
    Сделан без использования арифметических действий.

    3.14159265, 21 Июля 2022

    Комментарии (6)
  3. Си / Говнокод #28274

    +1

    1. 1
    2. 2
    3. 3
    4. 4
    int my_strcmp(const char *out, const char *in   ){
      for( ;*(in) , *(out) &&  *(in) == *(out); *out++,*in++  );
    	   return   *in <= *out ?  *out > *in   : -1   ;
    	}

    Бульк

    lazy_8, 06 Июля 2022

    Комментарии (8)
  4. Си / Говнокод #28185

    −1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    #include <stdio.h> //Нужная библиотека, как обычно;
    
    int main(){ //Типа начало программы;
        FILE *in,*out; //Входной и выходной файлы;
        unsigned long int h,i; //Высота изображения и итератор цикла;
        unsigned char px,s; //Прочитанный пиксель и насчитанный сэмпл аудио;
        in=fopen("1.bmp","rb"); //Открываем файл на чтение;
        out=fopen("1.pcm","wb"); //Открываем файл на запись;
        fseek(in,22,SEEK_SET); //Позиционируемся в то место заголовка, где записана высота изображения
        fread(&h,4,1,in); //Считываем высоту изображения (4 байта);
        for(i=0;i<h;i++){ //Цикл - пробег по строкам;
            fseek(in,0x436+i*256,SEEK_SET); //Позиционируемся на начало i-ой строки
            s=0; //Инициализируем счётчик (значение сэмпла);
            do{ //Подсчёт нечёрных пикселей;
                fread(&px,1,1,in); //Считываем цвет пикселя;
                s+=1; //Увеличиваем счётчик на единицу;
            }while(px); //И так, пока не встретится чёрный пиксель;
            fwrite(&s,1,1,out); //Записываем получившийся результат в выходной файл;
        }
        fclose(in); //Закрываем входной файл;
        fclose(out); //Закрываем выходной файл;
        return 0; //Типа конец программы;
    }

    https://habr.com/ru/post/419527/

    kcalbCube, 23 Мая 2022

    Комментарии (0)
  5. Си / Говнокод #28096

    0

    1. 1
    #exclude <conio.h>

    конио

    3_dar, 29 Марта 2022

    Комментарии (13)
  6. Си / Говнокод #28088

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    /*
     *  libcaca       Colour ASCII-Art library
     *  Copyright (c) 2002-2010 Sam Hocevar <[email protected]>
     *                All Rights Reserved
     *
     *  This library is free software. It comes without any warranty, to
     *  the extent permitted by applicable law. You can redistribute it
     *  and/or modify it under the terms of the Do What The Fuck You Want
     *  To Public License, Version 2, as published by Sam Hocevar. See
     *  http://sam.zoy.org/wtfpl/COPYING for more details.
     */
    
    /*
     *  This header contains a conio.h reimplementation.
     */
    
    #ifndef __CACA_CONIO_H__
    #define __CACA_CONIO_H__
    
    /* Since we're going to redefine standard functions, include these
     * headers first to avoid errors upon later inclusion. */
    #if !defined(__KERNEL__)
    #   include <stdio.h>
    #endif
    
    #include <caca.h>
    
    #if !defined _DOXYGEN_SKIP_ME && !defined __LIBCACA__
    #   undef BLINK
    #   define BLINK CACA_CONIO_BLINK
    #   undef BLACK
    #   define BLACK CACA_CONIO_BLACK
    #   undef BLUE
    #   define BLUE CACA_CONIO_BLUE
    #   undef GREEN
    #   define GREEN CACA_CONIO_GREEN
    #   undef CYAN
    #   define CYAN CACA_CONIO_CYAN
    #   undef RED
    #   define RED CACA_CONIO_RED
    #   undef MAGENTA
    #   define MAGENTA CACA_CONIO_MAGENTA
    #   undef BROWN
    #   define BROWN CACA_CONIO_BROWN
    #   undef LIGHTGRAY
    #   define LIGHTGRAY CACA_CONIO_LIGHTGRAY
    #   undef DARKGRAY
    #   define DARKGRAY CACA_CONIO_DARKGRAY
    #   undef LIGHTBLUE
    #   define LIGHTBLUE CACA_CONIO_LIGHTBLUE
    #   undef LIGHTGREEN
    #   define LIGHTGREEN CACA_CONIO_LIGHTGREEN
    #   undef LIGHTCYAN
    #   define LIGHTCYAN CACA_CONIO_LIGHTCYAN
    #   undef LIGHTRED
    #   define LIGHTRED CACA_CONIO_LIGHTRED
    #   undef LIGHTMAGENTA
    #   define LIGHTMAGENTA CACA_CONIO_LIGHTMAGENTA
    #   undef YELLOW
    #   define YELLOW CACA_CONIO_YELLOW
    #   undef WHITE
    #   define WHITE CACA_CONIO_WHITE
    #endif

    3_dar, 22 Марта 2022

    Комментарии (3)
  7. Си / Говнокод #28077

    0

    1. 1
    int (*parray)[sizeof *parray] = malloc(sizeof *parray);

    kcalbCube, 11 Марта 2022

    Комментарии (8)
  8. Си / Говнокод #28071

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    85. 85
    86. 86
    87. 87
    88. 88
    89. 89
    90. 90
    91. 91
    92. 92
    93. 93
    94. 94
    95. 95
    96. 96
    97. 97
    98. 98
    /*
       xdrv_96_blacklist.ino - Blacklist for Tasmota
    
       SPDX-FileCopyrightText: 2022 Theo Arends
    
       SPDX-License-Identifier: GPL-3.0-only
     */
     #define USE_BLACKLIST
    
     #ifdef USE_BLACKLIST
     /*********************************************************************************************\
      * Blacklist support
      *
      * Check language and user set latitude/longitude against blacklist table
     \*********************************************************************************************/
     #define XDRV_96            96
    
     typedef struct {
       int16_t latitude_tl;    // - 8999 to 8999
       int16_t longitude_tl;   // -17999 to 17999
       int16_t latitude_br;
       int16_t longitude_br;
       uint16_t lcid;
     } tBlArray;
    
     //const char BlacklistText[] PROGMEM = "Stop war - Free Ukrain|Stop war - Free Ukrain|";
     const char BlacklistText[] PROGMEM = "Stop war, Free Ukrain";
    
     //                   lat_tl lon_tl lat_br lon_br lcid
     tBlArray BlArray[] {  5900,  3200,  5300,  4400, 1049,     // Around Moscow
                           5450,  2633,  5280,  2900, 1049      // Around Minsk
                        };
    
     uint8_t blist_show = 0;
    
     void BListEverySecond(void) {
       if (Rtc.utc_time < 1648771200) {                         // Only until 2022-04-01
         if (0 == (TasmotaGlobal.uptime % 20)) {                // Only every 20 seconds
           if (TasmotaGlobal.power) {                           // Only if any power on
             uint32_t latitude = Settings->latitude / 10000;
             uint32_t longitude = Settings->longitude / 10000;
             uint32_t count = sizeof(BlArray) / sizeof(tBlArray);
             for (uint32_t i = 0; i < count; i++) {
               // Currently only supports top-right quarter of the earth
               if ((LANGUAGE_LCID == BlArray[i].lcid) &&        // Check language id
                   (latitude < BlArray[i].latitude_tl) &&       // Check user set latitude and longitude against table
                   (latitude > BlArray[i].latitude_br) &&
                   (longitude > BlArray[i].longitude_tl) &&
                   (longitude < BlArray[i].longitude_br)) {
    
     //            char bl_text[100];
     //            snprintf_P(bl_text, sizeof(bl_text), PSTR("Power0 0"));   // Turn all power off - annoying
     //            snprintf_P(bl_text, sizeof(bl_text), PSTR("Restart 1"));  // Restart - more annoying
     //            snprintf_P(bl_text, sizeof(bl_text), PSTR("Reset 1"));    // Reset - disastrous
     //            ExecuteCommand(bl_text, SRC_IGNORE);
    
     //            char bl_text[100];
     //            AddLog(LOG_LEVEL_NONE, PSTR("**** %s ****"), GetTextIndexed(bl_text, sizeof(bl_text), i, BlacklistText));
                 AddLog(LOG_LEVEL_NONE, PSTR("**** %s ****"), BlacklistText);
                 blist_show = i +1;                             // Set GUI message id
                 break;
               }
             }
           }
         } else if (0 == (TasmotaGlobal.uptime % 10)) {         // Only every 10 seconds
           blist_show = 0;                                      // Reset GUI message id after 10 seconds
         }
       }
     }
    
     void BListShow(bool json) {
       if (blist_show) {
     //    char bl_text[100];
     //    WSContentSend_PD(PSTR("{s}**** %s ****{m}{e}"), GetTextIndexed(bl_text, sizeof(bl_text), blist_show -1, BlacklistText));
         WSContentSend_P(PSTR("{s}**** %s ****{m}{e}"), BlacklistText);
       }
     }
    
     /*********************************************************************************************\
      * Interface
     \*********************************************************************************************/
    
     bool Xdrv96(uint8_t function) {
       bool result = false;
    
       switch (function) {
         case FUNC_EVERY_SECOND:
           BListEverySecond();
           break;
     #ifdef USE_WEBSERVER
         case FUNC_WEB_SENSOR:
           BListShow(0);
           break;
     #endif  // USE_WEBSERVER
       }
    
       return result;
     }

    https://github.com/arendst/Tasmota/commit/98cbf2587a1a914bbd16996ebb48dd451d3da448

    3_dar, 05 Марта 2022

    Комментарии (9)
  9. Си / Говнокод #28061

    +1

    1. 1
    2. 2
    3. 3
    4. 4
    #pragma aux __cdecl "_*"                            \
                    parm caller [ ]                          \
                    value struct float struct routine [eax] \
                    modify [eax ecx edx]

    kcalbCube, 28 Февраля 2022

    Комментарии (11)
  10. Си / Говнокод #28058

    +1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    #include <stdio.h>
    
    int main(void)
    {
    	(
    		*********************
    		**  **     ** ***** *
    		* ** * **** ** *** **
    		* ** * **** *** * ***
    		* ** * **** **** ****
    		* ** * **** *** * ***
    		* ** **    *** *** **
    		*********************
    	printf)("pidor");
    }

    kcalbCube, 26 Февраля 2022

    Комментарии (13)