Class: Kibuvits_krl171bt4_os_codelets

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/kibuvits_ruby_library_krl171bt4_.rb

Overview

It’s just a namespace for various operating system specific, relatively small, subroutines.

Constant Summary collapse

@@cache =
Hash.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeKibuvits_krl171bt4_os_codelets

Returns a new instance of Kibuvits_krl171bt4_os_codelets.



7183
7184
7185
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 7183

def initialize
   #@mx=Mutex.new
end

Instance Attribute Details

#s_xxObject

Returns the value of attribute s_xx.



7181
7182
7183
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 7181

def s_xx
  @s_xx
end

Class Method Details

.convert_file_path_2_os_specific_format(s_file_path, msgcs) ⇒ Object

This method is DEPRECATED. One should always use UNIX paths. It will be deleted after it has been refactored out from the rest of the library.



7533
7534
7535
7536
7537
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 7533

def Kibuvits_krl171bt4_os_codelets.convert_file_path_2_os_specific_format(s_file_path,msgcs)
   s_out=Kibuvits_krl171bt4_os_codelets.instance.convert_file_path_2_os_specific_format(
   s_file_path,msgcs)
   return s_out
end

.convert_file_path_2_unix_format(s_file_path, msgcs) ⇒ Object

convert_file_path_2_unix_format



7472
7473
7474
7475
7476
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 7472

def Kibuvits_krl171bt4_os_codelets.convert_file_path_2_unix_format(s_file_path,msgcs)
   s_p=Kibuvits_krl171bt4_os_codelets.instance.convert_file_path_2_unix_format(
   s_file_path,msgcs)
   return s_p
end

.convert_file_path_2_windows_format(s_file_path, msgcs) ⇒ Object

convert_file_path_2_windows_format



7512
7513
7514
7515
7516
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 7512

def Kibuvits_krl171bt4_os_codelets.convert_file_path_2_windows_format(s_file_path,msgcs)
   s_p=Kibuvits_krl171bt4_os_codelets.instance.convert_file_path_2_windows_format(
   s_file_path,msgcs)
   return s_p
end

.generate_tmp_file_absolute_path(s_file_name_prefix = "tmp_file_", s_file_name_suffix = "e.txt") ⇒ Object

generate_tmp_file_absolute_path



7571
7572
7573
7574
7575
7576
7577
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 7571

def Kibuvits_krl171bt4_os_codelets.generate_tmp_file_absolute_path(
   s_file_name_prefix="tmp_file_",
   s_file_name_suffix="e.txt") # 'e' is a [^\d]
   s_out=Kibuvits_krl171bt4_os_codelets.instance.generate_tmp_file_absolute_path(
   s_file_name_prefix,nil,s_file_name_suffix)
   return s_out
end

.generate_tmp_file_name(s_file_name_prefix = "tmp_file_", s_file_name_suffix = "e.txt") ⇒ Object

generate_tmp_file_name



7552
7553
7554
7555
7556
7557
7558
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 7552

def Kibuvits_krl171bt4_os_codelets.generate_tmp_file_name(
   s_file_name_prefix="tmp_file_",
   s_file_name_suffix="e.txt") # 'e' is a [^\d]
   s_out=Kibuvits_krl171bt4_os_codelets.instance.generate_tmp_file_name(
   s_file_name_prefix,s_file_name_suffix)
   return s_out
end

.get_os_typeObject

get_os_type



7232
7233
7234
7235
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 7232

def Kibuvits_krl171bt4_os_codelets.get_os_type
   s_out=Kibuvits_krl171bt4_os_codelets.instance.get_os_type
   return s_out
end

.get_tmp_folder_pathObject

get_tmp_folder_path



7440
7441
7442
7443
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 7440

def Kibuvits_krl171bt4_os_codelets.get_tmp_folder_path
   s_out=Kibuvits_krl171bt4_os_codelets.instance.get_tmp_folder_path
   return s_out
end

.ht_path_2_os_type(s_file_path_subject_to_analyze, msgcs) ⇒ Object

ht_path_2_os_type



7408
7409
7410
7411
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 7408

