1. C++ / Говнокод #6342

    +172

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    if (!cpMenu->IsSelected())
    	{
    		std::vector<UINT> menus;
    		menus.push_back(_menuIndex.Value);	
    		if (!_SelectMenu(cpTarget, _instanceID.HasValue ? _instanceID.Value : -1, &menus[0], (UINT)menus.size(), SelectionType::Exclusive))
    			return CPNGObject();
    	}

    без вектора тут ну не обойтись :)

    qwertyu, 13 Апреля 2011

    Комментарии (8)
  2. C++ / Говнокод #6323

    +150

    1. 1
    2. 2
    nPosition = !bInvert ? data->pos_back
                                       : data->pos_front;

    Aleskey, 12 Апреля 2011

    Комментарии (17)
  3. C++ / Говнокод #6286

    +158

    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
    #ifndef _header_hpp_included_
    #define _header_hpp_included_
     
    #include <iostream>
    #include <cstdio>
    #include <boost/bind.hpp>
    #include <boost/asio.hpp>
    #include <boost/thread.hpp>
    #include <boost/lexical_cast.hpp>
     
    enum { recv_buffer_size = 13 };
    enum { send_buffer_size = 13 };
     
    volatile size_t counter = 0;
     
    void client_readed(
       boost::asio::ip::tcp::socket&,
       char*,
       FILE*,
       const boost::system::error_code&
    );
     
    void client_read(
       boost::asio::ip::tcp::socket& sock,
       FILE* out
    ) {
       char* buf = new char[recv_buffer_size];
       boost::asio::async_read(
          sock,
          boost::asio::buffer(buf, recv_buffer_size),
          boost::bind(
             &client_readed,
             boost::ref(sock),
             buf,
             out,
             boost::asio::placeholders::error));}
     
    void client_readed(
       boost::asio::ip::tcp::socket& sock,
       char* buf,
       FILE* out,
       const boost::system::error_code& e) {
       if ( e ) {
          if ( !counter ) return;
          std::cout << "read handler: " << e.message() << std::endl;
          return;
       }
        fwrite(buf, recv_buffer_size, 1, out);
        counter--;
     
    #ifdef _my_debug_
       printf("client_readed(): %s", buf);
       fflush(stdout);
    #endif
     
       static size_t idx = 0;
       size_t tmp = 0;
       char* p = strchr(buf, ':');
       if ( p ) {
          p++;
          sscanf(p, "%8d", &tmp);
       } else
          throw std::runtime_error("input data error!");
       delete[] buf;
       if ( idx != tmp ) {
          std::ostringstream os;
          os << "read error. expected " << idx << " get " << tmp;
          throw std::runtime_error(os.str());
       }
       idx++;
       client_read(sock, out);
    }
     
    void writen(
       char*,
       FILE*,
       const boost::system::error_code&
    );
     
    void start_write(
       boost::asio::ip::tcp::socket& sock,
       char* buf,
       FILE* out) {
       counter++;
       boost::asio::async_write(
          sock,
          boost::asio::buffer(buf, send_buffer_size),
          boost::bind(
             &writen,
             buf,
             out,
             boost::asio::placeholders::error) 
        ); 
    }

    qbasic, 08 Апреля 2011

    Комментарии (12)
  4. C++ / Говнокод #6285

    +161

    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
    #include "header.hpp"
     
    int main(int argc, char** argv) {
       if ( argc != 4 ) {
          std::cout << "client ip port 0/1 - sleed disabled/enabled" << std::endl;
          return 0;
       }
       std::string ip = argv[1];
       boost::uint16_t port = boost::lexical_cast<boost::uint16_t>(argv[2]);
       bool wsleep = (argv[3][0] == '1');
       std::cout << "sleep " << (wsleep?"enabled":"disabled") << std::endl;
       
       FILE* in = fopen("client_in.log", "wb");
       FILE* out= fopen("client_out.log", "wb");
       if ( !out || !in ) {
          std::cout << "can`t open file!" << std::endl;
          return 1;
       }
     
       boost::asio::ip::tcp::endpoint endpoint(
          boost::asio::ip::address::from_string(ip), port
       );
     
       boost::asio::io_service ios;
       boost::shared_ptr<boost::asio::io_service::work> work(new boost::asio::io_service::work(ios));
     
       boost::thread thread(boost::bind(&boost::asio::io_service::run, &ios));
       
       boost::asio::ip::tcp::socket socket(ios);
       socket.connect(endpoint);
     
       boost::asio::socket_base::non_blocking_io non_blocking_io(true);
       socket.io_control(non_blocking_io);
     
       client_read(socket, in);
     
       for ( size_t idx = 0; idx < 100000000; ++idx ) {
          char* buf = new char[send_buffer_size];
          sprintf(buf, "cs:%8dn", idx);
          start_write(socket, buf, out);
          if ( wsleep ) {
             boost::this_thread::sleep(boost::posix_time::microseconds(1000));
          }
       }
     
       std::cout
       << "send data to server finished!" << std::endl
       << "waiting for all ask`s from server..." << std::endl;
     
       work.reset();
     
       while ( counter ) {
          boost::this_thread::sleep(boost::posix_time::microseconds(1000));
          std::cout << "." << std::flush;
       }
     
       std::cout << std::endl << std::endl
       << "all ask`s received." << std::endl
       << "terminate client..." << std::endl;
     
       socket.cancel();
       socket.close();
     
       thread.join();
       fclose(in);
       fclose(out);
    }

    qbasic, 08 Апреля 2011

    Комментарии (10)
  5. C++ / Говнокод #6280

    +163

    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
    class CStateShortMap
    {
    public:
      CStateShortMap(int id, TDateTime time, TZReadOnlyQuery* query);
      void Load(TZReadOnlyQuery* query, TDateTime time);
      int GetTypeSize() const { return Items.size(); }
      char GetType(int idx)
      {
        return Items[idx].first;
      }
      std::vector< std::vector<CStateShortItem> >& GetItems(char type)
      {
        for(int i=0; i < Items.size(); i++)
        {
          if( Items[i].first == type )
            return Items[i].second;
        }
        return Items[0].second;
      }
    private:
      const int Id;
      std::vector< std::pair<char, std::vector< std::vector<CStateShortItem> > > > Items;
    };
    
    CStateShortMap::CStateShortMap(int id, TDateTime time, TZReadOnlyQuery* query)
     : Id(id)
    {
      Load( query, time );
    }
    
    void CStateShortMap::Load(TZReadOnlyQuery* query, TDateTime time)
    {
      Items.clear();
      String sql;
      sql.sprintf( "select map.id_, equipment.type_"
                   " from map left join equipment on map.equipment_id=equipment.id_"
                   " where map.station_id=%d order by equipment.type_", Id );
      query->SQL->Text = sql;
      query->Open();
      vector< pair<int, char> > mapId;
      while( !query->Eof )
      {
        mapId.push_back( pair<int, char>(query->FieldByName( "id_" )->AsInteger, query->FieldByName( "type_" )->AsString[1]) );
        query->Next();
      }
      Items.clear();
      for(int i=0; i<mapId.size(); i++)
      {
        sql.sprintf( "select status.color, status.name_, map.number_equipment, equipment_status.status_id, equipment_status.begin_, equipment_status.plan_end_"
                     " from equipment_status left join map on equipment_status.map_id=map.id_"
                     " left join status on equipment_status.status_id=status.id_"
                     " where equipment_status.map_id=%d and begin_<='%s' order by equipment_status.begin_ desc limit 1",
                     mapId[i].first, time.FormatString("yyyy-mm-dd hh:nn:ss") );   //equipment_status.id_
        query->SQL->Text = sql;
        query->Open();
        if( query->Eof ) continue;
        int id[] = { 0, 1, 1, 1, 1, 2, 3, 1, 1, 1 };
        int j;
        for(j=0; j<Items.size(); j++)
          if( Items[j].first==mapId[i].second )
            break;
        if( j!=Items.size() )
          Items[j].second[id[query->FieldByName( "status_id" )->AsInteger]].push_back( CStateShortItem( query ) );
        else
        {
          Items.push_back( std::pair<char, std::vector< std::vector<CStateShortItem> > >( mapId[i].second, std::vector< std::vector<CStateShortItem> >() ) );
          Items[ Items.size()-1 ].second.resize( 4 );
          Items[ Items.size()-1 ].second[ id[query->FieldByName( "status_id" )->AsInteger] ].push_back( CStateShortItem( query ) );
        }
      }
      for(int i=0; i<Items.size(); i++)
      {
        sort( Items[i].second[0].begin(), Items[i].second[0].end() );
        sort( Items[i].second[1].begin(), Items[i].second[1].end() );
        sort( Items[i].second[2].begin(), Items[i].second[2].end() );
        sort( Items[i].second[3].begin(), Items[i].second[3].end() );
      }
    }

    старый проект на борландбыдлере, найденный на новой работе

    ni3_inv, 08 Апреля 2011

    Комментарии (5)
  6. C++ / Говнокод #6277

    +165

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    ID3DBlob* pErrorBlob;
        hr = D3DX11CompileFromFile( szFileName, NULL, NULL, szEntryPoint, szShaderModel, 
            dwShaderFlags, 0, NULL, ppBlobOut, &pErrorBlob, NULL );
        if( FAILED(hr) )
        {
            if( pErrorBlob != NULL )
                OutputDebugStringA( (char*)pErrorBlob->GetBufferPointer() );
            if( pErrorBlob ) pErrorBlob->Release();
            return hr;
        }
        if( pErrorBlob ) pErrorBlob->Release();

    Текст примера из MS DXSDK. Проверка - а вдруг pErrorBlob самоуничтожается после прочтения?

    CHayT, 07 Апреля 2011

    Комментарии (9)
  7. C++ / Говнокод #6252

    +171

    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
    ...
    
    bool GameLocations::checkButtonsEnabled() const
    {
    	GameClassT& gc = GameClass::instance();
    	return
    		!gc.getCurrentLocationPopup() &&
    		!gc.getHud().getCurrentWindowFore() &&
    		!gc.isMenuOpen() &&
    		!gc.isEndOfDay() &&
    		!GameClass::instance().isMouseConsumedThisFrame();
    }
    
    ...
    
    void LocationPopupBase::update(float dt)
    {
    	...
    
    	const bool inputEnabled =
    		m_isActive &&
    		!m_talentUsedWindowActive &&
    		!m_dialogueManager.isVisible() &&
    		!GameClass::instance().getHud().getCurrentWindowBack() &&
    		!GameClass::instance().getHud().getCurrentWindowFore() &&
    		(!m_currentAction ||
    		 ((*m_currentActionPhase == AP_Finalize) && !m_currentAction->m_immediateFinalize)) &&
    		 m_actionSequenceCallbacks.empty();
    
    	setInputEnabled(inputEnabled);
    }
    
    ...

    Вот во что со временем превращаются игровые проекты, в которых нет никакой стейт-машины или хоть какого-нибудь её аналога.

    Это - только вершина айсберга. Разнообразные (старые и новые) баги обработки ввода постоянно появляются из ниоткуда, исчезают в никуда, а фиксить их приходится минимум по пять раз в неделю.

    Kirinyale, 06 Апреля 2011

    Комментарии (12)
  8. C++ / Говнокод #6236

    +160

    1. 1
    std::cout << (valid_flag + prior ? 1 : 0) << " " << valid_flag+1-1 << std::endl;

    Говнокод - загадка. Какой тип у valid_flag?

    seregakabancheg, 05 Апреля 2011

    Комментарии (9)
  9. C++ / Говнокод #6229

    +177

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    if(x*x > = 0)
    {
    // какие-то действия
    }
    else
    {
    // какие-то действия
    }

    Код встретил у знакомой студентки :3 Не, ну а в вдруг?

    minuzZ, 05 Апреля 2011

    Комментарии (37)
  10. C++ / Говнокод #6211

    +168

    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
    template <typename T> T min3 (T v1, T v2, T v3) {
      T min = v1;
      if (min > v2)
        min = v2;
      if (min > v3)
        min = v3;
      return min;
    };
    
    template <typename T> T max3 (T v1, T v2, T v3) {
      T max = v1;
      if (max < v2)
        max = v2;
      if (max < v3)
        max = v3;
      return max;
    };

    Как говорится - главное, чтоб работало.

    panter_dsd, 04 Апреля 2011

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