💾 Archived View for icolotl.com › repos › morgrot › src › main.rs captured on 2024-08-18 at 19:17:20.

View Raw

More Information

⬅️ Previous capture (2024-05-26)

-=-=-=-=-=-=-

/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * MORGROT (main.rs)
 * Morgana's Offline-First Rot13 Program (improved(?) rust edition)
 * Copyright (C) 2024 Grace Morgana <morgana@icolotl.com>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

use std::env;
use std::process;
use std::io;

fn main() {

    // Vectors of chars to index into and shift from
    let lowercase: Vec<char> = "abcdefghijklmnopqrstuvwxyz".chars().collect();
    let uppercase: Vec<char> = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".chars().collect();

    // Get command line argument vector
    let args: Vec<String> = env::args().collect();
    
    // Display help and exit if help flag is ever found
    for arg in &args {
        if arg == "-h" || arg == "--help" {
            println!("Usage: morgrot [shift]");
            process::exit(0);
        }
    }

    // Grab shift amount from args
    let mut shift: i32 = match &args[..] {
        [_] => 13,
        [_, a] => a.trim().parse().expect("Invalid shift amount"),
        _ => panic!("Too many arguments")
    };

    // Apply modulo to shift and fix if negative
    // This will constrain shift to the 0 to 25 range
    shift %= 26;
    if shift < 0 {
        shift += 26;
    }

    // Prepare to iterate over input
    let stdin = io::stdin();
    let mut output = String::new();
    let mut buffer = String::new();

    // Attempt to process chars in each line until EOF
    while stdin.read_line(&mut buffer).expect("Failed to read line") != 0 {
        for c in buffer.chars() {
            // If the char is in lowercase, apply shift and reindex
            if let Some(mut pos) = lowercase.iter().position(|&x| x == c) {
                pos += shift as usize;
                if pos >= 26 {
                    pos -= 26;
                }
                output.push(lowercase[pos]);
            // If the char is in uppercase, apply shift and reindex
            } else if let Some(mut pos) = uppercase.iter().position(|&x| x == c) {
                pos += shift as usize;
                if pos >= 26 {
                    pos -= 26;
                }
                output.push(uppercase[pos]);
            // If the char is not in lowercase or uppercase, just use as is
            } else {
                output.push(c);
            }
        }
        // Clear the buffer for the next line
        buffer.clear()
    }

    // Display the encoded/decoded output
    println!("\n{}", output);

}