def Kibuvits_krl171bt4_os_codelets.ht_path_2_os_type(s_file_path_subject_to_analyze,msgcs)
   ht_out=Kibuvits_krl171bt4_os_codelets.instance.ht_path_2_os_type(s_file_path_subject_to_analyze,msgcs)
   return ht_out
end

.selftestObject



7581
7582
7583
7584
7585
7586
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 7581

def Kibuvits_krl171bt4_os_codelets.selftest
   ar_msgs=Array.new
   bn=binding()
   kibuvits_krl171bt4_testeval bn, "Kibuvits_krl171bt4_os_codelets.test_get_os_type"
   return ar_msgs
end

.test_get_os_typeObject

Kibuvits_krl171bt4_os_codelets.get_os_type



7237
7238
7239
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 7237

def Kibuvits_krl171bt4_os_codelets.test_get_os_type
   Kibuvits_krl171bt4_os_codelets.get_os_type
end

Instance Method Details

#convert_file_path_2_os_specific_format(s_file_path, msgcs) ⇒ Object

This method is DEPRECATED. One should always use UNIX paths. It will be deleted after it has been refactored out from the rest of the library.



7524
7525
7526
7527
7528
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 7524

def convert_file_path_2_os_specific_format(s_file_path,msgcs)
   # TODO: throw it out after the resto of the library has been refactored.
   s_out=""+s_file_path
   return s_out
end

#convert_file_path_2_unix_format(s_file_path, msgcs) ⇒ Object




7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 7446

def convert_file_path_2_unix_format(s_file_path, msgcs)
   if KIBUVITS_krl171bt4_b_DEBUG
      bn=binding()
      kibuvits_krl171bt4_typecheck bn, String, s_file_path
      kibuvits_krl171bt4_typecheck bn, Kibuvits_krl171bt4_msgc_stack, msgcs
   end # if
   ht_ostypes=self.ht_path_2_os_type(s_file_path,msgcs)
   return "" if msgcs.b_failure
   return ""+s_file_path if ht_ostypes.has_key? "kibuvits_krl171bt4_ostype_unixlike"
   # TODO: add various tests that analyze the path,i.e.
   # forbidden characters, whether the path goes higher than
   # the root, etc.
   s_pt=s_file_path.sub(":\\","/")
   i_len_diff=(s_file_path.length-s_pt.length)
   if 1<i_len_diff
      # There can be at most one ":\" in the path, like
      # C:\plapla
      kibuvits_krl171bt4_throw "\""+s_file_path+"\" is not a file path."
      # TODO: add checks
   end # if
   s_pt=s_pt.gsub("\\","/")
   s_pt="/"+s_pt if i_len_diff==1
   s_pt=s_pt.gsub(/[\/]+/,"/")
   return s_pt
end

#convert_file_path_2_windows_format(s_file_path, msgcs) ⇒ Object

“/C/Program\ Files” -> “C:\Program\ Files”



7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 7482

def convert_file_path_2_windows_format(s_file_path,msgcs)
   if KIBUVITS_krl171bt4_b_DEBUG
      bn=binding()
      kibuvits_krl171bt4_typecheck bn, String, s_file_path
      kibuvits_krl171bt4_typecheck bn, Kibuvits_krl171bt4_msgc_stack, msgcs
   end # if
   ht_ostypes=self.ht_path_2_os_type(s_file_path,msgcs)
   return "" if msgcs.b_failure
   return ""+s_file_path if ht_ostypes.has_key? "kibuvits_krl171bt4_ostype_windows"
   # TODO: add various tests that analyze the path,i.e.
   # forbidden characters, whether the path goes higher than
   # the root, etc.
   return s_file_path if s_file_path.length==0
   s_backslash="\\"
   s_p=s_file_path.gsub("/",s_backslash)
   s_p=s_p.gsub(/[\\]+/,s_backslash)
   s_char0=s_p[0..0]
   if (s_char0!=s_backslash)&&(s_char0!=".")
      s_p=s_backslash+s_p
   end # if
   return "" if s_p.length==1
   return s_p if s_p[0..0]=="."
   s_p=s_p[1..(-1)]
   s_clbsl=":\\"
   return s_p if s_p.include? s_clbsl
   ar=Kibuvits_krl171bt4_str.ar_bisect(s_p, s_backslash)
   s_p=ar[0]+s_clbsl+ar[1]
   return s_p
