Skip to content
العربية

Project: folder line counter

The idea: like a small cloc: it walks a folder and everything under it, counts the files, lines and characters for each extension, and counts the binary and forbidden files.

A «وثائق» (documents) folder with a subfolder, text files with different extensions, a file with no extension, and a binary file:

وثائق/اقرأني
بلا امتداد
وثائق/دليل.md
# دليل

سطر أول
سطر ثانٍ
وثائق/صورة.bin

Binary file (5 bytes): not shown.

وثائق/فرعي/برنامج.م
اطبع("مرحبا")
اطبع("وداعا")
اطبع("!")
وثائق/فرعي/ملاحظات.md
# ملاحظات
فقرة
عداد_اسطر.م
# برنامج نظامي صغير: يمشي في شجرة مجلد ويعدّ الملفات والأسطر لكل امتداد (كـ cloc مصغّر)
استورد نظام
استورد ملفات
استورد مسارات
استورد رياضيات
هيكل احصاء:
امتداد نص
ملفات عدد64
اسطر عدد64
بايتات عدد64
هيكل حصيلة:
مجلدات عدد64
ثنائية عدد64
ممنوعة عدد64
داله سجّل(جدول قائمة[احصاء], فهرس قاموس[نص, عدد64], امتداد نص, اسطر عدد64, بايتات عدد64) فراغ:
اذا !يوجد(فهرس, امتداد):
فهرس[امتداد] = طول(جدول)
اضف(جدول, احصاء(قص(امتداد, 0), 0, 0, 0)) # نسخة: امتداد عرض على مسار سيُحرَّر
م = فهرس[امتداد]
ا = جدول[م]
ا.ملفات += 1
ا.اسطر += اسطر
ا.بايتات += بايتات
جدول[م] = ا
داله امش(مسار نص, جدول قائمة[احصاء], فهرس قاموس[نص, عدد64], ح مؤشر[حصيلة], عمق عدد64) فراغ:
اذا عمق > 40:
ارجع # لا طريقة لكشف الروابط الرمزية: حد للعمق بدلها
اسماء = ملفات.سرد_مجلد(مسار) والا خ:
حالات خ:
حاله خطأ.ليس_مجلدا:
عد_ملف(مسار, جدول, فهرس, ح)
حاله خطأ.ممنوع:
محتوى(ح).ممنوعة += 1
حاله _:
اطبع("تعذّر: ", مسار, " (", خ, ")")
ارجع
خاتمه حرر(اسماء)
محتوى(ح).مجلدات += 1
لكل اسم في اسماء:
اذا يبدأ_بـ(اسم, "."):
تجاوز
كامل = مسارات.ضم(مسار, اسم)
امش(كامل, جدول, فهرس, ح, عمق + 1)
حرر(كامل)
داله عد_ملف(مسار نص, جدول قائمة[احصاء], فهرس قاموس[نص, عدد64], ح مؤشر[حصيلة]) فراغ:
محتوى_الملف = ملفات.اقرأ_ملف(مسار) والا خ:
حالات خ:
حاله خطأ.ليس_نصا:
محتوى(ح).ثنائية += 1
حاله خطأ.ممنوع:
محتوى(ح).ممنوعة += 1
حاله _:
اطبع("تعذّر: ", مسار, " (", خ, ")")
ارجع
اسطر = 0
لكل ح_ في محتوى_الملف:
اذا ح_ == '\n':
اسطر += 1
بايتات = طول(محتوى_الملف) # بالأحرف لا بالبايتات (كما في التقرير؛ طول_بايت منذ 0.14.0)
امتداد = مسارات.الامتداد(مسار) والا ""
اذا طول(امتداد) == 0:
امتداد = "(بلا)"
سجّل(جدول, فهرس, امتداد, اسطر, بايتات)
حرر(محتوى_الملف)
داله قبل(ا احصاء, ب احصاء) منطقي:
ارجع ا.اسطر > ب.اسطر
وسائط = نظام.وسائط()
البداية = "."
اذا طول(وسائط) > 0:
البداية = وسائط[0]
جدول قائمة[احصاء] = []
فهرس قاموس[نص, عدد64] = {}
ح = جديد(حصيلة(0, 0, 0))
خاتمه حرر(ح)
امش(البداية, جدول, فهرس, ح, 0)
رتب_ب(جدول, قبل)
اطبع("الامتداد الملفات الأسطر الأحرف")
مج_ملفات = 0
مج_اسطر = 0
لكل ا في جدول:
اطبع(ا.امتداد, "\t", رياضيات.نسق_عدد(ا.ملفات, 8, خطاء), رياضيات.نسق_عدد(ا.اسطر, 11, خطاء), رياضيات.نسق_عدد(ا.بايتات, 12, خطاء))
مج_ملفات += ا.ملفات
مج_اسطر += ا.اسطر
اطبع("المجموع: ", مج_ملفات, " ملفاً، ", مج_اسطر, " سطراً، ", محتوى(ح).مجلدات, " مجلداً؛ ثنائية: ", محتوى(ح).ثنائية, "، ممنوعة: ", محتوى(ح).ممنوعة)
Output recorded from the compilermeem عداد_اسطر.م -- وثائق · meem 0.17.3+434e13f
الامتداد       الملفات     الأسطر      الأحرف.md	       2          6          40.م	       1          3          38(بلا)	       1          1          11المجموع: 4 ملفاً، 10 سطراً، 2 مجلداً؛ ثنائية: 1، ممنوعة: 0

Also built with meem build when the site was built: a static file of 71.4 kB, run on its own, and its output matched the output above.

  1. Walking: امش (walk) reads the folder’s names with ملفات.سرد_مجلد (list folder). If that returns the error ليس_مجلدا (not a folder), the path is a file, so عد_ملف counts it. And حالات خ tells ممنوع (forbidden) apart from other errors.
  2. The file: ملفات.اقرأ_ملف rejects anything that is not UTF-8 with the error ليس_نصا (not text), so the file is counted as binary. Lines are counted by going through the file’s characters.
  3. The extension: مسارات.الامتداد(مسار) والا "" (extension; it returns نص؟ since 0.12.0), then سجّل (record) stores it in the table as a copy with قص(امتداد, 0).
  4. The totals: a حصيلة (totals) struct is allocated with جديد, and changed through محتوى(ح) in every function.
  • The dangling view, the worst thing in the report: مسارات.الامتداد returns a view onto the path. The view was first stored in the struct, then the path was freed, and the extension was printed corrupted (ss. instead of .md) with exit code 0. --debug-mem did not catch it in 0.9.1; it actually fixed the output and so hid the error. In 0.10.0, the detector that is on by default in a direct run catches it as error E0430, with the lines where the string was created and freed; in a file built without the detector, the output stays undefined. The program here keeps a copy with قص, so it has no dangling view.
  • No way to find a file’s type (no stat), so a folder is told apart from a file by the error from سرد_مجلد.
  • No detection of symbolic links, so the depth is limited to forty.
  • طول counts characters, not bytes: the column is «characters», not «bytes». There was no function for the byte count in 0.9.1; since 0.14.0 طول_بايت gives it, and the program here is as it was written for the report.
  • Handling errors with حالات خ turns «folder or file, allowed or forbidden, text or binary» into clear branches.
  • Everything that مسارات.الامتداد, المجلد, اسم_الملف, جزء and قص_الفراغات return is a view: if it must live longer than its original string, copy it.