- 1
- 2
nPosition = !bInvert ? data->pos_back
: data->pos_front;
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
+150
nPosition = !bInvert ? data->pos_back
: data->pos_front;
+158
#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)
);
}
+161
#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);
}
+163
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() );
}
}
старый проект на борландбыдлере, найденный на новой работе
+165
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 самоуничтожается после прочтения?
+171
...
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);
}
...
Вот во что со временем превращаются игровые проекты, в которых нет никакой стейт-машины или хоть какого-нибудь её аналога.
Это - только вершина айсберга. Разнообразные (старые и новые) баги обработки ввода постоянно появляются из ниоткуда, исчезают в никуда, а фиксить их приходится минимум по пять раз в неделю.
+160
std::cout << (valid_flag + prior ? 1 : 0) << " " << valid_flag+1-1 << std::endl;
Говнокод - загадка. Какой тип у valid_flag?
+177
if(x*x > = 0)
{
// какие-то действия
}
else
{
// какие-то действия
}
Код встретил у знакомой студентки :3 Не, ну а в вдруг?
+168
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;
};
Как говорится - главное, чтоб работало.
+156
if (licenseImageAvailable) {
QPoint p1, p2, p3;
p1.setX (top->getUIntSeq ("X1",licenseImageAvailable));
p1.setY (top->getUIntSeq ("Y1",licenseImageAvailable));
p2.setX (top->getUIntSeq ("X2",licenseImageAvailable));
p2.setY (top->getUIntSeq ("Y2",licenseImageAvailable));
p3.setX (top->getUIntSeq ("X3",licenseImageAvailable));
p3.setY (top->getUIntSeq ("Y3",licenseImageAvailable));
if (licenseImageAvailable)
seq = seq && imageElement->getNeedJPEG (targetSize.width (),
targetSize.height (), &targetImg, top, &licenseImage, p1, p2, p3);
else
seq = seq && imageElement->getNeedJPEG (targetSize.width (), targetSize.height (), &targetImg, top);
} else {
seq = seq && imageElement->getNeedJPEG (targetSize.width (), targetSize.height (), &targetImg, top);
}