end

#generate_tmp_file_absolute_path(s_file_name_prefix = "tmp_file_", msgcs = nil, s_file_name_suffix = "e.txt") ⇒ Object




7561
7562
7563
7564
7565
7566
7567
7568
7569
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 7561

def generate_tmp_file_absolute_path(s_file_name_prefix="tmp_file_",
   msgcs=nil, s_file_name_suffix="e.txt") # 'e' is a [^\d]
   # TODO: refactor the msgcs part here
   #interpret_msgcs_var(msgcs,b_msgcs_received)
   s_fp0=get_tmp_folder_path+"/"+generate_tmp_file_name(
   s_file_name_prefix,s_file_name_suffix)
   #interpret_msgcs_var(msgcs,b_msgcs_received)
   return s_fp0
end

#generate_tmp_file_name(s_file_name_prefix = "tmp_file_", s_file_name_suffix = "e.txt") ⇒ Object



7542
7543
7544
7545
7546
7547
7548
7549
7550
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 7542

def generate_tmp_file_name(s_file_name_prefix="tmp_file_",
   s_file_name_suffix="e.txt") # 'e' is a [^\d]
   s=s_file_name_prefix+(Time.new.to_s).gsub!(/[\s;.\\\/:+]/,"_")
   # 2147483647==2^(32-1)-1, i.e. 0 included
   s=s+'r'+Kernel.rand(2147483647).to_s
   s=s+'r'+Kernel.rand(2147483647).to_s
   s=s+'r'+Kernel.rand(2147483647).to_s+s_file_name_suffix
   return s
end

#get_os_typeObject




7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 7188

def get_os_type
   s_key='os_type'
   if @@cache.has_key? s_key
      s_out=""+@@cache[s_key]
      return s_out
   end # if
   s=RUBY_PLATFORM
   s_out='not_determined'
   if 	s.include? 'linux'
      s_out=$kibuvits_krl171bt4_lc_kibuvits_krl171bt4_ostype_unixlike
   elsif 	s.include? 'bsd' # on DesktopBSD it's "i386-freebsd7"
      s_out=$kibuvits_krl171bt4_lc_kibuvits_krl171bt4_ostype_unixlike
   elsif (s.include? 'win')||(s.include? 'mingw')
      s_out=$kibuvits_krl171bt4_lc_kibuvits_krl171bt4_ostype_windows
   elsif 	s.include? 'java' # JRuby
      s_out=$kibuvits_krl171bt4_lc_kibuvits_krl171bt4_ostype_java
      if system("ver")
         s_out=$kibuvits_krl171bt4_lc_kibuvits_krl171bt4_ostype_windows
      else
         s_fp="/tmp/"+generate_tmp_file_name()
         if system("uname")
            if system("uname > s_fp")
               if File.exists? s_fp
                  s=kibuvits_krl171bt4_file2str(s_fp)
                  File.delete s_fp
                  if s.include? "CYGWIN"
                     s_out=$kibuvits_krl171bt4_lc_kibuvits_krl171bt4_ostype_windows
                  end # if
               end # if
            end # if
            File.delete s_fp if File.exists? s_fp
         end # if
      end # if
   else
      kibuvits_krl171bt4_throw 'RUBY_PLATFORM=='+RUBY_PLATFORM+
      ' is not supported by this library.'
   end # elsif
   # There's no point of synchronizing it, because all
   # threads will insert a same result.

   @@cache[s_key]=$kibuvits_krl171bt4_lc_emptystring+s_out
   return s_out
end

#get_tmp_folder_pathObject




7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 7414

