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

    +4

    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
    int DataBase::garbageCollector(string filename){ 
        fstream from(filename.c_str());
        fstream from0((filename+".index").c_str());
        Index x;
        Data d;
        char *a/*,*b*/;
        int oldpos = 0 ;
        bool done = false;
        from.seekg(0,ios::end);
        int dbend = static_cast<int>(from.tellp());
        from0.seekg(0,ios::end);
        int indexend = static_cast<int>(from0.tellp());
        if(dbend == BlockSize || indexend == IndexSize){
    	from.close();
    	from0.close();
    	return 0;
        }
        from.seekg(0,ios::beg);
        from0.seekg(0,ios::beg);
        while(true){ 
            from0 >> x;
            if(from0.eof())break;
            if(!x.Num()){ 
                int count = dbend - x.Pointer() - BlockSize;
                int count0 = indexend - (static_cast<int>(from0.tellp())+1);
                from.seekg(x.Pointer()+BlockSize);
                a = new char[count]; 
                from.read(a,count);
                from.seekp(x.Pointer());
                from.write(a,count);
                delete a;
                a = new char[count0];
                from0.seekp(1,ios::cur);
                int test = from0.tellp();
                from0.read(a,count0);
                from0.clear();
                from0.seekp(indexend - count0 - IndexSize,ios::beg);
                test = from0.tellp();
                from0.write(a,count0);
                test = from0.tellp();
                delete a;
                ofstream to("tmp.base",ios::trunc),to0("tmp.index",ios::trunc);
                a = new char[dbend - BlockSize];
                from.seekg(0,ios::beg);
                from.read(a,dbend - BlockSize);
                to.write(a,dbend - BlockSize);
                delete a;
                a = new char[indexend - IndexSize];
                from0.seekg(0,ios::beg);
                test = from0.tellp();
                from0.read(a,indexend - IndexSize);
                to0.write(a,indexend - IndexSize);
                delete a;
                from.close();
                from0.close();
                if(remove(filename.c_str()) || rename("tmp.base",filename.c_str())) return -1;
                if(remove((filename+".index").c_str()) || rename("tmp.index",(filename+".index").c_str())) return -1;
                oldpos = indexend - count0 - IndexSize;
                done = true;
                break;
            }
        }
        if(done){
            from0.open((filename+".index").c_str());
            while(true){ 
                from0 >> x;
                if(from0.eof())break;
                if(x.Pointer() > static_cast<unsigned>((oldpos/23)*BlockSize) ){ 
                    x.Pointer(x.Pointer()-BlockSize);
                    from0.seekp(-(IndexSize-1),ios::cur);
                    from0 << x;
                    int test = from0.tellp();
                }
                else{ 
                    from0.seekp(-(IndexSize-1),ios::cur);
                    from0 << x;
                    int test = from0.tellp();
                    from0.clear();
                }
            }
        }
        return 0;
    }

    Собираем пустые блоки

    Abbath, 15 Октября 2013

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

    +6

    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
    int DataBase::split(fstream& from,fstream& from0,unsigned int p){
        int pos0 = static_cast<int>(from.tellp());
        char *a,*b;
        Data d,d0;
        Index x,x0;
        from.clear();
        from.seekp(0,ios::end);
        int PosEnd = static_cast<int>(from.tellp());
        PosEnd -=static_cast<int>(p);
        a = new char[PosEnd];
        from.seekp(p,ios::beg);
        from.read(a,PosEnd); 
        from.seekp(p+BlockSize,ios::beg);
        from.write(a,PosEnd); 
        from.seekp(p+BlockSize,ios::beg);
        b = a+5*RecordSize;
        from.write(b,RecordSize*5);
        for (int i = 0; i < 5; ++i){ 
            from << d;
        }
        from.seekp(p+RecordSize*5,ios::beg);
        for (int i = 0; i < 5; ++i){ 
            from << d;
        }
        from.seekg(p+RecordSize*4);
        from >> d;
        int newmax = d.Id(); 
        from0.seekp(0,ios::end);
        int indexend =static_cast<int>(from0.tellp());
        from0.seekg(0,ios::beg);
        while(true){ 
            from0 >> x;
            if(from0.eof())break;
            if(x.Pointer() == p){ 
                int oldpos = static_cast<int>(from0.tellp());
                oldpos++;
                int count = indexend - oldpos;
                char *c = new char[count];
                from0.seekg(1,ios::cur);
                from0.read(c,count);
                from0.seekp(oldpos,ios::beg);
                x0.Max(x.Max()); 
                x0.Num(5);
                x0.Pointer(p + BlockSize);
                from0 << x0;
                from0.write(c,count);
                from0.seekp(oldpos - IndexSize,ios::beg);
                x.Max(newmax); 
                x.Num(5);
                from0 << x;
                delete c;
                from.clear();
                from0.seekp(0,ios::beg);
                break;
            }
        }
        from.clear();
        bool first = false;
        while(true){ 
            from0 >> x;
            if(from0.eof())break;
            if(x.Pointer() > p ){
                if(!first){ 
                    from0.seekp(-(IndexSize-1),ios::cur);
                    from0 << x;
                    int test = from0.tellp();
                    from0.clear();
                    first = true;
                    continue;
                }
                x.Pointer(x.Pointer()+BlockSize);
                from0.seekp(-(IndexSize-1),ios::cur);
                from0 << x;
                int test = from0.tellp();
            }
            else{ 
                from0.seekp(-(IndexSize-1),ios::cur);
                from0 << x;
                int test = from0.tellp();
                from0.clear();
            }
        }
        from.clear();
        from.seekp(pos0);
        delete a;
        //delete b;
        return 0;
    }

    Нарезаем

    Abbath, 15 Октября 2013

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

    +10

    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
    void ImageArea::saveConf(bool def)
    {
        static bool fp = true;
        static QString filename = QString("default.conf");
        if(!def){
            filename = QFileDialog::getSaveFileName(this, tr("Save config"), "", tr("Config files (*.conf)"));
            if(fp){
                QFile file(filename);
                if(file.open(QFile::WriteOnly)){
                    QTextStream str(&file);
                    str << crop[0].x() << " " << crop[0].y() << "\n";
                    str << crop[1].x() << " " << crop[1].y() << "\n";
                    str << square[0].x() << " " << square[0].y() << "\n";
                    str << square[1].x() << " " << square[1].y() << "\n";
                    str << square[2].x() << " " << square[2].y() << "\n";
                    QMessageBox::information(this, tr("Next step"), tr("Put 3 points then press Save again"));
                }else{
                    QMessageBox::warning(this, tr("Error"), tr("Can not open a file"));
                }
                fp = false;
            }else{
                QFile file(filename);
                if(file.open(QFile::Append)){
                    QTextStream str(&file);
                    str << square[0].x() << " " << square[0].y() << "\n";
                    str << square[1].x() << " " << square[1].y() << "\n";
                    str << square[2].x() << " " << square[2].y() << "\n";
                }else{
                    QMessageBox::warning(this, tr("Error"), tr("Can not open a file"));
                }
                fp = true;
            }
        }else{
            QFile file(filename);
            if(file.open(QFile::WriteOnly)){
                QTextStream str(&file);
                str << conf.crop[0].x() << " " << conf.crop[0].y() << "\n";
                str << conf.crop[1].x() << " " << conf.crop[1].y() << "\n";
                str << conf.square[0].x() << " " << conf.square[0].y() << "\n";
                str << conf.square[1].x() << " " << conf.square[1].y() << "\n";
                str << conf.square[2].x() << " " << conf.square[2].y() << "\n";
                str << conf.square0[0].x() << " " << conf.square0[0].y() << "\n";
                str << conf.square0[1].x() << " " << conf.square0[1].y() << "\n";
                str << conf.square0[2].x() << " " << conf.square0[2].y() << "\n";
            }
        }
    }

    Сохраняем конфиг в джва захода.

    Abbath, 14 Октября 2013

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

    +13

    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
    if(ortho){
                if( abs(e->x() - line.p1().x() ) * 2 < abs( e->y() - line.p1().y() ) ){
                    y = e->y();
                    x = line.p1().x();
                }else if(abs(e->x() - line.p1().x() ) > 2 * abs( e->y() - line.p1().y() )){
                    x = e->x();
                    y = line.p1().y();
                }else if(abs(e->x() - line.p1().x() ) * 2 > abs( e->y() - line.p1().y() ) &&
                         abs(e->x() - line.p1().x() ) < abs( e->y() - line.p1().y() )){
                    if((e->x() < line.p1().x() && e->y() < line.p1().y()) || (e->x() > line.p1().x() && e->y() < line.p1().y())){
                        x = e->x();
                        y = line.p1().y() - abs(line.p1().x() - e->x());
                    }else if((e->x() > line.p1().x() && e->y() > line.p1().y()) || (e->x() < line.p1().x() && e->y() > line.p1().y())){
                        x = e->x();
                        y = line.p1().y() + abs(line.p1().x() - e->x());
                    }
                }else if(abs(e->x() - line.p1().x() ) < 2 * abs( e->y() - line.p1().y() ) &&
                         abs(e->x() - line.p1().x() ) > abs( e->y() - line.p1().y() )){
                    if((e->x() < line.p1().x() && e->y() < line.p1().y()) || (e->x() < line.p1().x() && e->y() > line.p1().y())){
                        y = e->y();
                        x = line.p1().x() - abs(line.p1().y() - e->y());
                    }else if((e->x() > line.p1().x() && e->y() > line.p1().y()) || (e->x() > line.p1().x() && e->y() < line.p1().y())){
                        y = e->y();
                        x = line.p1().x() + abs(line.p1().y() - e->y());
                    }
                }else{
                    x = e->x();
                    y = e->y();
                }

    Abbath, 14 Октября 2013

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

    +8

    1. 1
    2. 2
    3. 3
    4. 4
    n = strlen(pName);
    name = new char[n + 1];
    memset(name, 0, n + 1);
    memcpy(name, pName, n);

    боянчик. std::string наверное религия не позволяет. а strdup() слишком С. oh wait...

    Dummy00001, 09 Октября 2013

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

    −4

    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
    static Singleton * g_pInstance = NULL;
    
    Singleton* Singleton::sharedInstance()
    {
        if (g_pInstance)
            return g_pInstance;
        else {
            g_pInstance = new Singleton();
            if (g_pInstance->init()) {
                return g_pInstance;
            } else {
                delete g_pInstance;
                g_pInstance = NULL;
            }
            return g_pInstance;
        }
    }

    Ещё из жизни синглтонов. Даёшь больше return'ов!

    Deacon, 08 Октября 2013

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

    +16

    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
    // in .h file
    class Singleton
    {
    public:
        Singleton();
        ~Singleton();
    private:
        static Singleton* m_Instance;
        friend Singleton& GetInstance();
    };
    
    inline Singleton& GetInstance()
    {
        assert(Singleton::m_Instance);
        return *Singleton::m_Instance;
    }
    
    // in .cpp file
    Singleton* Singleton::m_Instance = NULL;
    
    Singleton::Singleton()
    {
        assert(!m_Instance);
        m_Instance = this;
    }
    
    Singleton::~Singleton()
    {
        m_Instance = NULL;
    }

    Вот такую реализацию синглтона увидел в одном проекте.
    ЗЫ: Для его корректной работы, в main было написано конечно же:
    main() {
    Singleton* s = new Singleton;
    ...
    delete s;
    }

    Deacon, 08 Октября 2013

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

    +8

    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
    void SaveEncryptedFile( const char *text, int len, const char* filename )
    {
    	char* pEncryptedText = new char[strlen(text)+1];
    	string x1 = "you'll";
    	string x2 = "never";
    	string x3 = "get a";
    	string x4 = "password";
    	char l_pBuf[255];
    	sprintf(l_pBuf,"%d",30*11/3);
    	string result = x1+x2+x2+x1+l_pBuf+x3;
    	encryptString(text,pEncryptedText,result.c_str(),strlen(text),result.length()); // там внутри xor
    
    	FILE* pFile = fopen(filename, "wb");
    	if (pFile)
    	{
    		fwrite(pEncryptedText,sizeof(char),len,pFile);
    		fclose(pFile);
    	}
    
    	delete[] pEncryptedText;
    }

    Нашёл в рабочем проекте. Для "расшифровки" файла используется ещё одна такая же функция.

    Deacon, 07 Октября 2013

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

    +8

    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
    const Registry & Registry::getInstance()
    {
    	Registry *instance = RegistrySingleton::instance();
    	if (!instance->mRootNode) {
    		instance->load();
    	}
    	return *instance;
    }
    
    void Registry::load()
    {
    	try {
    		// ...
    		if (!mReader) {
    			mReader = XMLReaderFactory::createXMLReader();
    		}
    		// ...
    		mReader->parse( ... );
    	} catch (...) {
    		// ...
    		throw; // удачи всем пользователям обрабатывать исключения xerces...
    	}
    }

    боян синглтонно-абстрактный для чтения xml конфигурации с помощью xerces.

    и не только ошибки не обрабатаешь (потому что getInstance() их бросает, угадай какой именно вызов из сотен загружает конфигурацию), но и в добавок народ не впечатал как многопоточность сделать правильно (RegistrySingleton это специализация шаблона который синхронизирует инициализацию mInstance переменной, и только).

    Dummy00001, 07 Октября 2013

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

    +4

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    int eval (const Expr& e)
    {
        Match(e)
        Case(const Value& x) return x.value;
        Case(const Plus& x) return eval (x.e1)+eval(x.e2);
        Case(const Minus& x) return eval(x.e1)−eval(x.e2);
        Case(const Times& x) return eval(x.e1)∗eval(x.e2);
        Case(const Divide& x) return eval(x.e1)/eval (x.e2);
        EndMatch
    }

    Бьёрн Страуструп выбирает борщ.
    http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3449.pdf
    http://www.linux.org.ru/forum/development/9525806

    Заметим, что не только Страуструп раскаялся в прошлом. Кармак с энтузиазмом рассказывает, как с головой погрузился в Haskell и Scheme, объясняет, почему хаскель невероятно крут и почему сегодня он бы, вероятно, сделал QuakeScheme вместо QuakeC. Он пишет на хаскеле порт wolf3D.

    LispGovno, 05 Октября 2013

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