Annotation of solidite/utils/head.cpp, revision 1.1.1.1

1.1       user        1: /* Copyright (c) 2026 The Solidite Developers
                      2:  *
                      3:  * This program is free software: you can redistribute it and/or modify it under
                      4:  * the terms of the GNU Affero General Public License as published by the Free
                      5:  * Software Foundation, either version 3 of the License, or (at your option)
                      6:  * any later version.
                      7:  *
                      8:  * This program is distributed in the hope that it will be useful, but WITHOUT
                      9:  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
                     10:  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
                     11:  * details.
                     12:  *
                     13:  * You should have received a copy of the GNU Affero General Public License
                     14:  * along with this program. If not, see <https://www.gnu.org/licenses/>. */
                     15: 
                     16: #include <bitset>
                     17: #include <fstream>
                     18: #include <iostream>
                     19: #include <locale>
                     20: #include <string>
                     21: #include <vector>
                     22: 
                     23: #include <cstring>
                     24: #include <cstdint>
                     25: 
                     26: int main(int argc, char* argv[]) {
                     27:   // locale work
                     28:   std::locale::global(std::locale(""));
                     29:   std::cerr.imbue(std::locale()); 
                     30: 
                     31:   size_t number = 10;
                     32:   std::vector<std::string> files;
                     33:   std::bitset<3> options = 0b000;
                     34:   /* options[0]=stop process options (-- stop processing options if set to 1)
                     35:    * options[1]=skip next argv - in case of -c 10, dont process 10 as a file
                     36:    * options[2]=type (either -c or -n, default=0 -n 10) */
                     37: 
                     38:   for (int i = 1; i < argc; ++i) {
                     39:     if (options.test(1) == 1) {
                     40:       options.reset(1);
                     41:       continue;
                     42:     } else if (options.test(0) == 0) {
                     43:       if (std::strcmp(argv[i], "--") == 0) {
                     44:         options.set(0);
                     45:         continue;
                     46:       } else if (std::strcmp(argv[i], "-c") == 0) {
                     47:         if (i + 1 > argc - 1) {
                     48:           std::cerr << "byte required after -c\n";
                     49:           return 1;
                     50:         } else {
                     51:           number = std::atoi(argv[i + 1]);
                     52:           options.set(2);
                     53:           options.set(1);
                     54:           continue;
                     55:         }
                     56:       } else if (std::strcmp(argv[i], "-n") == 0) {
                     57:         if (i + 1 > argc - 1) {
                     58:           std::cerr << "line number required after -n\n";
                     59:           return 1;
                     60:         } else {
                     61:           number = std::atoi(argv[i + 1]);
                     62:           options.reset(2);
                     63:           options.set(1);
                     64:           continue;
                     65:         }
                     66:       }
                     67:     }
                     68:     files.push_back(argv[i]);
                     69:   }
                     70: 
                     71:   if (files.empty())
                     72:     files.push_back("-");
                     73:   // induce an stdin into the files if empty
                     74:   for (std::string file : files) {
                     75:     if (file == "-") {
                     76:       std::string line;
                     77:       if (options.test(2) == 0) {
                     78:         for (size_t i = 0; i < number; ++i) {
                     79:           std::getline(std::cin, line);
                     80:           std::cout << line + '\n';
                     81:         }
                     82:       } else if (options.test(2) == 1) {
                     83:         size_t chars = 0;
                     84:         while (chars < number) {
                     85:           std::getline(std::cin, line);
                     86:           if (chars + line.length() < number) {
                     87:             line += '\n';
                     88:             chars += line.length();
                     89:           } else {
                     90:             line = line.substr(0, number - chars);
                     91:             std::cout << line;
                     92:             break;
                     93:           }
                     94:           std::cout << line;
                     95:         }
                     96:       }
                     97:       continue;
                     98:     }
                     99: 
                    100:     std::ifstream read(file);
                    101:     if (!read.is_open()) {
                    102:       std::cerr << "failed to read: " + file + '\n';
                    103:       continue;
                    104:     }
                    105:     
                    106:     std::string line;
                    107:     if (options.test(2) == 0) {
                    108:       for (size_t i = 0; i < number; ++i) {
                    109:         std::getline(read, line);
                    110:         std::cout << line + '\n';
                    111:       }
                    112:     } else if (options.test(2) == 1) {
                    113:       size_t chars = 0;
                    114:       while (chars < number) {
                    115:         std::getline(read, line);
                    116:         if (chars + line.length() < number) {
                    117:           line += '\n';
                    118:           chars += line.length();
                    119:         } else {
                    120:           line = line.substr(0, number - chars);
                    121:           std::cout << line;
                    122:           break;
                    123:         }
                    124:         std::cout << line;
                    125:       }
                    126:     }
                    127:     read.close();
                    128:   }
                    129:   return 0;
                    130: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>