def get_tmp_folder_path
   s_system_name=self.get_os_type()
   s_out=''
   if defined? KIBUVITS_krl171bt4_TMP_FOLDER_PATH
      s_out=KIBUVITS_krl171bt4_TMP_FOLDER_PATH
   elsif s_system_name=='kibuvits_krl171bt4_ostype_unixlike'
      s_out='/tmp'
   elsif s_system_name=='kibuvits_krl171bt4_ostype_windows'
      #s_out=ENV['TEMP']
      #kibuvits_krl171bt4_throw "ENV['TEMP']==nil" if s_out==nil
      # If cygwin or something alike is used, then the
      # cygwin uses the Linux file paths, i.e. /c/blabla, but
      # the ENV['TEMP'] gives c:/blablabla  and that breaks things.
      # the solution:
      s_out=KIBUVITS_krl171bt4_HOME+"/src/include/bonnet/tmp"
      # There's nothing lost with that, because KRL relies on
      # unix tools anyway, which means that on Windows the KRL runs
      # on cygwin or something similar.
   elsif s_system_name=="kibuvits_krl171bt4_ostype_java"
      s_out=KIBUVITS_krl171bt4_HOME+"/src/include/bonnet/tmp"
   else
      kibuvits_krl171bt4_throw 'System "'+s_system_name+'" is not supported.'
   end # elsif
   return s_out
end

#ht_path_2_os_type(s_file_path_subject_to_analyze, msgcs) ⇒ Object

Returns a hashtable, where the keys match with their corresponding values and the keys are the operating system types that use the format of the s_file_path_subject_to_analyze

Example of a file path that is accepted by unix-like operating systems and the windows: —-verbatim–start—-

nice_filename.txt

—-verbatim–end—— It does not throw on the s_file_path_subject_to_analyze verification failures.



7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 7350

def ht_path_2_os_type(s_file_path_subject_to_analyze, msgcs)
   if KIBUVITS_krl171bt4_b_DEBUG
      bn=binding()
      kibuvits_krl171bt4_typecheck bn, String, s_file_path_subject_to_analyze
      kibuvits_krl171bt4_typecheck bn, Kibuvits_krl171bt4_msgc_stack, msgcs
   end # if
   s_in=s_file_path_subject_to_analyze
   if s_in.length==0
      s_default_msg="s_file_path_subject_to_analyze.length==0"
      i_message_code=1
      b_failure=true
      s_default_language=$kibuvits_krl171bt4_lc_English
      msgc=Kibuvits_krl171bt4_msgc.new(s_default_msg,i_message_code,b_failure,s_default_language)
      msgcs << msgc
      return
   end # if
   if (s_in.include? "/")&&(s_in.include? "\\")
      s_default_msg="The file/folder path contains both, "+
      "Windows and Unix path characters, i.e. "+
      "slashes and backslashes. s_file_path_subject_to_analyze=="+
      s_file_path_subject_to_analyze
      i_message_code=2
      b_failure=true
      s_default_language=$Ekibuvits_krl171bt4_lc_English
      msgc=Kibuvits_krl171bt4_msgc.new(s_default_msg,i_message_code,b_failure,s_default_language)
      msgc[$kibuvits_krl171bt4_lc_Estonian]="Faili või kataloogi rada sisaldab nii Unix'i kui "+
      "Windowsi raja spetsiifilisi eraldusmärke."
      msgcs << msgc
      return
   end # if
   if /([.]{3})|([?])/.match(s_in,0)!=nil
      # some invalid paths:
      # "abcd/.../hi.exe"
      # "abcd/hi?.exe"
      s_default_msg="The file/folder path contained either 3 dots "+
      "or a question mark. The current specification of "+
      "this method considers that sort of paths to be invalid."
      s_file_path_subject_to_analyze
      i_message_code=3
      b_failure=true
      s_default_language=$kibuvits_krl171bt4_lc_English
      msgc=Kibuvits_krl171bt4_msgc.new(s_default_msg,i_message_code,b_failure,s_default_language)
      msgc[$kibuvits_krl171bt4_lc_Estonian]="Faili või kataloogi rada sisaldab kas "+
      "kolme järjestikust punkti või küsimärki. Selle meetodi praeguse "+
      "spetsifikatsiooni järgi loetakse seda sorti faili/kataloogi rajad vigaseks."
      msgcs << msgc
      return
   end # if
   ht_out=Hash.new
   rgx_common1=/^[\w][\w\d _.]*$/
   ht_path_2_os_type_check_for_Windows_compatibility(ht_out,s_in,
   rgx_common1,msgcs)
   return if msgcs.b_failure
   ht_path_2_os_type_check_for_Linux_compatibility(ht_out,s_in,
   rgx_common1,msgcs)
   return ht_out
end

#ht_path_2_os_type_check_for_Linux_compatibility(ht_out, s_in, rgx_common1, msgcs) ⇒ Object

ht_path_2_os_type_check_for_Windows_compatibility



7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 7288

def ht_path_2_os_type_check_for_Linux_compatibility(ht_out,s_in,
   rgx_common1,msgcs)

   if /[\\]/.match(s_in,0)!=nil
      # some invalid paths:
      # "abcd\\hi.exe" a Windows path style
      return
   end # if
   s_lc_ostype="kibuvits_krl171bt4_ostype_unixlike"
   if /[\/]/.match(s_in,0)!=nil
      # "/"
      # "/hi.txt"
      # "/abcd"
      # "/abcd/"
      # "/abcd/hi.exe"
      # "/abcd/../hi.exe"
      # "/abcd/../tmp/"
      # "/abcd/../tmp/hi.exe"

      # "./xxx"
      # "./gg.txt"
      # "./x/"
      # "./../../../nice.txt"
      # "./../abc/../nice.txt"

      # "../"
      # "../../../"
      # "../x/"

      # "abcd/hi.exe"
      ht_out[s_lc_ostype]=s_lc_ostype
      return
   end # if
   if rgx_common1.match(s_in,0)!=nil  # rgx_common1==/^[\w][\w\d _.]*$/
      # "hallo"
      # "hallo.txt"
      # "awesome.tar.gz"
      # "awesome......."
      # "awesome... .. .."
      ht_out[s_lc_ostype]=s_lc_ostype
      return
   end # if

end

#ht_path_2_os_type_check_for_Windows_compatibility(ht_out, s_in, rgx_common1, msgcs) ⇒ Object




7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 7242

def ht_path_2_os_type_check_for_Windows_compatibility(ht_out,s_in,
   rgx_common1,msgcs)
   if /[\/]/.match(s_in,0)!=nil
      # some invalid paths:
      # "abcd/hi.exe" a Linux path style
      return
   end # if
   s_lc_ostype="kibuvits_krl171bt4_ostype_windows"
   if /^[\w][\w\d_]*:/.match(s_in)!=nil
      # "X:"
      # "C:\\xx\\yy"
      # "ScoobyDoo:\\xx\\yy"
      ht_out[s_lc_ostype]=s_lc_ostype
      return
   end # if
   if /[\\]/.match(s_in)!=nil
      # ".\\"
      # ".\\xx\\yy"
      # ".\\..\\"
      # ".\\..\\zz\\gg.txt"
      # ".\\..\\zz\\..\\ff\\ll.txt"
      # "..\\..\\"
      # "..\\..\\zz"
      # "xx\\"
      # "xx\\yy"
      ht_out[s_lc_ostype]=s_lc_ostype
      return
   end # if
   if /^[%][\w][\w\d_]*[%]/.match(s_in)!=nil
      # "%windir%\\yy"
      # "%windir%\\..\\"
      # "%windir%"
      ht_out[s_lc_ostype]=s_lc_ostype
      return
   end # if
   if rgx_common1.match(s_in)!=nil  # rgx_common1==/^[\w][\w\d _.]*$/
      # "hallo"
      # "hallo.txt"
      # "awesome.tar.gz"
      # "awesome......."
      # "awesome... .. .."
      ht_out[s_lc_ostype]=s_lc_ostype
      return
   end # if
end

#s_normalize_Linux_path(s_path_in, msgcs) ⇒ Object

ht_path_2_os_type_check_for_Linux_compatibility



7333
7334
7335
7336
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 7333

def s_normalize_Linux_path(s_path_in,msgcs)
   #msgcs=Kibuvits_krl171bt4_msgc_stack.new)
   s_out=s_path_in
end